use of com.google.api.client.http.HttpTransport in project local-data-aragopedia by aragonopendata.
the class GoogleDriveAPI method authorize.
private static GoogleCredential authorize() throws IOException, GeneralSecurityException {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(Prop.acountId).setServiceAccountScopes(SCOPES).setServiceAccountPrivateKeyFromP12File(new java.io.File(Prop.p12File)).build();
return credential;
}
use of com.google.api.client.http.HttpTransport in project ddf by codice.
the class MetadataConfigurationParser method buildEntityDescriptor.
private void buildEntityDescriptor(String entityDescription) throws IOException {
EntityDescriptor entityDescriptor = null;
entityDescription = entityDescription.trim();
if (entityDescription.startsWith(HTTPS) || entityDescription.startsWith(HTTP)) {
if (entityDescription.startsWith(HTTP)) {
LOGGER.warn("Retrieving metadata via HTTP instead of HTTPS. The metadata configuration is unsafe!!!");
}
PropertyResolver propertyResolver = new PropertyResolver(entityDescription);
HttpTransport httpTransport = new NetHttpTransport();
HttpRequest httpRequest = httpTransport.createRequestFactory().buildGetRequest(new GenericUrl(propertyResolver.getResolvedString()));
httpRequest.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setBackOffRequired(HttpBackOffUnsuccessfulResponseHandler.BackOffRequired.ALWAYS));
httpRequest.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()));
ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
ListenableFuture<HttpResponse> httpResponseFuture = service.submit(httpRequest::execute);
Futures.addCallback(httpResponseFuture, new FutureCallback<HttpResponse>() {
@Override
public void onSuccess(HttpResponse httpResponse) {
if (httpResponse != null) {
try {
String parsedResponse = httpResponse.parseAsString();
buildEntityDescriptor(parsedResponse);
} catch (IOException e) {
LOGGER.info("Unable to parse metadata from: {}", httpResponse.getRequest().getUrl().toString(), e);
}
}
}
@Override
public void onFailure(Throwable throwable) {
LOGGER.info("Unable to retrieve metadata.", throwable);
}
});
service.shutdown();
} else if (entityDescription.startsWith(FILE + System.getProperty("ddf.home"))) {
String pathStr = StringUtils.substringAfter(entityDescription, FILE);
Path path = Paths.get(pathStr);
if (Files.isReadable(path)) {
try (InputStream fileInputStream = Files.newInputStream(path)) {
entityDescriptor = readEntityDescriptor(new InputStreamReader(fileInputStream, "UTF-8"));
}
}
} else if (entityDescription.startsWith("<") && entityDescription.endsWith(">")) {
entityDescriptor = readEntityDescriptor(new StringReader(entityDescription));
} else {
LOGGER.info("Skipping unknown metadata configuration value: {}", entityDescription);
}
if (entityDescriptor != null) {
entityDescriptorMap.put(entityDescriptor.getEntityID(), entityDescriptor);
if (updateCallback != null) {
updateCallback.accept(entityDescriptor);
}
}
}
use of com.google.api.client.http.HttpTransport in project ddf by codice.
the class PaosInInterceptor method getHttpResponse.
HttpResponseWrapper getHttpResponse(String responseConsumerURL, String soapResponse, Message message) throws IOException {
//This used to use the ApacheHttpTransport which appeared to not work with 2 way TLS auth but this one does
HttpTransport httpTransport = new NetHttpTransport();
HttpContent httpContent = new InputStreamContent(TEXT_XML, new ByteArrayInputStream(soapResponse.getBytes("UTF-8")));
//this handles redirects for us
((InputStreamContent) httpContent).setRetrySupported(true);
HttpRequest httpRequest = httpTransport.createRequestFactory().buildPostRequest(new GenericUrl(responseConsumerURL), httpContent);
HttpUnsuccessfulResponseHandler httpUnsuccessfulResponseHandler = (request, response, supportsRetry) -> {
String redirectLocation = response.getHeaders().getLocation();
if (request.getFollowRedirects() && HttpStatusCodes.isRedirect(response.getStatusCode()) && redirectLocation != null) {
String method = (String) message.get(Message.HTTP_REQUEST_METHOD);
HttpContent content = null;
if (!isRedirectable(method)) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
message.setContent(OutputStream.class, byteArrayOutputStream);
BodyWriter bodyWriter = new BodyWriter();
bodyWriter.handleMessage(message);
content = new InputStreamContent((String) message.get(Message.CONTENT_TYPE), new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
}
// resolve the redirect location relative to the current location
request.setUrl(new GenericUrl(request.getUrl().toURL(redirectLocation)));
request.setRequestMethod(method);
request.setContent(content);
// remove Authorization and If-* headers
request.getHeaders().setAuthorization((String) null);
request.getHeaders().setIfMatch(null);
request.getHeaders().setIfNoneMatch(null);
request.getHeaders().setIfModifiedSince(null);
request.getHeaders().setIfUnmodifiedSince(null);
request.getHeaders().setIfRange(null);
request.getHeaders().setCookie((String) ((List) response.getHeaders().get("set-cookie")).get(0));
return true;
}
return false;
};
httpRequest.setUnsuccessfulResponseHandler(httpUnsuccessfulResponseHandler);
httpRequest.getHeaders().put(SOAP_ACTION, HTTP_WWW_OASIS_OPEN_ORG_COMMITTEES_SECURITY);
//has 20 second timeout by default
HttpResponse httpResponse = httpRequest.execute();
HttpResponseWrapper httpResponseWrapper = new HttpResponseWrapper();
httpResponseWrapper.statusCode = httpResponse.getStatusCode();
httpResponseWrapper.content = httpResponse.getContent();
return httpResponseWrapper;
}
use of com.google.api.client.http.HttpTransport in project druid by druid-io.
the class GoogleStorageDruidModule method getGoogleStorage.
@Provides
@LazySingleton
public GoogleStorage getGoogleStorage(final GoogleAccountConfig config) throws IOException, GeneralSecurityException {
LOG.info("Building Cloud Storage Client...");
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory);
if (credential.createScopedRequired()) {
credential = credential.createScoped(StorageScopes.all());
}
Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential).setApplicationName(APPLICATION_NAME).build();
return new GoogleStorage(storage);
}
use of com.google.api.client.http.HttpTransport in project pinpoint by naver.
the class HttpRequestIT method executeAsync.
@Test
public void executeAsync() throws Exception {
HttpTransport NET_HTTP_TRANSPORT = new NetHttpTransport();
HttpRequestFactory requestFactory = NET_HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) {
}
});
GenericUrl url = new GenericUrl("http://google.com");
HttpRequest request = null;
HttpResponse response = null;
try {
request = requestFactory.buildGetRequest(url);
response = request.executeAsync().get();
response.disconnect();
} catch (IOException ignored) {
} finally {
if (response != null) {
response.disconnect();
}
}
Method executeAsyncMethod = HttpRequest.class.getDeclaredMethod("executeAsync", Executor.class);
Method callMethod = Callable.class.getDeclaredMethod("call");
Method executeMethod = HttpRequest.class.getDeclaredMethod("execute");
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.printCache();
// async
verifier.verifyTrace(Expectations.async(Expectations.event("GOOGLE_HTTP_CLIENT_INTERNAL", executeAsyncMethod), Expectations.event("ASYNC", "Asynchronous Invocation")));
}
Aggregations