DatastoreExportServlet extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { String outputUrlPrefix = request.getParameter("output_url_prefix"); if (outputUrlPrefix == null || !outputUrlPrefix.matches("^gs: response.setStatus(HttpServletResponse.SC_CONFLICT); response.setContentType("text/plain"); response.getWriter().println("Error: Must provide a valid output_url_prefix."); } else { URL url = new URL("https: HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.addRequestProperty("Content-Type", "application/json"); ArrayList<String> scopes = new ArrayList<String>(); scopes.add("https: final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService(); final AppIdentityService.GetAccessTokenResult accessToken = AppIdentityServiceFactory.getAppIdentityService().getAccessToken(scopes); connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken()); JSONObject exportRequest = new JSONObject(); if (!outputUrlPrefix.endsWith("/")) { String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); outputUrlPrefix = outputUrlPrefix + "/" + timeStamp + "/"; } exportRequest.put("output_url_prefix", outputUrlPrefix); JSONObject entityFilter = new JSONObject(); String[] kinds = request.getParameterValues("kind"); if (kinds != null) { JSONArray kindsJson = new JSONArray(kinds); entityFilter.put("kinds", kindsJson); } String[] namespaces = request.getParameterValues("namespace_id"); if (namespaces != null) { JSONArray namespacesJson = new JSONArray(namespaces); entityFilter.put("namespaceIds", namespacesJson); } exportRequest.put("entityFilter", entityFilter); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); exportRequest.write(writer); writer.close(); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { InputStream s = connection.getErrorStream(); InputStreamReader r = new InputStreamReader(s, StandardCharsets.UTF_8); String errorMessage = String.format( "got error (%d) response %s from %s", connection.getResponseCode(), CharStreams.toString(r), connection.toString()); log.warning(errorMessage); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.setContentType("text/plain"); response.getWriter().println( "Failed to initiate export."); return; } JSONObject exportResponse = new JSONObject(new JSONTokener(connection.getInputStream())); response.setContentType("text/plain"); response.getWriter().println( "Export started:\n" + exportResponse.toString(4)); } }  @Override void doGet(HttpServletRequest request, HttpServletResponse response);  }
@Test public void badOutputUrlReturnsError() throws Exception { when(mockRequest.getParameter("output_url_prefix")).thenReturn("gs:bucket/"); servletUnderTest.doGet(mockRequest, mockResponse); assertWithMessage("CloudDatastoreExport response") .that(responseWriter.toString()) .contains("Must provide a valid output_url_prefix"); }