List of usage examples for javax.servlet RequestDispatcher INCLUDE_CONTEXT_PATH
String INCLUDE_CONTEXT_PATH
To view the source code for javax.servlet RequestDispatcher INCLUDE_CONTEXT_PATH.
Click Source Link
From source file:com.github.zhanhb.ckfinder.download.PathPartial.java
/** * Serve the specified resource, optionally including the data content. * * @param request The servlet request we are processing * @param response The servlet response we are creating * @param content Should the content be included? * @param path the resource to serve//ww w .j a v a2 s . co m * * @exception IOException if an input/output error occurs */ private void serveResource(HttpServletRequest request, HttpServletResponse response, boolean content, Path path) throws IOException, ServletException { ActionContext context = new ActionContext().put(HttpServletRequest.class, request) .put(HttpServletResponse.class, response).put(ServletContext.class, request.getServletContext()) .put(Path.class, path); if (path == null) { notFound.handle(context); return; } BasicFileAttributes attr; try { attr = Files.readAttributes(path, BasicFileAttributes.class); } catch (IOException ex) { notFound.handle(context); return; } context.put(BasicFileAttributes.class, attr); boolean isError = response.getStatus() >= HttpServletResponse.SC_BAD_REQUEST; // Check if the conditions specified in the optional If headers are // satisfied. // Checking If headers boolean included = (request.getAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH) != null); String etag = this.eTag.getValue(context); if (!included && !isError && !checkIfHeaders(request, response, attr, etag)) { return; } // Find content type. String contentType = contentTypeResolver.getValue(context); // Get content length long contentLength = attr.size(); // Special case for zero length files, which would cause a // (silent) ISE boolean serveContent = content && contentLength != 0; Range[] ranges = null; if (!isError) { if (useAcceptRanges) { // Accept ranges header response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes"); } // Parse range specifier ranges = serveContent ? parseRange(request, response, attr, etag) : FULL; // ETag header response.setHeader(HttpHeaders.ETAG, etag); // Last-Modified header response.setDateHeader(HttpHeaders.LAST_MODIFIED, attr.lastModifiedTime().toMillis()); } ServletOutputStream ostream = null; if (serveContent) { ostream = response.getOutputStream(); } String disposition = contentDisposition.getValue(context); if (disposition != null) { response.setHeader(HttpHeaders.CONTENT_DISPOSITION, disposition); } // Check to see if a Filter, Valve of wrapper has written some content. // If it has, disable range requests and setting of a content length // since neither can be done reliably. if (isError || ranges == FULL) { // Set the appropriate output headers if (contentType != null) { log.debug("serveFile: contentType='{}'", contentType); response.setContentType(contentType); } if (contentLength >= 0) { setContentLengthLong(response, contentLength); } // Copy the input stream to our output stream (if requested) if (serveContent) { log.trace("Serving bytes"); Files.copy(path, ostream); } } else if (ranges != null && ranges.length != 0) { // Partial content response. response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); if (ranges.length == 1) { Range range = ranges[0]; response.addHeader(HttpHeaders.CONTENT_RANGE, range.toString()); long length = range.end - range.start + 1; setContentLengthLong(response, length); if (contentType != null) { log.debug("serveFile: contentType='{}'", contentType); response.setContentType(contentType); } if (serveContent) { try (InputStream stream = Files.newInputStream(path)) { copyRange(stream, ostream, range, new byte[Math.min((int) length, 8192)]); } } } else { response.setContentType("multipart/byteranges; boundary=" + MIME_SEPARATION); if (serveContent) { copy(path, ostream, ranges, contentType, new byte[Math.min((int) contentLength, 8192)]); } } } }
From source file:org.ireland.jnetty.webapp.RequestDispatcherImpl.java
private void doInclude(HttpServletRequest request, HttpServletResponse response, HttpInvocation invocation) throws ServletException, IOException { //Wrap the request IncludeRequest wrequest = new IncludeRequest(request, response, invocation); // If we have already been include previously, then keep using the established // original value. Otherwise, this is the first include and we need to establish the values. // Note: the established value on the original request for pathInfo and // for queryString is allowed to be null, but cannot be null for the other values. if (request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) == null) { // ?Include, wrequest.setAttribute(RequestDispatcher.INCLUDE_REQUEST_URI, request.getRequestURI()); wrequest.setAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH, request.getContextPath()); wrequest.setAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH, request.getServletPath()); wrequest.setAttribute(RequestDispatcher.INCLUDE_PATH_INFO, request.getPathInfo()); wrequest.setAttribute(RequestDispatcher.INCLUDE_QUERY_STRING, request.getQueryString()); }/* w ww . j a v a2 s . co m*/ boolean isValid = false; try { invocation.getFilterChainInvocation().service(wrequest, response); isValid = true; } finally { if (request.getAsyncContext() != null) { // An async request was started during the forward, don't close the // response as it may be written to during the async handling return; } // server/106r, ioc/0310 if (isValid) { finishResponse(response); } } }