From 6a63048585642a823cc7d0a339146e6cdd670277 Mon Sep 17 00:00:00 2001 From: Young Gyu Park Date: Fri, 9 Feb 2018 16:03:38 +0900 Subject: GCS Log File Info Access from appspot project Test: Tested with browser on GAE(go/vts-web-staging/show_gcs_log) Bug: 73090948 Change-Id: I4061ff2b8cf29886d6387c987a5fa59ee888a3a5 --- .gitignore | 5 +- pom.xml | 22 ++- .../com/android/vts/servlet/ShowGcsLogServlet.java | 149 +++++++++++++++++++++ src/main/webapp/WEB-INF/appengine-web.xml | 5 +- src/main/webapp/WEB-INF/jsp/show_gcs_log.jsp | 69 ++++++++++ src/main/webapp/WEB-INF/web.xml | 10 ++ 6 files changed, 252 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/android/vts/servlet/ShowGcsLogServlet.java create mode 100644 src/main/webapp/WEB-INF/jsp/show_gcs_log.jsp diff --git a/.gitignore b/.gitignore index 9a94e36..80791fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Google App Engine generated folder appengine-generated/ +# Secutiry Key file folder +src/main/webapp/WEB-INF/keys + # Java *.class @@ -42,4 +45,4 @@ service-account.json Session.vim .netrwhist *~ -tags \ No newline at end of file +tags diff --git a/pom.xml b/pom.xml index 7008b51..ee3e319 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,10 @@ vts-dashboard + s~google.com:android-vts-staging + 4 + 1.9.62 + @@ -32,6 +36,10 @@ + + + + 1.8 1.8 true @@ -99,7 +107,7 @@ com.google.appengine appengine-api-1.0-sdk - 1.9.60 + ${appengine.version} @@ -118,19 +126,19 @@ com.google.appengine appengine-testing - 1.9.60 + ${appengine.version} test com.google.appengine appengine-api-stubs - 1.9.60 + ${appengine.version} test com.google.appengine appengine-tools-sdk - 1.9.60 + ${appengine.version} test @@ -172,7 +180,11 @@ com.google.appengine appengine-maven-plugin - 1.9.60 + ${appengine.version} + + ${app.id} + ${app.version} + diff --git a/src/main/java/com/android/vts/servlet/ShowGcsLogServlet.java b/src/main/java/com/android/vts/servlet/ShowGcsLogServlet.java new file mode 100644 index 0000000..337494f --- /dev/null +++ b/src/main/java/com/android/vts/servlet/ShowGcsLogServlet.java @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2018 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you + * may not use this file except in compliance with the License. You may + * obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.android.vts.servlet; + +import com.google.auth.oauth2.ServiceAccountCredentials; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.Storage.BlobListOption; +import com.google.cloud.storage.StorageOptions; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.logging.Level; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * A GCS log servlet read log zip file from Google Cloud Storage bucket and show the content in it + * from the zip file by unarchiving it + */ +@SuppressWarnings("serial") +public class ShowGcsLogServlet extends BaseServlet { + + private static final String GCS_LOG_JSP = "WEB-INF/jsp/show_gcs_log.jsp"; + + private static final String GCS_PROJECT_ID = System.getProperty("GCS_PROJECT_ID"); + private static final String GCS_KEY_FILE = System.getProperty("GCS_KEY_FILE"); + private static final String GCS_BUCKET_NAME = System.getProperty("GCS_BUCKET_NAME"); + + /** + * This is the key file to access vtslab-gcs project. It will allow the dashboard to have a full + * controll of the bucket. + */ + private InputStream keyFileInputStream; + + /** This is the instance of java google storage library */ + private Storage storage; + + @Override + public void init(ServletConfig cfg) throws ServletException { + super.init(cfg); + keyFileInputStream = + this.getServletContext().getResourceAsStream("/WEB-INF/keys/" + GCS_KEY_FILE); + + try { + storage = + StorageOptions.newBuilder() + .setProjectId(GCS_PROJECT_ID) + .setCredentials( + ServiceAccountCredentials.fromStream(keyFileInputStream)) + .build() + .getService(); + } catch (IOException e) { + logger.log(Level.SEVERE, "Error on creating storage instance!"); + } + } + + @Override + public PageType getNavParentType() { + return PageType.TOT; + } + + @Override + public List getBreadcrumbLinks(HttpServletRequest request) { + return null; + } + + @Override + public void doGetHandler(HttpServletRequest request, HttpServletResponse response) + throws IOException { + + String path = request.getParameter("path") == null ? "" : request.getParameter("path"); + Path pathInfo = Paths.get(path); + + Bucket vtsReportBucket = storage.get(GCS_BUCKET_NAME); + + List dirList = new ArrayList<>(); + List fileList = new ArrayList<>(); + if (pathInfo.endsWith(".zip")) { + Blob blobFile = vtsReportBucket.get(path); + } else { + + logger.log(Level.INFO, "path info => " + pathInfo); + logger.log(Level.INFO, "path name count => " + pathInfo.getNameCount()); + + BlobListOption[] listOptions; + if (pathInfo.getNameCount() == 0) { + listOptions = new BlobListOption[] {BlobListOption.currentDirectory()}; + } else { + if (pathInfo.getNameCount() <= 1) { + dirList.add("/"); + } else { + dirList.add(pathInfo.getParent().toString()); + } + listOptions = + new BlobListOption[] { + BlobListOption.currentDirectory(), + BlobListOption.prefix(pathInfo.toString() + "/") + }; + } + + Iterator blobIterator = vtsReportBucket.list(listOptions).iterateAll(); + while (blobIterator.hasNext()) { + Blob blob = blobIterator.next(); + logger.log(Level.INFO, "blob name => " + blob); + if (blob.isDirectory()) { + logger.log(Level.INFO, "directory name => " + blob.getName()); + dirList.add(blob.getName()); + } else { + logger.log(Level.INFO, "file name => " + blob.getName()); + fileList.add(blob.getName()); + } + } + } + + response.setStatus(HttpServletResponse.SC_OK); + request.setAttribute("dirList", dirList); + request.setAttribute("fileList", fileList); + request.setAttribute("path", path); + RequestDispatcher dispatcher = request.getRequestDispatcher(GCS_LOG_JSP); + try { + dispatcher.forward(request, response); + } catch (ServletException e) { + logger.log(Level.SEVERE, "Servlet Excpetion caught : ", e); + } + } +} diff --git a/src/main/webapp/WEB-INF/appengine-web.xml b/src/main/webapp/WEB-INF/appengine-web.xml index a7d50bf..4023fb1 100644 --- a/src/main/webapp/WEB-INF/appengine-web.xml +++ b/src/main/webapp/WEB-INF/appengine-web.xml @@ -13,8 +13,6 @@ --> - s~google.com:android-vts-staging - 4 true true java8 @@ -28,6 +26,9 @@ + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/jsp/show_gcs_log.jsp b/src/main/webapp/WEB-INF/jsp/show_gcs_log.jsp new file mode 100644 index 0000000..5c175ea --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/show_gcs_log.jsp @@ -0,0 +1,69 @@ +<%-- + ~ Copyright (c) 2018 Google Inc. All Rights Reserved. + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); you + ~ may not use this file except in compliance with the License. You may + ~ obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + ~ implied. See the License for the specific language governing + ~ permissions and limitations under the License. + --%> +<%@ page contentType='text/html;charset=UTF-8' language='java' %> +<%@ taglib prefix='fn' uri='http://java.sun.com/jsp/jstl/functions' %> +<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core'%> + + + <%@ include file="header.jsp" %> + + + + + + + + + + +
+
+

Directory List

+ +

+ + + + (Move to Parent) + + +

+ + +
+
+
Current Directory Path : ${path}
+
+

File List

+ +

+ + + +

+ + +
+
+
+ <%@ include file="footer.jsp" %> + + diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index dc1cf76..4c13bd7 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -85,6 +85,11 @@ Copyright 2016 Google Inc. All Rights Reserved. com.android.vts.servlet.ShowTestAcknowledgmentServlet + + show_gcs_log + com.android.vts.servlet.ShowGcsLogServlet + + test_data com.android.vts.api.TestDataForDevServlet @@ -210,6 +215,11 @@ Copyright 2016 Google Inc. All Rights Reserved. /show_test_acknowledgments/* + + show_gcs_log + /show_gcs_log/* + + bigtable_legacy /api/bigtable/* -- cgit v1.2.3