Home | History | Annotate | Download | only in servlet
      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.ArrayList;
     25 import java.util.List;
     26 import java.util.Optional;
     27 import java.util.logging.Level;
     28 import java.util.logging.Logger;
     29 import javax.servlet.RequestDispatcher;
     30 import javax.servlet.ServletException;
     31 import javax.servlet.http.HttpServlet;
     32 import javax.servlet.http.HttpServletRequest;
     33 import javax.servlet.http.HttpServletResponse;
     34 import javax.servlet.http.HttpSession;
     35 
     36 public abstract class BaseServlet extends HttpServlet {
     37     protected final Logger logger = Logger.getLogger(getClass().getName());
     38 
     39     // Environment variables
     40     protected static final String GERRIT_URI = System.getProperty("GERRIT_URI");
     41     protected static final String GERRIT_SCOPE = System.getProperty("GERRIT_SCOPE");
     42     protected static final String CLIENT_ID = System.getProperty("CLIENT_ID");
     43     protected static final String ANALYTICS_ID = System.getProperty("ANALYTICS_ID");
     44 
     45     protected static final String TREE_DEFAULT_PARAM = "treeDefault";
     46 
     47     public enum PageType {
     48         TOT("Test", "/"),
     49         RELEASE("Release", "/show_release"),
     50         COVERAGE_OVERVIEW("Coverage", "/show_coverage_overview"),
     51         PROFILING_LIST("Profiling", "/show_profiling_list"),
     52         TABLE("", "/show_table"),
     53         TREE("", "/show_tree"),
     54         GRAPH("Profiling", "/show_graph"),
     55         COVERAGE("Coverage", "/show_coverage"),
     56         PERFORMANCE_DIGEST("Performance Digest", "/show_performance_digest"),
     57         PLAN_RELEASE("", "/show_plan_release"),
     58         PLAN_RUN("Plan Run", "/show_plan_run"),
     59         PROFILING_OVERVIEW("", "/show_profiling_overview");
     60 
     61         public final String defaultName;
     62         public final String defaultUrl;
     63 
     64         PageType(String defaultName, String defaultUrl) {
     65             this.defaultName = defaultName;
     66             this.defaultUrl = defaultUrl;
     67         }
     68     }
     69 
     70     public static class Page {
     71         private final PageType type;
     72         private final String name;
     73         private final String url;
     74 
     75         public Page(PageType type) {
     76             this.type = type;
     77             this.name = type.defaultName;
     78             this.url = type.defaultUrl;
     79         }
     80 
     81         public Page(PageType type, String name, String url) {
     82             this.type = type;
     83             this.name = type.defaultName + name;
     84             this.url = type.defaultUrl + url;
     85         }
     86 
     87         public Page(PageType type, String url) {
     88             this.type = type;
     89             this.name = type.defaultName;
     90             this.url = type.defaultUrl + url;
     91         }
     92 
     93         public String getName() {
     94             return name;
     95         }
     96 
     97         public String getUrl() {
     98             return url;
     99         }
    100     }
    101 
    102     public static final List<Page> navbarLinks;
    103 
    104     static {
    105         List<Page> links = new ArrayList<>();
    106         links.add(new Page(PageType.TOT));
    107         links.add(new Page(PageType.RELEASE));
    108         links.add(new Page(PageType.COVERAGE_OVERVIEW));
    109         links.add(new Page(PageType.PROFILING_LIST));
    110         navbarLinks = links;
    111     }
    112 
    113     public abstract PageType getNavParentType();
    114 
    115     /**
    116      * Get a list of URL/Display name pairs for the breadcrumb hierarchy.
    117      *
    118      * @param request The HttpServletRequest object for the page request.
    119      * @return a list of Page entries.
    120      */
    121     public abstract List<Page> getBreadcrumbLinks(HttpServletRequest request);
    122 
    123     @Override
    124     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    125         // If the user is logged out, allow them to log back in and return to the page.
    126         // Set the logout URL to direct back to a login page that directs to the current request.
    127         UserService userService = UserServiceFactory.getUserService();
    128         Optional<User> currentUser = Optional.ofNullable(userService.getCurrentUser());
    129         String currentUserEmail =
    130                 currentUser.isPresent()
    131                         ? currentUser.map(user -> user.getEmail().trim()).orElse("")
    132                         : "";
    133         String requestUri = request.getRequestURI();
    134         String requestArgs = request.getQueryString();
    135         String loginURI = userService.createLoginURL(requestUri + '?' + requestArgs);
    136         String logoutURI = userService.createLogoutURL(loginURI);
    137         if (currentUserEmail != "") {
    138 
    139             int activeIndex;
    140             switch (getNavParentType()) {
    141                 case PROFILING_LIST:
    142                     activeIndex = 3;
    143                     break;
    144                 case COVERAGE_OVERVIEW:
    145                     activeIndex = 2;
    146                     break;
    147                 case RELEASE:
    148                     activeIndex = 1;
    149                     break;
    150                 default:
    151                     activeIndex = 0;
    152                     break;
    153             }
    154             if (request.getParameter(TREE_DEFAULT_PARAM) != null) {
    155                 HttpSession session = request.getSession(true);
    156                 boolean treeDefault = request.getParameter(TREE_DEFAULT_PARAM).equals("true");
    157                 session.setAttribute(TREE_DEFAULT_PARAM, treeDefault);
    158             }
    159 
    160             request.setAttribute("serverName", request.getServerName());
    161             request.setAttribute("logoutURL", logoutURI);
    162             request.setAttribute("email", currentUserEmail);
    163             request.setAttribute("analyticsID", new Gson().toJson(ANALYTICS_ID));
    164             request.setAttribute("breadcrumbLinks", getBreadcrumbLinks(request));
    165             request.setAttribute("navbarLinks", navbarLinks);
    166             request.setAttribute("activeIndex", activeIndex);
    167             response.setContentType("text/html");
    168 
    169             if (currentUserEmail.endsWith("@google.com")) {
    170                 doGetHandler(request, response);
    171             } else {
    172                 RequestDispatcher dispatcher =
    173                         request.getRequestDispatcher("WEB-INF/jsp/auth_error.jsp");
    174                 try {
    175                     dispatcher.forward(request, response);
    176                 } catch (ServletException e) {
    177                     logger.log(Level.SEVERE, "Servlet Exception caught : ", e);
    178                 }
    179             }
    180         } else {
    181             response.sendRedirect(loginURI);
    182         }
    183     }
    184 
    185     /**
    186      * Implementation of the doGet method to be executed by servlet subclasses.
    187      *
    188      * @param request The HttpServletRequest object.
    189      * @param response The HttpServletResponse object.
    190      * @throws IOException
    191      */
    192     public abstract void doGetHandler(HttpServletRequest request, HttpServletResponse response)
    193             throws IOException;
    194 }
    195