1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.hadoop.chukwa.hicc.rest;
1920import java.io.StringWriter;
21import java.net.URI;
22import java.net.URISyntaxException;
2324import javax.servlet.http.HttpServletRequest;
25import javax.ws.rs.GET;
26import javax.ws.rs.POST;
27import javax.ws.rs.Path;
28import javax.ws.rs.core.Context;
29import javax.ws.rs.core.Response;
3031import org.apache.hadoop.chukwa.datastore.ChukwaHBaseStore;
32import org.apache.velocity.Template;
33import org.apache.velocity.VelocityContext;
34import org.apache.velocity.app.VelocityEngine;
3536import com.sun.jersey.api.client.ClientResponse.Status;
3738 @Path("/login")
39publicclassLoginController {
40 @Context
41 VelocityEngine velocity;
4243static {
44 ChukwaHBaseStore.populateDefaults();
45 }
4647 @GET
48 @Path("check")
49public String login(String buffer) {
50 VelocityContext context = new VelocityContext();
51 StringWriter sw = null;
52try {
53 Template template = velocity.getTemplate("login.vm");
54 sw = new StringWriter();
55 template.merge(context, sw);
56 } catch (Exception e) {
57 e.printStackTrace();
58return e.getMessage();
59 }
60return sw.toString();
61 }
6263 @POST
64 @Path("check")
65public Response check(@Context HttpServletRequest request) {
66 VelocityContext context = new VelocityContext();
67if(request.getRemoteUser()!=null) {
68 URI location;
69try {
70 location = new URI("/hicc/");
71return 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);
79return Response.status(Status.FORBIDDEN).entity(sw.toString()).build();
80 }
81 }