org.apache.tomcat.util.http.RequestUtil Java Examples

The following examples show how to use org.apache.tomcat.util.http.RequestUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebdavServlet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Propfind helper method. Displays the properties of a lock-null resource.
 *
 * @param req The servlet request
 * @param generatedXML XML response to the Propfind request
 * @param path Path of the current resource
 * @param type Propfind type
 * @param propertiesVector If the propfind type is find properties by
 * name, then this Vector contains those properties
 */
private void parseLockNullProperties(HttpServletRequest req,
                                     XMLWriter generatedXML,
                                     String path, int type,
                                     Vector<String> propertiesVector) {

    // Exclude any resource in the /WEB-INF and /META-INF subdirectories
    if (isSpecialPath(path))
        return;

    // Retrieving the lock associated with the lock-null resource
    LockInfo lock = resourceLocks.get(path);

    if (lock == null)
        return;

    String absoluteUri = req.getRequestURI();
    String relativePath = getRelativePath(req);
    String toAppend = path.substring(relativePath.length());
    if (!toAppend.startsWith("/"))
        toAppend = "/" + toAppend;

    String rewrittenUrl = rewriteUrl(RequestUtil.normalize(
            absoluteUri + toAppend));

    generatePropFindResponse(generatedXML, rewrittenUrl, path, type, propertiesVector,
            true, true, lock.creationDate.getTime(), lock.creationDate.getTime(),
            0, "", "");
}
 
Example #2
Source File: StandardRoot.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Ensures that this object is in a valid state to serve resources, checks
 * that the path is a String that starts with '/' and checks that the path
 * can be normalized without stepping outside of the root.
 *
 * @param path
 * @return  the normalized path
 */
private String validate(String path) {
    if (!getState().isAvailable()) {
        throw new IllegalStateException(
                sm.getString("standardRoot.checkStateNotStarted"));
    }

    if (path == null || path.length() == 0 || !path.startsWith("/")) {
        throw new IllegalArgumentException(
                sm.getString("standardRoot.invalidPath", path));
    }

    String result;
    if (File.separatorChar == '\\') {
        // On Windows '\\' is a separator so in case a Windows style
        // separator has managed to make it into the path, replace it.
        result = RequestUtil.normalize(path, true);
    } else {
        // On UNIX and similar systems, '\\' is a valid file name so do not
        // convert it to '/'
        result = RequestUtil.normalize(path, false);
    }
    if (result == null || result.length() == 0 || !result.startsWith("/")) {
        throw new IllegalArgumentException(
                sm.getString("standardRoot.invalidPathNormal", path, result));
    }

    return result;
}
 
Example #3
Source File: SSIServletExternalResolver.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
protected String getAbsolutePath(String path) throws IOException {
    String pathWithoutContext = SSIServletRequestUtil.getRelativePath(req);
    String prefix = getPathWithoutFileName(pathWithoutContext);
    if (prefix == null) {
        throw new IOException("Couldn't remove filename from path: "
                + pathWithoutContext);
    }
    String fullPath = prefix + path;
    String retVal = RequestUtil.normalize(fullPath);
    if (retVal == null) {
        throw new IOException("Normalization yielded null on path: "
                + fullPath);
    }
    return retVal;
}
 
Example #4
Source File: SSIServletExternalResolver.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
protected ServletContextAndPath getServletContextAndPathFromVirtualPath(
        String virtualPath) throws IOException {

    if (!virtualPath.startsWith("/") && !virtualPath.startsWith("\\")) {
        return new ServletContextAndPath(context,
                getAbsolutePath(virtualPath));
    }

    String normalized = RequestUtil.normalize(virtualPath);
    if (isVirtualWebappRelative) {
        return new ServletContextAndPath(context, normalized);
    }

    ServletContext normContext = context.getContext(normalized);
    if (normContext == null) {
        throw new IOException("Couldn't get context for path: "
                + normalized);
    }
    //If it's the root context, then there is no context element
    // to remove,
    // ie:
    // '/file1.shtml' vs '/appName1/file1.shtml'
    if (!isRootContext(normContext)) {
        String noContext = getPathWithoutContext(
                normContext.getContextPath(), normalized);
        if (noContext == null) {
            throw new IOException(
                    "Couldn't remove context from path: "
                            + normalized);
        }
        return new ServletContextAndPath(normContext, noContext);
    }

    return new ServletContextAndPath(normContext, normalized);
}
 
Example #5
Source File: SSIServletExternalResolver.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
protected String getAbsolutePath(String path) throws IOException {
    String pathWithoutContext = SSIServletRequestUtil.getRelativePath(req);
    String prefix = getPathWithoutFileName(pathWithoutContext);
    if (prefix == null) {
        throw new IOException("Couldn't remove filename from path: "
                + pathWithoutContext);
    }
    String fullPath = prefix + path;
    String retVal = RequestUtil.normalize(fullPath);
    if (retVal == null) {
        throw new IOException("Normalization yielded null on path: "
                + fullPath);
    }
    return retVal;
}
 
Example #6
Source File: SSIServletExternalResolver.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
protected ServletContextAndPath getServletContextAndPathFromVirtualPath(
        String virtualPath) throws IOException {

    if (!virtualPath.startsWith("/") && !virtualPath.startsWith("\\")) {
        return new ServletContextAndPath(context,
                getAbsolutePath(virtualPath));
    }

    String normalized = RequestUtil.normalize(virtualPath);
    if (isVirtualWebappRelative) {
        return new ServletContextAndPath(context, normalized);
    }

    ServletContext normContext = context.getContext(normalized);
    if (normContext == null) {
        throw new IOException("Couldn't get context for path: "
                + normalized);
    }
    //If it's the root context, then there is no context element
    // to remove,
    // ie:
    // '/file1.shtml' vs '/appName1/file1.shtml'
    if (!isRootContext(normContext)) {
        String noContext = getPathWithoutContext(
                normContext.getContextPath(), normalized);
        if (noContext == null) {
            throw new IOException(
                    "Couldn't remove context from path: "
                            + normalized);
        }
        return new ServletContextAndPath(normContext, noContext);
    }

    return new ServletContextAndPath(normContext, normalized);
}
 
Example #7
Source File: ApplicationContext.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Return a Set containing the resource paths of resources member of the
 * specified collection. Each path will be a String starting with
 * a "/" character. The returned set is immutable.
 *
 * @param path Collection path
 */
@Override
public Set<String> getResourcePaths(String path) {

    // Validate the path argument
    if (path == null) {
        return null;
    }
    if (!path.startsWith("/")) {
        throw new IllegalArgumentException
            (sm.getString("applicationContext.resourcePaths.iae", path));
    }

    String normalizedPath;
    if (File.separatorChar == '\\') {
        // On Windows '\\' is a separator so in case a Windows style
        // separator has managed to make it into the path, replace it.
        normalizedPath = RequestUtil.normalize(path, true);
    } else {
        // On UNIX and similar systems, '\\' is a valid file name so do not
        // convert it to '/'
        normalizedPath = RequestUtil.normalize(path, false);
    }
    if (normalizedPath == null)
        return (null);

    DirContext resources = context.getResources();
    if (resources != null) {
        return (getResourcePathsInternal(resources, normalizedPath));
    }
    return (null);

}
 
Example #8
Source File: SSIServletExternalResolver.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
protected String getAbsolutePath(String path) throws IOException {
    String pathWithoutContext = SSIServletRequestUtil.getRelativePath(req);
    String prefix = getPathWithoutFileName(pathWithoutContext);
    if (prefix == null) {
        throw new IOException("Couldn't remove filename from path: "
                + pathWithoutContext);
    }
    String fullPath = prefix + path;
    String retVal = RequestUtil.normalize(fullPath);
    if (retVal == null) {
        throw new IOException("Normalization yielded null on path: "
                + fullPath);
    }
    return retVal;
}
 
Example #9
Source File: SSIServletExternalResolver.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
protected ServletContextAndPath getServletContextAndPathFromVirtualPath(
        String virtualPath) throws IOException {

    if (!virtualPath.startsWith("/") && !virtualPath.startsWith("\\")) {
        return new ServletContextAndPath(context,
                getAbsolutePath(virtualPath));
    }

    String normalized = RequestUtil.normalize(virtualPath);
    if (isVirtualWebappRelative) {
        return new ServletContextAndPath(context, normalized);
    }

    ServletContext normContext = context.getContext(normalized);
    if (normContext == null) {
        throw new IOException("Couldn't get context for path: "
                + normalized);
    }
    //If it's the root context, then there is no context element
    // to remove,
    // ie:
    // '/file1.shtml' vs '/appName1/file1.shtml'
    if (!isRootContext(normContext)) {
        String noContext = getPathWithoutContext(
                normContext.getContextPath(), normalized);
        if (noContext == null) {
            throw new IOException(
                    "Couldn't remove context from path: "
                            + normalized);
        }
        return new ServletContextAndPath(normContext, noContext);
    }

    return new ServletContextAndPath(normContext, normalized);
}
 
Example #10
Source File: ApplicationContext.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Return a Set containing the resource paths of resources member of the
 * specified collection. Each path will be a String starting with
 * a "/" character. The returned set is immutable.
 *
 * @param path Collection path
 */
@Override
public Set<String> getResourcePaths(String path) {

    // Validate the path argument
    if (path == null) {
        return null;
    }
    if (!path.startsWith("/")) {
        throw new IllegalArgumentException
            (sm.getString("applicationContext.resourcePaths.iae", path));
    }

    String normalizedPath;
    if (File.separatorChar == '\\') {
        // On Windows '\\' is a separator so in case a Windows style
        // separator has managed to make it into the path, replace it.
        normalizedPath = RequestUtil.normalize(path, true);
    } else {
        // On UNIX and similar systems, '\\' is a valid file name so do not
        // convert it to '/'
        normalizedPath = RequestUtil.normalize(path, false);
    }
    if (normalizedPath == null)
        return (null);

    DirContext resources = context.getResources();
    if (resources != null) {
        return (getResourcePathsInternal(resources, normalizedPath));
    }
    return (null);

}
 
Example #11
Source File: AbstractFileResourceSet.java    From Tomcat8-Source-Read with MIT License 2 votes vote down vote up
/**
 * Return a context-relative path, beginning with a "/", that represents
 * the canonical version of the specified path after ".." and "." elements
 * are resolved out.  If the specified path attempts to go outside the
 * boundaries of the current context (i.e. too many ".." path elements
 * are present), return <code>null</code> instead.
 *
 * @param path Path to be normalized
 */
private String normalize(String path) {
    return RequestUtil.normalize(path, File.separatorChar == '\\');
}
 
Example #12
Source File: FileDirContext.java    From Tomcat7.0.67 with Apache License 2.0 votes vote down vote up
/**
 * Return a context-relative path, beginning with a "/", that represents
 * the canonical version of the specified path after ".." and "." elements
 * are resolved out.  If the specified path attempts to go outside the
 * boundaries of the current context (i.e. too many ".." path elements
 * are present), return <code>null</code> instead.
 *
 * @param path Path to be normalized
 */
protected String normalize(String path) {

    return RequestUtil.normalize(path, File.separatorChar == '\\');

}
 
Example #13
Source File: FileDirContext.java    From tomcatsrc with Apache License 2.0 votes vote down vote up
/**
 * Return a context-relative path, beginning with a "/", that represents
 * the canonical version of the specified path after ".." and "." elements
 * are resolved out.  If the specified path attempts to go outside the
 * boundaries of the current context (i.e. too many ".." path elements
 * are present), return <code>null</code> instead.
 *
 * @param path Path to be normalized
 */
protected String normalize(String path) {

    return RequestUtil.normalize(path, File.separatorChar == '\\');

}