use of org.eclipse.jetty.http.HttpContent in project jetty.project by eclipse.
the class ResourceService method doGet.
/* ------------------------------------------------------------ */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String servletPath = null;
String pathInfo = null;
Enumeration<String> reqRanges = null;
boolean included = request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) != null;
if (included) {
servletPath = _pathInfoOnly ? "/" : (String) request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
pathInfo = (String) request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO);
if (servletPath == null) {
servletPath = request.getServletPath();
pathInfo = request.getPathInfo();
}
} else {
servletPath = _pathInfoOnly ? "/" : request.getServletPath();
pathInfo = request.getPathInfo();
// Is this a Range request?
reqRanges = request.getHeaders(HttpHeader.RANGE.asString());
if (!hasDefinedRange(reqRanges))
reqRanges = null;
}
String pathInContext = URIUtil.addPaths(servletPath, pathInfo);
boolean endsWithSlash = (pathInfo == null ? request.getServletPath() : pathInfo).endsWith(URIUtil.SLASH);
boolean checkPrecompressedVariants = _precompressedFormats.length > 0 && !endsWithSlash && !included && reqRanges == null;
HttpContent content = null;
boolean release_content = true;
try {
// Find the content
content = _contentFactory.getContent(pathInContext, response.getBufferSize());
if (LOG.isDebugEnabled())
LOG.info("content={}", content);
// Not found?
if (content == null || !content.getResource().exists()) {
if (included)
throw new FileNotFoundException("!" + pathInContext);
notFound(request, response);
return;
}
// Directory?
if (content.getResource().isDirectory()) {
sendWelcome(content, pathInContext, endsWithSlash, included, request, response);
return;
}
// Strip slash?
if (endsWithSlash && pathInContext.length() > 1) {
String q = request.getQueryString();
pathInContext = pathInContext.substring(0, pathInContext.length() - 1);
if (q != null && q.length() != 0)
pathInContext += "?" + q;
response.sendRedirect(response.encodeRedirectURL(URIUtil.addPaths(request.getContextPath(), pathInContext)));
return;
}
// Conditional response?
if (!included && !passConditionalHeaders(request, response, content))
return;
// Precompressed variant available?
Map<CompressedContentFormat, ? extends HttpContent> precompressedContents = checkPrecompressedVariants ? content.getPrecompressedContents() : null;
if (precompressedContents != null && precompressedContents.size() > 0) {
// Tell caches that response may vary by accept-encoding
response.addHeader(HttpHeader.VARY.asString(), HttpHeader.ACCEPT_ENCODING.asString());
List<String> preferredEncodings = getPreferredEncodingOrder(request);
CompressedContentFormat precompressedContentEncoding = getBestPrecompressedContent(preferredEncodings, precompressedContents.keySet());
if (precompressedContentEncoding != null) {
HttpContent precompressedContent = precompressedContents.get(precompressedContentEncoding);
if (LOG.isDebugEnabled())
LOG.debug("precompressed={}", precompressedContent);
content = precompressedContent;
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), precompressedContentEncoding._encoding);
}
}
// TODO this should be done by HttpContent#getContentEncoding
if (isGzippedContent(pathInContext))
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
// Send the data
release_content = sendData(request, response, included, content, reqRanges);
} catch (IllegalArgumentException e) {
LOG.warn(Log.EXCEPTION, e);
if (!response.isCommitted())
response.sendError(500, e.getMessage());
} finally {
if (release_content) {
if (content != null)
content.release();
}
}
}
use of org.eclipse.jetty.http.HttpContent in project jetty.project by eclipse.
the class DefaultServletTest method testDirectFromResourceHttpContent.
@Test
public void testDirectFromResourceHttpContent() throws Exception {
if (!OS.IS_LINUX)
return;
testdir.ensureEmpty();
File resBase = testdir.getPathFile("docroot").toFile();
FS.ensureDirExists(resBase);
context.setBaseResource(Resource.newResource(resBase));
File index = new File(resBase, "index.html");
createFile(index, "<h1>Hello World</h1>");
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/");
defholder.setInitParameter("dirAllowed", "true");
defholder.setInitParameter("redirectWelcome", "false");
defholder.setInitParameter("useFileMappedBuffer", "true");
defholder.setInitParameter("welcomeServlets", "exact");
defholder.setInitParameter("gzip", "false");
defholder.setInitParameter("resourceCache", "resourceCache");
String response;
response = connector.getResponse("GET /context/index.html HTTP/1.0\r\n\r\n");
assertResponseContains("<h1>Hello World</h1>", response);
ResourceContentFactory factory = (ResourceContentFactory) context.getServletContext().getAttribute("resourceCache");
HttpContent content = factory.getContent("/index.html", 200);
ByteBuffer buffer = content.getDirectBuffer();
Assert.assertTrue(buffer.isDirect());
content = factory.getContent("/index.html", 5);
buffer = content.getDirectBuffer();
Assert.assertTrue(buffer == null);
}
Aggregations