Search in sources :

Example 16 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project google-api-java-client by google.

the class AbstractGoogleClientRequestTest method testExecuteUnparsed_error.

public void testExecuteUnparsed_error() throws Exception {
    HttpTransport transport = new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(final String method, final String url) {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() {
                    assertEquals("GET", method);
                    assertEquals("https://www.googleapis.com/test/path/v1/tests/foo", url);
                    MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
                    result.setStatusCode(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED);
                    result.setContentType(Json.MEDIA_TYPE);
                    result.setContent(ERROR_CONTENT);
                    return result;
                }
            };
        }
    };
    MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName("Test Application").build();
    MockGoogleClientRequest<String> request = new MockGoogleClientRequest<String>(client, HttpMethods.GET, URI_TEMPLATE, null, String.class);
    try {
        request.put("testId", "foo");
        request.executeUnparsed();
        fail("expected " + HttpResponseException.class);
    } catch (HttpResponseException e) {
        // expected
        assertEquals("401" + StringUtils.LINE_SEPARATOR + ERROR_CONTENT, e.getMessage());
    }
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) MockGoogleClient(com.google.api.client.googleapis.testing.services.MockGoogleClient) MockGoogleClientRequest(com.google.api.client.googleapis.testing.services.MockGoogleClientRequest) HttpResponseException(com.google.api.client.http.HttpResponseException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest)

Example 17 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project beam by apache.

the class SplunkEventWriter method flush.

/**
 * Flushes a batch of requests via {@link HttpEventPublisher}.
 *
 * @param receiver Receiver to write {@link SplunkWriteError}s to
 */
private void flush(OutputReceiver<SplunkWriteError> receiver, BagState<SplunkEvent> bufferState, ValueState<Long> countState) throws IOException {
    if (!bufferState.isEmpty().read()) {
        HttpResponse response = null;
        List<SplunkEvent> events = Lists.newArrayList(bufferState.read());
        try {
            // Important to close this response to avoid connection leak.
            response = publisher.execute(events);
            if (!response.isSuccessStatusCode()) {
                flushWriteFailures(events, response.getStatusMessage(), response.getStatusCode(), receiver);
                logWriteFailures(countState);
            } else {
                LOG.info("Successfully wrote {} events", countState.read());
                SUCCESS_WRITES.inc(countState.read());
            }
        } catch (HttpResponseException e) {
            LOG.error("Error writing to Splunk. StatusCode: {}, content: {}, StatusMessage: {}", e.getStatusCode(), e.getContent(), e.getStatusMessage());
            logWriteFailures(countState);
            flushWriteFailures(events, e.getStatusMessage(), e.getStatusCode(), receiver);
        } catch (IOException ioe) {
            LOG.error("Error writing to Splunk: {}", ioe.getMessage());
            logWriteFailures(countState);
            flushWriteFailures(events, ioe.getMessage(), null, receiver);
        } finally {
            // States are cleared regardless of write success or failure since we
            // write failed events to an output PCollection.
            bufferState.clear();
            countState.clear();
            if (response != null) {
                response.disconnect();
            }
        }
    }
}
Also used : HttpResponse(com.google.api.client.http.HttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) IOException(java.io.IOException)

Example 18 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project AndroidSDK-RecipeBook by gabu.

the class Recipe098 method handleException.

private void handleException(IOException e) {
    if (e instanceof HttpResponseException) {
        int statusCode = ((HttpResponseException) e).response.statusCode;
        if (statusCode == 401 || statusCode == 403) {
            AccountManager manager = AccountManager.get(this);
            // キャッシュを削除
            manager.invalidateAuthToken("com.google", mAuthToken);
            Toast.makeText(getApplicationContext(), "キャッシュを削除しました。アプリを再起動してください。", Toast.LENGTH_LONG).show();
        }
        return;
    } else {
        e.printStackTrace();
    }
}
Also used : HttpResponseException(com.google.api.client.http.HttpResponseException) AccountManager(android.accounts.AccountManager)

Example 19 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project AndroidSDK-RecipeBook by gabu.

the class Recipe101 method handleException.

private void handleException(IOException e) {
    if (e instanceof HttpResponseException) {
        int statusCode = ((HttpResponseException) e).response.statusCode;
        System.out.println(statusCode);
        if (statusCode == 401 || statusCode == 403) {
            AccountManager manager = AccountManager.get(this);
            // キャッシュを削除
            manager.invalidateAuthToken("com.google", mAuthToken);
            Toast.makeText(getApplicationContext(), "キャッシュを削除しました。アプリを再起動してください。", Toast.LENGTH_LONG).show();
        }
        return;
    } else {
        e.printStackTrace();
    }
}
Also used : HttpResponseException(com.google.api.client.http.HttpResponseException) AccountManager(android.accounts.AccountManager)

Example 20 with HttpResponseException

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

Aggregations

HttpResponseException (com.google.api.client.http.HttpResponseException)38 GenericUrl (com.google.api.client.http.GenericUrl)15 IOException (java.io.IOException)15 HttpResponse (com.google.api.client.http.HttpResponse)13 Test (org.junit.Test)13 HttpRequest (com.google.api.client.http.HttpRequest)10 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)6 HttpHeaders (com.google.api.client.http.HttpHeaders)4 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)4 ErrorResponseTemplate (com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate)4 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)3 ErrorEntryTemplate (com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PServiceCall (net.morimekta.providence.PServiceCall)3 Request (net.morimekta.test.providence.client.Request)3 TestService (net.morimekta.test.providence.client.TestService)3 AccountManager (android.accounts.AccountManager)2 ByteArrayContent (com.google.api.client.http.ByteArrayContent)2 HttpTransport (com.google.api.client.http.HttpTransport)2 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)2