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);
}
}
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);
}
}
}
}
}
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;
}
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);
}
}
}
}
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);
}
Aggregations