use of com.google.api.client.http.HttpHeaders in project google-cloud-java by GoogleCloudPlatform.
the class StorageExceptionTest method testStorageException.
@Test
public void testStorageException() {
StorageException exception = new StorageException(500, "message");
assertEquals(500, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertTrue(exception.isRetryable());
exception = new StorageException(502, "message");
assertEquals(502, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertTrue(exception.isRetryable());
exception = new StorageException(503, "message");
assertEquals(503, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertTrue(exception.isRetryable());
exception = new StorageException(504, "message");
assertEquals(504, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertTrue(exception.isRetryable());
exception = new StorageException(429, "message");
assertEquals(429, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertTrue(exception.isRetryable());
exception = new StorageException(408, "message");
assertEquals(408, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertTrue(exception.isRetryable());
exception = new StorageException(400, "message");
assertEquals(400, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertFalse(exception.isRetryable());
IOException cause = new SocketTimeoutException();
exception = new StorageException(cause);
assertNull(exception.getReason());
assertNull(exception.getMessage());
assertTrue(exception.isRetryable());
assertSame(cause, exception.getCause());
GoogleJsonError error = new GoogleJsonError();
error.setCode(503);
error.setMessage("message");
exception = new StorageException(error);
assertEquals(503, exception.getCode());
assertEquals("message", exception.getMessage());
assertTrue(exception.isRetryable());
exception = new StorageException(400, "message", cause);
assertEquals(400, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertFalse(exception.isRetryable());
assertSame(cause, exception.getCause());
HttpResponseException httpResponseException = new HttpResponseException.Builder(404, "Service Unavailable", new HttpHeaders()).build();
exception = new StorageException(httpResponseException);
assertEquals(404, exception.getCode());
assertFalse(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(503, null, new HttpHeaders()).build();
exception = new StorageException(httpResponseException);
assertEquals(503, exception.getCode());
assertTrue(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(502, null, new HttpHeaders()).build();
exception = new StorageException(httpResponseException);
assertEquals(502, exception.getCode());
assertTrue(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(500, null, new HttpHeaders()).build();
exception = new StorageException(httpResponseException);
assertEquals(500, exception.getCode());
assertTrue(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(429, null, new HttpHeaders()).build();
exception = new StorageException(httpResponseException);
assertEquals(429, exception.getCode());
assertTrue(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(408, null, new HttpHeaders()).build();
exception = new StorageException(httpResponseException);
assertEquals(408, exception.getCode());
assertTrue(exception.isRetryable());
}
use of com.google.api.client.http.HttpHeaders in project google-cloud-java by GoogleCloudPlatform.
the class BigQueryExceptionTest method testBigqueryException.
@Test
public void testBigqueryException() {
BigQueryException exception = new BigQueryException(500, "message");
assertEquals(500, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertNull(exception.getError());
assertTrue(exception.isRetryable());
exception = new BigQueryException(502, "message");
assertEquals(502, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertNull(exception.getError());
assertTrue(exception.isRetryable());
exception = new BigQueryException(503, "message");
assertEquals(503, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertNull(exception.getError());
assertTrue(exception.isRetryable());
exception = new BigQueryException(504, "message");
assertEquals(504, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertNull(exception.getError());
assertTrue(exception.isRetryable());
exception = new BigQueryException(400, "message");
assertEquals(400, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertNull(exception.getError());
assertFalse(exception.isRetryable());
BigQueryError error = new BigQueryError("reason", null, null);
exception = new BigQueryException(504, "message", error);
assertEquals(504, exception.getCode());
assertEquals("message", exception.getMessage());
assertEquals("reason", exception.getReason());
assertEquals(error, exception.getError());
assertTrue(exception.isRetryable());
IOException cause = new SocketTimeoutException("socketTimeoutMessage");
exception = new BigQueryException(cause);
assertEquals(BigQueryException.UNKNOWN_CODE, exception.getCode());
assertNull(exception.getReason());
assertEquals("socketTimeoutMessage", exception.getMessage());
assertEquals(cause, exception.getCause());
assertTrue(exception.isRetryable());
assertSame(cause, exception.getCause());
exception = new BigQueryException(504, "message", cause);
assertEquals(504, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertNull(exception.getError());
assertTrue(exception.isRetryable());
assertSame(cause, exception.getCause());
HttpResponseException httpResponseException = new HttpResponseException.Builder(404, "Service Unavailable", new HttpHeaders()).build();
exception = new BigQueryException(httpResponseException);
assertEquals(404, exception.getCode());
assertFalse(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(504, null, new HttpHeaders()).build();
exception = new BigQueryException(httpResponseException);
assertEquals(504, exception.getCode());
assertTrue(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(503, null, new HttpHeaders()).build();
exception = new BigQueryException(httpResponseException);
assertEquals(503, exception.getCode());
assertTrue(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(502, null, new HttpHeaders()).build();
exception = new BigQueryException(httpResponseException);
assertEquals(502, exception.getCode());
assertTrue(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(500, null, new HttpHeaders()).build();
exception = new BigQueryException(httpResponseException);
assertEquals(500, exception.getCode());
assertTrue(exception.isRetryable());
}
use of com.google.api.client.http.HttpHeaders in project google-cloud-java by GoogleCloudPlatform.
the class HttpTransportOptions method getHttpRequestInitializer.
/**
* Returns a request initializer responsible for initializing requests according to service
* options.
*/
public HttpRequestInitializer getHttpRequestInitializer(final ServiceOptions<?, ?> serviceOptions) {
Credentials scopedCredentials = serviceOptions.getScopedCredentials();
final HttpRequestInitializer delegate = scopedCredentials != null && scopedCredentials != NoCredentials.getInstance() ? new HttpCredentialsAdapter(scopedCredentials) : null;
return new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
if (delegate != null) {
delegate.initialize(httpRequest);
}
if (connectTimeout >= 0) {
httpRequest.setConnectTimeout(connectTimeout);
}
if (readTimeout >= 0) {
httpRequest.setReadTimeout(readTimeout);
}
HttpHeaders headers = httpRequest.getHeaders();
headers.set("x-goog-api-client", getXGoogApiClientHeader(serviceOptions));
}
};
}
use of com.google.api.client.http.HttpHeaders in project beam by apache.
the class GcsUtil method enqueueCopy.
private void enqueueCopy(final GcsPath from, final GcsPath to, BatchRequest batch) throws IOException {
Storage.Objects.Copy copyRequest = storageClient.objects().copy(from.getBucket(), from.getObject(), to.getBucket(), to.getObject(), null);
copyRequest.queue(batch, new JsonBatchCallback<StorageObject>() {
@Override
public void onSuccess(StorageObject obj, HttpHeaders responseHeaders) {
LOG.debug("Successfully copied {} to {}", from, to);
}
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
throw new IOException(String.format("Error trying to copy %s to %s: %s", from, to, e));
}
});
}
use of com.google.api.client.http.HttpHeaders in project beam by apache.
the class GcsUtil method enqueueDelete.
private void enqueueDelete(final GcsPath file, BatchRequest batch) throws IOException {
Storage.Objects.Delete deleteRequest = storageClient.objects().delete(file.getBucket(), file.getObject());
deleteRequest.queue(batch, new JsonBatchCallback<Void>() {
@Override
public void onSuccess(Void obj, HttpHeaders responseHeaders) {
LOG.debug("Successfully deleted {}", file);
}
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
throw new IOException(String.format("Error trying to delete %s: %s", file, e));
}
});
}
Aggregations