Search in sources :

Example 46 with HttpTransport

use of com.google.api.client.http.HttpTransport in project cogcomp-nlp by CogComp.

the class QueryMQL method getCursorAndResponse.

public JSONObject getCursorAndResponse(String mqlQuery, String cursor) throws IOException, ParseException {
    HttpTransport httpTransport = new NetHttpTransport();
    HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
    JSONParser parser = new JSONParser();
    GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/mqlread");
    url.put("query", mqlQuery);
    url.put("key", apikey);
    url.put("cursor", cursor);
    logger.debug("QUERY URL: " + url.toString());
    HttpRequest request = requestFactory.buildGetRequest(url);
    HttpResponse httpResponse = request.execute();
    JSONObject response = (JSONObject) parser.parse(httpResponse.parseAsString());
    return response;
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) JSONObject(org.json.simple.JSONObject) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpResponse(com.google.api.client.http.HttpResponse) JSONParser(org.json.simple.parser.JSONParser) GenericUrl(com.google.api.client.http.GenericUrl)

Example 47 with HttpTransport

use of com.google.api.client.http.HttpTransport in project cogcomp-nlp by CogComp.

the class QueryMQL method getResponse.

public JSONObject getResponse(String mqlQuery) throws IOException, ParseException {
    HttpTransport httpTransport = new NetHttpTransport();
    HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
    JSONParser parser = new JSONParser();
    GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/mqlread");
    url.put("query", mqlQuery);
    url.put("key", apikey);
    logger.debug("Querying Freebase QUERY URL: " + url.toString());
    HttpRequest request = requestFactory.buildGetRequest(url);
    HttpResponse httpResponse;
    try {
        httpResponse = request.execute();
    } catch (HttpResponseException e) {
        e.printStackTrace();
        int statusCode = e.getStatusCode();
        logger.error("StatusCode " + statusCode);
        logger.error("Query URL was " + url.toString());
        logger.error("Query was " + mqlQuery);
        if (// max limit reached for a day
        statusCode == 403) {
            System.exit(-1);
        }
        return null;
    } catch (SocketTimeoutException e) {
        e.printStackTrace();
        return null;
    }
    JSONObject response = (JSONObject) parser.parse(httpResponse.parseAsString());
    return response;
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) SocketTimeoutException(java.net.SocketTimeoutException) JSONObject(org.json.simple.JSONObject) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpResponse(com.google.api.client.http.HttpResponse) JSONParser(org.json.simple.parser.JSONParser) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl)

Example 48 with HttpTransport

use of com.google.api.client.http.HttpTransport in project ddf by codice.

the class PaosInInterceptor method getHttpResponse.

@VisibleForTesting
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 ByteArrayContent(TEXT_XML, soapResponse.getBytes("UTF-8"));
    HttpRequest httpRequest = httpTransport.createRequestFactory().buildPostRequest(new GenericUrl(responseConsumerURL), httpContent);
    HttpUnsuccessfulResponseHandler httpUnsuccessfulResponseHandler = getHttpUnsuccessfulResponseHandler(message);
    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();
    httpResponseWrapper.headers = httpResponse.getHeaders().entrySet();
    return httpResponseWrapper;
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpTransport(com.google.api.client.http.HttpTransport) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpUnsuccessfulResponseHandler(com.google.api.client.http.HttpUnsuccessfulResponseHandler) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) ByteArrayContent(com.google.api.client.http.ByteArrayContent) HttpContent(com.google.api.client.http.HttpContent) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 49 with HttpTransport

use of com.google.api.client.http.HttpTransport in project quickutil by quickutil.

the class GAUtil method buildData.

private static Data buildData(String clientID, InputStream p12InputStream, String applicationName) {
    try {
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        KeyStore keystore = KeyStore.getInstance("PKCS12");
        keystore.load(p12InputStream, null);
        PrivateKey privateKey = (PrivateKey) keystore.getKey("privatekey", "notasecret".toCharArray());
        GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(clientID).setServiceAccountPrivateKey(privateKey).setServiceAccountScopes(Collections.singleton(AnalyticsScopes.ANALYTICS_READONLY)).build();
        return new Analytics.Builder(httpTransport, jsonFactory, credential).setApplicationName(applicationName).build().data();
    } catch (Exception e) {
        LOGGER.error(Symbol.BLANK, e);
        return null;
    }
}
Also used : HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) PrivateKey(java.security.PrivateKey) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) KeyStore(java.security.KeyStore) Analytics(com.google.api.services.analytics.Analytics) MissingParametersException(com.quickutil.platform.exception.MissingParametersException)

Example 50 with HttpTransport

use of com.google.api.client.http.HttpTransport in project googleads-java-lib by googleads.

the class DependencyBootstrapperImplTest method testGetNonSessionUtility.

/**
 * Tests that the bootstrapper is able to retrieve an instance of a type that is not annotated
 * with {@link SessionUtility}.
 */
@Test
public void testGetNonSessionUtility() {
    HttpTransport httpTransport = bootstrapper.getInstanceOf(session, HttpTransport.class);
    assertThat(httpTransport, Matchers.instanceOf(NetHttpTransport.class));
}
Also used : NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) Test(org.junit.Test)

Aggregations

HttpTransport (com.google.api.client.http.HttpTransport)106 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)40 JsonFactory (com.google.api.client.json.JsonFactory)35 HttpRequest (com.google.api.client.http.HttpRequest)29 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)28 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)28 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)27 IOException (java.io.IOException)24 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)21 HttpResponse (com.google.api.client.http.HttpResponse)21 GenericUrl (com.google.api.client.http.GenericUrl)15 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)12 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)11 Credential (com.google.api.client.auth.oauth2.Credential)10 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)10 Storage (com.google.api.services.storage.Storage)9 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)8 Test (org.junit.Test)8 Monitor (org.datatransferproject.api.launcher.Monitor)7 ErrorTransport (com.google.api.client.googleapis.json.GoogleJsonErrorTest.ErrorTransport)6