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());
}
}
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();
}
}
}
}
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();
}
}
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();
}
}
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;
}
Aggregations