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.api; 18 19 import com.android.vts.proto.VtsReportMessage.TestReportMessage; 20 import com.android.vts.util.DatastoreHelper; 21 import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; 22 import com.google.api.client.http.javanet.NetHttpTransport; 23 import com.google.api.client.json.jackson.JacksonFactory; 24 import com.google.api.services.oauth2.Oauth2; 25 import com.google.api.services.oauth2.model.Tokeninfo; 26 import com.google.protobuf.InvalidProtocolBufferException; 27 import java.io.BufferedReader; 28 import java.io.IOException; 29 import java.util.logging.Level; 30 import java.util.logging.Logger; 31 import javax.servlet.http.HttpServlet; 32 import javax.servlet.http.HttpServletRequest; 33 import javax.servlet.http.HttpServletResponse; 34 import org.apache.commons.codec.binary.Base64; 35 import org.json.JSONException; 36 import org.json.JSONObject; 37 38 /** REST endpoint for posting data JSON to the Dashboard. */ 39 @Deprecated 40 public class BigtableLegacyJsonServlet extends HttpServlet { 41 private static final String SERVICE_CLIENT_ID = System.getProperty("SERVICE_CLIENT_ID"); 42 private static final Logger logger = 43 Logger.getLogger(BigtableLegacyJsonServlet.class.getName()); 44 45 @Override doPost(HttpServletRequest request, HttpServletResponse response)46 public void doPost(HttpServletRequest request, HttpServletResponse response) 47 throws IOException { 48 // Retrieve the params 49 String payload = new String(); 50 JSONObject payloadJson; 51 try { 52 String line = null; 53 BufferedReader reader = request.getReader(); 54 while ((line = reader.readLine()) != null) { 55 payload += line; 56 } 57 payloadJson = new JSONObject(payload); 58 } catch (IOException | JSONException e) { 59 response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 60 logger.log(Level.WARNING, "Invalid JSON: " + payload); 61 return; 62 } 63 64 // Verify service account access token. 65 boolean authorized = false; 66 if (payloadJson.has("accessToken")) { 67 String accessToken = payloadJson.getString("accessToken").trim(); 68 GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken); 69 Oauth2 oauth2 = 70 new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credential) 71 .build(); 72 Tokeninfo tokenInfo = oauth2.tokeninfo().setAccessToken(accessToken).execute(); 73 if (tokenInfo.getIssuedTo().equals(SERVICE_CLIENT_ID)) { 74 authorized = true; 75 } 76 } 77 78 if (!authorized) { 79 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 80 return; 81 } 82 83 // Parse the desired action and execute the command 84 try { 85 if (payloadJson.has("verb")) { 86 switch (payloadJson.getString("verb")) { 87 case "createTable": 88 logger.log(Level.INFO, "Deprecated verb: createTable."); 89 break; 90 case "insertRow": 91 insertData(payloadJson); 92 break; 93 default: 94 logger.log(Level.WARNING, 95 "Invalid Datastore REST verb: " + payloadJson.getString("verb")); 96 throw new IOException("Unsupported POST verb."); 97 } 98 } 99 } catch (IOException e) { 100 response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 101 return; 102 } 103 response.setStatus(HttpServletResponse.SC_OK); 104 } 105 106 /** 107 * Inserts a data into the Cloud Datastore 108 * 109 * @param payloadJson The JSON object representing the row to be inserted. Of the form: { 110 * (deprecated) 'tableName' : 'table', (deprecated) 'rowKey' : 'row', (deprecated) 'family' 111 * : 112 * 'family', (deprecated) 'qualifier' : 'qualifier', 'value' : 'value' } 113 * @throws IOException 114 */ insertData(JSONObject payloadJson)115 private void insertData(JSONObject payloadJson) throws IOException { 116 if (!payloadJson.has("value")) { 117 logger.log(Level.WARNING, "Missing attributes for datastore api insertRow()."); 118 return; 119 } 120 try { 121 byte[] value = Base64.decodeBase64(payloadJson.getString("value")); 122 TestReportMessage testReportMessage = TestReportMessage.parseFrom(value); 123 DatastoreHelper.insertData(testReportMessage); 124 } catch (InvalidProtocolBufferException e) { 125 logger.log(Level.WARNING, "Invalid report posted to dashboard."); 126 } 127 } 128 } 129