remm 2004/07/13 02:43:19
Modified: catalina/src/share/org/apache/catalina/servlets
WebdavServlet.java DefaultServlet.java
Log:
- Simplify the code of DefaultServlet a bit, by removing ResourceInfo (which
duplicates information from CacheEntry).
- This is also a small performance improvement.
Revision Changes Path
1.15 +30 -82
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/servlets/WebdavServlet.java
Index: WebdavServlet.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/servlets/WebdavServlet.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- WebdavServlet.java 26 May 2004 16:03:14 -0000 1.14
+++ WebdavServlet.java 13 Jul 2004 09:43:19 -0000 1.15
@@ -43,7 +43,9 @@
import org.apache.catalina.util.DOMWriter;
import org.apache.catalina.util.RequestUtil;
import org.apache.catalina.util.XMLWriter;
+import org.apache.naming.resources.CacheEntry;
import org.apache.naming.resources.Resource;
+import org.apache.naming.resources.ResourceAttributes;
import org.apache.tomcat.util.http.FastHttpDateFormat;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -275,10 +277,10 @@
*/
protected boolean checkIfHeaders(HttpServletRequest request,
HttpServletResponse response,
- ResourceInfo resourceInfo)
+ ResourceAttributes resourceAttributes)
throws IOException {
- if (!super.checkIfHeaders(request, response, resourceInfo))
+ if (!super.checkIfHeaders(request, response, resourceAttributes))
return false;
// TODO : Checking the WebDAV If header
@@ -295,14 +297,6 @@
resp.addHeader("DAV", "1,2");
- // Retrieve the resources
- DirContext resources = getResources();
-
- if (resources == null) {
- resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- return;
- }
-
StringBuffer methodsAllowed = determineMethodsAllowed(resources,
req);
@@ -319,14 +313,6 @@
throws ServletException, IOException {
if (!listings) {
- // Retrieve the resources
- DirContext resources = getResources();
-
- if (resources == null) {
- resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- return;
- }
-
// Get allowed methods
StringBuffer methodsAllowed = determineMethodsAllowed(resources,
req);
@@ -429,14 +415,6 @@
}
- // Retrieve the resources
- DirContext resources = getResources();
-
- if (resources == null) {
- resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- return;
- }
-
boolean exists = true;
Object object = null;
try {
@@ -496,7 +474,7 @@
XMLWriter.OPENING);
if (depth == 0) {
- parseProperties(req, resources, generatedXML, path, type,
+ parseProperties(req, generatedXML, path, type,
properties);
} else {
// The stack always contains the object of the current level
@@ -509,7 +487,7 @@
while ((!stack.isEmpty()) && (depth >= 0)) {
String currentPath = (String) stack.pop();
- parseProperties(req, resources, generatedXML, currentPath,
+ parseProperties(req, generatedXML, currentPath,
type, properties);
try {
@@ -625,14 +603,6 @@
return;
}
- // Retrieve the resources
- DirContext resources = getResources();
-
- if (resources == null) {
- resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- return;
- }
-
boolean exists = true;
Object object = null;
try {
@@ -991,14 +961,6 @@
lock.path = path;
- // Retrieve the resources
- DirContext resources = getResources();
-
- if (resources == null) {
- resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- return;
- }
-
boolean exists = true;
Object object = null;
try {
@@ -1552,14 +1514,6 @@
// Overwriting the destination
- // Retrieve the resources
- DirContext resources = getResources();
-
- if (resources == null) {
- resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- return false;
- }
-
boolean exists = true;
try {
resources.lookup(destinationPath);
@@ -1737,14 +1691,6 @@
return false;
}
- // Retrieve the resources
- DirContext resources = getResources();
-
- if (resources == null) {
- resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- return false;
- }
-
boolean exists = true;
Object object = null;
try {
@@ -1943,7 +1889,7 @@
* name, then this Vector contains those properties
*/
private void parseProperties(HttpServletRequest req,
- DirContext resources, XMLWriter generatedXML,
+ XMLWriter generatedXML,
String path, int type,
Vector propertiesVector) {
@@ -1953,7 +1899,7 @@
path.toUpperCase().startsWith("/META-INF"))
return;
- ResourceInfo resourceInfo = new ResourceInfo(path, resources);
+ CacheEntry cacheEntry = resources.lookupCache(path);
generatedXML.writeElement(null, "response", XMLWriter.OPENING);
String status = new String("HTTP/1.1 " + WebdavStatus.SC_OK + " "
@@ -1968,7 +1914,7 @@
href += path.substring(1);
else
href += path;
- if ((resourceInfo.collection) && (!href.endsWith("/")))
+ if ((cacheEntry.context != null) && (!href.endsWith("/")))
href += "/";
generatedXML.writeText(rewriteUrl(href));
@@ -1989,24 +1935,25 @@
generatedXML.writeProperty
(null, "creationdate",
- getISOCreationDate(resourceInfo.creationDate));
+ getISOCreationDate(cacheEntry.attributes.getCreation()));
generatedXML.writeElement(null, "displayname", XMLWriter.OPENING);
generatedXML.writeData(resourceName);
generatedXML.writeElement(null, "displayname", XMLWriter.CLOSING);
- if (!resourceInfo.collection) {
+ if (cacheEntry.resource != null) {
generatedXML.writeProperty
- (null, "getlastmodified", resourceInfo.httpDate);
+ (null, "getlastmodified", FastHttpDateFormat.formatDate
+ (cacheEntry.attributes.getLastModified(), null));
generatedXML.writeProperty
(null, "getcontentlength",
- String.valueOf(resourceInfo.length));
+ String.valueOf(cacheEntry.attributes.getContentLength()));
String contentType = getServletContext().getMimeType
- (resourceInfo.path);
+ (cacheEntry.name);
if (contentType != null) {
generatedXML.writeProperty(null, "getcontenttype",
contentType);
}
generatedXML.writeProperty(null, "getetag",
- getETag(resourceInfo));
+ getETag(cacheEntry.attributes));
generatedXML.writeElement(null, "resourcetype",
XMLWriter.NO_CONTENT);
} else {
@@ -2052,7 +1999,7 @@
XMLWriter.NO_CONTENT);
generatedXML.writeElement(null, "displayname",
XMLWriter.NO_CONTENT);
- if (!resourceInfo.collection) {
+ if (cacheEntry.resource != null) {
generatedXML.writeElement(null, "getcontentlanguage",
XMLWriter.NO_CONTENT);
generatedXML.writeElement(null, "getcontentlength",
@@ -2096,7 +2043,7 @@
if (property.equals("creationdate")) {
generatedXML.writeProperty
(null, "creationdate",
- getISOCreationDate(resourceInfo.creationDate));
+ getISOCreationDate(cacheEntry.attributes.getCreation()));
} else if (property.equals("displayname")) {
generatedXML.writeElement
(null, "displayname", XMLWriter.OPENING);
@@ -2104,45 +2051,46 @@
generatedXML.writeElement
(null, "displayname", XMLWriter.CLOSING);
} else if (property.equals("getcontentlanguage")) {
- if (resourceInfo.collection) {
+ if (cacheEntry.context != null) {
propertiesNotFound.addElement(property);
} else {
generatedXML.writeElement(null, "getcontentlanguage",
XMLWriter.NO_CONTENT);
}
} else if (property.equals("getcontentlength")) {
- if (resourceInfo.collection) {
+ if (cacheEntry.context != null) {
propertiesNotFound.addElement(property);
} else {
generatedXML.writeProperty
(null, "getcontentlength",
- (String.valueOf(resourceInfo.length)));
+
(String.valueOf(cacheEntry.attributes.getContentLength())));
}
} else if (property.equals("getcontenttype")) {
- if (resourceInfo.collection) {
+ if (cacheEntry.context != null) {
propertiesNotFound.addElement(property);
} else {
generatedXML.writeProperty
(null, "getcontenttype",
getServletContext().getMimeType
- (resourceInfo.path));
+ (cacheEntry.name));
}
} else if (property.equals("getetag")) {
- if (resourceInfo.collection) {
+ if (cacheEntry.context != null) {
propertiesNotFound.addElement(property);
} else {
generatedXML.writeProperty
- (null, "getetag", getETag(resourceInfo));
+ (null, "getetag", getETag(cacheEntry.attributes));
}
} else if (property.equals("getlastmodified")) {
- if (resourceInfo.collection) {
+ if (cacheEntry.context != null) {
propertiesNotFound.addElement(property);
} else {
generatedXML.writeProperty
- (null, "getlastmodified", resourceInfo.httpDate);
+ (null, "getlastmodified", FastHttpDateFormat.formatDate
+ (cacheEntry.attributes.getLastModified(),
null));
}
} else if (property.equals("resourcetype")) {
- if (resourceInfo.collection) {
+ if (cacheEntry.context != null) {
generatedXML.writeElement(null, "resourcetype",
XMLWriter.OPENING);
generatedXML.writeElement(null, "collection",
1.24 +139 -401
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java
Index: DefaultServlet.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- DefaultServlet.java 26 May 2004 16:03:06 -0000 1.23
+++ DefaultServlet.java 13 Jul 2004 09:43:19 -0000 1.24
@@ -34,8 +34,6 @@
import java.io.StringWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
-import java.sql.Timestamp;
-import java.util.Date;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;
@@ -44,10 +42,10 @@
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
-import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
+import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -62,9 +60,10 @@
import org.apache.catalina.util.ServerInfo;
import org.apache.catalina.util.StringManager;
import org.apache.catalina.util.URLEncoder;
+import org.apache.naming.resources.CacheEntry;
+import org.apache.naming.resources.ProxyDirContext;
import org.apache.naming.resources.Resource;
import org.apache.naming.resources.ResourceAttributes;
-import org.apache.tomcat.util.http.FastHttpDateFormat;
/**
@@ -149,6 +148,11 @@
protected String readmeFile = null;
+ /**
+ * Proxy directory context.
+ */
+ protected ProxyDirContext resources = null;
+
// ----------------------------------------------------- Static Initializer
@@ -244,7 +248,6 @@
localXsltFile = getServletConfig().getInitParameter("localXsltFile");
readmeFile = getServletConfig().getInitParameter("readmeFile");
-
// Sanity check on the specified buffer sizes
if (input < 256)
input = 256;
@@ -260,139 +263,36 @@
try {
md5Helper = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- throw new IllegalStateException();
+ throw new UnavailableException("No MD5");
}
- }
-
-
-
- // ------------------------------------------------------ Protected Methods
-
-
- /**
- * Get resources. This method will try to retrieve the resources through
- * JNDI first, then in the servlet context if JNDI has failed (it could be
- * disabled). It will return null.
- *
- * @return A JNDI DirContext, or null.
- */
- protected DirContext getResources() {
-
- DirContext result = null;
-
- // Try the servlet context
+ // Load the proxy dir context.
try {
- result = (DirContext) getServletContext()
+ resources = (ProxyDirContext) getServletContext()
.getAttribute(Globals.RESOURCES_ATTR);
- } catch (ClassCastException e) {
+ } catch(ClassCastException e) {
// Failed : Not the right type
}
-
- if (result != null)
- return result;
-
- // Try JNDI
- try {
- result =
- (DirContext) new InitialContext().lookup(RESOURCES_JNDI_NAME);
- } catch (NamingException e) {
- // Failed
- } catch (ClassCastException e) {
- // Failed : Not the right type
- }
-
- return result;
-
- }
-
-
- /**
- * Show HTTP header information.
- */
- protected void showRequestInfo(HttpServletRequest req) {
-
- System.out.println();
- System.out.println("SlideDAV Request Info");
- System.out.println();
-
- // Show generic info
- System.out.println("Encoding : " + req.getCharacterEncoding());
- System.out.println("Length : " + req.getContentLength());
- System.out.println("Type : " + req.getContentType());
-
- System.out.println();
- System.out.println("Parameters");
-
- Enumeration parameters = req.getParameterNames();
-
- while (parameters.hasMoreElements()) {
- String paramName = (String) parameters.nextElement();
- String[] values = req.getParameterValues(paramName);
- System.out.print(paramName + " : ");
- for (int i = 0; i < values.length; i++) {
- System.out.print(values[i] + ", ");
+ if (resources == null) {
+ try {
+ resources =
+ (ProxyDirContext) new InitialContext()
+ .lookup(RESOURCES_JNDI_NAME);
+ } catch (NamingException e) {
+ // Failed
+ } catch (ClassCastException e) {
+ // Failed : Not the right type
}
- System.out.println();
}
- System.out.println();
-
- System.out.println("Protocol : " + req.getProtocol());
- System.out.println("Address : " + req.getRemoteAddr());
- System.out.println("Host : " + req.getRemoteHost());
- System.out.println("Scheme : " + req.getScheme());
- System.out.println("Server Name : " + req.getServerName());
- System.out.println("Server Port : " + req.getServerPort());
-
- System.out.println();
- System.out.println("Attributes");
-
- Enumeration attributes = req.getAttributeNames();
-
- while (attributes.hasMoreElements()) {
- String attributeName = (String) attributes.nextElement();
- System.out.print(attributeName + " : ");
- System.out.println(req.getAttribute(attributeName).toString());
- }
-
- System.out.println();
-
- // Show HTTP info
- System.out.println();
- System.out.println("HTTP Header Info");
- System.out.println();
-
- System.out.println("Authentication Type : " + req.getAuthType());
- System.out.println("HTTP Method : " + req.getMethod());
- System.out.println("Path Info : " + req.getPathInfo());
- System.out.println("Path translated : " + req.getPathTranslated());
- System.out.println("Query string : " + req.getQueryString());
- System.out.println("Remote user : " + req.getRemoteUser());
- System.out.println("Requested session id : "
- + req.getRequestedSessionId());
- System.out.println("Request URI : " + req.getRequestURI());
- System.out.println("Context path : " + req.getContextPath());
- System.out.println("Servlet path : " + req.getServletPath());
- System.out.println("User principal : " + req.getUserPrincipal());
-
-
- System.out.println();
- System.out.println("Headers : ");
-
- Enumeration headers = req.getHeaderNames();
-
- while (headers.hasMoreElements()) {
- String headerName = (String) headers.nextElement();
- System.out.print(headerName + " : ");
- System.out.println(req.getHeader(headerName));
+ if (resources == null) {
+ throw new UnavailableException("No resources");
}
+
+ }
- System.out.println();
- System.out.println();
- }
+ // ------------------------------------------------------ Protected Methods
/**
@@ -440,9 +340,6 @@
HttpServletResponse response)
throws IOException, ServletException {
- if (debug > 999)
- showRequestInfo(request);
-
// Serve the requested resource, including the data content
try {
serveResource(request, response, true);
@@ -512,14 +409,6 @@
String path = getRelativePath(req);
- // Retrieve the resources
- DirContext resources = getResources();
-
- if (resources == null) {
- resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- return;
- }
-
boolean exists = true;
try {
resources.lookup(path);
@@ -535,7 +424,6 @@
// Input stream for temp. content file used to support partial PUT
FileInputStream contentFileInStream = null;
- ResourceInfo resourceInfo = new ResourceInfo(path, resources);
Range range = parseContentRange(req, resp);
InputStream resourceInputStream = null;
@@ -603,7 +491,7 @@
Resource oldResource = null;
try {
- Object obj = getResources().lookup(path);
+ Object obj = resources.lookup(path);
if (obj instanceof Resource)
oldResource = (Resource) obj;
} catch (NamingException e) {
@@ -662,15 +550,6 @@
String path = getRelativePath(req);
- // Retrieve the Catalina context
- // Retrieve the resources
- DirContext resources = getResources();
-
- if (resources == null) {
- resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- return;
- }
-
boolean exists = true;
try {
resources.lookup(path);
@@ -710,13 +589,13 @@
*/
protected boolean checkIfHeaders(HttpServletRequest request,
HttpServletResponse response,
- ResourceInfo resourceInfo)
+ ResourceAttributes resourceAttributes)
throws IOException {
- return checkIfMatch(request, response, resourceInfo)
- && checkIfModifiedSince(request, response, resourceInfo)
- && checkIfNoneMatch(request, response, resourceInfo)
- && checkIfUnmodifiedSince(request, response, resourceInfo);
+ return checkIfMatch(request, response, resourceAttributes)
+ && checkIfModifiedSince(request, response, resourceAttributes)
+ && checkIfNoneMatch(request, response, resourceAttributes)
+ && checkIfUnmodifiedSince(request, response, resourceAttributes);
}
@@ -726,14 +605,15 @@
*
* @param resourceInfo File object
*/
- protected String getETag(ResourceInfo resourceInfo) {
- if (resourceInfo.strongETag != null) {
- return resourceInfo.strongETag;
- } else if (resourceInfo.weakETag != null) {
- return resourceInfo.weakETag;
+ protected String getETag(ResourceAttributes resourceAttributes) {
+ String result = null;
+ if ((result = resourceAttributes.getETag(true)) != null) {
+ return result;
+ } else if ((result = resourceAttributes.getETag()) != null) {
+ return result;
} else {
- return "W/\"" + resourceInfo.length + "-"
- + resourceInfo.date + "\"";
+ return "W/\"" + resourceAttributes.getContentLength() + "-"
+ + resourceAttributes.getLastModified() + "\"";
}
}
@@ -855,11 +735,9 @@
path + "' headers only");
}
- // Retrieve the Catalina context and Resources implementation
- DirContext resources = getResources();
- ResourceInfo resourceInfo = new ResourceInfo(path, resources);
+ CacheEntry cacheEntry = resources.lookupCache(path);
- if (!resourceInfo.exists) {
+ if (!cacheEntry.exists) {
response.sendError(HttpServletResponse.SC_NOT_FOUND,
request.getRequestURI());
return;
@@ -867,7 +745,7 @@
// If the resource is not a collection, and the resource path
// ends with "/" or "\", return NOT FOUND
- if (!resourceInfo.collection) {
+ if (cacheEntry.context == null) {
if (path.endsWith("/") || (path.endsWith("\\"))) {
response.sendError(HttpServletResponse.SC_NOT_FOUND,
request.getRequestURI());
@@ -877,25 +755,28 @@
// Check if the conditions specified in the optional If headers are
// satisfied.
- if (!resourceInfo.collection) {
+ if (cacheEntry.context == null) {
// Checking If headers
boolean included =
(request.getAttribute(Globals.INCLUDE_CONTEXT_PATH_ATTR) != null);
if (!included
- && !checkIfHeaders(request, response, resourceInfo)) {
+ && !checkIfHeaders(request, response, cacheEntry.attributes)) {
return;
}
}
// Find content type.
- String contentType =
- getServletContext().getMimeType(resourceInfo.path);
+ String contentType = cacheEntry.attributes.getMimeType();
+ if (contentType == null) {
+ contentType = getServletContext().getMimeType(cacheEntry.name);
+ cacheEntry.attributes.setMimeType(contentType);
+ }
Vector ranges = null;
- if (resourceInfo.collection) {
+ if (cacheEntry.context != null) {
// Skip directory listings if we have been configured to
// suppress them
@@ -910,16 +791,14 @@
// Parse range specifier
- ranges = parseRange(request, response, resourceInfo);
+ ranges = parseRange(request, response, cacheEntry.attributes);
// ETag header
- response.setHeader("ETag", getETag(resourceInfo));
+ response.setHeader("ETag", getETag(cacheEntry.attributes));
// Last-Modified header
- if (debug > 0)
- log("DefaultServlet.serveFile: lastModified='" +
- (new Timestamp(resourceInfo.date)).toString() + "'");
- response.setHeader("Last-Modified", resourceInfo.httpDate);
+ response.setHeader("Last-Modified",
+ cacheEntry.attributes.getLastModifiedHttp());
}
@@ -945,7 +824,7 @@
}
- if ( (resourceInfo.collection) ||
+ if ( (cacheEntry.context != null) ||
( ((ranges == null) || (ranges.isEmpty()))
&& (request.getHeader("Range") == null) ) ) {
@@ -956,20 +835,21 @@
contentType + "'");
response.setContentType(contentType);
}
- long contentLength = resourceInfo.length;
- if ((!resourceInfo.collection) && (contentLength >= 0)) {
+ long contentLength = cacheEntry.attributes.getContentLength();
+ if ((cacheEntry.resource != null) && (contentLength >= 0)) {
if (debug > 0)
log("DefaultServlet.serveFile: contentLength=" +
contentLength);
response.setContentLength((int) contentLength);
}
- if (resourceInfo.collection) {
+ InputStream renderResult = null;
+ if (cacheEntry.context != null) {
if (content) {
// Serve the directory browser
- resourceInfo.setStream
- (render(request.getContextPath(), resourceInfo));
+ renderResult =
+ render(request.getContextPath(), cacheEntry);
}
}
@@ -982,9 +862,9 @@
// Silent catch
}
if (ostream != null) {
- copy(resourceInfo, ostream);
+ copy(cacheEntry, renderResult, ostream);
} else {
- copy(resourceInfo, writer);
+ copy(cacheEntry, renderResult, writer);
}
}
@@ -1020,9 +900,9 @@
// Silent catch
}
if (ostream != null) {
- copy(resourceInfo, ostream, range);
+ copy(cacheEntry, ostream, range);
} else {
- copy(resourceInfo, writer, range);
+ copy(cacheEntry, writer, range);
}
}
@@ -1038,10 +918,10 @@
// Silent catch
}
if (ostream != null) {
- copy(resourceInfo, ostream, ranges.elements(),
+ copy(cacheEntry, ostream, ranges.elements(),
contentType);
} else {
- copy(resourceInfo, writer, ranges.elements(),
+ copy(cacheEntry, writer, ranges.elements(),
contentType);
}
}
@@ -1123,7 +1003,7 @@
*/
protected Vector parseRange(HttpServletRequest request,
HttpServletResponse response,
- ResourceInfo resourceInfo)
+ ResourceAttributes resourceAttributes)
throws IOException {
// Checking If-Range
@@ -1138,8 +1018,8 @@
;
}
- String eTag = getETag(resourceInfo);
- long lastModified = resourceInfo.date;
+ String eTag = getETag(resourceAttributes);
+ long lastModified = resourceAttributes.getLastModified();
if (headerValueTime == (-1L)) {
@@ -1160,7 +1040,7 @@
}
- long fileLength = resourceInfo.length;
+ long fileLength = resourceAttributes.getContentLength();
if (fileLength == 0)
return null;
@@ -1275,14 +1155,14 @@
* Decide which way to render. HTML or XML.
*/
protected InputStream render
- (String contextPath, ResourceInfo resourceInfo) {
+ (String contextPath, CacheEntry cacheEntry) {
InputStream xsltInputStream =
- findXsltInputStream(resourceInfo.directory);
+ findXsltInputStream(cacheEntry.context);
if (xsltInputStream==null) {
- return renderHtml(contextPath, resourceInfo);
+ return renderHtml(contextPath, cacheEntry);
} else {
- return renderXml(contextPath, resourceInfo, xsltInputStream);
+ return renderXml(contextPath, cacheEntry, xsltInputStream);
}
}
@@ -1295,39 +1175,35 @@
* relative
*/
protected InputStream renderXml(String contextPath,
- ResourceInfo resourceInfo,
+ CacheEntry cacheEntry,
InputStream xsltInputStream) {
StringBuffer sb = new StringBuffer();
-
-
sb.append("<?xml version=\"1.0\"?>");
sb.append("<listing ");
sb.append(" contextPath='");
sb.append(contextPath);
sb.append("'");
sb.append(" directory='");
- sb.append(resourceInfo.path);
+ sb.append(cacheEntry.name);
sb.append("' ");
- sb.append(" hasParent='").append(!resourceInfo.path.equals("/"));
+ sb.append(" hasParent='").append(!cacheEntry.name.equals("/"));
sb.append("'>");
sb.append("<entries>");
-
try {
// Render the directory entries within this directory
- DirContext directory = resourceInfo.directory;
- NamingEnumeration enum =
- resourceInfo.resources.list(resourceInfo.path);
+ DirContext directory = cacheEntry.context;
+ NamingEnumeration enum = resources.list(cacheEntry.name);
while (enum.hasMoreElements()) {
NameClassPair ncPair = (NameClassPair) enum.nextElement();
String resourceName = ncPair.getName();
- ResourceInfo childResourceInfo =
- new ResourceInfo(resourceName, directory);
+ CacheEntry childCacheEntry =
+ resources.lookupCache(cacheEntry.name + resourceName);
String trimmed = resourceName/*.substring(trim)*/;
if (trimmed.equalsIgnoreCase("WEB-INF") ||
@@ -1335,28 +1211,27 @@
trimmed.equalsIgnoreCase(localXsltFile))
continue;
-
sb.append("<entry");
sb.append(" type='")
- .append(childResourceInfo.collection?"dir":"file")
+ .append((childCacheEntry.context != null)?"dir":"file")
.append("'");
sb.append(" urlPath='")
.append(rewriteUrl(contextPath))
- .append(rewriteUrl(resourceInfo.path + resourceName))
- .append(childResourceInfo.collection?"/":"")
+ .append(rewriteUrl(cacheEntry.name + resourceName))
+ .append((childCacheEntry.context != null)?"/":"")
.append("'");
- if (!childResourceInfo.collection) {
+ if (childCacheEntry.resource != null) {
sb.append(" size='")
- .append(renderSize(childResourceInfo.length))
+
.append(renderSize(childCacheEntry.attributes.getContentLength()))
.append("'");
}
sb.append(" date='")
- .append(childResourceInfo.httpDate)
+ .append(childCacheEntry.attributes.getLastModifiedHttp())
.append("'");
sb.append(">");
sb.append(trimmed);
- if (childResourceInfo.collection)
+ if (childCacheEntry.context != null)
sb.append("/");
sb.append("</entry>");
@@ -1369,7 +1244,7 @@
sb.append("</entries>");
- String readme = getReadme(resourceInfo.directory);
+ String readme = getReadme(cacheEntry.context);
if (readme!=null) {
sb.append("<readme><![CDATA[");
@@ -1395,7 +1270,7 @@
return (new ByteArrayInputStream(stream.toByteArray()));
} catch (Exception e) {
log("directory transform failure: " + e.getMessage());
- return renderHtml(contextPath, resourceInfo);
+ return renderHtml(contextPath, cacheEntry);
}
}
@@ -1407,9 +1282,9 @@
* relative
*/
protected InputStream renderHtml
- (String contextPath, ResourceInfo resourceInfo) {
+ (String contextPath, CacheEntry cacheEntry) {
- String name = resourceInfo.path;
+ String name = cacheEntry.name;
// Number of characters to trim from the beginnings of filenames
int trim = name.length();
@@ -1490,16 +1365,15 @@
try {
// Render the directory entries within this directory
- DirContext directory = resourceInfo.directory;
- NamingEnumeration enum =
- resourceInfo.resources.list(resourceInfo.path);
+ DirContext directory = cacheEntry.context;
+ NamingEnumeration enum = resources.list(cacheEntry.name);
boolean shade = false;
while (enum.hasMoreElements()) {
NameClassPair ncPair = (NameClassPair) enum.nextElement();
String resourceName = ncPair.getName();
- ResourceInfo childResourceInfo =
- new ResourceInfo(resourceName, directory);
+ CacheEntry childCacheEntry =
+ resources.lookupCache(cacheEntry.name + resourceName);
String trimmed = resourceName/*.substring(trim)*/;
if (trimmed.equalsIgnoreCase("WEB-INF") ||
@@ -1517,23 +1391,23 @@
sb.append(rewriteUrl(contextPath));
resourceName = rewriteUrl(name + resourceName);
sb.append(resourceName);
- if (childResourceInfo.collection)
+ if (childCacheEntry.context != null)
sb.append("/");
sb.append("\"><tt>");
sb.append(trimmed);
- if (childResourceInfo.collection)
+ if (childCacheEntry.context != null)
sb.append("/");
sb.append("</tt></a></td>\r\n");
sb.append("<td align=\"right\"><tt>");
- if (childResourceInfo.collection)
+ if (childCacheEntry.context != null)
sb.append(" ");
else
- sb.append(renderSize(childResourceInfo.length));
+
sb.append(renderSize(childCacheEntry.attributes.getContentLength()));
sb.append("</tt></td>\r\n");
sb.append("<td align=\"right\"><tt>");
- sb.append(childResourceInfo.httpDate);
+ sb.append(childCacheEntry.attributes.getLastModifiedHttp());
sb.append("</tt></td>\r\n");
sb.append("</tr>\r\n");
@@ -1549,7 +1423,7 @@
sb.append("<HR size=\"1\" noshade=\"noshade\">");
- String readme = getReadme(resourceInfo.directory);
+ String readme = getReadme(cacheEntry.context);
if (readme!=null) {
sb.append(readme);
sb.append("<HR size=\"1\" noshade=\"noshade\">");
@@ -1615,6 +1489,7 @@
* Return the xsl template inputstream (if possible)
*/
protected InputStream findXsltInputStream(DirContext directory) {
+
if (localXsltFile!=null) {
try {
Object obj = directory.lookup(localXsltFile);
@@ -1630,7 +1505,6 @@
}
}
-
/* Open and read in file in one fell swoop to reduce chance
* chance of leaving handle open.
*/
@@ -1662,6 +1536,7 @@
}
+
// -------------------------------------------------------- Private Methods
@@ -1677,10 +1552,10 @@
*/
private boolean checkIfMatch(HttpServletRequest request,
HttpServletResponse response,
- ResourceInfo resourceInfo)
+ ResourceAttributes resourceAttributes)
throws IOException {
- String eTag = getETag(resourceInfo);
+ String eTag = getETag(resourceAttributes);
String headerValue = request.getHeader("If-Match");
if (headerValue != null) {
if (headerValue.indexOf('*') == -1) {
@@ -1722,11 +1597,11 @@
*/
private boolean checkIfModifiedSince(HttpServletRequest request,
HttpServletResponse response,
- ResourceInfo resourceInfo)
+ ResourceAttributes resourceAttributes)
throws IOException {
try {
long headerValue = request.getDateHeader("If-Modified-Since");
- long lastModified = resourceInfo.date;
+ long lastModified = resourceAttributes.getLastModified();
if (headerValue != -1) {
// If an If-None-Match header has been specified, if modified since
@@ -1759,10 +1634,10 @@
*/
private boolean checkIfNoneMatch(HttpServletRequest request,
HttpServletResponse response,
- ResourceInfo resourceInfo)
+ ResourceAttributes resourceAttributes)
throws IOException {
- String eTag = getETag(resourceInfo);
+ String eTag = getETag(resourceAttributes);
String headerValue = request.getHeader("If-None-Match");
if (headerValue != null) {
@@ -1817,10 +1692,10 @@
*/
private boolean checkIfUnmodifiedSince(HttpServletRequest request,
HttpServletResponse response,
- ResourceInfo resourceInfo)
+ ResourceAttributes resourceAttributes)
throws IOException {
try {
- long lastModified = resourceInfo.date;
+ long lastModified = resourceAttributes.getLastModified();
long headerValue = request.getDateHeader("If-Unmodified-Since");
if (headerValue != -1) {
if ( lastModified > (headerValue + 1000)) {
@@ -1848,22 +1723,26 @@
*
* @exception IOException if an input/output error occurs
*/
- private void copy(ResourceInfo resourceInfo, ServletOutputStream ostream)
+ private void copy(CacheEntry cacheEntry, InputStream is,
+ ServletOutputStream ostream)
throws IOException {
IOException exception = null;
+ InputStream resourceInputStream = null;
// Optimization: If the binary content has already been loaded, send
// it directly
- if (resourceInfo.file != null) {
- byte buffer[] = resourceInfo.file.getContent();
+ if (cacheEntry.resource != null) {
+ byte buffer[] = cacheEntry.resource.getContent();
if (buffer != null) {
ostream.write(buffer, 0, buffer.length);
return;
}
+ resourceInputStream = cacheEntry.resource.streamContent();
+ } else {
+ resourceInputStream = is;
}
- InputStream resourceInputStream = resourceInfo.getStream();
InputStream istream = new BufferedInputStream
(resourceInputStream, input);
@@ -1894,12 +1773,17 @@
*
* @exception IOException if an input/output error occurs
*/
- private void copy(ResourceInfo resourceInfo, PrintWriter writer)
+ private void copy(CacheEntry cacheEntry, InputStream is, PrintWriter writer)
throws IOException {
IOException exception = null;
- InputStream resourceInputStream = resourceInfo.getStream();
+ InputStream resourceInputStream = null;
+ if (cacheEntry.resource != null) {
+ resourceInputStream = cacheEntry.resource.streamContent();
+ } else {
+ resourceInputStream = is;
+ }
// FIXME : i18n ?
Reader reader = new InputStreamReader(resourceInputStream);
@@ -1930,13 +1814,13 @@
* @param range Range the client wanted to retrieve
* @exception IOException if an input/output error occurs
*/
- private void copy(ResourceInfo resourceInfo, ServletOutputStream ostream,
+ private void copy(CacheEntry cacheEntry, ServletOutputStream ostream,
Range range)
throws IOException {
IOException exception = null;
- InputStream resourceInputStream = resourceInfo.getStream();
+ InputStream resourceInputStream = cacheEntry.resource.streamContent();
InputStream istream =
new BufferedInputStream(resourceInputStream, input);
exception = copyRange(istream, ostream, range.start, range.end);
@@ -1965,13 +1849,13 @@
* @param range Range the client wanted to retrieve
* @exception IOException if an input/output error occurs
*/
- private void copy(ResourceInfo resourceInfo, PrintWriter writer,
+ private void copy(CacheEntry cacheEntry, PrintWriter writer,
Range range)
throws IOException {
IOException exception = null;
- InputStream resourceInputStream = resourceInfo.getStream();
+ InputStream resourceInputStream = cacheEntry.resource.streamContent();
Reader reader = new InputStreamReader(resourceInputStream);
exception = copyRange(reader, writer, range.start, range.end);
@@ -2000,7 +1884,7 @@
* @param contentType Content type of the resource
* @exception IOException if an input/output error occurs
*/
- private void copy(ResourceInfo resourceInfo, ServletOutputStream ostream,
+ private void copy(CacheEntry cacheEntry, ServletOutputStream ostream,
Enumeration ranges, String contentType)
throws IOException {
@@ -2008,8 +1892,8 @@
while ( (exception == null) && (ranges.hasMoreElements()) ) {
- InputStream resourceInputStream = resourceInfo.getStream();
- InputStream istream = // FIXME: internationalization???????
+ InputStream resourceInputStream = cacheEntry.resource.streamContent();
+ InputStream istream =
new BufferedInputStream(resourceInputStream, input);
Range currentRange = (Range) ranges.nextElement();
@@ -2057,7 +1941,7 @@
* @param contentType Content type of the resource
* @exception IOException if an input/output error occurs
*/
- private void copy(ResourceInfo resourceInfo, PrintWriter writer,
+ private void copy(CacheEntry cacheEntry, PrintWriter writer,
Enumeration ranges, String contentType)
throws IOException {
@@ -2065,7 +1949,7 @@
while ( (exception == null) && (ranges.hasMoreElements()) ) {
- InputStream resourceInputStream = resourceInfo.getStream();
+ InputStream resourceInputStream = cacheEntry.resource.streamContent();
Reader reader = new InputStreamReader(resourceInputStream);
Range currentRange = (Range) ranges.nextElement();
@@ -2292,152 +2176,6 @@
end = 0;
length = 0;
}
-
- }
-
-
- // ---------------------------------------------- ResourceInfo Inner Class
-
-
- protected class ResourceInfo {
-
-
- /**
- * Constructor.
- *
- * @param path Path name of the file
- */
- public ResourceInfo(String path, DirContext resources) {
- set(path, resources);
- }
-
-
- public Object object;
- public DirContext directory;
- public Resource file;
- public Attributes attributes;
- public String path;
- public long creationDate;
- public String httpDate;
- public long date;
- public long length;
- public boolean collection;
- public String weakETag;
- public String strongETag;
- public boolean exists;
- public DirContext resources;
- protected InputStream is;
-
-
- public void recycle() {
- object = null;
- directory = null;
- file = null;
- attributes = null;
- path = null;
- creationDate = 0;
- httpDate = null;
- date = 0;
- length = -1;
- collection = true;
- weakETag = null;
- strongETag = null;
- exists = false;
- resources = null;
- is = null;
- }
-
-
- public void set(String path, DirContext resources) {
-
- recycle();
-
- this.path = path;
- this.resources = resources;
- exists = true;
- try {
- object = resources.lookup(path);
- if (object instanceof Resource) {
- file = (Resource) object;
- collection = false;
- } else if (object instanceof DirContext) {
- directory = (DirContext) object;
- collection = true;
- } else {
- // Don't know how to serve another object type
- exists = false;
- }
- } catch (NamingException e) {
- exists = false;
- }
- if (exists) {
- try {
- attributes = resources.getAttributes(path);
- if (attributes instanceof ResourceAttributes) {
- ResourceAttributes tempAttrs =
- (ResourceAttributes) attributes;
- Date tempDate = tempAttrs.getCreationDate();
- if (tempDate != null)
- creationDate = tempDate.getTime();
- tempDate = tempAttrs.getLastModifiedDate();
- if (tempDate != null) {
- date = tempDate.getTime();
- httpDate =
- FastHttpDateFormat.formatDate(date, null);
- } else {
- httpDate = FastHttpDateFormat.getCurrentDate();
- }
- weakETag = tempAttrs.getETag();
- strongETag = tempAttrs.getETag(true);
- length = tempAttrs.getContentLength();
- }
- } catch (NamingException e) {
- // Shouldn't happen, the implementation of the DirContext
- // is probably broken
- exists = false;
- }
- }
-
- }
-
-
- /**
- * Test if the associated resource exists.
- */
- public boolean exists() {
- return exists;
- }
-
-
- /**
- * String representation.
- */
- public String toString() {
- return path;
- }
-
-
- /**
- * Set IS.
- */
- public void setStream(InputStream is) {
- this.is = is;
- }
-
-
- /**
- * Get IS from resource.
- */
- public InputStream getStream()
- throws IOException {
- if (is != null)
- return is;
- if (file != null)
- return (file.streamContent());
- else
- return null;
- }
-
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]