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 static com.googlecode.objectify.ObjectifyService.ofy;
20 
21 import com.android.vts.entity.CoverageEntity;
22 import com.android.vts.entity.RoleEntity;
23 import com.android.vts.entity.TestEntity;
24 import com.android.vts.entity.TestRunEntity;
25 import com.android.vts.entity.UserEntity;
26 import com.google.appengine.api.datastore.DatastoreService;
27 import com.google.appengine.api.datastore.DatastoreServiceFactory;
28 import com.google.appengine.api.datastore.Entity;
29 import com.google.appengine.api.datastore.Key;
30 import com.google.appengine.api.datastore.KeyFactory;
31 import com.google.appengine.api.datastore.Query;
32 import com.google.gson.Gson;
33 import com.googlecode.objectify.Ref;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Optional;
41 import java.util.logging.Level;
42 import javax.servlet.RequestDispatcher;
43 import javax.servlet.ServletException;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46 
47 /**
48  * Servlet for handling requests to show code coverage.
49  */
50 public class ShowCoverageServlet extends BaseServlet {
51 
52   private static final String COVERAGE_JSP = "WEB-INF/jsp/show_coverage.jsp";
53   private static final String TREE_JSP = "WEB-INF/jsp/show_tree.jsp";
54 
55   @Override
getNavParentType()56   public PageType getNavParentType() {
57     return PageType.TOT;
58   }
59 
60   @Override
getBreadcrumbLinks(HttpServletRequest request)61   public List<Page> getBreadcrumbLinks(HttpServletRequest request) {
62     List<Page> links = new ArrayList<>();
63     String testName = request.getParameter("testName");
64     links.add(new Page(PageType.TABLE, testName, "?testName=" + testName));
65 
66     String startTime = request.getParameter("startTime");
67     links.add(new Page(PageType.COVERAGE, "?testName=" + testName + "&startTime=" + startTime));
68     return links;
69   }
70 
71   @Override
doGetHandler(HttpServletRequest request, HttpServletResponse response)72   public void doGetHandler(HttpServletRequest request, HttpServletResponse response)
73       throws IOException {
74     RequestDispatcher dispatcher = null;
75 
76     String test = request.getParameter("testName");
77     String timeString = request.getParameter("startTime");
78 
79     Boolean isModerator = false;
80     String currentUserEmail = request.getAttribute("email").toString();
81     Optional<UserEntity> userEntityOptional = Optional
82         .ofNullable(UserEntity.getUser(currentUserEmail));
83     if (userEntityOptional.isPresent()) {
84       Ref refRole = Ref.create(RoleEntity.getRole("coverage-moderator"));
85       isModerator = userEntityOptional.get().getRoles().contains(refRole);
86     }
87 
88     // Process the time key requested
89     long time = -1;
90     try {
91       time = Long.parseLong(timeString);
92     } catch (NumberFormatException e) {
93       request.setAttribute("testName", test);
94       dispatcher = request.getRequestDispatcher(TREE_JSP);
95       return;
96     }
97 
98     com.googlecode.objectify.Key testKey = com.googlecode.objectify.Key
99         .create(TestEntity.class, test);
100     com.googlecode.objectify.Key testRunKey = com.googlecode.objectify.Key
101         .create(testKey, TestRunEntity.class, time);
102 
103     List<CoverageEntity> coverageEntityList = ofy().load().type(CoverageEntity.class)
104         .ancestor(testRunKey).list();
105 
106     Collections.sort(coverageEntityList, CoverageEntity.isIgnoredComparator);
107 
108     request.setAttribute("isModerator", isModerator);
109     request.setAttribute("testName", request.getParameter("testName"));
110     request.setAttribute("gerritURI", new Gson().toJson(GERRIT_URI));
111     request.setAttribute("gerritScope", new Gson().toJson(GERRIT_SCOPE));
112     request.setAttribute("clientId", new Gson().toJson(CLIENT_ID));
113     request.setAttribute("startTime", request.getParameter("startTime"));
114     request.setAttribute("coverageEntityList", coverageEntityList);
115 
116     dispatcher = request.getRequestDispatcher(COVERAGE_JSP);
117     try {
118       dispatcher.forward(request, response);
119     } catch (ServletException e) {
120       logger.log(Level.SEVERE, "Servlet Exception caught : ", e);
121     }
122   }
123 }
124