Java Code Examples for org.apache.catalina.WebResource#getCanonicalPath()

The following examples show how to use org.apache.catalina.WebResource#getCanonicalPath() . 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: DefaultServlet.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Check if sendfile can be used.
 * @param request The Servlet request
 * @param response The Servlet response
 * @param resource The resource
 * @param length The length which will be written (will be used only if
 *  range is null)
 * @param range The range that will be written
 * @return <code>true</code> if sendfile should be used (writing is then
 *  delegated to the endpoint)
 */
protected boolean checkSendfile(HttpServletRequest request,
                              HttpServletResponse response,
                              WebResource resource,
                              long length, Range range) {
    String canonicalPath;
    if (sendfileSize > 0
        && length > sendfileSize
        && (Boolean.TRUE.equals(request.getAttribute(Globals.SENDFILE_SUPPORTED_ATTR)))
        && (request.getClass().getName().equals("org.apache.catalina.connector.RequestFacade"))
        && (response.getClass().getName().equals("org.apache.catalina.connector.ResponseFacade"))
        && resource.isFile()
        && ((canonicalPath = resource.getCanonicalPath()) != null)
        ) {
        request.setAttribute(Globals.SENDFILE_FILENAME_ATTR, canonicalPath);
        if (range == null) {
            request.setAttribute(Globals.SENDFILE_FILE_START_ATTR, Long.valueOf(0L));
            request.setAttribute(Globals.SENDFILE_FILE_END_ATTR, Long.valueOf(length));
        } else {
            request.setAttribute(Globals.SENDFILE_FILE_START_ATTR, Long.valueOf(range.start));
            request.setAttribute(Globals.SENDFILE_FILE_END_ATTR, Long.valueOf(range.end + 1));
        }
        return true;
    }
    return false;
}
 
Example 2
Source File: AbstractTestResourceSet.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public final void testGetCanonicalPathDoesNotExist() {
    WebResource exists =
            resourceRoot.getResource(getMount() + "/d1/d1-f1.txt");
    WebResource doesNotExist =
            resourceRoot.getResource(getMount() + "/d1/dummy.txt");
    String doesNotExistCanonicalPath = doesNotExist.getCanonicalPath();

    URL existsUrl = exists.getURL();
    if ("file".equals(existsUrl.getProtocol())) {
        // Should be possible to construct a canonical path for a resource
        // that doesn't exist given that a resource that does exist in the
        // same directory has a URL with the file protocol
        Assert.assertNotNull(doesNotExistCanonicalPath);
    } else {
        Assert.assertNull(doesNotExistCanonicalPath);
    }
}
 
Example 3
Source File: AbstractTestResourceSet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public final void testGetCanonicalPathExists() {
    WebResource exists =
            resourceRoot.getResource(getMount() + "/d1/d1-f1.txt");
    String existsCanonicalPath = exists.getCanonicalPath();

    URL existsUrl = exists.getURL();
    if ("file".equals(existsUrl.getProtocol())) {
        // Should have a canonical path
        Assert.assertNotNull(existsCanonicalPath);
    } else {
        Assert.assertNull(existsCanonicalPath);
    }
}
 
Example 4
Source File: TomcatWebAppBuilder.java    From tomee with Apache License 2.0 4 votes vote down vote up
private static DeploymentLoader.ExternalConfiguration configuredClasspath(final StandardContext standardContext) {
    Loader loader = standardContext.getLoader();
    if (loader != null && LazyStopLoader.class.isInstance(loader)) {
        loader = LazyStopLoader.class.cast(loader).getDelegateLoader();
    }
    if (loader != null) {
        final ClassLoader cl = standardContext.getLoader().getClassLoader();
        if (cl == null) {
            return null;
        }

        final Collection<String> cp = new LinkedList<>();

        final WebResourceRoot webResources = standardContext.getResources();
        if (webResources != null) { // to enhance
            for (final WebResourceSet[] sets : asList(webResources.getPreResources(), webResources.getPostResources(), webResources.getJarResources())) {
                for (final WebResourceSet wr : sets) {
                    final URL base = wr.getBaseUrl();
                    if (base != null) {
                        final File baseFile = URLs.toFile(base);
                        if (baseFile.isDirectory()) {
                            final String[] libs = wr.list("/WEB-INF/lib/");
                            if (libs != null) {
                                for (final String resource : libs) {
                                    cp.add(new File(baseFile, resource).getAbsolutePath());
                                }
                            }

                            final WebResource classes = wr.getResource("/WEB-INF/classes/");
                            if (classes != null) {
                                final String path = classes.getCanonicalPath();
                                if (path != null) {
                                    cp.add(path);
                                }
                            }
                        } else if (baseFile.exists() && baseFile.getName().endsWith(".jar") && wr.getResource("/WEB-INF/classes/").exists()) {
                            try {
                                cp.add(baseFile.getCanonicalPath());
                            } catch (final IOException e) {
                                throw new IllegalStateException(e);
                            }
                        }
                    }
                }
            }
        }

        if (!cp.isEmpty()) {
            return new DeploymentLoader.ExternalConfiguration(cp.toArray(new String[cp.size()]), null /*for now doesnt make sense, todo: configure*/);
        }
    }
    return null;
}