Search in sources :

Example 21 with HttpResponse

use of com.google.api.client.http.HttpResponse in project OpenRefine by OpenRefine.

the class DeAuthorizeCommand method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        String sessionToken = TokenCookie.getToken(request);
        if (sessionToken != null) {
            // No method to do this in Google's client lib, so roll our own
            HttpRequestFactory factory = HTTP_TRANSPORT.createRequestFactory();
            GenericUrl url = new GenericUrl("https://accounts.google.com/o/oauth2/revoke?token=" + sessionToken);
            HttpRequest rqst = factory.buildGetRequest(url);
            HttpResponse resp = rqst.execute();
            if (resp.getStatusCode() != 200) {
                respond(response, String.valueOf(resp.getStatusCode()), resp.getStatusMessage());
            }
            TokenCookie.deleteToken(request, response);
        }
        respond(response, "200 OK", "");
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 22 with HttpResponse

use of com.google.api.client.http.HttpResponse 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")));
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) Method(java.lang.reflect.Method) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Test(org.junit.Test)

Example 23 with HttpResponse

use of com.google.api.client.http.HttpResponse in project pinpoint by naver.

the class HttpRequestIT method execute.

@Test
public void execute() 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.execute();
        response.disconnect();
    } catch (IOException ignored) {
    } finally {
        if (response != null) {
            response.disconnect();
        }
    }
    Method executeMethod = HttpRequest.class.getDeclaredMethod("execute");
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    verifier.verifyTrace(Expectations.event("GOOGLE_HTTP_CLIENT_INTERNAL", executeMethod));
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) Method(java.lang.reflect.Method) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Test(org.junit.Test)

Example 24 with HttpResponse

use of com.google.api.client.http.HttpResponse in project google-cloud-java by GoogleCloudPlatform.

the class HttpStorageRpc method write.

@Override
public void write(String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length, boolean last) {
    try {
        if (length == 0 && !last) {
            return;
        }
        GenericUrl url = new GenericUrl(uploadId);
        HttpRequest httpRequest = storage.getRequestFactory().buildPutRequest(url, new ByteArrayContent(null, toWrite, toWriteOffset, length));
        long limit = destOffset + length;
        StringBuilder range = new StringBuilder("bytes ");
        if (length == 0) {
            range.append('*');
        } else {
            range.append(destOffset).append('-').append(limit - 1);
        }
        range.append('/');
        if (last) {
            range.append(limit);
        } else {
            range.append('*');
        }
        httpRequest.getHeaders().setContentRange(range.toString());
        int code;
        String message;
        IOException exception = null;
        try {
            HttpResponse response = httpRequest.execute();
            code = response.getStatusCode();
            message = response.getStatusMessage();
        } catch (HttpResponseException ex) {
            exception = ex;
            code = ex.getStatusCode();
            message = ex.getStatusMessage();
        }
        if (!last && code != 308 || last && !(code == 200 || code == 201)) {
            if (exception != null) {
                throw exception;
            }
            GoogleJsonError error = new GoogleJsonError();
            error.setCode(code);
            error.setMessage(message);
            throw translate(error);
        }
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) ByteArrayContent(com.google.api.client.http.ByteArrayContent)

Example 25 with HttpResponse

use of com.google.api.client.http.HttpResponse in project google-cloud-java by GoogleCloudPlatform.

the class HttpBigQueryRpc method open.

@Override
public String open(JobConfiguration configuration) {
    try {
        Job loadJob = new Job().setConfiguration(configuration);
        StringBuilder builder = new StringBuilder().append(BASE_RESUMABLE_URI).append(options.getProjectId()).append("/jobs");
        GenericUrl url = new GenericUrl(builder.toString());
        url.set("uploadType", "resumable");
        JsonFactory jsonFactory = bigquery.getJsonFactory();
        HttpRequestFactory requestFactory = bigquery.getRequestFactory();
        HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, loadJob));
        httpRequest.getHeaders().set("X-Upload-Content-Value", "application/octet-stream");
        HttpResponse response = httpRequest.execute();
        return response.getHeaders().getLocation();
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) JsonFactory(com.google.api.client.json.JsonFactory) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) IOException(java.io.IOException) Job(com.google.api.services.bigquery.model.Job)

Aggregations

HttpResponse (com.google.api.client.http.HttpResponse)34 IOException (java.io.IOException)23 HttpRequest (com.google.api.client.http.HttpRequest)21 GenericUrl (com.google.api.client.http.GenericUrl)19 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)11 HttpTransport (com.google.api.client.http.HttpTransport)8 InputStream (java.io.InputStream)8 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)7 Test (org.junit.Test)7 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)6 HttpBackOffIOExceptionHandler (com.google.api.client.http.HttpBackOffIOExceptionHandler)5 HttpBackOffUnsuccessfulResponseHandler (com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler)5 HttpUnsuccessfulResponseHandler (com.google.api.client.http.HttpUnsuccessfulResponseHandler)5 ExponentialBackOff (com.google.api.client.util.ExponentialBackOff)5 HttpResponseException (com.google.api.client.http.HttpResponseException)4 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)4 JsonFactory (com.google.api.client.json.JsonFactory)4 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)4 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)4 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)4