Search in sources :

Example 16 with GenericUrl

use of com.google.api.client.http.GenericUrl 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 17 with GenericUrl

use of com.google.api.client.http.GenericUrl 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 18 with GenericUrl

use of com.google.api.client.http.GenericUrl 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 19 with GenericUrl

use of com.google.api.client.http.GenericUrl in project api-samples by youtube.

the class RetrieveReports method downloadReport.

/**
     * Download the report specified by the URL. (media.download)
     *
     * @param reportUrl The URL of the report to be downloaded.
     * @throws IOException
     */
private static boolean downloadReport(String reportUrl) throws IOException {
    // Call the YouTube Reporting API's media.download method to download a report.
    Download request = youtubeReporting.media().download("");
    FileOutputStream fop = new FileOutputStream(new File("report"));
    request.getMediaHttpDownloader().download(new GenericUrl(reportUrl), fop);
    return true;
}
Also used : FileOutputStream(java.io.FileOutputStream) GenericUrl(com.google.api.client.http.GenericUrl) Download(com.google.api.services.youtubereporting.YouTubeReporting.Media.Download) File(java.io.File)

Example 20 with GenericUrl

use of com.google.api.client.http.GenericUrl 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)

Aggregations

GenericUrl (com.google.api.client.http.GenericUrl)28 HttpResponse (com.google.api.client.http.HttpResponse)19 HttpRequest (com.google.api.client.http.HttpRequest)15 IOException (java.io.IOException)15 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)8 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)7 InputStream (java.io.InputStream)7 HttpTransport (com.google.api.client.http.HttpTransport)6 HttpResponseException (com.google.api.client.http.HttpResponseException)4 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)4 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)4 File (com.google.api.services.drive.model.File)4 GeneralSecurityException (java.security.GeneralSecurityException)4 MockGoogleCredential (com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential)3 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)3 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)3 JsonFactory (com.google.api.client.json.JsonFactory)3 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)3 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)3 MockSleeper (com.google.api.client.testing.util.MockSleeper)3