• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you
5  * may not use this file except in compliance with the License. You may
6  * obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 
17 package com.android.vts.servlet;
18 
19 import com.google.appengine.api.users.User;
20 import com.google.appengine.api.users.UserService;
21 import com.google.appengine.api.users.UserServiceFactory;
22 import com.google.gson.Gson;
23 import java.io.IOException;
24 import java.util.List;
25 import java.util.logging.Logger;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 
30 public abstract class BaseServlet extends HttpServlet {
31     protected final Logger logger = Logger.getLogger(getClass().getName());
32 
33     // Environment variables
34     protected static final String GERRIT_URI = System.getProperty("GERRIT_URI");
35     protected static final String GERRIT_SCOPE = System.getProperty("GERRIT_SCOPE");
36     protected static final String CLIENT_ID = System.getProperty("CLIENT_ID");
37     protected static final String ANALYTICS_ID = System.getProperty("ANALYTICS_ID");
38 
39     // Common constants
40     protected static final long ONE_DAY = 86400000000L; // units microseconds
41     protected static final long MILLI_TO_MICRO =
42             1000; // conversion factor from milli to micro units
43     protected static final String TABLE_PREFIX = "result_";
44     protected static final String CURRENT_PAGE = "#";
45 
46     public enum Page {
47         HOME("VTS Dashboard Home", "/"),
48         PREFERENCES("Preferences", "/show_preferences"),
49         TABLE("", "/show_table"),
50         GRAPH("Profiling", "/show_graph"),
51         COVERAGE("Coverage", "/show_coverage"),
52         PERFORMANCE("Performance Digest", "/show_performance_digest");
53 
54         private final String name;
55         private final String url;
56 
Page(String name, String url)57         Page(String name, String url) {
58             this.name = name;
59             this.url = url;
60         }
61 
getName()62         public String getName() {
63             return name;
64         }
65 
getUrl()66         public String getUrl() {
67             return url;
68         }
69     }
70 
71     /**
72      * Get a list of URL/Display name pairs for the navbar heirarchy.
73      *
74      * @param request The HttpServletRequest object for the page request.
75      * @return a list of 2-entried String arrays in the order [page url, page name]
76      */
getNavbarLinks(HttpServletRequest request)77     public abstract List<String[]> getNavbarLinks(HttpServletRequest request);
78 
79     @Override
doGet(HttpServletRequest request, HttpServletResponse response)80     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
81         // If the user is logged out, allow them to log back in and return to the page.
82         // Set the logout URL to direct back to a login page that directs to the current request.
83         UserService userService = UserServiceFactory.getUserService();
84         User currentUser = userService.getCurrentUser();
85         String requestUri = request.getRequestURI();
86         String requestArgs = request.getQueryString();
87         String loginURI = userService.createLoginURL(requestUri + '?' + requestArgs);
88         String logoutURI = userService.createLogoutURL(loginURI);
89         if (currentUser == null || currentUser.getEmail() == null) {
90             response.sendRedirect(loginURI);
91             return;
92         }
93         request.setAttribute("logoutURL", logoutURI);
94         request.setAttribute("email", currentUser.getEmail());
95         request.setAttribute("analyticsID", new Gson().toJson(ANALYTICS_ID));
96         request.setAttribute("navbarLinksJson", new Gson().toJson(getNavbarLinks(request)));
97         request.setAttribute("navbarLinks", getNavbarLinks(request));
98         response.setContentType("text/html");
99         doGetHandler(request, response);
100     }
101 
102     /**
103      * Implementation of the doGet method to be executed by servlet subclasses.
104      *
105      * @param request The HttpServletRequest object.
106      * @param response The HttpServletResponse object.
107      * @throws IOException
108      */
doGetHandler(HttpServletRequest request, HttpServletResponse response)109     public abstract void doGetHandler(HttpServletRequest request, HttpServletResponse response)
110             throws IOException;
111 }
112