• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.google.apphosting.api.ApiProxy;
20 import java.util.Properties;
21 import java.util.logging.Logger;
22 import javax.servlet.ServletConfig;
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServlet;
25 import javax.servlet.http.HttpServletResponse;
26 
27 /** An abstract class to be subclassed to create API Servlet */
28 public class BaseApiServlet extends HttpServlet {
29 
30     private static final Logger logger = Logger.getLogger(BaseApiServlet.class.getName());
31 
32     /** System Configuration Property class */
33     protected Properties systemConfigProp = new Properties();
34 
35     /** Appengine server host name */
36     protected String hostName;
37 
38     /**
39      * This variable is for maximum number of entities per transaction You can find the detail here
40      * (https://cloud.google.com/datastore/docs/concepts/limits)
41      */
42     protected int MAX_ENTITY_SIZE_PER_TRANSACTION = 300;
43 
44     @Override
init(ServletConfig cfg)45     public void init(ServletConfig cfg) throws ServletException {
46         super.init(cfg);
47 
48         systemConfigProp =
49                 Properties.class.cast(cfg.getServletContext().getAttribute("systemConfigProp"));
50 
51         this.MAX_ENTITY_SIZE_PER_TRANSACTION =
52                 Integer.parseInt(systemConfigProp.getProperty("datastore.maxEntitySize"));
53 
54         ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
55         hostName =
56                 env.getAttributes()
57                         .get("com.google.appengine.runtime.default_version_hostname")
58                         .toString();
59     }
60 
setAccessControlHeaders(HttpServletResponse resp)61     protected void setAccessControlHeaders(HttpServletResponse resp) {
62         resp.setHeader("Access-Control-Allow-Origin", hostName);
63         resp.setHeader("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS, DELETE");
64         resp.addHeader("Access-Control-Allow-Headers", "Content-Type");
65         resp.addHeader("Access-Control-Max-Age", "86400");
66     }
67 }
68