use of com.google.api.client.http.GenericUrl in project elasticsearch by elastic.
the class RetryHttpInitializerWrapperTests method testIOExceptionRetry.
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));
}
use of com.google.api.client.http.GenericUrl in project che by eclipse.
the class OAuthAuthenticator method callback.
/**
* Process callback request.
*
* @param requestUrl
* request URI. URI should contain OAuth token and OAuth verifier.
* @return id of authenticated user
* @throws OAuthAuthenticationException
* if authentication failed or {@code requestUrl} does not contain required parameters.
*/
String callback(final URL requestUrl) throws OAuthAuthenticationException {
try {
final GenericUrl callbackUrl = new GenericUrl(requestUrl.toString());
if (callbackUrl.getFirst(OAUTH_TOKEN_PARAM_KEY) == null) {
throw new OAuthAuthenticationException("Missing oauth_token parameter");
}
if (callbackUrl.getFirst(OAUTH_VERIFIER_PARAM_KEY) == null) {
throw new OAuthAuthenticationException("Missing oauth_verifier parameter");
}
final String state = (String) callbackUrl.getFirst(STATE_PARAM_KEY);
String requestMethod = getParameterFromState(state, REQUEST_METHOD_PARAM_KEY);
String signatureMethod = getParameterFromState(state, SIGNATURE_METHOD_PARAM_KEY);
final String oauthTemporaryToken = (String) callbackUrl.getFirst(OAUTH_TOKEN_PARAM_KEY);
OAuthGetAccessToken getAccessToken;
if (requestMethod != null && "post".equals(requestMethod.toLowerCase())) {
getAccessToken = new OAuthPostAccessToken(accessTokenUri);
} else {
getAccessToken = new OAuthGetAccessToken(accessTokenUri);
}
getAccessToken.consumerKey = clientId;
getAccessToken.temporaryToken = oauthTemporaryToken;
getAccessToken.verifier = (String) callbackUrl.getFirst(OAUTH_VERIFIER_PARAM_KEY);
getAccessToken.transport = httpTransport;
if (signatureMethod != null && "rsa".equals(signatureMethod.toLowerCase())) {
getAccessToken.signer = getOAuthRsaSigner();
} else {
getAccessToken.signer = getOAuthHmacSigner(clientSecret, sharedTokenSecrets.remove(oauthTemporaryToken));
}
final OAuthCredentialsResponse credentials = getAccessToken.execute();
String userId = getParameterFromState(state, USER_ID_PARAM_KEY);
credentialsStoreLock.lock();
try {
final OAuthCredentialsResponse userId2Credential = credentialsStore.get(userId);
if (userId2Credential == null) {
credentialsStore.put(userId, credentials);
} else {
userId2Credential.token = credentials.token;
userId2Credential.tokenSecret = credentials.tokenSecret;
}
} finally {
credentialsStoreLock.unlock();
}
return userId;
} catch (Exception e) {
throw new OAuthAuthenticationException(e.getMessage());
}
}
use of com.google.api.client.http.GenericUrl in project beam by apache.
the class UploadIdResponseInterceptorTest method testResponseLogs.
/**
* Check that a response logs with the correct log.
*/
@Test
public void testResponseLogs() throws IOException {
new UploadIdResponseInterceptor().interceptResponse(buildHttpResponse("abc", null, "type"));
GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL);
url.put("uploadType", "type");
String worker = System.getProperty("worker_id");
expectedLogs.verifyDebug("Upload ID for url " + url + " on worker " + worker + " is abc");
}
use of com.google.api.client.http.GenericUrl in project google-cloud-java by GoogleCloudPlatform.
the class HttpStorageRpc method open.
@Override
public String open(StorageObject object, Map<Option, ?> options) {
try {
Insert req = storage.objects().insert(object.getBucket(), object);
GenericUrl url = req.buildHttpRequest().getUrl();
String scheme = url.getScheme();
String host = url.getHost();
String path = "/upload" + url.getRawPath();
url = new GenericUrl(scheme + "://" + host + path);
url.set("uploadType", "resumable");
url.set("name", object.getName());
for (Option option : options.keySet()) {
Object content = option.get(options);
if (content != null) {
url.set(option.value(), content.toString());
}
}
JsonFactory jsonFactory = storage.getJsonFactory();
HttpRequestFactory requestFactory = storage.getRequestFactory();
HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, object));
HttpHeaders requestHeaders = httpRequest.getHeaders();
requestHeaders.set("X-Upload-Content-Type", firstNonNull(object.getContentType(), "application/octet-stream"));
String key = Option.CUSTOMER_SUPPLIED_KEY.getString(options);
if (key != null) {
BaseEncoding base64 = BaseEncoding.base64();
HashFunction hashFunction = Hashing.sha256();
requestHeaders.set("x-goog-encryption-algorithm", "AES256");
requestHeaders.set("x-goog-encryption-key", key);
requestHeaders.set("x-goog-encryption-key-sha256", base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes()));
}
HttpResponse response = httpRequest.execute();
if (response.getStatusCode() != 200) {
GoogleJsonError error = new GoogleJsonError();
error.setCode(response.getStatusCode());
error.setMessage(response.getStatusMessage());
throw translate(error);
}
return response.getHeaders().getLocation();
} catch (IOException ex) {
throw translate(ex);
}
}
use of com.google.api.client.http.GenericUrl in project google-cloud-java by GoogleCloudPlatform.
the class HttpBigQueryRpc method write.
@Override
public Job write(String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length, boolean last) {
try {
GenericUrl url = new GenericUrl(uploadId);
HttpRequest httpRequest = bigquery.getRequestFactory().buildPutRequest(url, new ByteArrayContent(null, toWrite, toWriteOffset, length));
httpRequest.setParser(bigquery.getObjectParser());
long limit = destOffset + length;
StringBuilder range = new StringBuilder("bytes ");
range.append(destOffset).append('-').append(limit - 1).append('/');
if (last) {
range.append(limit);
} else {
range.append('*');
}
httpRequest.getHeaders().setContentRange(range.toString());
int code;
String message;
IOException exception = null;
HttpResponse response = null;
try {
response = httpRequest.execute();
code = response.getStatusCode();
message = response.getStatusMessage();
} catch (HttpResponseException ex) {
exception = ex;
code = ex.getStatusCode();
message = ex.getStatusMessage();
}
if (!last && code != HTTP_RESUME_INCOMPLETE || last && !(code == HTTP_OK || code == HTTP_CREATED)) {
if (exception != null) {
throw exception;
}
throw new BigQueryException(code, message);
}
return last && response != null ? response.parseAs(Job.class) : null;
} catch (IOException ex) {
throw translate(ex);
}
}
Aggregations