This project has retired. For details please refer to its Attic page.
LoginController xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.chukwa.hicc.rest;
19  
20  import java.io.StringWriter;
21  import java.net.URI;
22  import java.net.URISyntaxException;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.ws.rs.GET;
26  import javax.ws.rs.POST;
27  import javax.ws.rs.Path;
28  import javax.ws.rs.core.Context;
29  import javax.ws.rs.core.Response;
30  
31  import org.apache.hadoop.chukwa.datastore.ChukwaHBaseStore;
32  import org.apache.velocity.Template;
33  import org.apache.velocity.VelocityContext;
34  import org.apache.velocity.app.VelocityEngine;
35  
36  import com.sun.jersey.api.client.ClientResponse.Status;
37  
38  @Path("/login")
39  public class LoginController {
40    @Context
41    VelocityEngine velocity;
42    
43    static {
44      ChukwaHBaseStore.populateDefaults();
45    }
46  
47    @GET
48    @Path("check")
49    public String login(String buffer) {
50      VelocityContext context = new VelocityContext();
51      StringWriter sw = null;
52      try {
53        Template template = velocity.getTemplate("login.vm");
54        sw = new StringWriter();
55        template.merge(context, sw);
56      } catch (Exception e) {
57        e.printStackTrace();
58        return e.getMessage();
59      }
60      return sw.toString();
61    }
62  
63    @POST
64    @Path("check")
65    public Response check(@Context HttpServletRequest request) {
66      VelocityContext context = new VelocityContext();
67      if(request.getRemoteUser()!=null) {
68        URI location;
69        try {
70          location = new URI("/hicc/");
71          return Response.temporaryRedirect(location).build();
72        } catch (URISyntaxException e) {
73        }
74      }
75      context.put("invalid", true);
76      Template template = velocity.getTemplate("login.vm");
77      StringWriter sw = new StringWriter();
78      template.merge(context, sw);
79      return Response.status(Status.FORBIDDEN).entity(sw.toString()).build();
80    }
81  }