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

The following examples show how to use org.apache.catalina.WebResource#getInputStream() . 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
/**
 * Copy the contents of the specified input stream to the specified
 * output stream, and ensure that both streams are closed before returning
 * (even in the face of an exception).
 *
 * @param resource  The source resource
 * @param ostream   The output stream to write to
 * @param range     Range the client wanted to retrieve
 * @exception IOException if an input/output error occurs
 */
protected void copy(WebResource resource, ServletOutputStream ostream,
                  Range range)
    throws IOException {

    IOException exception = null;

    InputStream resourceInputStream = resource.getInputStream();
    InputStream istream =
        new BufferedInputStream(resourceInputStream, input);
    exception = copyRange(istream, ostream, range.start, range.end);

    // Clean up the input stream
    istream.close();

    // Rethrow any exception that has occurred
    if (exception != null)
        throw exception;

}
 
Example 2
Source File: DefaultServlet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Copy the contents of the specified input stream to the specified
 * output stream, and ensure that both streams are closed before returning
 * (even in the face of an exception).
 *
 * @param resource      The source resource
 * @param ostream       The output stream to write to
 * @param ranges        Enumeration of the ranges the client wanted to
 *                          retrieve
 * @param contentType   Content type of the resource
 * @exception IOException if an input/output error occurs
 */
protected void copy(WebResource resource, ServletOutputStream ostream,
                  Iterator<Range> ranges, String contentType)
    throws IOException {

    IOException exception = null;

    while ( (exception == null) && (ranges.hasNext()) ) {

        InputStream resourceInputStream = resource.getInputStream();
        try (InputStream istream = new BufferedInputStream(resourceInputStream, input)) {

            Range currentRange = ranges.next();

            // Writing MIME header.
            ostream.println();
            ostream.println("--" + mimeSeparation);
            if (contentType != null)
                ostream.println("Content-Type: " + contentType);
            ostream.println("Content-Range: bytes " + currentRange.start
                           + "-" + currentRange.end + "/"
                           + currentRange.length);
            ostream.println();

            // Printing content
            exception = copyRange(istream, ostream, currentRange.start,
                                  currentRange.end);
        }
    }

    ostream.println();
    ostream.print("--" + mimeSeparation + "--");

    // Rethrow any exception that has occurred
    if (exception != null)
        throw exception;

}
 
Example 3
Source File: ExtractingRoot.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
protected void processWebInfLib() throws LifecycleException {

    // Don't extract JAR files unless the application is deployed as a
    // packed WAR file.
    if (!super.isPackedWarFile()) {
        super.processWebInfLib();
        return;
    }

    File expansionTarget = getExpansionTarget();
    if (!expansionTarget.isDirectory()) {
        if (!expansionTarget.mkdirs()) {
            throw new LifecycleException(
                    sm.getString("extractingRoot.targetFailed", expansionTarget));
        }
    }

    WebResource[] possibleJars = listResources("/WEB-INF/lib", false);

    for (WebResource possibleJar : possibleJars) {
        if (possibleJar.isFile() && possibleJar.getName().endsWith(".jar")) {
            try {
                File dest = new File(expansionTarget, possibleJar.getName());
                dest = dest.getCanonicalFile();
                try (InputStream sourceStream = possibleJar.getInputStream();
                        OutputStream destStream= new FileOutputStream(dest)) {
                    IOTools.flow(sourceStream, destStream);
                }

                createWebResourceSet(ResourceSetType.CLASSES_JAR,
                        "/WEB-INF/classes", dest.toURI().toURL(), "/");
            } catch (IOException ioe) {
                throw new LifecycleException(
                        sm.getString("extractingRoot.jarFailed", possibleJar.getName()), ioe);
            }
        }
    }
}
 
Example 4
Source File: TestJarInputStreamWrapper.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private InputStream getWrappedClosedInputStream() throws IOException {
    StandardRoot root = new StandardRoot();
    root.setCachingAllowed(false);
    JarResourceSet jarResourceSet =
            new JarResourceSet(root, "/", "test/webresources/non-static-resources.jar", "/");
    WebResource webResource = jarResourceSet.getResource("/META-INF/MANIFEST.MF");
    InputStream wrapped = webResource.getInputStream();
    wrapped.close();
    return wrapped;
}
 
Example 5
Source File: WebappClassLoaderBase.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Find the resource with the given name, and return an input stream
 * that can be used for reading it.  The search order is as described
 * for <code>getResource()</code>, after checking to see if the resource
 * data has been previously cached.  If the resource cannot be found,
 * return <code>null</code>.
 *
 * @param name Name of the resource to return an input stream for
 */
@Override
public InputStream getResourceAsStream(String name) {

    if (log.isDebugEnabled())
        log.debug("getResourceAsStream(" + name + ")");

    checkStateForResourceLoading(name);

    InputStream stream = null;

    boolean delegateFirst = delegate || filter(name, false);

    // (1) Delegate to parent if requested
    if (delegateFirst) {
        if (log.isDebugEnabled())
            log.debug("  Delegating to parent classloader " + parent);
        stream = parent.getResourceAsStream(name);
        if (stream != null) {
            if (log.isDebugEnabled())
                log.debug("  --> Returning stream from parent");
            return stream;
        }
    }

    // (2) Search local repositories
    if (log.isDebugEnabled())
        log.debug("  Searching local repositories");
    String path = nameToPath(name);
    WebResource resource = resources.getClassLoaderResource(path);
    if (resource.exists()) {
        stream = resource.getInputStream();
        trackLastModified(path, resource);
    }
    try {
        if (hasExternalRepositories && stream == null) {
            URL url = super.findResource(name);
            if (url != null) {
                stream = url.openStream();
            }
        }
    } catch (IOException e) {
        // Ignore
    }
    if (stream != null) {
        if (log.isDebugEnabled())
            log.debug("  --> Returning stream from local");
        return stream;
    }

    // (3) Delegate to parent unconditionally
    if (!delegateFirst) {
        if (log.isDebugEnabled())
            log.debug("  Delegating to parent classloader unconditionally " + parent);
        stream = parent.getResourceAsStream(name);
        if (stream != null) {
            if (log.isDebugEnabled())
                log.debug("  --> Returning stream from parent");
            return stream;
        }
    }

    // (4) Resource was not found
    if (log.isDebugEnabled())
        log.debug("  --> Resource not found, returning null");
    return null;
}