Search in sources :

Example 1 with ProxyDirContext

use of org.apache.naming.resources.ProxyDirContext in project Payara by payara.

the class WebappClassLoader method setResources.

/**
 * Set associated resources.
 */
public void setResources(DirContext resources) {
    this.resources = resources;
    DirContext res = resources;
    if (resources instanceof ProxyDirContext) {
        ProxyDirContext proxyRes = (ProxyDirContext) res;
        contextName = proxyRes.getContextName();
        res = proxyRes.getDirContext();
    }
    if (res instanceof WebDirContext) {
        ((WebDirContext) res).setJarFileResourcesProvider(this);
    }
}
Also used : WebDirContext(org.apache.naming.resources.WebDirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext) DirContext(javax.naming.directory.DirContext) WebDirContext(org.apache.naming.resources.WebDirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext)

Example 2 with ProxyDirContext

use of org.apache.naming.resources.ProxyDirContext in project Payara by payara.

the class DefaultServlet method serveResource.

/**
 * Serve the specified resource, optionally including the data content.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param content Should the content be included?
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet-specified error occurs
 */
protected void serveResource(HttpServletRequest request, HttpServletResponse response, boolean content) throws IOException, ServletException {
    // Identify the requested resource path
    String path = getRelativePath(request);
    if (debug > 0) {
        if (content)
            log("DefaultServlet.serveResource:  Serving resource '" + path + "' headers and data");
        else
            log("DefaultServlet.serveResource:  Serving resource '" + path + "' headers only");
    }
    CacheEntry cacheEntry = null;
    ProxyDirContext proxyDirContext = resources;
    if (alternateDocBases == null || alternateDocBases.size() == 0) {
        cacheEntry = proxyDirContext.lookupCache(path);
    } else {
        AlternateDocBase match = AlternateDocBase.findMatch(path, alternateDocBases);
        if (match != null) {
            cacheEntry = ((ProxyDirContext) ContextsAdapterUtility.unwrap(match.getResources())).lookupCache(path);
        } else {
            // None of the url patterns for alternate docbases matched
            cacheEntry = proxyDirContext.lookupCache(path);
        }
    }
    if (!cacheEntry.exists) {
        // Check if we're included so we can return the appropriate
        // missing resource name in the error
        String requestUri = (String) request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);
        /* IASRI 4878272 
            if (requestUri == null) {
                requestUri = request.getRequestURI();
            } else {
            */
        if (requestUri != null) {
            /*
                 * We're included, and the response.sendError() below is going
                 * to be ignored by the including resource (see SRV.8.3,
                 * "The Include Method").
                 * Therefore, the only way we can let the including resource
                 * know about the missing resource is by throwing an 
                 * exception
                */
            throw new FileNotFoundException(requestUri);
        }
        /* IASRI 4878272
            response.sendError(HttpServletResponse.SC_NOT_FOUND,
                               requestUri);
            */
        // BEGIN IASRI 4878272
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        // END IASRI 4878272
        return;
    }
    // ends with "/" or "\", return NOT FOUND
    if (cacheEntry.context == null) {
        if (path.endsWith("/") || (path.endsWith("\\"))) {
            /* IASRI 4878272
                // Check if we're included so we can return the appropriate 
                // missing resource name in the error
                String requestUri = (String) request.getAttribute(
                    RequestDispatcher.INCLUDE_REQUEST_URI);
                if (requestUri == null) {
                    requestUri = request.getRequestURI();
                }
                 response.sendError(HttpServletResponse.SC_NOT_FOUND,
                                    requestUri);
                */
            // BEGIN IASRI 4878272
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            // END IASRI 4878272
            return;
        }
    }
    // satisfied.
    if (cacheEntry.context == null) {
        // Checking If headers
        boolean included = (request.getAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH) != null);
        if (!included && !checkIfHeaders(request, response, cacheEntry.attributes)) {
            return;
        }
    }
    // Find content type.
    String contentType = cacheEntry.attributes.getMimeType();
    if (contentType == null && !cacheEntry.attributes.isMimeTypeInitialized()) {
        contentType = getServletContext().getMimeType(cacheEntry.name);
        cacheEntry.attributes.setMimeType(contentType);
    }
    ArrayList<Range> ranges = null;
    long contentLength = -1L;
    if (cacheEntry.context != null) {
        // suppress them
        if (!listings) {
            /* IASRI 4878272
                 response.sendError(HttpServletResponse.SC_NOT_FOUND,
                                    request.getRequestURI());
                */
            // BEGIN IASRI 4878272
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            // END IASRI 4878272
            return;
        }
        contentType = "text/html;charset=UTF-8";
    } else {
        if (useAcceptRanges) {
            // Accept ranges header
            response.setHeader("Accept-Ranges", "bytes");
        }
        // Parse range specifier
        ranges = parseRange(request, response, cacheEntry.attributes);
        // ETag header
        response.setHeader("ETag", cacheEntry.attributes.getETag());
        // Last-Modified header
        response.setHeader("Last-Modified", cacheEntry.attributes.getLastModifiedHttp());
        // Get content length
        contentLength = cacheEntry.attributes.getContentLength();
        // (silent) ISE when setting the output buffer size
        if (contentLength == 0L) {
            content = false;
        }
    }
    ServletOutputStream ostream = null;
    PrintWriter writer = null;
    if (content) {
        try {
            ostream = response.getOutputStream();
        } catch (IllegalStateException e) {
            // trying to serve a text file
            if ((contentType == null) || (contentType.startsWith("text")) || (contentType.startsWith("xml"))) {
                writer = response.getWriter();
            } else {
                throw e;
            }
        }
    }
    if ((cacheEntry.context != null) || (((ranges == null) || (ranges.isEmpty())) && (request.getHeader("Range") == null)) || (ranges == FULL)) {
        // Set the appropriate output headers
        if (contentType != null) {
            if (debug > 0)
                log("DefaultServlet.serveFile:  contentType='" + contentType + "'");
            response.setContentType(contentType);
        }
        if ((cacheEntry.resource != null) && (contentLength >= 0)) {
            if (debug > 0)
                log("DefaultServlet.serveFile:  contentLength=" + contentLength);
            if (contentLength < Integer.MAX_VALUE) {
                response.setContentLength((int) contentLength);
            } else {
                // Set the content-length as String to be able to use a long
                response.setHeader("content-length", "" + contentLength);
            }
        }
        InputStream renderResult = null;
        if (cacheEntry.context != null) {
            if (content) {
                // Serve the directory browser
                renderResult = render(request.getContextPath(), cacheEntry, proxyDirContext);
            }
        }
        // Copy the input stream to our output stream (if requested)
        if (content) {
            try {
                response.setBufferSize(output);
            } catch (IllegalStateException e) {
            // Silent catch
            }
            if (ostream != null) {
                if (!checkSendfile(request, response, cacheEntry, contentLength, null))
                    copy(cacheEntry, renderResult, ostream);
            } else {
                copy(cacheEntry, renderResult, writer);
            }
        }
    } else {
        if ((ranges == null) || (ranges.isEmpty()))
            return;
        if (maxHeaderRangeItems >= 0 && ranges.size() > maxHeaderRangeItems) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
        // Partial content response.
        response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
        if (ranges.size() == 1) {
            Range range = ranges.get(0);
            response.addHeader("Content-Range", "bytes " + range.start + "-" + range.end + "/" + range.length);
            long length = range.end - range.start + 1;
            if (length < Integer.MAX_VALUE) {
                response.setContentLength((int) length);
            } else {
                // Set the content-length as String to be able to use a long
                response.setHeader("content-length", "" + length);
            }
            if (contentType != null) {
                if (debug > 0)
                    log("DefaultServlet.serveFile:  contentType='" + contentType + "'");
                response.setContentType(contentType);
            }
            if (content) {
                try {
                    response.setBufferSize(output);
                } catch (IllegalStateException e) {
                // Silent catch
                }
                if (ostream != null) {
                    if (!checkSendfile(request, response, cacheEntry, range.end - range.start + 1, range))
                        copy(cacheEntry, ostream, range);
                } else {
                    copy(cacheEntry, writer, range);
                }
            }
        } else {
            response.setContentType("multipart/byteranges; boundary=" + mimeSeparation);
            if (content) {
                try {
                    response.setBufferSize(output);
                } catch (IllegalStateException e) {
                // Silent catch
                }
                if (ostream != null) {
                    copy(cacheEntry, ostream, ranges.iterator(), contentType);
                } else {
                    copy(cacheEntry, writer, ranges.iterator(), contentType);
                }
            }
        }
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) CacheEntry(org.apache.naming.resources.CacheEntry) AlternateDocBase(org.glassfish.grizzly.http.server.util.AlternateDocBase) ProxyDirContext(org.apache.naming.resources.ProxyDirContext) PrintWriter(java.io.PrintWriter)

Example 3 with ProxyDirContext

use of org.apache.naming.resources.ProxyDirContext in project Payara by payara.

the class StandardContext method resourcesStart.

/**
 * Allocate resources, including proxy.
 * Return <code>true</code> if initialization was successfull,
 * or <code>false</code> otherwise.
 */
public boolean resourcesStart() {
    boolean ok = true;
    Hashtable<String, String> env = new Hashtable<String, String>();
    if (getParent() != null) {
        env.put(ProxyDirContext.HOST, getParent().getName());
    }
    env.put(ProxyDirContext.CONTEXT, getName());
    try {
        ProxyDirContext proxyDirContext = new ProxyDirContext(env, webappResources);
        if (webappResources instanceof BaseDirContext) {
            ((BaseDirContext) webappResources).setDocBase(getBasePath(getDocBase()));
            ((BaseDirContext) webappResources).allocate();
        }
        this.resources = proxyDirContext;
    } catch (Throwable t) {
        if (log.isLoggable(Level.FINE)) {
            String msg = MessageFormat.format(rb.getString(LogFacade.STARTING_RESOURCES_EXCEPTION), getName());
            log.log(Level.SEVERE, msg, t);
        } else {
            log.log(Level.SEVERE, LogFacade.STARTING_RESOURCE_EXCEPTION_MESSAGE, new Object[] { getName(), t.getMessage() });
        }
        ok = false;
    }
    return ok;
}
Also used : Hashtable(java.util.Hashtable) BaseDirContext(org.apache.naming.resources.BaseDirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext)

Example 4 with ProxyDirContext

use of org.apache.naming.resources.ProxyDirContext in project Payara by payara.

the class StandardContext method alternateResourcesStart.

/**
 * Starts this context's alternate doc base resources.
 */
public void alternateResourcesStart() throws LifecycleException {
    if (alternateDocBases == null || alternateDocBases.isEmpty()) {
        return;
    }
    Hashtable<String, String> env = new Hashtable<String, String>();
    if (getParent() != null) {
        env.put(ProxyDirContext.HOST, getParent().getName());
    }
    env.put(ProxyDirContext.CONTEXT, getName());
    for (AlternateDocBase alternateDocBase : alternateDocBases) {
        String basePath = alternateDocBase.getBasePath();
        DirContext alternateWebappResources = ContextsAdapterUtility.unwrap(alternateDocBase.getWebappResources());
        try {
            ProxyDirContext proxyDirContext = new ProxyDirContext(env, alternateWebappResources);
            if (alternateWebappResources instanceof BaseDirContext) {
                ((BaseDirContext) alternateWebappResources).setDocBase(basePath);
                ((BaseDirContext) alternateWebappResources).allocate();
            }
            alternateDocBase.setResources(ContextsAdapterUtility.wrap(proxyDirContext));
        } catch (Throwable t) {
            if (log.isLoggable(Level.FINE)) {
                String msg = MessageFormat.format(rb.getString(LogFacade.STARTING_RESOURCES_EXCEPTION), getName());
                throw new LifecycleException(msg, t);
            } else {
                String msg = MessageFormat.format(rb.getString(LogFacade.STARTING_RESOURCE_EXCEPTION_MESSAGE), new Object[] { getName(), t.getMessage() });
                throw new LifecycleException(msg);
            }
        }
    }
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) AlternateDocBase(org.glassfish.grizzly.http.server.util.AlternateDocBase) Hashtable(java.util.Hashtable) BaseDirContext(org.apache.naming.resources.BaseDirContext) WARDirContext(org.apache.naming.resources.WARDirContext) BaseDirContext(org.apache.naming.resources.BaseDirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext) DirContext(javax.naming.directory.DirContext) WebDirContext(org.apache.naming.resources.WebDirContext) FileDirContext(org.apache.naming.resources.FileDirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext)

Example 5 with ProxyDirContext

use of org.apache.naming.resources.ProxyDirContext in project Payara by payara.

the class ContainerBase method setResources.

/**
 * Set the resources DirContext object with which this Container is
 * associated.
 *
 * @param resources The newly associated DirContext
 * @throws Exception
 */
// XXX: Is exception ever thrown?
@Override
public void setResources(DirContext resources) throws Exception {
    // Called from StandardContext.setResources()
    // <- StandardContext.start()
    // <- ContainerBase.addChildInternal()
    // Change components if necessary
    DirContext oldResources;
    try {
        writeLock.lock();
        oldResources = this.resources;
        if (oldResources == resources)
            return;
        Hashtable<String, String> env = new Hashtable<String, String>();
        if (getParent() != null)
            env.put(ProxyDirContext.HOST, getParent().getName());
        env.put(ProxyDirContext.CONTEXT, getName());
        this.resources = new ProxyDirContext(env, resources);
    // Report this property change to interested listeners
    } finally {
        writeLock.unlock();
    }
    support.firePropertyChange("resources", oldResources, this.resources);
}
Also used : ProxyDirContext(org.apache.naming.resources.ProxyDirContext) DirContext(javax.naming.directory.DirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext)

Aggregations

ProxyDirContext (org.apache.naming.resources.ProxyDirContext)5 DirContext (javax.naming.directory.DirContext)3 Hashtable (java.util.Hashtable)2 BaseDirContext (org.apache.naming.resources.BaseDirContext)2 WebDirContext (org.apache.naming.resources.WebDirContext)2 AlternateDocBase (org.glassfish.grizzly.http.server.util.AlternateDocBase)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 LifecycleException (org.apache.catalina.LifecycleException)1 CacheEntry (org.apache.naming.resources.CacheEntry)1 FileDirContext (org.apache.naming.resources.FileDirContext)1 WARDirContext (org.apache.naming.resources.WARDirContext)1