use of org.glassfish.grizzly.http.server.util.AlternateDocBase 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.isEmpty()) {
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.glassfish.grizzly.http.server.util.AlternateDocBase in project Payara by payara.
the class StandardContext method alternateResourcesStop.
/**
* Stops this context's alternate doc base resources.
*/
public boolean alternateResourcesStop() {
boolean ok = true;
if (alternateDocBases == null || alternateDocBases.isEmpty()) {
return ok;
}
for (AlternateDocBase alternateDocBase : alternateDocBases) {
final DirContext alternateResources = ContextsAdapterUtility.unwrap(alternateDocBase.getResources());
if (alternateResources instanceof Lifecycle) {
try {
((Lifecycle) alternateResources).stop();
} catch (Throwable t) {
log.log(Level.SEVERE, LogFacade.STOPPING_RESOURCES_EXCEPTION, t);
ok = false;
}
}
final DirContext alternateWebappResources = ContextsAdapterUtility.unwrap(alternateDocBase.getWebappResources());
if (alternateWebappResources instanceof BaseDirContext) {
try {
((BaseDirContext) alternateWebappResources).release();
} catch (Throwable t) {
log.log(Level.SEVERE, LogFacade.STOPPING_RESOURCES_EXCEPTION, t);
ok = false;
}
}
}
this.alternateDocBases = null;
return (ok);
}
use of org.glassfish.grizzly.http.server.util.AlternateDocBase 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<>();
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.glassfish.grizzly.http.server.util.AlternateDocBase in project Payara by payara.
the class StandardContext method getResourcePaths.
/**
* 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.
*/
@Override
public Set<String> getResourcePaths(String path) {
// Validate the path argument
if (path == null) {
return null;
}
if (!path.startsWith("/")) {
String msg = MessageFormat.format(rb.getString(LogFacade.INCORRECT_PATH), path);
throw new IllegalArgumentException(msg);
}
path = RequestUtil.normalize(path);
if (path == null) {
return (null);
}
DirContext resources = null;
if (alternateDocBases == null || alternateDocBases.isEmpty()) {
resources = getResources();
} else {
AlternateDocBase match = AlternateDocBase.findMatch(path, alternateDocBases);
if (match != null) {
resources = ContextsAdapterUtility.unwrap(match.getResources());
} else {
// None of the url patterns for alternate doc bases matched
resources = getResources();
}
}
if (resources != null) {
return (getResourcePathsInternal(resources, path));
}
return (null);
}
use of org.glassfish.grizzly.http.server.util.AlternateDocBase in project Payara by payara.
the class StandardContext method addAlternateDocBase.
/**
* Configures this context's alternate doc base mappings.
*
* @param urlPattern
* @param docBase
*/
public void addAlternateDocBase(String urlPattern, String docBase) {
if (urlPattern == null || docBase == null) {
throw new IllegalArgumentException(rb.getString(LogFacade.MISS_PATH_OR_URL_PATTERN_EXCEPTION));
}
AlternateDocBase alternateDocBase = new AlternateDocBase();
alternateDocBase.setUrlPattern(urlPattern);
alternateDocBase.setDocBase(docBase);
alternateDocBase.setBasePath(getBasePath(docBase));
if (alternateDocBases == null) {
alternateDocBases = new ArrayList<>();
}
alternateDocBases.add(alternateDocBase);
}
Aggregations