/** * @return a URL to continue pushing the BLOB to, or {@code null} if the BLOB already exists on * the registry */ @Nullable @Override public String handleResponse(Response response) throws RegistryErrorException { switch (response.getStatusCode()) { case HttpStatusCodes.STATUS_CODE_CREATED: // The BLOB exists in the registry. return null; case HttpURLConnection.HTTP_ACCEPTED: return extractLocationHeader(response); default: throw buildRegistryErrorException( "Received unrecognized status code " + response.getStatusCode()); } }
@Test public void testPush_missingBlobs() throws IOException, RegistryException { RegistryClient registryClient = new RegistryClient(null, "gcr.io", "distroless/java"); ManifestTemplate manifestTemplate = registryClient.pullManifest("latest"); registryClient = new RegistryClient(null, "localhost:5000", "busybox"); try { registryClient.pushManifest((V22ManifestTemplate) manifestTemplate, "latest"); Assert.fail("Pushing manifest without its BLOBs should fail"); } catch (RegistryErrorException ex) { HttpResponseException httpResponseException = (HttpResponseException) ex.getCause(); Assert.assertEquals( HttpStatusCodes.STATUS_CODE_BAD_REQUEST, httpResponseException.getStatusCode()); } }
@Test public void testHandleHttpResponseException() throws IOException, RegistryErrorException { HttpResponseException mockHttpResponseException = Mockito.mock(HttpResponseException.class); Mockito.when(mockHttpResponseException.getStatusCode()) .thenReturn(HttpStatusCodes.STATUS_CODE_NOT_FOUND); ErrorResponseTemplate emptyErrorResponseTemplate = new ErrorResponseTemplate() .addError(new ErrorEntryTemplate(ErrorCodes.BLOB_UNKNOWN.name(), "some message")); Mockito.when(mockHttpResponseException.getContent()) .thenReturn(Blobs.writeToString(JsonTemplateMapper.toBlob(emptyErrorResponseTemplate))); BlobDescriptor blobDescriptor = testBlobChecker.handleHttpResponseException(mockHttpResponseException); Assert.assertNull(blobDescriptor); }
@Test public void testHandleHttpResponseException_hasOtherErrors() throws IOException, RegistryErrorException { HttpResponseException mockHttpResponseException = Mockito.mock(HttpResponseException.class); Mockito.when(mockHttpResponseException.getStatusCode()) .thenReturn(HttpStatusCodes.STATUS_CODE_NOT_FOUND); ErrorResponseTemplate emptyErrorResponseTemplate = new ErrorResponseTemplate() .addError(new ErrorEntryTemplate(ErrorCodes.BLOB_UNKNOWN.name(), "some message")) .addError(new ErrorEntryTemplate(ErrorCodes.MANIFEST_UNKNOWN.name(), "some message")); Mockito.when(mockHttpResponseException.getContent()) .thenReturn(Blobs.writeToString(JsonTemplateMapper.toBlob(emptyErrorResponseTemplate))); try { testBlobChecker.handleHttpResponseException(mockHttpResponseException); Assert.fail("Non-BLOB_UNKNOWN errors should not be handled"); } catch (HttpResponseException ex) { Assert.assertEquals(mockHttpResponseException, ex); } }
@Test public void testHandleHttpResponseException_notBlobUnknown() throws IOException, RegistryErrorException { HttpResponseException mockHttpResponseException = Mockito.mock(HttpResponseException.class); Mockito.when(mockHttpResponseException.getStatusCode()) .thenReturn(HttpStatusCodes.STATUS_CODE_NOT_FOUND); ErrorResponseTemplate emptyErrorResponseTemplate = new ErrorResponseTemplate(); Mockito.when(mockHttpResponseException.getContent()) .thenReturn(Blobs.writeToString(JsonTemplateMapper.toBlob(emptyErrorResponseTemplate))); try { testBlobChecker.handleHttpResponseException(mockHttpResponseException); Assert.fail("Non-BLOB_UNKNOWN errors should not be handled"); } catch (HttpResponseException ex) { Assert.assertEquals(mockHttpResponseException, ex); } }
@Test public void tsetHandleHttpResponseException_noHeader() throws HttpResponseException { Mockito.when(mockHttpResponseException.getStatusCode()) .thenReturn(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED); Mockito.when(mockHttpResponseException.getHeaders()).thenReturn(mockHeaders); Mockito.when(mockHeaders.getAuthenticate()).thenReturn(null); try { testAuthenticationMethodRetriever.handleHttpResponseException(mockHttpResponseException); Assert.fail( "Authentication method retriever should fail if 'WWW-Authenticate' header is not found"); } catch (RegistryErrorException ex) { Assert.assertThat( ex.getMessage(), CoreMatchers.containsString("'WWW-Authenticate' header not found")); } }
@Test public void testHandleHttpResponseException_badAuthenticationMethod() throws HttpResponseException { String authenticationMethod = "bad authentication method"; Mockito.when(mockHttpResponseException.getStatusCode()) .thenReturn(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED); Mockito.when(mockHttpResponseException.getHeaders()).thenReturn(mockHeaders); Mockito.when(mockHeaders.getAuthenticate()).thenReturn(authenticationMethod); try { testAuthenticationMethodRetriever.handleHttpResponseException(mockHttpResponseException); Assert.fail( "Authentication method retriever should fail if 'WWW-Authenticate' header failed to parse"); } catch (RegistryErrorException ex) { Assert.assertThat( ex.getMessage(), CoreMatchers.containsString( "Failed get authentication method from 'WWW-Authenticate' header")); } }
@Test public void testHandleHttpResponseException_pass() throws RegistryErrorException, HttpResponseException, MalformedURLException { String authenticationMethod = "Bearer realm=\"https://somerealm\",service=\"someservice\",scope=\"somescope\""; Mockito.when(mockHttpResponseException.getStatusCode()) .thenReturn(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED); Mockito.when(mockHttpResponseException.getHeaders()).thenReturn(mockHeaders); Mockito.when(mockHeaders.getAuthenticate()).thenReturn(authenticationMethod); RegistryAuthenticator registryAuthenticator = testAuthenticationMethodRetriever.handleHttpResponseException(mockHttpResponseException); Assert.assertEquals( new URL("https://somerealm?service=someservice&scope=repository:someImageName:pull"), registryAuthenticator.getAuthenticationUrl()); }
public void testSimpleRetry() throws Exception { FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, 3); MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder() .build(); MockSleeper mockSleeper = new MockSleeper(); RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, mockSleeper, TimeValue.timeValueSeconds(5)); Compute client = new Compute.Builder(fakeTransport, new JacksonFactory(), null) .setHttpRequestInitializer(retryHttpInitializerWrapper) .setApplicationName("test") .build(); HttpRequest request = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null); HttpResponse response = request.execute(); assertThat(mockSleeper.getCount(), equalTo(3)); assertThat(response.getStatusCode(), equalTo(200)); }
public void testIOExceptionRetry() throws Exception { FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, 1, true); MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder() .build(); MockSleeper mockSleeper = new MockSleeper(); RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, mockSleeper, TimeValue.timeValueMillis(500)); Compute client = new Compute.Builder(fakeTransport, new JacksonFactory(), null) .setHttpRequestInitializer(retryHttpInitializerWrapper) .setApplicationName("test") .build(); HttpRequest request = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null); HttpResponse response = request.execute(); assertThat(mockSleeper.getCount(), equalTo(1)); assertThat(response.getStatusCode(), equalTo(200)); }
@Override public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { callCount ++; if (!HttpMethods.GET.equals(method) || !expectedUrl.equals(url)) { // Throw RuntimeException to fail the test. throw new RuntimeException(); } return new MockLowLevelHttpRequest() { @Override public LowLevelHttpResponse execute() throws IOException { MockLowLevelHttpResponse response = new MockLowLevelHttpResponse(); response.setStatusCode(HttpStatusCodes.STATUS_CODE_OK); response.setContentType(Json.MEDIA_TYPE); response.setContent(jsonResponse); return response; } }; }
Optional<Table> getTable(String projectId, String datasetId, String tableId, boolean rowsExist) throws IOException { try { Table ret = client.tables().get(projectId, datasetId, tableId).execute(); if(rowsExist && ret.getNumRows().compareTo(BigInteger.ZERO) <= 0) { return Optional.absent(); } return Optional.of(ret); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) { return Optional.absent(); } throw e; } }
@Test public void testNonExistentObjectReturnsEmptyResult() throws IOException { GcsOptions pipelineOptions = gcsOptionsWithTestCredential(); GcsUtil gcsUtil = pipelineOptions.getGcsUtil(); Storage mockStorage = Mockito.mock(Storage.class); gcsUtil.setStorageClient(mockStorage); Storage.Objects mockStorageObjects = Mockito.mock(Storage.Objects.class); Storage.Objects.Get mockStorageGet = Mockito.mock(Storage.Objects.Get.class); GcsPath pattern = GcsPath.fromUri("gs://testbucket/testdirectory/nonexistentfile"); GoogleJsonResponseException expectedException = googleJsonResponseException(HttpStatusCodes.STATUS_CODE_NOT_FOUND, "It don't exist", "Nothing here to see"); when(mockStorage.objects()).thenReturn(mockStorageObjects); when(mockStorageObjects.get(pattern.getBucket(), pattern.getObject())).thenReturn( mockStorageGet); when(mockStorageGet.execute()).thenThrow(expectedException); assertEquals(Collections.EMPTY_LIST, gcsUtil.expand(pattern)); }
@Test public void testAccessDeniedObjectThrowsIOException() throws IOException { GcsOptions pipelineOptions = gcsOptionsWithTestCredential(); GcsUtil gcsUtil = pipelineOptions.getGcsUtil(); Storage mockStorage = Mockito.mock(Storage.class); gcsUtil.setStorageClient(mockStorage); Storage.Objects mockStorageObjects = Mockito.mock(Storage.Objects.class); Storage.Objects.Get mockStorageGet = Mockito.mock(Storage.Objects.Get.class); GcsPath pattern = GcsPath.fromUri("gs://testbucket/testdirectory/accessdeniedfile"); GoogleJsonResponseException expectedException = googleJsonResponseException(HttpStatusCodes.STATUS_CODE_FORBIDDEN, "Waves hand mysteriously", "These aren't the buckets you're looking for"); when(mockStorage.objects()).thenReturn(mockStorageObjects); when(mockStorageObjects.get(pattern.getBucket(), pattern.getObject())).thenReturn( mockStorageGet); when(mockStorageGet.execute()).thenThrow(expectedException); thrown.expect(IOException.class); thrown.expectMessage("Unable to get the file object for path"); gcsUtil.expand(pattern); }
@Test public void testFileSizeWhenFileNotFoundNonBatch() throws Exception { MockLowLevelHttpResponse notFoundResponse = new MockLowLevelHttpResponse(); notFoundResponse.setContent(""); notFoundResponse.setStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND); MockHttpTransport mockTransport = new MockHttpTransport.Builder().setLowLevelHttpResponse(notFoundResponse).build(); GcsOptions pipelineOptions = gcsOptionsWithTestCredential(); GcsUtil gcsUtil = pipelineOptions.getGcsUtil(); gcsUtil.setStorageClient(new Storage(mockTransport, Transport.getJsonFactory(), null)); thrown.expect(FileNotFoundException.class); gcsUtil.fileSize(GcsPath.fromComponents("testbucket", "testobject")); }
@Test public void testCreateBucketAccessErrors() throws IOException { GcsOptions pipelineOptions = gcsOptionsWithTestCredential(); GcsUtil gcsUtil = pipelineOptions.getGcsUtil(); Storage mockStorage = Mockito.mock(Storage.class); gcsUtil.setStorageClient(mockStorage); Storage.Buckets mockStorageObjects = Mockito.mock(Storage.Buckets.class); Storage.Buckets.Insert mockStorageInsert = Mockito.mock(Storage.Buckets.Insert.class); BackOff mockBackOff = BackOffAdapter.toGcpBackOff(FluentBackoff.DEFAULT.backoff()); GoogleJsonResponseException expectedException = googleJsonResponseException(HttpStatusCodes.STATUS_CODE_FORBIDDEN, "Waves hand mysteriously", "These aren't the buckets you're looking for"); when(mockStorage.buckets()).thenReturn(mockStorageObjects); when(mockStorageObjects.insert( any(String.class), any(Bucket.class))).thenReturn(mockStorageInsert); when(mockStorageInsert.execute()) .thenThrow(expectedException); thrown.expect(AccessDeniedException.class); gcsUtil.createBucket("a", new Bucket(), mockBackOff, new FastNanoClockAndSleeper()); }
@Test public void testBucketDoesNotExistBecauseOfAccessError() throws IOException { GcsOptions pipelineOptions = gcsOptionsWithTestCredential(); GcsUtil gcsUtil = pipelineOptions.getGcsUtil(); Storage mockStorage = Mockito.mock(Storage.class); gcsUtil.setStorageClient(mockStorage); Storage.Buckets mockStorageObjects = Mockito.mock(Storage.Buckets.class); Storage.Buckets.Get mockStorageGet = Mockito.mock(Storage.Buckets.Get.class); BackOff mockBackOff = BackOffAdapter.toGcpBackOff( FluentBackoff.DEFAULT.backoff()); GoogleJsonResponseException expectedException = googleJsonResponseException(HttpStatusCodes.STATUS_CODE_FORBIDDEN, "Waves hand mysteriously", "These aren't the buckets you're looking for"); when(mockStorage.buckets()).thenReturn(mockStorageObjects); when(mockStorageObjects.get("testbucket")).thenReturn(mockStorageGet); when(mockStorageGet.execute()) .thenThrow(expectedException); assertFalse(gcsUtil.bucketAccessible(GcsPath.fromComponents("testbucket", "testobject"), mockBackOff, new FastNanoClockAndSleeper())); }
@Test public void testBucketDoesNotExist() throws IOException { GcsOptions pipelineOptions = gcsOptionsWithTestCredential(); GcsUtil gcsUtil = pipelineOptions.getGcsUtil(); Storage mockStorage = Mockito.mock(Storage.class); gcsUtil.setStorageClient(mockStorage); Storage.Buckets mockStorageObjects = Mockito.mock(Storage.Buckets.class); Storage.Buckets.Get mockStorageGet = Mockito.mock(Storage.Buckets.Get.class); BackOff mockBackOff = BackOffAdapter.toGcpBackOff( FluentBackoff.DEFAULT.backoff()); when(mockStorage.buckets()).thenReturn(mockStorageObjects); when(mockStorageObjects.get("testbucket")).thenReturn(mockStorageGet); when(mockStorageGet.execute()) .thenThrow(googleJsonResponseException(HttpStatusCodes.STATUS_CODE_NOT_FOUND, "It don't exist", "Nothing here to see")); assertFalse(gcsUtil.bucketAccessible(GcsPath.fromComponents("testbucket", "testobject"), mockBackOff, new FastNanoClockAndSleeper())); }
@Test public void testGetBucketNotExists() throws IOException { GcsOptions pipelineOptions = gcsOptionsWithTestCredential(); GcsUtil gcsUtil = pipelineOptions.getGcsUtil(); Storage mockStorage = Mockito.mock(Storage.class); gcsUtil.setStorageClient(mockStorage); Storage.Buckets mockStorageObjects = Mockito.mock(Storage.Buckets.class); Storage.Buckets.Get mockStorageGet = Mockito.mock(Storage.Buckets.Get.class); BackOff mockBackOff = BackOffAdapter.toGcpBackOff( FluentBackoff.DEFAULT.backoff()); when(mockStorage.buckets()).thenReturn(mockStorageObjects); when(mockStorageObjects.get("testbucket")).thenReturn(mockStorageGet); when(mockStorageGet.execute()) .thenThrow(googleJsonResponseException(HttpStatusCodes.STATUS_CODE_NOT_FOUND, "It don't exist", "Nothing here to see")); thrown.expect(FileNotFoundException.class); thrown.expectMessage("It don't exist"); gcsUtil.getBucket(GcsPath.fromComponents("testbucket", "testobject"), mockBackOff, new FastNanoClockAndSleeper()); }
@Override public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry) throws IOException { if (!supportsRetry) { return false; } if (response.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) { authoriser.refresh(); return true; } // check if back-off is required for this response if (isRequired(response)) { try { return BackOffUtils.next(sleeper, backOff); } catch (InterruptedException exception) { // ignore } } return false; }
void createDataset(String projectId, Dataset dataset) throws IOException { try { client.datasets().insert(projectId, dataset) .execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_CONFLICT) { logger.debug("Dataset already exists: {}:{}", dataset.getDatasetReference()); } else { throw e; } } }
void deleteDataset(String projectId, String datasetId) throws IOException { if (datasetExists(projectId, datasetId)) { deleteTables(projectId, datasetId); try { client.datasets().delete(projectId, datasetId).execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) { // Already deleted return; } throw e; } } }
void createTable(String projectId, Table table) throws IOException { String datasetId = table.getTableReference().getDatasetId(); try { client.tables().insert(projectId, datasetId, table) .execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_CONFLICT) { logger.debug("Table already exists: {}:{}.{}", projectId, datasetId, table.getTableReference().getTableId()); } else { throw e; } } }
@Before public void setUp() throws Exception { accessDenied = googleJsonResponseException( HttpStatusCodes.STATUS_CODE_FORBIDDEN, "Forbidden", "Forbidden"); statusOk = googleJsonResponseException( HttpStatusCodes.STATUS_CODE_OK, "A reason", "ok"); notFound = googleJsonResponseException( HttpStatusCodes.STATUS_CODE_NOT_FOUND, "Not found", "Not found"); badRange = googleJsonResponseException( ApiErrorExtractor.STATUS_CODE_RANGE_NOT_SATISFIABLE, "Bad range", "Bad range"); alreadyExists = googleJsonResponseException( 409, "409", "409"); resourceNotReady = googleJsonResponseException( 400, ApiErrorExtractor.RESOURCE_NOT_READY_REASON_CODE, "Resource not ready"); // This works because googleJsonResponseException takes final ErrorInfo ErrorInfo errorInfo = new ErrorInfo(); errorInfo.setReason(ApiErrorExtractor.RATE_LIMITED_REASON_CODE); notRateLimited = googleJsonResponseException(POSSIBLE_RATE_LIMIT, errorInfo, ""); errorInfo.setDomain(ApiErrorExtractor.USAGE_LIMITS_DOMAIN); rateLimited = googleJsonResponseException(POSSIBLE_RATE_LIMIT, errorInfo, ""); errorInfo.setDomain(ApiErrorExtractor.GLOBAL_DOMAIN); bigqueryRateLimited = googleJsonResponseException(POSSIBLE_RATE_LIMIT, errorInfo, ""); }
/** * Validates itemNotFound(). */ @Test public void testItemNotFound() { // Check success cases. assertTrue(errorExtractor.itemNotFound(notFound)); GoogleJsonError gje = new GoogleJsonError(); gje.setCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND); assertTrue(errorExtractor.itemNotFound(gje)); assertTrue(errorExtractor.itemNotFound(new IOException(notFound))); assertTrue(errorExtractor.itemNotFound(new IOException(new IOException(notFound)))); // Check failure case. assertFalse(errorExtractor.itemNotFound(statusOk)); assertFalse(errorExtractor.itemNotFound(new IOException())); assertFalse(errorExtractor.itemNotFound(new IOException(new IOException()))); }
/** * Creates a Cloud Pub/Sub topic if it doesn't exist. * * @param client Pubsub client object. * @throws IOException when API calls to Cloud Pub/Sub fails. */ private void setupTopic(final Pubsub client) throws IOException { String fullName = String.format("projects/%s/topics/%s", PubsubUtils.getProjectId(), PubsubUtils.getAppTopicName()); try { client.projects().topics().get(fullName).execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) { // Create the topic if it doesn't exist client.projects().topics() .create(fullName, new Topic()) .execute(); } else { throw e; } } }
@GuardedBy("fetchDataLock") private String fetchAndCacheData() throws IOException { long currentTimeInMillis = getCurrentTimeInMillis(); HttpRequest httpRequest = httpTransport.createRequestFactory().buildGetRequest(new GenericUrl(url)); HttpResponse httpResponse = httpRequest.execute(); if (httpResponse.getStatusCode() != HttpStatusCodes.STATUS_CODE_OK) { throw new IOException("Unexpected status code = " + httpResponse.getStatusCode()); } String data; InputStream contentStream = httpResponse.getContent(); try { InputStreamReader reader = new InputStreamReader(contentStream, UTF_8); data = readerToString(reader); } finally { contentStream.close(); } synchronized (instanceStateLock) { this.cachedTimeInMillis = currentTimeInMillis; this.cacheExpirationDurationInMillis = getExpirationDurationInSeconds(httpResponse.getHeaders()) * 1000; this.cachedData = data; } return data; }
@Test public void shouldThrowOnNonSuccessHttpResponses() throws Exception { httpResponseBuilder = new HttpResponseBuilder().setStatusCode(HttpStatusCodes.STATUS_CODE_NO_CONTENT); KeysDownloader instance = newInstanceForTests(); try { instance.download(); fail(); } catch (IOException expected) { assertTrue( "Message " + expected.getMessage() + " should contain " + HttpStatusCodes.STATUS_CODE_NO_CONTENT, expected.getMessage().contains(Integer.toString(HttpStatusCodes.STATUS_CODE_NO_CONTENT))); } }
@Test public void testMissingBody() throws IOException { HttpContent httpContent = new ByteArrayContent("application/json", new byte[]{}); GenericUrl url = new GenericUrl(); url.setScheme("http"); url.setHost("localhost"); url.setPort(port); url.setRawPath("/tox"); HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory(); HttpRequest httpRequest = requestFactory.buildPostRequest(url, httpContent); HttpResponse httpResponse = httpRequest.execute(); Assert.assertEquals(HttpStatusCodes.STATUS_CODE_OK, httpResponse.getStatusCode()); Reader reader = new InputStreamReader(httpResponse.getContent(), Charsets.UTF_8); JsonRpcResponse response = JsonRpcResponse.fromJson(new JsonParser().parse(reader) .getAsJsonObject()); Assert.assertTrue(response.isError()); Assert.assertEquals(400, response.error().status().code()); }
/** * Logs the specified request and response information. * * <p>Note that in order to avoid any temptation to consume the contents of the response, this * does <em>not</em> take an {@link com.google.api.client.http.HttpResponse} object, but instead * accepts the status code and message from the response. */ public void logRequest( @Nullable HttpRequest request, int statusCode, @Nullable String statusMessage) { boolean isSuccess = HttpStatusCodes.isSuccess(statusCode); if (!loggerDelegate.isSummaryLoggable(isSuccess) && !loggerDelegate.isDetailsLoggable(isSuccess)) { return; } // Populate the RequestInfo builder from the request. RequestInfo requestInfo = buildRequestInfo(request); // Populate the ResponseInfo builder from the response. ResponseInfo responseInfo = buildResponseInfo(request, statusCode, statusMessage); RemoteCallReturn.Builder remoteCallReturnBuilder = new RemoteCallReturn.Builder().withRequestInfo(requestInfo).withResponseInfo(responseInfo); if (!isSuccess) { remoteCallReturnBuilder.withException( new ReportException(String.format("%s: %s", statusCode, statusMessage))); } RemoteCallReturn remoteCallReturn = remoteCallReturnBuilder.build(); loggerDelegate.logRequestSummary(remoteCallReturn); loggerDelegate.logRequestDetails(remoteCallReturn); }
@Override public BlobDescriptor handleHttpResponseException(HttpResponseException httpResponseException) throws RegistryErrorException, HttpResponseException { if (httpResponseException.getStatusCode() != HttpStatusCodes.STATUS_CODE_NOT_FOUND) { throw httpResponseException; } // Finds a BLOB_UNKNOWN error response code. String errorContent = httpResponseException.getContent(); if (errorContent == null) { // TODO: The Google HTTP client gives null content for HEAD requests. Make the content never be null, even for HEAD requests. return null; } else { try { ErrorResponseTemplate errorResponse = JsonTemplateMapper.readJson(errorContent, ErrorResponseTemplate.class); List<ErrorEntryTemplate> errors = errorResponse.getErrors(); if (errors.size() == 1) { ErrorCodes errorCode = ErrorCodes.valueOf(errors.get(0).getCode()); if (errorCode.equals(ErrorCodes.BLOB_UNKNOWN)) { return null; } } } catch (IOException ex) { throw new RegistryErrorExceptionBuilder(getActionDescription(), ex) .addReason("Failed to parse registry error response body") .build(); } } // BLOB_UNKNOWN was not found as a error response code. throw httpResponseException; }
@Override public RegistryAuthenticator handleHttpResponseException( HttpResponseException httpResponseException) throws HttpResponseException, RegistryErrorException { // Only valid for status code of '401 Unauthorized'. if (httpResponseException.getStatusCode() != HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) { throw httpResponseException; } // Checks if the 'WWW-Authenticate' header is present. String authenticationMethod = httpResponseException.getHeaders().getAuthenticate(); if (authenticationMethod == null) { throw new RegistryErrorExceptionBuilder(getActionDescription(), httpResponseException) .addReason("'WWW-Authenticate' header not found") .build(); } // Parses the header to retrieve the components. try { return RegistryAuthenticator.fromAuthenticationMethod( authenticationMethod, registryEndpointProperties.getImageName()); } catch (RegistryAuthenticationFailedException | MalformedURLException ex) { throw new RegistryErrorExceptionBuilder(getActionDescription(), ex) .addReason("Failed get authentication method from 'WWW-Authenticate' header") .build(); } }
public void testRetryWaitTooLong() throws Exception { TimeValue maxWaitTime = TimeValue.timeValueMillis(10); int maxRetryTimes = 50; FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, maxRetryTimes); JsonFactory jsonFactory = new JacksonFactory(); MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder() .build(); MockSleeper oneTimeSleeper = new MockSleeper() { @Override public void sleep(long millis) throws InterruptedException { Thread.sleep(maxWaitTime.getMillis()); super.sleep(0); // important number, use this to get count } }; RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, oneTimeSleeper, maxWaitTime); Compute client = new Compute.Builder(fakeTransport, jsonFactory, null) .setHttpRequestInitializer(retryHttpInitializerWrapper) .setApplicationName("test") .build(); HttpRequest request1 = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null); try { request1.execute(); fail("Request should fail if wait too long"); } catch (HttpResponseException e) { assertThat(e.getStatusCode(), equalTo(HttpStatusCodes.STATUS_CODE_SERVER_ERROR)); // should only retry once. assertThat(oneTimeSleeper.getCount(), lessThan(maxRetryTimes)); } }
public String requestNewAccessToken(GuildSettings settings, AESEncryption encryption) { try { String body = "client_id=" + clientData.getClientId() + "&client_secret=" + clientData.getClientSecret() + "&refresh_token=" + encryption.decrypt(settings.getEncryptedRefreshToken()) + "&grant_type=refresh_token"; com.mashape.unirest.http.HttpResponse<JsonNode> httpResponse = Unirest.post("https://www.googleapis.com/oauth2/v4/token").header("Content-Type", "application/x-www-form-urlencoded").body(body).asJson(); if (httpResponse.getStatus() == HttpStatusCodes.STATUS_CODE_OK) { Type type = new TypeToken<AuthRefreshResponse>() { }.getType(); AuthRefreshResponse response = new Gson().fromJson(httpResponse.getBody().toString(), type); //Update Db data. settings.setEncryptedAccessToken(encryption.encrypt(response.access_token)); DatabaseManager.getManager().updateSettings(settings); //Okay, we can return the access token to be used when this method is called. return response.access_token; } else { //Failed to get OK. Send debug info. ExceptionHandler.sendDebug(null, "Error requesting new access token.", "Status code: " + httpResponse.getStatus() + " | " + httpResponse.getStatusText() + " | " + httpResponse.getBody().toString(), this.getClass()); return null; } } catch (Exception e) { //Error occurred, lets just log it and return null. ExceptionHandler.sendException(null, "Failed to request new access token.", e, this.getClass()); return null; } }
/** Create a topic if it doesn't exist. */ public static void createTopic(Pubsub client, String fullTopicName) throws IOException { try { client.projects().topics().get(fullTopicName).execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) { Topic topic = client.projects().topics().create(fullTopicName, new Topic()).execute(); System.out.printf("Topic %s was created.\n", topic.getName()); } } }
/** * Create a topic if it doesn't exist. */ public static void createTopic(Pubsub client, String fullTopicName) throws IOException { try { client.projects().topics().get(fullTopicName).execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) { Topic topic = client.projects().topics() .create(fullTopicName, new Topic()) .execute(); System.out.printf("Topic %s was created.\n", topic.getName()); } } }
/** * Fetches the service configuration with the given service name and service version. * * @param serviceName the given service name * @param serviceVersion the given service version * @return a {@link Service} object generated by the JSON response from Google Service Management. */ private Service fetch(String serviceName, @Nullable String serviceVersion) { Preconditions.checkArgument( !Strings.isNullOrEmpty(serviceName), "service name must be specified"); if (serviceVersion == null) { serviceVersion = fetchLatestServiceVersion(serviceName); } final HttpResponse httpResponse; try { httpResponse = serviceManagement.services().configs().get(serviceName, serviceVersion) .setFields(FIELD_MASKS) .executeUnparsed(); } catch (IOException exception) { throw new ServiceConfigException(exception); } int statusCode = httpResponse.getStatusCode(); if (statusCode != HttpStatusCodes.STATUS_CODE_OK) { String message = MessageFormat.format( "Failed to fetch service config (status code {0})", statusCode); throw new ServiceConfigException(message); } Service service = parseHttpResponse(httpResponse); validateServiceConfig(service, serviceName, serviceVersion); return service; }
/** * Create a topic if it doesn't exist. */ public static void createTopic(Pubsub client, String fullTopicName) throws IOException { try { client.projects().topics().get(fullTopicName).execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) { Topic topic = client.projects().topics() .create(fullTopicName, new Topic()) .execute(); System.out.printf("Topic %s was created.%n", topic.getName()); } } }
@Test public void testGetSizeBytesWhenFileNotFoundBatch() throws Exception { JsonFactory jsonFactory = new JacksonFactory(); String contentBoundary = "batch_foobarbaz"; String contentBoundaryLine = "--" + contentBoundary; String endOfContentBoundaryLine = "--" + contentBoundary + "--"; GenericJson error = new GenericJson() .set("error", new GenericJson().set("code", 404)); error.setFactory(jsonFactory); String content = contentBoundaryLine + "\n" + "Content-Type: application/http\n" + "\n" + "HTTP/1.1 404 Not Found\n" + "Content-Length: -1\n" + "\n" + error.toString() + "\n" + "\n" + endOfContentBoundaryLine + "\n"; thrown.expect(FileNotFoundException.class); MockLowLevelHttpResponse notFoundResponse = new MockLowLevelHttpResponse() .setContentType("multipart/mixed; boundary=" + contentBoundary) .setContent(content) .setStatusCode(HttpStatusCodes.STATUS_CODE_OK); MockHttpTransport mockTransport = new MockHttpTransport.Builder().setLowLevelHttpResponse(notFoundResponse).build(); GcsUtil gcsUtil = gcsOptionsWithTestCredential().getGcsUtil(); gcsUtil.setStorageClient(new Storage(mockTransport, Transport.getJsonFactory(), null)); gcsUtil.fileSizes(ImmutableList.of(GcsPath.fromComponents("testbucket", "testobject"))); }
@Test public void testPackageUploadFailsWithPermissionsErrorGivesDetailedMessage() throws Exception { File tmpFile = makeFileWithContents("file.txt", "This is a test!"); when(mockGcsUtil.getObjects(anyListOf(GcsPath.class))) .thenReturn(ImmutableList.of(StorageObjectOrIOException.create( new FileNotFoundException("some/path")))); when(mockGcsUtil.create(any(GcsPath.class), anyString())) .thenThrow(new IOException("Failed to write to GCS path " + STAGING_PATH, googleJsonResponseException( HttpStatusCodes.STATUS_CODE_FORBIDDEN, "Permission denied", "Test message"))); try (PackageUtil directPackageUtil = PackageUtil.withExecutorService(MoreExecutors.newDirectExecutorService())) { directPackageUtil.stageClasspathElements( ImmutableList.of(tmpFile.getAbsolutePath()), STAGING_PATH, fastNanoClockAndSleeper, createOptions); fail("Expected RuntimeException"); } catch (RuntimeException e) { assertThat("Expected RuntimeException wrapping IOException.", e.getCause(), instanceOf(RuntimeException.class)); assertThat("Expected IOException containing detailed message.", e.getCause().getCause(), instanceOf(IOException.class)); assertThat(e.getCause().getCause().getMessage(), Matchers.allOf( Matchers.containsString("Uploaded failed due to permissions error"), Matchers.containsString( "Stale credentials can be resolved by executing 'gcloud auth application-default " + "login'"))); } finally { verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class)); verify(mockGcsUtil).create(any(GcsPath.class), anyString()); verifyNoMoreInteractions(mockGcsUtil); } }