@Override protected void setup(HeadMethod method) throws IOException { super.setup(method); method.addRequestHeader(NEWEST); } });
private HttpMethod getHeadMethod(Protocol protocol) throws Exception { HeadMethod head = new HeadMethod(protocol.getTarget()); head.setPath(target.toString()); for(String name : header) { head.addRequestHeader(name, header.get(name)); } return head; }
/** Determine the value for the Content-Length header using a HEAD call * * @param url * @return * @throws HttpException */ public static int getContentLength(String url) throws HttpException, IOException { int result = 0; //Execute a GET method on the URL HeadMethod head = new HeadMethod(url); head.setFollowRedirects(true); head.addRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 4.0)"); try { _httpClient.executeMethod(head); // Retrieve just the Content-Length header value. String length = head.getResponseHeader("Content-Length").getValue(); result = Integer.parseInt(length); } catch (NumberFormatException nfe) { //header not found } finally { head.releaseConnection(); } return result; }
/** {@inheritDoc} */ public boolean exists() throws ResourceException { HeadMethod headMethod = new HeadMethod(resourceUrl); headMethod.addRequestHeader("Connection", "close"); try { httpClient.executeMethod(headMethod); if (headMethod.getStatusCode() != HttpStatus.SC_OK) { return false; } return true; } catch (IOException e) { throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e); } finally { headMethod.releaseConnection(); } }
/** {@inheritDoc} */ public DateTime getLastModifiedTime() throws ResourceException { HeadMethod headMethod = new HeadMethod(resourceUrl); headMethod.addRequestHeader("Connection", "close"); try { httpClient.executeMethod(headMethod); if (headMethod.getStatusCode() != HttpStatus.SC_OK) { throw new ResourceException("Unable to retrieve resource URL " + resourceUrl + ", received HTTP status code " + headMethod.getStatusCode()); } Header lastModifiedHeader = headMethod.getResponseHeader("Last-Modified"); if (lastModifiedHeader != null && !DatatypeHelper.isEmpty(lastModifiedHeader.getValue())) { long lastModifiedTime = DateUtil.parseDate(lastModifiedHeader.getValue()).getTime(); return new DateTime(lastModifiedTime, ISOChronology.getInstanceUTC()); } return new DateTime(); } catch (IOException e) { throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e); } catch (DateParseException e) { throw new ResourceException("Unable to parse last modified date for resource:" + resourceUrl, e); } finally { headMethod.releaseConnection(); } }