private boolean tryAndGetRemoteSize() { Header contentLengthHeader = request.getResponseHeader("Content-Length"); boolean chunked = false; long reportedRemoteSize = 0; if (contentLengthHeader == null) { Header chunkedHeader = request.getResponseHeader("Transfer-Encoding"); if (chunkedHeader == null || !"chunked".equalsIgnoreCase(chunkedHeader.getValue())) { status = Status.UNRECOVERABLE_ERROR; errorString = " Failed to receive length of download "; return false; } else if ("chunked".equalsIgnoreCase(chunkedHeader.getValue())) { chunked = true; } } else { reportedRemoteSize = Long.parseLong(contentLengthHeader.getValue()); if (reportedRemoteSize == 0) { status = Status.DOWNLOAD_FINISHED; String downloaded = "(download complete remote=" + remoteSize + "bytes)"; errorString = "Downloaded " + totalBytes + " bytes " + downloaded; downloadTime = 0; return false; } } if (remoteSize == 0) { remoteSize = reportedRemoteSize; } return true; }
Header contentLengthHeader = getMethod.getResponseHeader("Content-Length"); Header contentTypeHeader = getMethod.getResponseHeader("Content-Type");
@Override public String getHeaderValue(String headerName) { if (activeMethod == null) { return null; } Header header = activeMethod.getResponseHeader(headerName); if (header == null) { return null; } return header.getValue(); } }
@Override public String getHeaderValue(String headerName) { if (activeMethod == null) { return null; } Header header = activeMethod.getResponseHeader(headerName); if (header == null) { return null; } return header.getValue(); } }
@Override public String getHeaderValue(String headerName) { if (activeMethod == null) { return null; } Header header = activeMethod.getResponseHeader(headerName); if (header == null) { return null; } return header.getValue(); } }
/** * Records the ETag and Last-Modified headers, from the response, if they are present. * * @param getMethod GetMethod containing a valid HTTP response */ protected void processConditionalRetrievalHeaders(GetMethod getMethod) { Header httpHeader = getMethod.getResponseHeader("ETag"); if (httpHeader != null) { cachedMetadataETag = httpHeader.getValue(); } httpHeader = getMethod.getResponseHeader("Last-Modified"); if (httpHeader != null) { cachedMetadataLastModified = httpHeader.getValue(); } }
private void setLastModified(GetMethod getMethod, GetDiamondResult getDiamondResult) { Header lastModifiedHeader = getMethod.getResponseHeader(Constants.LAST_MODIFIED); if (null == lastModifiedHeader) throw new RuntimeException("RP_OK without lastModifiedHeader"); String lastModified = lastModifiedHeader.getValue(); getDiamondResult.setLastModified(lastModified); }
private void setMd5(GetMethod getMethod, GetDiamondResult getDiamondResult) { Header md5Header = getMethod.getResponseHeader(Constants.CONTENT_MD5); if (null == md5Header) throw new RuntimeException("RP_NO_CHANGE without MD5"); getDiamondResult.setMd5(md5Header.getValue()); }
/** * Download widget from given URL * @param url the URL to download * @param ignoreContentType if set to true, will ignore invalid content types (not application/widget) * @return the File downloaded * @throws InvalidContentTypeException * @throws HttpException * @throws IOException */ private File download(URL url, boolean ignoreContentType) throws InvalidContentTypeException, HttpException, IOException { HttpClient client = new HttpClient(); GetMethod method = new GetMethod(url.toString()); client.executeMethod(method); if (!ignoreContentType){ Header header = method.getResponseHeader("Content-Type"); if (header == null) throw new InvalidContentTypeException("Problem downloading widget: expected a content type of "+WIDGET_CONTENT_TYPE+" but received no content type description."); String type = header.getValue(); if (!type.startsWith(WIDGET_CONTENT_TYPE)) throw new InvalidContentTypeException("Problem downloading widget: expected a content type of "+WIDGET_CONTENT_TYPE+" but received:"+type); } File file = File.createTempFile("wookie", null); FileUtils.writeByteArrayToFile(file, IOUtils.toByteArray(method.getResponseBodyAsStream())); method.releaseConnection(); return file; }
InputStream ins = getMethod.getResponseBodyAsStream(); Header httpHeader = getMethod.getResponseHeader("Content-Encoding"); if (httpHeader != null) { String contentEncoding = httpHeader.getValue();
public InputStream openStream(URL url) throws IOException { GetMethod get = doGet(url, 0); if (!checkStatusCode(url, get)) { get.releaseConnection(); throw new IOException("The HTTP response code for " + url + " did not indicate a success." + " See log for more detail."); } Header encoding = get.getResponseHeader("Content-Encoding"); return getDecodingInputStream(encoding == null ? null : encoding.getValue(), get.getResponseBodyAsStream()); }
File gd = new File(path); gd.mkdirs(); boolean comp = Boolean.parseBoolean(mtd.getResponseHeader("metadatacomp").getValue()); String pt = path + ng + ".map"; if (comp)
method.setFollowRedirects(true); client.executeMethod(method); String type = method.getResponseHeader("Content-Type").getValue(); int code = method.getStatusCode();
private void doRequest(String host, String url, int result) throws Exception { HttpClient client = new HttpClient(); GetMethod get = new GetMethod(url); try { int status = client.executeMethod(get); assertThat(result, is(status)); assertThat(get.getResponseHeader(WWW_AUTHENTICATE), not(nullValue())); assertThat(get.getResponseHeader(WWW_AUTHENTICATE).getValue(), containsString("mule-realm")); } finally { get.releaseConnection(); } }
Header contentTypeHeader = getMethod.getResponseHeader("Content-Type"); String contentType = (contentTypeHeader != null) ? contentTypeHeader.getValue() : "";
public void download(URL src, File dest, CopyProgressListener l) throws IOException { GetMethod get = doGet(src, 0); try { // We can only figure the content we got is want we want if the status is success. if (!checkStatusCode(src, get)) { throw new IOException("The HTTP response code for " + src + " did not indicate a success." + " See log for more detail."); } Header encoding = get.getResponseHeader("Content-Encoding"); InputStream is = getDecodingInputStream(encoding == null ? null : encoding.getValue(), get.getResponseBodyAsStream()); FileUtil.copy(is, dest, l); dest.setLastModified(getLastModified(get)); } finally { get.releaseConnection(); } }
@Override public GetMethod executeRequest( URL url, Map<String, String> queryParams, Integer backendTimeout) throws HttpException, IOException { GetMethod response = EasyMock.createMock(GetMethod.class); expect(response.getStatusCode()).andReturn(200); expect(response.getResponseBodyAsStream()) .andReturn(new ByteArrayInputStream(responseBody)); expect(response.getResponseCharSet()).andReturn("UTF-8"); expect(response.getResponseHeader("Content-Type")).andReturn(null); response.releaseConnection(); expectLastCall(); replay(response); return response; } });
@Override public void onSuccess(GetMethod getMethod) { if (getMethod.getStatusCode() >= 400) { if (getMethod.getStatusCode() == 401) { LOG.info("Ignoring HTTP response status code 401 (" + getMethod.getStatusText() + ") for image URL " + completeUrlAsString); } else { handleError("HTTP server responded with: " + getMethod.getStatusCode() + " " + getMethod.getStatusText()); } } else { final Header contentTypeHeader = getMethod.getResponseHeader("Content-Type"); if (contentTypeHeader == null) { handleError("HTTP response did not contain Content-Type header."); } else { final String contentType = contentTypeHeader.getValue(); if (!contentType.startsWith("image/")) { handleError("Content-Type HTTP response header \"" + contentType + "\" does not start with \"image/\"."); } else { // TODO: check if the response body is a valid image } } } }
@Override public GetMethod executeRequest( URL url, Map<String, String> queryParams, Integer backendTimeout, WMSLayer.HttpRequestMode httpRequestMode) throws HttpException, IOException { GetMethod response = EasyMock.createMock(GetMethod.class); expect(response.getStatusCode()).andReturn(200); expect(response.getResponseBodyAsStream()) .andReturn(new ByteArrayInputStream(responseBody)); expect(response.getResponseCharSet()).andReturn("UTF-8"); expect(response.getResponseHeader("Content-Type")) .andReturn(new Header("Content-Type", "image/png")); response.releaseConnection(); expectLastCall(); replay(response); return response; } });
Header location = get.getResponseHeader("Location"); printRedirectInformation(location);