Search in sources :

Example 1 with ServletMapping

use of javax.servlet.http.ServletMapping in project tomcat by apache.

the class ApplicationContext method getRequestDispatcher.

@Override
public RequestDispatcher getRequestDispatcher(String path) {
    // Validate the path argument
    if (path == null)
        return (null);
    if (!path.startsWith("/"))
        throw new IllegalArgumentException(sm.getString("applicationContext.requestDispatcher.iae", path));
    // Get query string
    String queryString = null;
    String normalizedPath = path;
    int pos = normalizedPath.indexOf('?');
    if (pos >= 0) {
        queryString = normalizedPath.substring(pos + 1);
        normalizedPath = normalizedPath.substring(0, pos);
    }
    normalizedPath = RequestUtil.normalize(normalizedPath);
    if (normalizedPath == null)
        return (null);
    if (getContext().getDispatchersUseEncodedPaths()) {
        // Decode
        String decodedPath;
        try {
            decodedPath = URLDecoder.decode(normalizedPath, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // Impossible
            return null;
        }
        // Security check to catch attempts to encode /../ sequences
        normalizedPath = RequestUtil.normalize(decodedPath);
        if (!decodedPath.equals(normalizedPath)) {
            getContext().getLogger().warn(sm.getString("applicationContext.illegalDispatchPath", path), new IllegalArgumentException());
            return null;
        }
    }
    pos = normalizedPath.length();
    // Use the thread local URI and mapping data
    DispatchData dd = dispatchData.get();
    if (dd == null) {
        dd = new DispatchData();
        dispatchData.set(dd);
    }
    MessageBytes uriMB = dd.uriMB;
    uriMB.recycle();
    // Use the thread local mapping data
    MappingData mappingData = dd.mappingData;
    // Map the URI
    CharChunk uriCC = uriMB.getCharChunk();
    try {
        uriCC.append(context.getPath(), 0, context.getPath().length());
        /*
             * Ignore any trailing path params (separated by ';') for mapping
             * purposes
             */
        int semicolon = normalizedPath.indexOf(';');
        if (pos >= 0 && semicolon > pos) {
            semicolon = -1;
        }
        uriCC.append(normalizedPath, 0, semicolon > 0 ? semicolon : pos);
        service.getMapper().map(context, uriMB, mappingData);
        if (mappingData.wrapper == null) {
            return (null);
        }
        /*
             * Append any trailing path params (separated by ';') that were
             * ignored for mapping purposes, so that they're reflected in the
             * RequestDispatcher's requestURI
             */
        if (semicolon > 0) {
            uriCC.append(normalizedPath, semicolon, pos - semicolon);
        }
    } catch (Exception e) {
        // Should never happen
        log(sm.getString("applicationContext.mapping.error"), e);
        return (null);
    }
    Wrapper wrapper = mappingData.wrapper;
    String wrapperPath = mappingData.wrapperPath.toString();
    String pathInfo = mappingData.pathInfo.toString();
    ServletMapping mapping = (new ApplicationMapping(mappingData)).getServletMapping();
    mappingData.recycle();
    String encodedUri = URLEncoder.DEFAULT.encode(uriCC.toString(), "UTF-8");
    // Construct a RequestDispatcher to process this request
    return new ApplicationDispatcher(wrapper, encodedUri, wrapperPath, pathInfo, queryString, mapping, null);
}
Also used : Wrapper(org.apache.catalina.Wrapper) ServletMapping(javax.servlet.http.ServletMapping) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) ServletException(javax.servlet.ServletException) NamingException(javax.naming.NamingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) MappingData(org.apache.catalina.mapper.MappingData) CharChunk(org.apache.tomcat.util.buf.CharChunk)

Aggregations

UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 NamingException (javax.naming.NamingException)1 ServletException (javax.servlet.ServletException)1 ServletMapping (javax.servlet.http.ServletMapping)1 Wrapper (org.apache.catalina.Wrapper)1 MappingData (org.apache.catalina.mapper.MappingData)1 CharChunk (org.apache.tomcat.util.buf.CharChunk)1 MessageBytes (org.apache.tomcat.util.buf.MessageBytes)1