List of usage examples for org.apache.http.client.utils DateUtils parseDate
public static Date parseDate(final String dateValue)
From source file:com.google.gerrit.util.http.testutil.FakeHttpServletRequest.java
@Override public long getDateHeader(String name) { String v = getHeader(name); return v != null ? DateUtils.parseDate(v).getTime() : 0; }
From source file:net.staticsnow.nexus.repository.apt.internal.proxy.AptProxyFacet.java
private DateTime getDateHeader(HttpResponse response, String name) { Header h = response.getLastHeader(name); if (h != null) { try {//from ww w .j av a 2 s . co m return new DateTime(DateUtils.parseDate(h.getValue()).getTime()); } catch (Exception ex) { log.warn("Invalid date '{}', will skip", h); } } return null; }
From source file:com.gargoylesoftware.htmlunit.Cache.java
/** * Parses and returns the specified date header of the specified response. This method * returns {@code null} if the specified header cannot be found or cannot be parsed as a date. * * @param response the response/*ww w. j av a 2s . c o m*/ * @param headerName the header name * @return the specified date header of the specified response */ protected Date parseDateHeader(final WebResponse response, final String headerName) { final String value = response.getResponseHeaderValue(headerName); if (value == null) { return null; } final Matcher matcher = DATE_HEADER_PATTERN.matcher(value); if (matcher.matches()) { return new Date(); } return DateUtils.parseDate(value); }
From source file:com.sap.dirigible.runtime.registry.RegistryServlet.java
private boolean setCacheHeaders(IEntity entity, HttpServletRequest request, HttpServletResponse response) throws IOException { boolean cached = false; IEntityInformation entityInformation = entity.getInformation(); String modifiedSinceHeader = request.getHeader(IF_MODIFIED_SINCE_HEADER); if ((entityInformation != null)) { Calendar lastModified = getCalendar(entityInformation.getModifiedAt()); if ((!StringUtils.isEmpty(modifiedSinceHeader))) { Calendar modifiedSince = getCalendar(DateUtils.parseDate(modifiedSinceHeader)); if (lastModified.compareTo(modifiedSince) <= 0) { Calendar expires = getCalendar(lastModified); expires.add(Calendar.MONTH, 1); response.setDateHeader(EXPIRES_HEADER, expires.getTimeInMillis()); response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); cached = true;/* w w w. j av a2 s . c om*/ } } response.setDateHeader(LAST_MODIFIED_HEADER, lastModified.getTimeInMillis()); } return cached; }
From source file:org.fcrepo.client.integration.FcrepoClientIT.java
@Test public void testGetUnmodified() throws Exception { // Check that get returns a 304 if the item hasn't changed according to last-modified/etag final FcrepoResponse response = create(); // Get tomorrows date to provide as the modified-since date final String lastModified = response.getHeaderValue(LAST_MODIFIED); final Date modDate = DateUtils.parseDate(lastModified); final Calendar cal = Calendar.getInstance(); cal.setTime(modDate);//from w w w. j a v a2 s.c om cal.add(Calendar.DATE, 1); final FcrepoResponse modResp = client.get(url).ifModifiedSince(DateUtils.formatDate(cal.getTime())) .perform(); assertEquals(NOT_MODIFIED.getStatusCode(), modResp.getStatusCode()); final String originalEtag = response.getHeaderValue(ETAG); final FcrepoResponse etagResp = client.get(url).ifNoneMatch(originalEtag).perform(); assertEquals(NOT_MODIFIED.getStatusCode(), etagResp.getStatusCode()); }
From source file:io.restassured.itest.java.CookieITest.java
@Test public void parsesValidExpiresDateCorrectly() throws Exception { Cookies cookies = when().get("/cookieWithValidExpiresDate").then().extract().detailedCookies(); assertThat(cookies.asList(), hasSize(1)); Cookie cookie = cookies.get("name"); assertThat(cookie.getExpiryDate(), equalTo(DateUtils.parseDate("Sat, 02 May 2009 23:38:25 GMT"))); }
From source file:org.apache.jmeter.protocol.http.control.CacheManager.java
private void setCache(String lastModified, String cacheControl, String expires, String etag, String url, String date) {/*from ww w . j av a 2 s .co m*/ if (log.isDebugEnabled()) { log.debug("setCache(" + lastModified + "," + cacheControl + "," + expires + "," + etag + "," + url + "," + date + ")"); } Date expiresDate = null; // i.e. not using Expires if (useExpires) {// Check that we are processing Expires/CacheControl final String MAX_AGE = "max-age="; if (cacheControl != null && cacheControl.contains("no-store")) { // We must not store an CacheEntry, otherwise a // conditional request may be made return; } if (expires != null) { try { expiresDate = org.apache.http.client.utils.DateUtils.parseDate(expires); } catch (IllegalArgumentException e) { if (log.isDebugEnabled()) { log.debug("Unable to parse Expires: '" + expires + "' " + e); } expiresDate = CacheManager.EXPIRED_DATE; // invalid dates must be treated as expired } } // if no-cache is present, ensure that expiresDate remains null, which forces revalidation if (cacheControl != null && !cacheControl.contains("no-cache")) { // the max-age directive overrides the Expires header, if (cacheControl.contains(MAX_AGE)) { long maxAgeInSecs = Long.parseLong(cacheControl .substring(cacheControl.indexOf(MAX_AGE) + MAX_AGE.length()).split("[, ]")[0] // Bug 51932 - allow for optional trailing attributes ); expiresDate = new Date(System.currentTimeMillis() + maxAgeInSecs * 1000); } else if (expires == null) { // No max-age && No expires if (!StringUtils.isEmpty(lastModified) && !StringUtils.isEmpty(date)) { try { Date responseDate = DateUtils.parseDate(date); Date lastModifiedAsDate = DateUtils.parseDate(lastModified); // see https://developer.mozilla.org/en/HTTP_Caching_FAQ // see http://www.ietf.org/rfc/rfc2616.txt#13.2.4 expiresDate = new Date(System.currentTimeMillis() + Math.round((responseDate.getTime() - lastModifiedAsDate.getTime()) * 0.1)); } catch (IllegalArgumentException e) { // date or lastModified may be null or in bad format if (log.isWarnEnabled()) { log.warn("Failed computing expiration date with following info:" + lastModified + "," + cacheControl + "," + expires + "," + etag + "," + url + "," + date); } // TODO Can't see anything in SPEC expiresDate = new Date(System.currentTimeMillis() + ONE_YEAR_MS); } } else { // TODO Can't see anything in SPEC expiresDate = new Date(System.currentTimeMillis() + ONE_YEAR_MS); } } // else expiresDate computed in (expires!=null) condition is used } } getCache().put(url, new CacheEntry(lastModified, expiresDate, etag)); }
From source file:io.restassured.itest.java.CookieITest.java
@Test public void removesDoubleQuotesFromCookieWithExpiresDate() throws Exception { Cookies cookies = when().get("/cookieWithDoubleQuoteExpiresDate").then().extract().detailedCookies(); assertThat(cookies.asList(), hasSize(1)); Cookie cookie = cookies.get("name"); assertThat(cookie.getExpiryDate(), equalTo(DateUtils.parseDate("Sat, 02 May 2009 23:38:25 GMT"))); }
From source file:com.hypersocket.netty.HttpRequestServletWrapper.java
@Override public long getDateHeader(String name) { if (getHeader(name) == null) { return -1; }//from www.ja v a 2 s .c om return DateUtils.parseDate(getHeader(name)).getTime(); }