Java Code Examples for javax.servlet.http.HttpServletRequest.getDateHeader()

The following are Jave code examples for showing how to use getDateHeader() of the javax.servlet.http.HttpServletRequest class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: tomcat7   File: DefaultServlet.java   View Source Code Vote up 6 votes
/**
 * Check if the if-modified-since condition is satisfied.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param resourceAttributes File object
 * @return boolean true if the resource meets the specified condition,
 * and false if the condition is not satisfied, in which case request
 * processing is stopped
 */
protected boolean checkIfModifiedSince(HttpServletRequest request,
        HttpServletResponse response,
        ResourceAttributes resourceAttributes) {
    try {
        long headerValue = request.getDateHeader("If-Modified-Since");
        long lastModified = resourceAttributes.getLastModified();
        if (headerValue != -1) {

            // If an If-None-Match header has been specified, if modified since
            // is ignored.
            if ((request.getHeader("If-None-Match") == null)
                && (lastModified < headerValue + 1000)) {
                // The entity has not been modified since the date
                // specified by the client. This is not an error case.
                response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                response.setHeader("ETag", resourceAttributes.getETag());

                return false;
            }
        }
    } catch (IllegalArgumentException illegalArgument) {
        return true;
    }
    return true;

}
 
Example 2
Project: tomcat7   File: DefaultServlet.java   View Source Code Vote up 6 votes
/**
 * Check if the if-unmodified-since condition is satisfied.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param resourceAttributes File object
 * @return boolean true if the resource meets the specified condition,
 * and false if the condition is not satisfied, in which case request
 * processing is stopped
 */
protected boolean checkIfUnmodifiedSince(HttpServletRequest request,
                                       HttpServletResponse response,
                                       ResourceAttributes resourceAttributes)
    throws IOException {
    try {
        long lastModified = resourceAttributes.getLastModified();
        long headerValue = request.getDateHeader("If-Unmodified-Since");
        if (headerValue != -1) {
            if ( lastModified >= (headerValue + 1000)) {
                // The entity has not been modified since the date
                // specified by the client. This is not an error case.
                response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
                return false;
            }
        }
    } catch(IllegalArgumentException illegalArgument) {
        return true;
    }
    return true;

}
 
Example 3
Project: airsonic   File: SubsonicRESTController.java   View Source Code Vote up 6 votes
@RequestMapping(value = "/download")
public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {
    request = wrapRequest(request);
    org.airsonic.player.domain.User user = securityService.getCurrentUser(request);
    if (!user.isDownloadRole()) {
        error(request, response, ErrorCode.NOT_AUTHORIZED, user.getUsername() + " is not authorized to download files.");
        return;
    }

    long ifModifiedSince = request.getDateHeader("If-Modified-Since");
    long lastModified = downloadController.getLastModified(request);

    if (ifModifiedSince != -1 && lastModified != -1 && lastModified <= ifModifiedSince) {
        response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
        return;
    }

    if (lastModified != -1) {
        response.setDateHeader("Last-Modified", lastModified);
    }

    downloadController.handleRequest(request, response);
}
 
Example 4
Project: Equella   File: ContentStreamWriter.java   View Source Code Vote up 6 votes
public boolean checkModifiedSince(HttpServletRequest request, HttpServletResponse response, long lastModified)
{
	long modifiedSince = request.getDateHeader("IF-MODIFIED-SINCE"); //$NON-NLS-1$
	boolean hasBeenModified = true;
	if( modifiedSince > 0 )
	{
		hasBeenModified = modifiedSince < (lastModified - (lastModified % 1000));
	}

	response.setDateHeader("Last-Modified", lastModified); //$NON-NLS-1$

	if( !hasBeenModified )
	{
		response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
	}

	return hasBeenModified;
}
 
Example 5
Project: lams   File: DefaultServlet.java   View Source Code Vote up 6 votes
/**
 * Check if the if-unmodified-since condition is satisfied.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param resourceInfo File object
 * @return boolean true if the resource meets the specified condition,
 * and false if the condition is not satisfied, in which case request
 * processing is stopped
 */
protected boolean checkIfUnmodifiedSince(HttpServletRequest request,
                                       HttpServletResponse response,
                                       ResourceAttributes resourceAttributes)
    throws IOException {
    try {
        long lastModified = resourceAttributes.getLastModified();
        long headerValue = request.getDateHeader("If-Unmodified-Since");
        if (headerValue != -1) {
            if ( lastModified >= (headerValue + 1000)) {
                // The entity has not been modified since the date
                // specified by the client. This is not an error case.
                response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
                return false;
            }
        }
    } catch(IllegalArgumentException illegalArgument) {
        return true;
    }
    return true;

}
 
Example 6
Project: jerrydog   File: DefaultServlet.java   View Source Code Vote up 6 votes
/**
 * Check if the if-modified-since condition is satisfied.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param resourceInfo File object
 * @return boolean true if the resource meets the specified condition,
 * and false if the condition is not satisfied, in which case request 
 * processing is stopped
 */
private boolean checkIfModifiedSince(HttpServletRequest request,
                                     HttpServletResponse response,
                                     ResourceInfo resourceInfo)
    throws IOException {
    try {
        long headerValue = request.getDateHeader("If-Modified-Since");
        long lastModified = resourceInfo.date;
        if (headerValue != -1) {

            // If an If-None-Match header has been specified, if modified since
            // is ignored.
            if ((request.getHeader("If-None-Match") == null) 
                && (lastModified <= headerValue + 1000)) {
                // The entity has not been modified since the date
                // specified by the client. This is not an error case.
                response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                return false;
            }
        }
    } catch(IllegalArgumentException illegalArgument) {
        return false;
    }
    return true;

}
 
Example 7
Project: jerrydog   File: DefaultServlet.java   View Source Code Vote up 6 votes
/**
 * Check if the if-unmodified-since condition is satisfied.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param resourceInfo File object
 * @return boolean true if the resource meets the specified condition,
 * and false if the condition is not satisfied, in which case request 
 * processing is stopped
 */
private boolean checkIfUnmodifiedSince(HttpServletRequest request,
                                       HttpServletResponse response,
                                       ResourceInfo resourceInfo)
    throws IOException {
    try {
        long lastModified = resourceInfo.date;
        long headerValue = request.getDateHeader("If-Unmodified-Since");
        if (headerValue != -1) {
            if ( lastModified > headerValue ) {
                // The entity has not been modified since the date
                // specified by the client. This is not an error case.
                response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
                return false;
            }
        }
    } catch(IllegalArgumentException illegalArgument) {
        return false;
    }
    return true;

}
 
Example 8
Project: apache-tomcat-7.0.73-with-comment   File: DefaultServlet.java   View Source Code Vote up 6 votes
/**
 * Check if the if-unmodified-since condition is satisfied.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param resourceAttributes File object
 * @return boolean true if the resource meets the specified condition,
 * and false if the condition is not satisfied, in which case request
 * processing is stopped
 */
protected boolean checkIfUnmodifiedSince(HttpServletRequest request,
                                       HttpServletResponse response,
                                       ResourceAttributes resourceAttributes)
    throws IOException {
    try {
        long lastModified = resourceAttributes.getLastModified();
        long headerValue = request.getDateHeader("If-Unmodified-Since");
        if (headerValue != -1) {
            if ( lastModified >= (headerValue + 1000)) {
                // The entity has not been modified since the date
                // specified by the client. This is not an error case.
                response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
                return false;
            }
        }
    } catch(IllegalArgumentException illegalArgument) {
        return true;
    }
    return true;

}
 
Example 9
Project: lazycat   File: DefaultServlet.java   View Source Code Vote up 6 votes
/**
 * Check if the if-modified-since condition is satisfied.
 *
 * @param request
 *            The servlet request we are processing
 * @param response
 *            The servlet response we are creating
 * @param resourceAttributes
 *            File object
 * @return boolean true if the resource meets the specified condition, and
 *         false if the condition is not satisfied, in which case request
 *         processing is stopped
 */
protected boolean checkIfModifiedSince(HttpServletRequest request, HttpServletResponse response,
		ResourceAttributes resourceAttributes) {
	try {
		long headerValue = request.getDateHeader("If-Modified-Since");
		long lastModified = resourceAttributes.getLastModified();
		if (headerValue != -1) {

			// If an If-None-Match header has been specified, if modified
			// since
			// is ignored.
			if ((request.getHeader("If-None-Match") == null) && (lastModified < headerValue + 1000)) {
				// The entity has not been modified since the date
				// specified by the client. This is not an error case.
				response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
				response.setHeader("ETag", resourceAttributes.getETag());

				return false;
			}
		}
	} catch (IllegalArgumentException illegalArgument) {
		return true;
	}
	return true;

}
 
Example 10
Project: lazycat   File: DefaultServlet.java   View Source Code Vote up 6 votes
/**
 * Check if the if-unmodified-since condition is satisfied.
 *
 * @param request
 *            The servlet request we are processing
 * @param response
 *            The servlet response we are creating
 * @param resourceAttributes
 *            File object
 * @return boolean true if the resource meets the specified condition, and
 *         false if the condition is not satisfied, in which case request
 *         processing is stopped
 */
protected boolean checkIfUnmodifiedSince(HttpServletRequest request, HttpServletResponse response,
		ResourceAttributes resourceAttributes) throws IOException {
	try {
		long lastModified = resourceAttributes.getLastModified();
		long headerValue = request.getDateHeader("If-Unmodified-Since");
		if (headerValue != -1) {
			if (lastModified >= (headerValue + 1000)) {
				// The entity has not been modified since the date
				// specified by the client. This is not an error case.
				response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
				return false;
			}
		}
	} catch (IllegalArgumentException illegalArgument) {
		return true;
	}
	return true;

}
 
Example 11
Project: framework   File: Servlets.java   View Source Code Vote up 5 votes
/**
 * 根据浏览器If-Modified-Since Header, 计算文件是否已被修改.
 * <p>
 * 如果无修改, checkIfModify返回false ,设置304 not modify status.
 *
 * @param lastModified 内容的最后修改时间.
 */
public static boolean checkIfModifiedSince(HttpServletRequest request, HttpServletResponse response, long lastModified) {
	long ifModifiedSince = request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
	if ((ifModifiedSince != -1) && (lastModified < ifModifiedSince + 1000)) {
		response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
		return false;
	}
	return true;
}
 
Example 12
Project: lemon   File: ServletUtils.java   View Source Code Vote up 5 votes
/**
 * 根据浏览器If-Modified-Since Header, 计算文件是否已被修改.
 * 
 * 如果无修改, checkIfModify返回false ,设置304 not modify status.
 * 
 * @param request
 *            HttpServletRequest
 * @param response
 *            HttpServletResponse
 * @param lastModified
 *            内容的最后修改时间.
 * @return boolean
 */
public static boolean checkIfModifiedSince(HttpServletRequest request,
        HttpServletResponse response, long lastModified) {
    long ifModifiedSince = request.getDateHeader("If-Modified-Since");

    if ((ifModifiedSince != -1)
            && (lastModified < (ifModifiedSince + MILL_SECONDS))) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);

        return false;
    }

    return true;
}
 
Example 13
Project: lams   File: DefaultServlet.java   View Source Code Vote up 5 votes
/**
 * Check if the if-modified-since condition is satisfied.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param resourceInfo File object
 * @return boolean true if the resource meets the specified condition,
 * and false if the condition is not satisfied, in which case request
 * processing is stopped
 */
protected boolean checkIfModifiedSince(HttpServletRequest request,
                                     HttpServletResponse response,
                                     ResourceAttributes resourceAttributes)
    throws IOException {
    try {
        long headerValue = request.getDateHeader("If-Modified-Since");
        long lastModified = resourceAttributes.getLastModified();
        if (headerValue != -1) {

            // If an If-None-Match header has been specified, if modified since
            // is ignored.
            if ((request.getHeader("If-None-Match") == null)
                && (lastModified < headerValue + 1000)) {
                // The entity has not been modified since the date
                // specified by the client. This is not an error case.
                response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                response.setHeader("ETag", resourceAttributes.getETag());

                return false;
            }
        }
    } catch (IllegalArgumentException illegalArgument) {
        return true;
    }
    return true;

}
 
Example 14
Project: Ins_fb_pictureSpider_WEB   File: ServletUtils.java   View Source Code Vote up 5 votes
/**
 * 根据浏览器If-Modified-Since Header, 计算文件是否已被修改.
 *
 * 如果无修改, checkIfModify返回false ,设置304 not modify status.
 *
 * @param lastModified 内容的最后修改时间.
 */
public static boolean checkIfModifiedSince(HttpServletRequest request, HttpServletResponse response,
                                           long lastModified) {
    long ifModifiedSince = request.getDateHeader("If-Modified-Since");
    if ((ifModifiedSince != -1) && (lastModified < ifModifiedSince + 1000)) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return false;
    }
    return true;
}
 
Example 15
Project: easyweb   File: Servlets.java   View Source Code Vote up 5 votes
/**
 * 根据浏览器If-Modified-Since Header, 计算文件是否已被修改.
 * 
 * 如果无修改, checkIfModify返回false ,设置304 not modify status.
 * 
 * @param lastModified 内容的最后修改时间.
 */
public static boolean checkIfModifiedSince(HttpServletRequest request, HttpServletResponse response,
                                              long lastModified) {
	long ifModifiedSince = request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
	if ((ifModifiedSince != -1) && (lastModified < ifModifiedSince + 1000)) {
		response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
		return false;
	}
	return true;
}
 
Example 16
Project: apache-tomcat-7.0.73-with-comment   File: DefaultServlet.java   View Source Code Vote up 5 votes
/**
 * Check if the if-modified-since condition is satisfied.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param resourceAttributes File object
 * @return boolean true if the resource meets the specified condition,
 * and false if the condition is not satisfied, in which case request
 * processing is stopped
 */
protected boolean checkIfModifiedSince(HttpServletRequest request,
        HttpServletResponse response,
        ResourceAttributes resourceAttributes) {
    try {
        long headerValue = request.getDateHeader("If-Modified-Since");
        long lastModified = resourceAttributes.getLastModified();
        if (headerValue != -1) {

            // If an If-None-Match header has been specified, if modified since
            // is ignored.
            if ((request.getHeader("If-None-Match") == null)
                && (lastModified < headerValue + 1000)) {
                // The entity has not been modified since the date
                // specified by the client. This is not an error case.
                response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                response.setHeader("ETag", resourceAttributes.getETag());

                return false;
            }
        }
    } catch (IllegalArgumentException illegalArgument) {
        return true;
    }
    return true;

}
 
Example 17
Project: appinventor-extensions   File: ModifiedHeaders.java   View Source Code Vote up 5 votes
/**
 * Compares the "If-Modified-Since" header in this request (ir present) to
 * the last build date (if known) in order to determine whether the requested
 * data has been modified since the prior request.
 *
 * @param req the request
 * @return {@code true} iff we're sure that request is for a resource that
 *         has not been modified since the prior request
 */
public static boolean notModified(HttpServletRequest req) {
  long ifModDate = req.getDateHeader("If-Modified-Since");
  if (BuildData.getTimestamp() > 0 && ifModDate > 0) {
    if (ifModDate >= BuildData.getTimestamp()) {
      return true;
    }
  }
  return false;
}
 
Example 18
Project: DWSurvey   File: ServletUtils.java   View Source Code Vote up 5 votes
/**
 * 根据浏览器If-Modified-Since Header, 计算文件是否已被修改.
 * 
 * 如果无修改, checkIfModify返回false ,设置304 not modify status.
 * 
 * @param lastModified 内容的最后修改时间.
 */
public static boolean checkIfModifiedSince(HttpServletRequest request, HttpServletResponse response,
		long lastModified) {
	long ifModifiedSince = request.getDateHeader("If-Modified-Since");
	if ((ifModifiedSince != -1) && (lastModified < ifModifiedSince + 1000)) {
		response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
		return false;
	}
	return true;
}
 
Example 19
Project: OftenPorter   File: HttpCacheUtil.java   View Source Code Vote up 4 votes
/**
 * 检测资源是否过期,若过期了则重新设置缓存时间.
 *
 * @param sec                   缓存多少秒
 * @param modelLastModifiedDate 上次修改日期
 * @param request
 * @param response
 * @return true:发送数据;false:不发送.
 */
public static boolean checkHeaderCache(long sec,
        long modelLastModifiedDate, HttpServletRequest request,
        HttpServletResponse response)
{
    request.setAttribute("myExpire", sec);

    // convert seconds to ms.
    long adddaysM = sec * 1000;
    long header = request.getDateHeader("If-Modified-Since");
    long now = System.currentTimeMillis();
    if (header > 0 && adddaysM > 0)
    {
        if (modelLastModifiedDate > header)
        {
            // adddays = 0; // reset
            response.setStatus(HttpServletResponse.SC_OK);
            return true;
        }
        if (header + adddaysM > now)
        {
            notModified(response);
            return false;
        }
    }

    // if over expire data, see the Etags;
    // ETags if ETags no any modified
    String previousToken = request.getHeader("If-None-Match");
    if (previousToken != null
            && previousToken.equals(Long.toString(modelLastModifiedDate)))
    {
        notModified(response);
        return false;
    }
    // if th model has modified , setup the new modified date
    response.setHeader("ETag", Long.toString(modelLastModifiedDate));
    setRespHeaderCache(sec, request, response);
    return true;

}
 
Example 20
Project: validator-web   File: CachingHandler.java   View Source Code Vote up 4 votes
/**
 * Do we need to send the content for this file
 * @param req The HTTP request
 * @return true iff the ETags and If-Modified-Since headers say we have not changed
 */
protected boolean isUpToDate(HttpServletRequest req, long lastModified) {
    String etag = "\"" + lastModified + '\"';

    if (ignoreLastModified) {
        return false;
    }

    long modifiedSince = -1;
    try {
        modifiedSince = req.getDateHeader(HttpConstants.HEADER_IF_MODIFIED);
    } catch (RuntimeException ex) {
        // ignore
    }
    
    if (modifiedSince != -1) {
        // Browsers are only accurate to the second
        modifiedSince -= modifiedSince % 1000;
    }
    String givenEtag = req.getHeader(HttpConstants.HEADER_IF_NONE);
    String cachedPath = getCachingKey(req);
    
    // Deal with missing etags
    if (givenEtag == null) {
        // There is no ETag, just go with If-Modified-Since
        if (modifiedSince >= lastModified) {
            if (log.isDebugEnabled()) {
                log.debug("Sending 304 for " + cachedPath + " If-Modified-Since=" + modifiedSince + ", Last-Modified=" + lastModified);
            }
            return true;
        }
        // There are no modified settings, carry on
        return false;
    }
    
    // Deal with missing If-Modified-Since
    if (modifiedSince == -1) {
        if (!etag.equals(givenEtag)) {
            // There is an ETag, but no If-Modified-Since
            if (log.isDebugEnabled()) {
                log.debug("Sending 304 for " + cachedPath + ", If-Modified-Since=-1, Old ETag=" + givenEtag + ", New ETag=" + etag);
            }
            return true;
        }
        // There are no modified settings, carry on
        return false;
    }
    
    // Do both values indicate that we are in-date?
    if (etag.equals(givenEtag) && modifiedSince >= lastModified) {
        if (log.isDebugEnabled()) {
            log.debug("Sending 304 for " + cachedPath + ", If-Modified-Since=" + modifiedSince + ", Last Modified=" + lastModified + ", Old ETag=" + givenEtag + ", New ETag=" + etag);
        }
        return true;
    }
    log.debug("Sending content for " + cachedPath + ", If-Modified-Since=" + modifiedSince + ", Last Modified=" + lastModified + ", Old ETag=" + givenEtag + ", New ETag=" + etag);
    return false;
}