1 /*
2  * Copyright (c) 2018 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.api;
18 
19 import com.android.vts.entity.BranchEntity;
20 import com.android.vts.entity.TestCoverageStatusEntity;
21 import com.google.gson.Gson;
22 
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25 import java.io.IOException;
26 import java.util.List;
27 import java.util.Objects;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 
31 import com.android.vts.entity.BuildTargetEntity;
32 
33 /** REST endpoint for getting all branch and buildFlavors information. */
34 public class DataRestServlet extends BaseApiServlet {
35 
36     private static final Logger logger = Logger.getLogger(DataRestServlet.class.getName());
37 
38     @Override
doGet(HttpServletRequest request, HttpServletResponse response)39     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
40         String pathInfo = request.getPathInfo();
41         String json = "";
42         if (Objects.nonNull(pathInfo)) {
43             String schKey =
44                     Objects.isNull(request.getParameter("schKey"))
45                             ? ""
46                             : request.getParameter("schKey");
47             if (pathInfo.equalsIgnoreCase("/branch")) {
48                 json = new Gson().toJson(BranchEntity.getByBranch(schKey));
49             } else if (pathInfo.equalsIgnoreCase("/device")) {
50                 json = new Gson().toJson(BuildTargetEntity.getByBuildTarget(schKey));
51             } else if (pathInfo.startsWith("/code/coverage/status/")) {
52                 List<TestCoverageStatusEntity> testCoverageStatusEntityList =
53                         TestCoverageStatusEntity.getAllTestCoverage();
54                 if (pathInfo.endsWith("branch")) {
55                     json =
56                             new Gson()
57                                     .toJson(
58                                             TestCoverageStatusEntity.getBranchSet(
59                                                     testCoverageStatusEntityList));
60                 } else {
61                     json =
62                             new Gson()
63                                     .toJson(
64                                             TestCoverageStatusEntity.getDeviceSet(
65                                                     testCoverageStatusEntityList));
66                 }
67             } else {
68                 json = "{error: 'true', message: 'unexpected path!!!'}";
69                 logger.log(Level.INFO, "Path Info => " + pathInfo);
70                 logger.log(Level.WARNING, "Unknown path access!");
71             }
72         } else {
73             json = "{error: 'true', message: 'the path info is not existed!!!'}";
74         }
75 
76         response.setStatus(HttpServletResponse.SC_OK);
77         response.setContentType("application/json");
78         response.setCharacterEncoding("UTF-8");
79         response.getWriter().write(json);
80     }
81 }
82