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    /**
48     * Password verification login screen
49     * 
50     * @param buffer holds any user input
51     * @return html page of login screen
52     * 
53     * @response.representation.200.doc Login screen
54     * @response.representation.200.mediaType text/html
55     * @response.representation.200.example Example available in HICC UI
56     */
57    @GET
58    @Path("check")
59    public String login(String buffer) {
60      VelocityContext context = new VelocityContext();
61      StringWriter sw = null;
62      try {
63        Template template = velocity.getTemplate("login.vm");
64        sw = new StringWriter();
65        template.merge(context, sw);
66      } catch (Exception e) {
67        e.printStackTrace();
68        return e.getMessage();
69      }
70      return sw.toString();
71    }
72  
73    /**
74     * Password verification REST API
75     * 
76     * @param request is HTTP request object
77     * @return login status code
78     * 
79     * @response.representation.200.doc User login successful
80     * @response.representation.200.mediaType text/plain
81     * @response.representation.200.example 200 OK
82     * @response.representation.403.doc Login denied
83     * @response.representation.403.mediaType text/plain
84     * @response.representation.403.example 403 FORBIDDEN
85     */
86    @POST
87    @Path("check")
88    public Response check(@Context HttpServletRequest request) {
89      VelocityContext context = new VelocityContext();
90      if(request.getRemoteUser()!=null) {
91        URI location;
92        try {
93          location = new URI("/hicc/");
94          return Response.temporaryRedirect(location).build();
95        } catch (URISyntaxException e) {
96        }
97      }
98      context.put("invalid", true);
99      Template template = velocity.getTemplate("login.vm");
100     StringWriter sw = new StringWriter();
101     template.merge(context, sw);
102     return Response.status(Status.FORBIDDEN).entity(sw.toString()).build();
103   }
104 }