本文整理匯總了Java中javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED屬性的典型用法代碼示例。如果您正苦於以下問題:Java HttpServletResponse.SC_NOT_MODIFIED屬性的具體用法?Java HttpServletResponse.SC_NOT_MODIFIED怎麽用?Java HttpServletResponse.SC_NOT_MODIFIED使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類javax.servlet.http.HttpServletResponse
的用法示例。
在下文中一共展示了HttpServletResponse.SC_NOT_MODIFIED屬性的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: shouldBodyBeZero
/**
* Performs a number of checks to ensure response saneness according to the rules of RFC2616:
* <ol>
* <li>If the response code is {@link javax.servlet.http.HttpServletResponse#SC_NO_CONTENT} then it is illegal for the body
* to contain anything. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5
* <li>If the response code is {@link javax.servlet.http.HttpServletResponse#SC_NOT_MODIFIED} then it is illegal for the body
* to contain anything. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5
* </ol>
*
* @param request the client HTTP request
* @param responseStatus the responseStatus
* @return true if the response should be 0, even if it is isn't.
*/
public static boolean shouldBodyBeZero(HttpServletRequest request, int responseStatus) {
//Check for NO_CONTENT
if (responseStatus == HttpServletResponse.SC_NO_CONTENT) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} resulted in a {} response. Removing message body in accordance with RFC2616.",
request.getRequestURL(), HttpServletResponse.SC_NO_CONTENT);
}
return true;
}
//Check for NOT_MODIFIED
if (responseStatus == HttpServletResponse.SC_NOT_MODIFIED) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} resulted in a {} response. Removing message body in accordance with RFC2616.",
request.getRequestURL(), HttpServletResponse.SC_NOT_MODIFIED);
}
return true;
}
return false;
}
示例2: checkChunking
void checkChunking(HttpResponseImpl response) {
// If any data has already been written to the stream, we must not
// change the chunking mode
if (count != 0)
return;
// Check the basic cases in which we chunk
useChunking =
(!response.isCommitted()
&& response.getContentLength() == -1
&& response.getStatus() != HttpServletResponse.SC_NOT_MODIFIED);
if (!response.isChunkingAllowed() && useChunking) {
// If we should chunk, but chunking is forbidden by the connector,
// we close the connection
response.setHeader("Connection", "close");
}
// Don't chunk is the connection will be closed
useChunking = (useChunking && !response.isCloseConnection());
if (useChunking) {
response.setHeader("Transfer-Encoding", "chunked");
} else if (response.isChunkingAllowed()) {
response.removeHeader("Transfer-Encoding", "chunked");
}
}
示例3: doResponseRedirectOrNotModifiedLogic
protected boolean doResponseRedirectOrNotModifiedLogic(HttpServletRequest servletRequest, HttpServletResponse servletResponse,
HttpResponse proxyResponse, int statusCode) throws ServletException, IOException {
// Check if the proxy response is a redirect
// The following code is adapted from org.tigris.noodle.filters.CheckForRedirect
if (statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */
&& statusCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) {
Header locationHeader = proxyResponse.getLastHeader(HttpHeaders.LOCATION);
if (locationHeader == null) {
throw new ServletException(
"Received status code: " + statusCode + " but no " + HttpHeaders.LOCATION + " header was found in the response");
}
// Modify the redirect to go to this proxy servlet rather that the proxied host
String locStr = rewriteUrlFromResponse(servletRequest, locationHeader.getValue());
servletResponse.sendRedirect(locStr);
return true;
}
// 304 needs special handling. See:
// http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304
// We get a 304 whenever passed an 'If-Modified-Since'
// header and the data on disk has not changed; server
// responds w/ a 304 saying I'm not going to send the
// body because the file has not changed.
if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) {
servletResponse.setIntHeader(HttpHeaders.CONTENT_LENGTH, 0);
servletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return true;
}
return false;
}
示例4: doFilter
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
if (!isIncluded(httpRequest) && acceptsGZipEncoding(httpRequest) && !response.isCommitted()) {
// Client accepts zipped content
if (log.isTraceEnabled()) {
log.trace("{} Written with gzip compression", httpRequest.getRequestURL());
}
// Create a gzip stream
final ByteArrayOutputStream compressed = new ByteArrayOutputStream();
final GZIPOutputStream gzout = new GZIPOutputStream(compressed);
// Handle the request
final GZipServletResponseWrapper wrapper = new GZipServletResponseWrapper(httpResponse, gzout);
wrapper.setDisableFlushBuffer(true);
chain.doFilter(request, wrapper);
wrapper.flush();
gzout.close();
// double check one more time before writing out
// repsonse might have been committed due to error
if (response.isCommitted()) {
return;
}
// return on these special cases when content is empty or unchanged
switch (wrapper.getStatus()) {
case HttpServletResponse.SC_NO_CONTENT:
case HttpServletResponse.SC_RESET_CONTENT:
case HttpServletResponse.SC_NOT_MODIFIED:
return;
default:
}
// Saneness checks
byte[] compressedBytes = compressed.toByteArray();
boolean shouldGzippedBodyBeZero = GZipResponseUtil.shouldGzippedBodyBeZero(compressedBytes, httpRequest);
boolean shouldBodyBeZero = GZipResponseUtil.shouldBodyBeZero(httpRequest, wrapper.getStatus());
if (shouldGzippedBodyBeZero || shouldBodyBeZero) {
// No reason to add GZIP headers or write body if no content was written or status code specifies no
// content
response.setContentLength(0);
return;
}
// Write the zipped body
GZipResponseUtil.addGzipHeader(httpResponse);
response.setContentLength(compressedBytes.length);
response.getOutputStream().write(compressedBytes);
} else {
// Client does not accept zipped content - don't bother zipping
if (log.isTraceEnabled()) {
log.trace("{} Written without gzip compression because the request does not accept gzip", httpRequest.getRequestURL());
}
chain.doFilter(request, response);
}
}