List of usage examples for javax.servlet.http HttpServletRequest getDateHeader
public long getDateHeader(String name);
long
value that represents a Date
object. From source file:com.meltmedia.cadmium.servlets.FileServletTest.java
public static HttpServletRequest mockGetWithIfModifiedSince(String pathInfo, Long lastModified) { HttpServletRequest request = mockGet(pathInfo); when(request.getDateHeader(IF_MODIFIED_SINCE_HEADER)).thenReturn(lastModified); when(request.getHeader(anyString())).thenReturn(null); return request; }
From source file:org.ambraproject.wombat.util.HttpMessageUtil.java
/** * Checks to see if we should serve the contents of the requested object, or just return a 304 response with no body, * based on cache-related request headers. * * @param request HttpServletRequest we will check for cache headers * @param lastModified last modified timestamp of the actual resource on the server * @param etag etag generated from the actual resource on the server * @return true if we should serve the bytes of the resource, false if we should return 304. *///from w ww.ja v a 2 s. c om public static boolean checkIfModifiedSince(HttpServletRequest request, Long lastModified, String etag) { // Let the Etag-based header take precedence over If-Modified-Since. String etagFromRequest = request.getHeader("If-None-Match"); if (etag != null && etagFromRequest != null) { return !etagFromRequest.equals(etag); } else { return lastModified == null || lastModified > request.getDateHeader("If-Modified-Since"); } }
From source file:net.siegmar.japtproxy.JaptProxy.java
/** * Analyzes (validates) request url and extract required information. * * @param request the object to populate with extracted information. * @throws net.siegmar.japtproxy.exception.InvalidRequestException is thrown if requested url is invalid. *//* www .ja v a2 s . c om*/ private static RequestedData buildRequestedData(final HttpServletRequest request, final Configuration configuration) throws InvalidRequestException { final String resource = request.getPathInfo(); final RequestedData requestedData = new RequestedData(); requestedData.setRequestedResource(resource); requestedData.setRequestModifiedSince(request.getDateHeader(HttpHeaderConstants.IF_MODIFIED_SINCE)); requestedData.setUserAgent(request.getHeader(HttpHeaderConstants.USER_AGENT)); requestedData.setUrl(getURL(request, configuration)); requestedData.setHostUrl(getURL(request, true, configuration)); requestedData.setScheme(request.getScheme()); requestedData.setServerName(request.getServerName()); requestedData.setServerPort(request.getServerPort()); final String requestedResource = requestedData.getRequestedResource(); // Reject if no requested resource is specified if (requestedResource == null) { throw new InvalidRequestException("Rejected request because it doesn't contain a resource request"); } // Reject requests that contain /../ for security reason if (requestedResource.contains("/../")) { throw new InvalidRequestException( "Rejected request '" + requestedResource + "' because it contains /../"); } // Reject requests that doesn't contain a backend final int endIdx = requestedResource.indexOf('/', 1); if (endIdx <= 1) { throw new InvalidRequestException( "Rejected request '" + requestedResource + "' because it doesn't specify a backend"); } // Reject requests that doesn't contain a target resource if (requestedResource.length() == endIdx + 1) { throw new InvalidRequestException("Rejected request '" + requestedResource + "' because it doesn't specify a target " + "resource"); } // Extract the backend and target resource parts of the request final String requestedBackend = requestedResource.substring(1, endIdx); final String requestedTarget = requestedResource.substring(endIdx); // Set requestedData requestedData.setRequestedTarget(requestedTarget); requestedData.setRequestedBackend(requestedBackend); return requestedData; }
From source file:com.meltmedia.cadmium.servlets.FileServletTest.java
public static HttpServletRequest mockGet(String pathInfo) { HttpServletRequest request = mock(HttpServletRequest.class); when(request.getMethod()).thenReturn(GET_METHOD); when(request.getRequestURI()).thenReturn(pathInfo); when(request.getDateHeader(anyString())).thenReturn(new Long(-1)); when(request.getHeader(anyString())).thenReturn(null); return request; }
From source file:com.meltmedia.cadmium.servlets.FileServletTest.java
public static HttpServletRequest mockGetWithGzip(String pathInfo) { HttpServletRequest request = mock(HttpServletRequest.class); when(request.getMethod()).thenReturn(GET_METHOD); when(request.getRequestURI()).thenReturn(pathInfo); when(request.getDateHeader(anyString())).thenReturn(new Long(-1)); when(request.getHeader(ACCEPT_ENCODING_HEADER)).thenReturn("gzip"); return request; }
From source file:com.meltmedia.cadmium.servlets.FileServletTest.java
public static HttpServletRequest mockGetWithoutGzip(String pathInfo) { HttpServletRequest request = mock(HttpServletRequest.class); when(request.getMethod()).thenReturn(GET_METHOD); when(request.getRequestURI()).thenReturn(pathInfo); when(request.getDateHeader(anyString())).thenReturn(new Long(-1)); when(request.getHeader(ACCEPT_ENCODING_HEADER)).thenReturn("gzip;q=0"); return request; }
From source file:org.apache.solr.servlet.cache.HttpCacheHeaderUtil.java
/** * Check for modify time related conditional headers and set status * /*www . j a v a 2 s .c o m*/ * @return true if no request processing is necessary and HTTP response status has been set, false otherwise. * @throws IOException */ public static boolean checkLastModValidators(final HttpServletRequest req, final HttpServletResponse resp, final long lastMod) throws IOException { try { // First check for If-Modified-Since because this is the common // used header by HTTP clients final long modifiedSince = req.getDateHeader("If-Modified-Since"); if (modifiedSince != -1L && lastMod <= modifiedSince) { // Send a "not-modified" sendNotModified(resp); return true; } final long unmodifiedSince = req.getDateHeader("If-Unmodified-Since"); if (unmodifiedSince != -1L && lastMod > unmodifiedSince) { // Send a "precondition failed" sendPreconditionFailed(resp); return true; } } catch (IllegalArgumentException iae) { // one of our date headers was not formated properly, ignore it /* NOOP */ } return false; }
From source file:ch.entwine.weblounge.common.content.ResourceUtils.java
/** * Returns <code>true</code> if the resource either is more recent than the * cached version on the client side or the request does not contain caching * information./* w ww . j a va2 s . com*/ * <p> * The calculation is made based on the availability of the * <code>If-Modified-Since</code> header. Use * {@link #hasChanged(HttpServletRequest, long)} to also take the * <code>If-None-Match</code> header into account. * * @param request * the client request * @param date * the date * @return <code>true</code> if the resource is more recent than the version * that is cached at the client. * @throws IllegalArgumentException * if the <code>If-Modified-Since</code> header cannot be converted * to a date. */ public static boolean isModified(HttpServletRequest request, long date) throws IllegalArgumentException { if (request.getHeader("If-Modified-Since") != null) { try { long cachedModificationDate = request.getDateHeader("If-Modified-Since"); return cachedModificationDate < date; } catch (IllegalArgumentException e) { logger.debug("Client sent malformed 'If-Modified-Since' header: {}"); } } return true; }
From source file:org.opencms.flex.CmsFlexController.java
/** * Checks if the request has the "If-Modified-Since" header set, and if so, * if the header date value is equal to the provided last modification date.<p> * /*from ww w . j a va 2 s. co m*/ * @param req the request to set the "If-Modified-Since" date header from * @param dateLastModified the date to compare the header with * * @return <code>true</code> if the header is set and the header date is equal to the provided date */ public static boolean isNotModifiedSince(HttpServletRequest req, long dateLastModified) { // check if the request contains a last modified header try { long lastModifiedHeader = req.getDateHeader(CmsRequestUtil.HEADER_IF_MODIFIED_SINCE); // if last modified header is set (> -1), compare it to the requested resource return ((lastModifiedHeader > -1) && (((dateLastModified / 1000) * 1000) == lastModifiedHeader)); } catch (Exception ex) { // some clients (e.g. User-Agent: BlackBerry7290/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/111) // send an invalid "If-Modified-Since" header (e.g. in german locale) // which breaks with http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html // this has to be caught because the subsequent request for the 500 error handler // would run into the same exception. LOG.warn(Messages.get().getBundle().key(Messages.ERR_HEADER_IFMODIFIEDSINCE_FORMAT_3, new Object[] { CmsRequestUtil.HEADER_IF_MODIFIED_SINCE, req.getHeader(CmsRequestUtil.HEADER_USER_AGENT), req.getHeader(CmsRequestUtil.HEADER_IF_MODIFIED_SINCE) })); } return false; }
From source file:org.olat.core.gui.media.ServletUtil.java
/** * @param httpReq//from w ww . j a v a2 s . c om * @param httpResp * @param mr */ public static void serveResource(HttpServletRequest httpReq, HttpServletResponse httpResp, MediaResource mr) { boolean debug = log.isDebug(); try { Long lastModified = mr.getLastModified(); if (lastModified != null) { // give browser a chance to cache images long ifModifiedSince = httpReq.getDateHeader("If-Modified-Since"); // TODO: if no such header, what is the return value long lastMod = lastModified.longValue(); if (ifModifiedSince >= (lastMod / 1000L) * 1000L) { httpResp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return; } httpResp.setDateHeader("Last-Modified", lastModified.longValue()); } if (isFlashPseudoStreaming(httpReq, mr)) { httpResp.setContentType("video/x-flv"); pseudoStreamFlashResource(httpReq, httpResp, mr); } else { String mime = mr.getContentType(); if (mime != null) { httpResp.setContentType(mime); } serveFullResource(httpReq, httpResp, mr); } // else there is no stream, but probably just headers // like e.g. in case of a 302 http-redirect } catch (Exception e) { if (debug) { log.warn("client browser abort when serving media resource", e); } } finally { try { mr.release(); } catch (Exception e) { //we did our best here to clean up } } }