byte[] bytes = response.getContent(); R resp; for (HTTPHeader header : response.getHeaders()) { if (header.getName().equalsIgnoreCase("Content-Type")) { contentType = header.getValue(); && contentType.startsWith("image") && responseClass == ImageResult.Response.class && response.getResponseCode() == 200) { ImageResult result = new ImageResult(contentType, bytes); return (T) result; } catch (JsonSyntaxException e) { if (response.getResponseCode() > 399) { String.format( "Server Error: %d %s", response.getResponseCode(), new String(response.getContent(), Charset.defaultCharset())));
@Override protected GcsFileMetadata wrap(HTTPResponse resp) throws IOException { long totalLength; switch (resp.getResponseCode()) { case 200: totalLength = getLengthFromHeader(resp, X_GOOG_CONTENT_LENGTH); break; case 206: totalLength = getLengthFromContentRange(resp); break; case 404: throw new FileNotFoundException("Could not find: " + filename); case 416: throw new BadRangeException("Requested Range not satisfiable; perhaps read past EOF? " + URLFetchUtils.describeRequestAndResponse(info, resp)); default: throw HttpErrorHandler.error(info, resp); } byte[] content = resp.getContent(); Preconditions.checkState(content.length <= want, "%s: got %s > wanted %s", this, content.length, want); dst.put(content); return getMetadataFromResponse(filename, resp, totalLength); }
private boolean shouldRetry(HTTPResponse response) { return RETRY_ERROR_CODES.contains(response.getResponseCode()) && cumulativeSleepTime < errorTimeOut && (maxRetries == null || retryCounter < maxRetries); }
static Response parseResponse(HTTPResponse response, HTTPRequest creatingRequest) { // Response URL will be null if it is the same as the request URL. URL responseUrl = response.getFinalUrl(); String urlString = (responseUrl != null ? responseUrl : creatingRequest.getURL()).toString(); int status = response.getResponseCode(); List<HTTPHeader> fetchHeaders = response.getHeaders(); List<Header> headers = new ArrayList<Header>(fetchHeaders.size()); String contentType = "application/octet-stream"; for (HTTPHeader fetchHeader : fetchHeaders) { String name = fetchHeader.getName(); String value = fetchHeader.getValue(); if ("Content-Type".equalsIgnoreCase(name)) { contentType = value; } headers.add(new Header(name, value)); } TypedByteArray body = null; byte[] fetchBody = response.getContent(); if (fetchBody != null) { body = new TypedByteArray(contentType, fetchBody); } return new Response(urlString, status, "", headers, body); } }
@Override public InputStream getContent() { byte[] content = fetchResponse.getContent(); return content == null ? null : new ByteArrayInputStream(content); }
static void appendResponse(HTTPResponse resp, StringBuilder b) { byte[] content = resp.getContent(); b.append(resp.getResponseCode()).append(" with ").append(content == null ? 0 : content.length); b.append(" bytes of content"); for (HTTPHeader h : resp.getHeadersUncombined()) { b.append('\n').append(h.getName()).append(": ").append(h.getValue()); } b.append('\n').append(content == null ? "" : new String(content, UTF_8)).append('\n'); }
protected void readResponseHeaders(GHttpEndpoint endpoint, Exchange exchange, HTTPResponse response) { HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy(); Message in = exchange.getIn(); Message out = exchange.getOut(); out.setHeaders(in.getHeaders()); out.setHeader(Exchange.HTTP_RESPONSE_CODE, response.getResponseCode()); String contentType = getResponseHeader("Content-Type", response); if (contentType != null) { out.setHeader(Exchange.CONTENT_TYPE, contentType); } for (HTTPHeader header : response.getHeaders()) { String name = header.getName(); String value = header.getValue(); if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) { out.setHeader(name, value); } } }
protected String getResponseHeader(String name, HTTPResponse response) { for (HTTPHeader header : response.getHeaders()) { if (header.getName().equalsIgnoreCase(name)) { return header.getValue(); } } return null; }
@Test public void testHeaders() throws Exception { URLFetchService service = URLFetchServiceFactory.getURLFetchService(); URL url = getFetchUrl(); HTTPRequest req = new HTTPRequest(url, HTTPMethod.POST); req.setHeader(new HTTPHeader("Content-Type", "application/octet-stream")); req.setPayload("Headers!".getBytes(UTF_8)); HTTPResponse response = service.fetch(req); boolean found = false; List<HTTPHeader> headers = response.getHeadersUncombined(); for (HTTPHeader h : headers) { if (h.getName().equals("ABC")) { Assert.assertEquals("123", h.getValue()); found = true; break; } } Assert.assertTrue("Cannot find matching header <ABC : 123>: " + headers, found); found = false; headers = response.getHeaders(); for (HTTPHeader h : headers) { if (h.getName().equals("XYZ")) { Assert.assertEquals("1, 2, 3", h.getValue()); found = true; break; } } Assert.assertTrue("Cannot find matching header <XYZ : 1,2,3>: " + headers, found); }
/** Gets all headers with the name {@code headerName}, case-insensitive. */ private static Iterable<HTTPHeader> getHeaders(HTTPResponse resp, String headerName) { final String lowercaseHeaderName = headerName.toLowerCase(); return Iterables.filter(resp.getHeadersUncombined(), new Predicate<HTTPHeader>() { @Override public boolean apply(HTTPHeader header) { return header.getName().toLowerCase().equals(lowercaseHeaderName); } }); }
@Override public URI getUri() { try { // final url is only non-null when we follow redirects URL finalUrl = response().getFinalUrl(); return finalUrl == null ? url.toURI() : finalUrl.toURI(); } catch (URISyntaxException e) { throw new HttpResponseException(e, "Uri cannot be parsed: %s", e.getMessage()); } }
byte[] bytes = resp.getContent(); this.successful = resp.getResponseCode() < 400; String encoding = getHeader(resp, "Content-Encoding");
@Override public int getStatusCode() { return fetchResponse.getResponseCode(); }
@Override public InputStream getContent() { byte[] content = fetchResponse.getContent(); return content == null ? null : new ByteArrayInputStream(content); }
private static Response toRequestorResponse(HTTPResponse response) { Map<String, List<String>> headers = new HashMap<String, List<String>>(); for (HTTPHeader header : response.getHeadersUncombined()) { List<String> existing = headers.get(header.getName()); if (existing == null) { existing = new ArrayList<String>(); headers.put(header.getName(), existing); } existing.add(header.getValue()); } return new Response(response.getResponseCode(), new ByteArrayInputStream(response.getContent()), headers); }
public HttpResponse receiveResponseHeader() throws HttpException, IOException { if (this.response == null) { flush(); } HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), this.response.getResponseCode(), null); // System.err.println("RECV: " + response.getStatusLine()); for (HTTPHeader h : this.response.getHeaders()) { // System.err.println("RECV: " + h.getName() + ": " + h.getValue()); response.addHeader(h.getName(), h.getValue()); } return response; }
HTTPResponse response = URLFetchServiceFactory.getURLFetchService() .fetch(new URL("url_to_fetch")); List<HTTPHeader> headers = response.getHeaders(); for (HTTPHeader h : headers) { if (h.getName().equals("Content-Type")) { /* * could be text/html; charset=iso-8859-1. */ if (h.getValue().startsWith("text/html")) { /* TODO do sth. */ } } }
/** Gets the values of all headers with the name {@code headerName}. */ List<String> getHeaders(HTTPResponse resp, String headerName) { List<String> b = new ArrayList<String>(); for (HTTPHeader h : resp.getHeadersUncombined()) { if (headerName.equalsIgnoreCase(h.getName())) { b.add(h.getValue()); } } return b; }
public void handle(HTTPResponse response) throws Exception { URL finalURL = response.getFinalUrl(); Assert.assertEquals(getUrl(""), finalURL); } });
private byte[] createResponse(HTTPResponse response) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); int code = response.getResponseCode(); byte[] content = response.getContent(); List<HTTPHeader> heads = response.getHeaders(); try { bos.write("HTTP/1.1 ".getBytes()); bos.write(String.valueOf(code).getBytes()); bos.write(" ".getBytes()); bos.write(getHttpReply(code).getBytes()); bos.write("\r\n".getBytes()); for (HTTPHeader responseHeader : heads) { bos.write(responseHeader.getName().getBytes()); bos.write(":".getBytes()); bos.write(responseHeader.getValue().getBytes()); bos.write("\r\n".getBytes()); } bos.write("\r\n".getBytes()); if (content != null) { bos.write(content); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bos.toByteArray(); }