Example usage for javax.servlet RequestDispatcher INCLUDE_REQUEST_URI

List of usage examples for javax.servlet RequestDispatcher INCLUDE_REQUEST_URI

Introduction

In this page you can find the example usage for javax.servlet RequestDispatcher INCLUDE_REQUEST_URI.

Prototype

String INCLUDE_REQUEST_URI

To view the source code for javax.servlet RequestDispatcher INCLUDE_REQUEST_URI.

Click Source Link

Document

The name of the request attribute under which the request URI of the target of an #include(ServletRequest,ServletResponse) include is stored

Usage

From source file:net.yacy.http.servlets.YaCyDefaultServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String pathInfo;//from   w  ww  .  j a  v  a2s . com
    Enumeration<String> reqRanges = null;
    boolean included = request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) != null;
    if (included) {
        pathInfo = (String) request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO);
        if (pathInfo == null) {
            pathInfo = request.getPathInfo();
        }
    } else {
        pathInfo = request.getPathInfo();

        // Is this a Range request?
        reqRanges = request.getHeaders(HeaderFramework.RANGE);
        if (!hasDefinedRange(reqRanges)) {
            reqRanges = null;
        }
    }

    String pathInContext = pathInfo == null ? "/" : pathInfo; // this is the path of the resource in _resourceBase (= path within htroot respective htDocs)
    boolean endsWithSlash = pathInContext.endsWith(URIUtil.SLASH);

    // Find the resource 
    Resource resource = null;

    try {

        // Look for a class resource
        boolean hasClass = false;
        if (reqRanges == null && !endsWithSlash) {
            final int p = pathInContext.lastIndexOf('.');
            if (p >= 0) {
                String pathofClass = pathInContext.substring(0, p) + ".class";
                Resource classresource = _resourceBase.addPath(pathofClass);
                // Does a class resource exist?
                if (classresource != null && classresource.exists() && !classresource.isDirectory()) {
                    hasClass = true;
                }
            }
        }

        // find resource
        resource = getResource(pathInContext);

        if (!hasClass && (resource == null || !resource.exists()) && !pathInContext.contains("..")) {
            // try to get this in the alternative htDocsPath
            resource = Resource.newResource(new File(_htDocsPath, pathInContext));
        }

        if (ConcurrentLog.isFine("FILEHANDLER")) {
            ConcurrentLog.fine("FILEHANDLER",
                    "YaCyDefaultServlet: uri=" + request.getRequestURI() + " resource=" + resource);
        }

        // Handle resource
        if (!hasClass && (resource == null || !resource.exists())) {
            if (included) {
                throw new FileNotFoundException("!" + pathInContext);
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        } else if (!resource.isDirectory()) {
            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(_servletContext.getContextPath(), pathInContext)));
            } else {
                if (hasClass) { // this is a YaCy servlet, handle the template
                    handleTemplate(pathInfo, request, response);
                } else {
                    if (included || passConditionalHeaders(request, response, resource)) {
                        sendData(request, response, included, resource, reqRanges);
                    }
                }
            }
        } else { // resource is directory
            String welcome;

            if (!endsWithSlash) {
                StringBuffer buf = request.getRequestURL();
                synchronized (buf) {
                    int param = buf.lastIndexOf(";");
                    if (param < 0) {
                        buf.append('/');
                    } else {
                        buf.insert(param, '/');
                    }
                    String q = request.getQueryString();
                    if (q != null && q.length() != 0) {
                        buf.append('?');
                        buf.append(q);
                    }
                    response.setContentLength(0);
                    response.sendRedirect(response.encodeRedirectURL(buf.toString()));
                }
            } // else look for a welcome file
            else if (null != (welcome = getWelcomeFile(pathInContext))) {
                ConcurrentLog.fine("FILEHANDLER", "welcome={}" + welcome);

                // Forward to the index
                RequestDispatcher dispatcher = request.getRequestDispatcher(welcome);
                if (dispatcher != null) {
                    if (included) {
                        dispatcher.include(request, response);
                    } else {
                        dispatcher.forward(request, response);
                    }
                }
            } else {
                if (included || passConditionalHeaders(request, response, resource)) {
                    sendDirectory(request, response, resource, pathInContext);
                }
            }
        }
    } catch (IllegalArgumentException e) {
        ConcurrentLog.logException(e);
        if (!response.isCommitted()) {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
        }
    } finally {
        if (resource != null) {
            resource.close();
        }
    }
}

From source file:org.dd4t.core.util.HttpUtils.java

public static String getCurrentURL(final HttpServletRequest request) {
    String url;//from   ww w.j a  v  a  2 s.co m

    DispatcherType dispatcherType = request.getDispatcherType();
    if (dispatcherType == DispatcherType.ERROR) {
        url = request.getRequestURI();
    } else if (dispatcherType == DispatcherType.INCLUDE) {
        url = (String) request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);
    } else {
        url = getOriginalUri(request);
    }

    return url;
}

From source file:org.ireland.jnetty.jsp.JspServletComposite.java

/**
 * jsp/*from w w  w  .jav a 2 s .  c om*/
 * @param request
 * @param response
 * @param jspUri
 * @throws ServletException
 * @throws IOException
 */
private void handleMissingResource(HttpServletRequest request, HttpServletResponse response, String jspUri)
        throws ServletException, IOException {

    String includeRequestUri = (String) request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);

    if (includeRequestUri != null) {
        // This file was included. Throw an exception as
        // a response.sendError() will be ignored
        String msg = Localizer.getMessage("jsp.error.file.not.found", jspUri);
        // Strictly, filtering this is an application
        // responsibility but just in case...
        throw new ServletException(SecurityUtil.filter(msg));
    } else {
        try //?404?
        {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI());
        } catch (IllegalStateException ise) {
            log.error(Localizer.getMessage("jsp.error.file.not.found", jspUri));
        }
    }
    return;
}

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  w w  .  j  a v a  2s .  c o  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);
        }
    }
}