move conditionalHeader to DefaultServlet

- by removing Jetty specific implementation detail
pull/1/head
reger 11 years ago
parent 92d9c56f9f
commit 089c5007ee

@ -38,15 +38,11 @@ import net.yacy.cora.protocol.HeaderFramework;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.kelondro.util.FileUtils;
import org.eclipse.jetty.http.HttpContent;
import org.eclipse.jetty.http.HttpHeaders;
import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.WriterOutputStream;
import org.eclipse.jetty.server.AbstractHttpConnection;
import org.eclipse.jetty.server.HttpOutput;
import org.eclipse.jetty.server.InclusiveByteRange;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.MultiPartOutputStream;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.resource.Resource;
@ -218,64 +214,6 @@ public class Jetty8YaCyDefaultServlet extends YaCyDefaultServlet {
}
}
/* ------------------------------------------------------------ */
/* Check modification date headers.
*/
@Override
protected boolean passConditionalHeaders(HttpServletRequest request, HttpServletResponse response, Resource resource)
throws IOException {
try {
if (!request.getMethod().equals(HttpMethods.HEAD)) {
HttpContent content = new HttpContent.ResourceAsHttpContent(resource, _mimeTypes.getMimeByExtension(resource.toString()), response.getBufferSize());
String ifms = request.getHeader(HttpHeaders.IF_MODIFIED_SINCE);
if (ifms != null) {
//Get jetty's Response impl
Response r = Response.getResponse(response);
if (content != null) {
Buffer mdlm = content.getLastModified();
if (mdlm != null) {
if (ifms.equals(mdlm.toString())) {
r.reset(true);
r.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
r.flushBuffer();
return false;
}
}
}
long ifmsl = request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
if (ifmsl != -1) {
if (resource.lastModified() / 1000 <= ifmsl / 1000) {
r.reset(true);
r.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
r.flushBuffer();
return false;
}
}
}
// Parse the if[un]modified dates and compare to resource
long date = request.getDateHeader(HttpHeaders.IF_UNMODIFIED_SINCE);
if (date != -1) {
if (resource.lastModified() / 1000 > date / 1000) {
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return false;
}
}
if (content != null) content.release();
}
} catch (IllegalArgumentException iae) {
if (!response.isCommitted()) {
response.sendError(400, iae.getMessage());
}
throw iae;
}
return true;
}
/* ------------------------------------------------------------ */
@Override
protected void sendData(HttpServletRequest request,

@ -68,6 +68,8 @@ import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.eclipse.jetty.http.HttpHeaders;
import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.Buffer;
@ -267,9 +269,45 @@ public abstract class YaCyDefaultServlet extends HttpServlet {
}
/* ------------------------------------------------------------ */
/* Check modification date headers.
* send a 304 response instead of content if not modified since
*/
abstract protected boolean passConditionalHeaders(HttpServletRequest request, HttpServletResponse response, Resource resource)
throws IOException;
protected boolean passConditionalHeaders(HttpServletRequest request, HttpServletResponse response, Resource resource)
throws IOException {
try {
if (!request.getMethod().equals(HttpMethods.HEAD)) {
String ifms = request.getHeader(HttpHeaders.IF_MODIFIED_SINCE);
if (ifms != null) {
long ifmsl = request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
if (ifmsl != -1) {
if (resource.lastModified() / 1000 <= ifmsl / 1000) {
response.reset();
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
response.flushBuffer();
return false;
}
}
}
// Parse the if[un]modified dates and compare to resource
long date = request.getDateHeader(HttpHeaders.IF_UNMODIFIED_SINCE);
if (date != -1) {
if (resource.lastModified() / 1000 > date / 1000) {
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return false;
}
}
}
} catch (IllegalArgumentException iae) {
if (!response.isCommitted()) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, iae.getMessage());
}
throw iae;
}
return true;
}
/* ------------------------------------------------------------------- */
protected void sendDirectory(HttpServletRequest request,

Loading…
Cancel
Save