@Override protected String doGetContent() throws IOException { try (InputStream is = response.getContent(); ByteArrayOutputStream bos = new ByteArrayOutputStream()) { copy(is, bos); return new String(bos.toByteArray(), response.getContentCharset()); } } }
private <A, T, E> A getParsedDataClass( Class<A> dataClass, HttpResponse response, RequestInfo<T, E> requestInfo) throws IOException { // TODO(yanivi): Remove the HttpResponse reference and directly parse the InputStream if (dataClass == Void.class) { return null; } return requestInfo.request.getParser().parseAndClose( response.getContent(), response.getContentCharset(), dataClass); }
/** * Parses the given error HTTP response using the given JSON factory. * * @param jsonFactory JSON factory * @param response HTTP response * @return new instance of the Google JSON error information * @throws IllegalArgumentException if content type is not {@link Json#MEDIA_TYPE} or if expected * {@code "data"} or {@code "error"} key is not found */ public static GoogleJsonError parse(JsonFactory jsonFactory, HttpResponse response) throws IOException { JsonObjectParser jsonObjectParser = new JsonObjectParser.Builder(jsonFactory).setWrapperKeys( Collections.singleton("error")).build(); return jsonObjectParser.parseAndClose( response.getContent(), response.getContentCharset(), GoogleJsonError.class); }
@Override protected TokenResponse executeRefreshToken() throws IOException { GenericUrl tokenUrl = new GenericUrl(getTokenServerEncodedUrl()); HttpRequest request = getTransport().createRequestFactory().buildGetRequest(tokenUrl); JsonObjectParser parser = new JsonObjectParser(getJsonFactory()); request.setParser(parser); request.getHeaders().set("Metadata-Flavor", "Google"); request.setThrowExceptionOnExecuteError(false); HttpResponse response = request.execute(); int statusCode = response.getStatusCode(); if (statusCode == HttpStatusCodes.STATUS_CODE_OK) { InputStream content = response.getContent(); if (content == null) { // Throw explicitly rather than allow a later null reference as default mock // transports return success codes with empty contents. throw new IOException("Empty content from metadata token server request."); } return parser.parseAndClose(content, response.getContentCharset(), TokenResponse.class); } if (statusCode == HttpStatusCodes.STATUS_CODE_NOT_FOUND) { throw new IOException(String.format("Error code %s trying to get security access token from" + " Compute Engine metadata for the default service account. This may be because" + " the virtual machine instance does not have permission scopes specified.", statusCode)); } throw new IOException(String.format("Unexpected Error code %s trying to get security access" + " token from Compute Engine metadata for the default service account: %s", statusCode, response.parseAsString())); } }
if (!httpResponse.isSuccessStatusCode()) { throw makeException(url, methodName, httpResponse.getContent(), httpResponse.getContentType(), httpResponse.getContentCharset(), null, httpResponse.getStatusCode());
try (InputStream content = GzipFixingInputStream.maybeWrap(httpResponse.getContent())) { throw makeException(url, methodName, content, httpResponse.getContentType(), httpResponse.getContentCharset(), null, httpResponse.getStatusCode());
try (InputStream content = GzipFixingInputStream.maybeWrap(httpResponse.getContent())) { throw makeException(url, methodName, content, httpResponse.getContentType(), httpResponse.getContentCharset(), null, httpResponse.getStatusCode());
"Response contents incorrect", expectedResponses.get(i).getBody(), Streams.readAll(response.getContent(), response.getContentCharset())); assertEquals( "Location header incorrect on response",