/** * @return the HTTP response body as a {@link Blob}. * @throws IOException if getting the HTTP response content fails. */ public Blob getBody() throws IOException { return Blobs.from(httpResponse.getContent()); }
@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()); } } }
static <T> T makePostRequest(HttpTransport httpTransport, String url, HttpContent httpContent, Class<T> clazz) throws IOException { HttpRequestFactory factory = httpTransport.createRequestFactory(); HttpRequest postRequest = factory.buildPostRequest(new GenericUrl(url), httpContent); HttpResponse response = postRequest.execute(); int statusCode = response.getStatusCode(); if (statusCode != 200) { throw new IOException( "Bad status code: " + statusCode + " error: " + response.getStatusMessage()); } String result = CharStreams .toString(new InputStreamReader(response.getContent(), Charsets.UTF_8)); return new ObjectMapper().readValue(result, clazz); } }
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); }
private <T> T makeGetRequest(String url, Optional<Map<String, String>> parameters, Class<T> clazz) throws IOException { HttpRequestFactory requestFactory = httpTransport.createRequestFactory(); HttpRequest getRequest = requestFactory .buildGetRequest(new GenericUrl(url + "?" + generateParamsString(parameters))); HttpResponse response = getRequest.execute(); int statusCode = response.getStatusCode(); if (statusCode != 200) { throw new IOException( "Bad status code: " + statusCode + " error: " + response.getStatusMessage()); } String result = CharStreams .toString(new InputStreamReader(response.getContent(), Charsets.UTF_8)); return objectMapper.readValue(result, clazz); }
private String getToken(String frob) throws IOException { URL signedUrl = signatureGenerator.getSignature( GET_TOKEN_URL, ImmutableMap.of("frob", frob, "method", GET_TOKEN_METHOD)); HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(); HttpRequest getRequest = requestFactory.buildGetRequest(new GenericUrl(signedUrl)); HttpResponse response = getRequest.execute(); int statusCode = response.getStatusCode(); if (statusCode != 200) { throw new IOException( "Bad status code: " + statusCode + " error: " + response.getStatusMessage()); } AuthElement authElement = xmlMapper.readValue(response.getContent(), AuthElement.class); Preconditions.checkState(authElement.stat.equals("ok"), "state must be ok: %s", authElement); Preconditions.checkState( !Strings.isNullOrEmpty(authElement.auth.token), "token must not be empty", authElement); return authElement.auth.token; } }
private <T> T makeRequest(String url, Class<T> clazz, TokensAndUrlAuthData authData) throws IOException { HttpRequestFactory requestFactory = httpTransport.createRequestFactory(); HttpRequest getRequest = requestFactory.buildGetRequest( new GenericUrl(url + "?access_token=" + authData.getAccessToken())); HttpResponse response = getRequest.execute(); int statusCode = response.getStatusCode(); if (statusCode != 200) { throw new IOException( "Bad status code: " + statusCode + " error: " + response.getStatusMessage()); } String result = CharStreams.toString(new InputStreamReader(response.getContent(), Charsets.UTF_8)); return objectMapper.readValue(result, clazz); } }
<T> T makePostRequest(String url, Optional<Map<String, String>> parameters, HttpContent httpContent, Class<T> clazz) throws IOException { HttpRequestFactory requestFactory = httpTransport.createRequestFactory(); HttpRequest postRequest = requestFactory .buildPostRequest(new GenericUrl(url + "?" + generateParamsString(parameters)), httpContent); HttpResponse response = postRequest.execute(); int statusCode = response.getStatusCode(); if (statusCode != 200) { throw new IOException( "Bad status code: " + statusCode + " error: " + response.getStatusMessage()); } String result = CharStreams .toString(new InputStreamReader(response.getContent(), Charsets.UTF_8)); if (clazz.isAssignableFrom(String.class)) { return (T) result; } else { return objectMapper.readValue(result, clazz); } }
private String requestRaw(String path) throws IOException { HttpRequest getRequest = TRANSPORT.createRequestFactory().buildGetRequest( new GenericUrl(baseUrl + path)); HttpHeaders headers = new HttpHeaders(); headers.setAuthorization("Bearer " + accessToken); getRequest.setHeaders(headers); HttpResponse response = getRequest.execute(); validateResponse(getRequest, response, 200); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); IOUtils.copy(response.getContent(), byteArrayOutputStream, true); return byteArrayOutputStream.toString(); }
private <T extends RememberTheMilkResponse> T makeRequest( Map<String, String> parameters, Class<T> dataClass) throws IOException { URL signedUrl = signatureGenerator.getSignature(BASE_URL, parameters); HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(); HttpRequest getRequest = requestFactory.buildGetRequest(new GenericUrl(signedUrl)); HttpResponse response = getRequest.execute(); int statusCode = response.getStatusCode(); if (statusCode != 200) { throw new IOException( "Bad status code: " + statusCode + " error: " + response.getStatusMessage()); } T parsedResponse = xmlMapper.readValue(response.getContent(), dataClass); if (parsedResponse.error != null) { throw new IOException( "Error making call to " + signedUrl + " error: " + parsedResponse.error); } return parsedResponse; }
/** * Parses the contents of a URL to produce an RDF model. */ public Model getModel(String url) throws IOException { HttpRequestFactory factory = TRANSPORT.createRequestFactory(); HttpRequest rootGetRequest = factory.buildGetRequest( new GenericUrl(url)); HttpHeaders headers = new HttpHeaders(); headers.setCookie(authCookie); headers.setAccept("text/turtle"); rootGetRequest.setHeaders(headers); HttpResponse response = rootGetRequest.execute(); if (response.getStatusCode() != 200) { throw new IOException("Unexpected return code: " + response.getStatusCode() + "\nMessage:\n" + response.getStatusMessage()); } StringWriter writer = new StringWriter(); IOUtils.copy(response.getContent(), writer, "UTF-8"); String fixedString = fixProblematicPeriods(writer.toString()); Model defaultModel = ModelFactory.createDefaultModel(); return defaultModel.read( new StringReader(fixedString), url, "TURTLE"); }
.export(file.getId(), newMimeType) .executeMedia() .getContent(); } else { inputStream = .setAlt("media") .executeMedia() .getContent();
new InputStreamReader(fileContents.getContent(), Charsets.UTF_8)); String line; while ((line = reader.readLine()) != null) {
/** * @return the HTTP response body as a {@link Blob}. * @throws IOException if getting the HTTP response content fails. */ public Blob getBody() throws IOException { return Blobs.from(httpResponse.getContent()); }
/** * Writes the response payload of forwarded request to the servlet response. */ protected void writeResponsePayload(HttpServletResponse resp, HttpResponse httpResp) throws IOException { try (InputStream in = httpResp.getContent()) { writeResponsePayload(resp, in); } }
public static void readToFile(String url,File target) throws IOException { HttpRequest buildGetRequest; buildGetRequest = createRequestFactory.buildGetRequest(new GenericUrl(url)); InputStream str=buildGetRequest.execute().getContent(); FileUtils.copyInputStreamToFile(str, target); }
public DeployInfo deploy(final DeployRequest... requests) throws IOException { try { String strCont = JSONSerDe.ser(new ArrayList<DeployRequest>(Arrays.asList(requests))); HttpContent content = new StringHttpContent(strCont); HttpRequest request = requestFactory.buildPostRequest(new GenericUrl( qNodes[(int) (Math.random() * qNodes.length)] + "/api/deploy"), content); HttpResponse resp = request.execute(); return JSONSerDe.deSer(asString(resp.getContent()), DeployInfo.class); } catch(JSONSerDeException e) { throw new IOException(e); } }
public Tablespace tablespace(String tablespace) throws IOException { HttpRequest request = requestFactory.buildGetRequest(new GenericUrl( qNodes[(int) (Math.random() * qNodes.length)] + "/api/tablespace/" + tablespace)); HttpResponse resp = request.execute(); try { return JSONSerDe.deSer(asString(resp.getContent()), Tablespace.class); } catch(JSONSerDeException e) { throw new IOException(e); } }
@SuppressWarnings("unchecked") public List<String> dNodeList() throws IOException { HttpRequest request = requestFactory.buildGetRequest(new GenericUrl( qNodes[(int) (Math.random() * qNodes.length)] + "/api/dnodelist")); HttpResponse resp = request.execute(); try { return JSONSerDe.deSer(asString(resp.getContent()), ArrayList.class); } catch(JSONSerDeException e) { throw new IOException(e); } }
public static InputStream getAttachmentInputStream(Credential credential, String timelineItemId, String attachmentId) throws IOException { Mirror mirrorService = getMirror(credential); Mirror.Timeline.Attachments attachments = mirrorService.timeline().attachments(); Attachment attachmentMetadata = attachments.get(timelineItemId, attachmentId).execute(); HttpResponse resp = mirrorService.getRequestFactory() .buildGetRequest(new GenericUrl(attachmentMetadata.getContentUrl())).execute(); return resp.getContent(); }