String result = null; if (attributes != null) { Attribute attribute = attributes.get(ETAG); if (attribute != null) { try { long contentLength = getContentLength(); long lastModified = getLastModified(); if ((contentLength >= 0) || (lastModified >= 0)) { weakETag = "W/\"" + contentLength + "-"
/** * Revalidate entry. */ protected boolean revalidate(CacheEntry entry) { // Get the attributes at the given path, and check the last // modification date if (!entry.exists) return false; if (entry.attributes == null) return false; long lastModified = entry.attributes.getLastModified(); long contentLength = entry.attributes.getContentLength(); if (lastModified <= 0) return false; try { Attributes tempAttributes = dirContext.getAttributes(entry.name); ResourceAttributes attributes = null; if (!(tempAttributes instanceof ResourceAttributes)) { attributes = new ResourceAttributes(tempAttributes); } else { attributes = (ResourceAttributes) tempAttributes; } long lastModified2 = attributes.getLastModified(); long contentLength2 = attributes.getContentLength(); return (lastModified == lastModified2) && (contentLength == contentLength2); } catch (NamingException e) { return false; } }
ResourceAttributes resourceAttributes) { try { long headerValue = request.getDateHeader("If-Modified-Since"); long lastModified = resourceAttributes.getLastModified(); if (headerValue != -1) { if ((request.getHeader("If-None-Match") == null) && (lastModified < headerValue + 1000)) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); response.setHeader("ETag", resourceAttributes.getETag());
@Override public Long[] getResourceAttributes(String name, Context context) { Long[] result = new Long[2]; try { ResourceAttributes resource = (ResourceAttributes) context.getResources().getAttributes(name); result[0] = resource.getContentLength(); result[1] = resource.getLastModified(); } catch (NamingException ex) { logger.trace("", ex); } return result; }
/** * Get the ETag associated with a file. * * @param resourceAttributes The resource information */ 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/\"" + resourceAttributes.getContentLength() + "-" + resourceAttributes.getLastModified() + "\""; } }
/** * Retrieves the number of attributes in the attribute set. */ public int size() { if (attributes == null) { int size = 0; if (getCreationDate() != null) size += 2; if (getLastModifiedDate() != null) size += 2; if (getName() != null) size++; if (getResourceType() != null) size += 2; if (getContentLength() >= 0) size += 2; if (getETag() != null) size += 2; return size; } else { return attributes.size(); } }
response.sendError(HttpServletResponse.SC_NOT_FOUND, requestUri); return; String requestUri = (String) request.getAttribute( RequestDispatcher.INCLUDE_REQUEST_URI); if (requestUri == null) { requestUri = request.getRequestURI(); response.sendError(HttpServletResponse.SC_NOT_FOUND, requestUri); return; String contentType = cacheEntry.attributes.getMimeType(); if (contentType == null) { contentType = getServletContext().getMimeType(cacheEntry.name); cacheEntry.attributes.setMimeType(contentType); response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI()); return; response.setHeader("ETag", cacheEntry.attributes.getETag()); response.setHeader("Last-Modified", cacheEntry.attributes.getLastModifiedHttp()); contentLength = cacheEntry.attributes.getContentLength();
/** * Check if sendfile can be used. */ protected boolean checkSendfile(HttpServletRequest request, HttpServletResponse response, CacheEntry entry, long length, Range range) { if ((sendfileSize > 0) && (entry.resource != null) && ((length > sendfileSize) || (entry.resource.getContent() == null)) && (entry.attributes.getCanonicalPath() != null) && (Boolean.TRUE == request.getAttribute("org.apache.tomcat.sendfile.support")) && (request.getClass().getName().equals("org.apache.catalina.connector.RequestFacade")) && (response.getClass().getName().equals("org.apache.catalina.connector.ResponseFacade"))) { request.setAttribute("org.apache.tomcat.sendfile.filename", entry.attributes.getCanonicalPath()); if (range == null) { request.setAttribute("org.apache.tomcat.sendfile.start", Long.valueOf(0L)); request.setAttribute("org.apache.tomcat.sendfile.end", Long.valueOf(length)); } else { request.setAttribute("org.apache.tomcat.sendfile.start", Long.valueOf(range.start)); request.setAttribute("org.apache.tomcat.sendfile.end", Long.valueOf(range.end + 1)); } return true; } return false; }
getISOCreationDate(cacheEntry.attributes.getCreation())); generatedXML.writeElement(null, "displayname", XMLWriter.OPENING); generatedXML.writeData(resourceName); generatedXML.writeProperty (null, "getlastmodified", FastHttpDateFormat.formatDate (cacheEntry.attributes.getLastModified(), null)); generatedXML.writeProperty (null, "getcontentlength", String.valueOf(cacheEntry.attributes.getContentLength())); String contentType = getServletContext().getMimeType (cacheEntry.name); cacheEntry.attributes.getETag()); generatedXML.writeElement(null, "resourcetype", XMLWriter.NO_CONTENT); generatedXML.writeProperty (null, "creationdate", getISOCreationDate(cacheEntry.attributes.getCreation())); } else if (property.equals("displayname")) { generatedXML.writeElement generatedXML.writeProperty (null, "getcontentlength", (String.valueOf(cacheEntry.attributes.getContentLength()))); } else { generatedXML.writeProperty
/** * Check if the if-unmodified-since condition is satisfied. * * @param request The servlet request we are processing * @param response The servlet response we are creating * @param resourceAttributes File object * @return boolean true if the resource meets the specified condition, * and false if the condition is not satisfied, in which case request * processing is stopped */ protected boolean checkIfUnmodifiedSince(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes) throws IOException { try { long lastModified = resourceAttributes.getLastModified(); long headerValue = request.getDateHeader("If-Unmodified-Since"); if (headerValue != -1) { if ( lastModified >= (headerValue + 1000)) { // The entity has not been modified since the date // specified by the client. This is not an error case. response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED); return false; } } } catch(IllegalArgumentException illegalArgument) { return true; } return true; }
/** * Retrieves selected attributes associated with a named object. * * @return the requested attributes; never null * @param name the name of the object from which to retrieve attributes * @param attrIds the identifiers of the attributes to retrieve. null * indicates that all attributes should be retrieved; an empty array * indicates that none should be retrieved * @exception NamingException if a naming exception is encountered */ public Attributes getAttributes(String name, String[] attrIds) throws NamingException { Attributes attributes = dirContext.getAttributes(parseName(name), attrIds); if (!(attributes instanceof ResourceAttributes)) { attributes = new ResourceAttributes(attributes); } return attributes; }
Object lookupResult = resources.lookup(fullPath); if (lookupResult instanceof Resource) { resource = (Resource) lookupResult; (ResourceAttributes) resources.getAttributes(fullPath); contentLength = (int) attributes.getContentLength(); String canonicalPath = attributes.getCanonicalPath(); if (canonicalPath != null) { entry.lastModified = attributes.getLastModified();
ResourceAttributes attrs = new ResourceAttributes(); attrs.setCreationDate(new Date(zipEntry.getTime())); attrs.setName(entry.getName()); if (!zipEntry.isDirectory()) attrs.setResourceType(""); else attrs.setCollection(true); attrs.setContentLength(zipEntry.getSize()); attrs.setLastModified(zipEntry.getTime());
if (childCacheEntry.resource != null) { sb.append(" size='") .append(renderSize(childCacheEntry.attributes.getContentLength())) .append("'"); .append(childCacheEntry.attributes.getLastModifiedHttp()) .append("'");
public int compare(NameClassPair p1, NameClassPair p2) { CacheEntry ce1 = resources.lookupCache( dirName + p1.getName()); long size1 = ce1.attributes.getContentLength(); CacheEntry ce2 = resources.lookupCache( dirName + p2.getName()); long size2 = ce2.attributes.getContentLength(); if (size1 < size2) { return -1; } else if (size1 > size2) { return 1; } else { return 0; } } }
/** * Check if sendfile can be used. */ protected boolean checkSendfile(HttpServletRequest request, HttpServletResponse response, String path, CacheEntry entry, long length, Range range) { if ((sendfileSize > 0) && (entry.resource != null) && ((length > sendfileSize) || (entry.resource.getContent() == null)) && (entry.attributes.getCanonicalPath() != null) && (request.getClass().getName().equals("org.apache.catalina.connector.RequestFacade")) && (response.getClass().getName().equals("org.apache.catalina.connector.ResponseFacade")) && ((RequestFacade) request).hasSendfile()) { ResponseFacade responseFacade = (ResponseFacade) response; if (range == null) { responseFacade.sendFile(path, entry.attributes.getCanonicalPath(), 0, length); } else { responseFacade.sendFile(path, entry.attributes.getCanonicalPath(), range.start, range.end + 1); } return true; } else { return false; } }
response.sendError(HttpServletResponse.SC_NOT_FOUND, requestUri); return; String requestUri = (String) request.getAttribute( RequestDispatcher.INCLUDE_REQUEST_URI); if (requestUri == null) { requestUri = request.getRequestURI(); response.sendError(HttpServletResponse.SC_NOT_FOUND, requestUri); return; response.getStatus() >= HttpServletResponse.SC_BAD_REQUEST; String contentType = cacheEntry.attributes.getMimeType(); if (contentType == null) { contentType = getServletContext().getMimeType(cacheEntry.name); cacheEntry.attributes.setMimeType(contentType); response.setHeader("ETag", cacheEntry.attributes.getETag()); response.setHeader("Last-Modified", cacheEntry.attributes.getLastModifiedHttp()); contentLength = cacheEntry.attributes.getContentLength();
/** * Check if sendfile can be used. */ protected boolean checkSendfile(HttpServletRequest request, HttpServletResponse response, CacheEntry entry, long length, Range range) { if ((sendfileSize > 0) && (entry.resource != null) && ((length > sendfileSize) || (entry.resource.getContent() == null)) && (entry.attributes.getCanonicalPath() != null) && (Boolean.TRUE == request.getAttribute(Globals.SENDFILE_SUPPORTED_ATTR)) && (request.getClass().getName().equals("org.apache.catalina.connector.RequestFacade")) && (response.getClass().getName().equals("org.apache.catalina.connector.ResponseFacade"))) { request.setAttribute(Globals.SENDFILE_FILENAME_ATTR, entry.attributes.getCanonicalPath()); if (range == null) { request.setAttribute(Globals.SENDFILE_FILE_START_ATTR, Long.valueOf(0L)); request.setAttribute(Globals.SENDFILE_FILE_END_ATTR, Long.valueOf(length)); } else { request.setAttribute(Globals.SENDFILE_FILE_START_ATTR, Long.valueOf(range.start)); request.setAttribute(Globals.SENDFILE_FILE_END_ATTR, Long.valueOf(range.end + 1)); } return true; } return false; }
getISOCreationDate(cacheEntry.attributes.getCreation())); generatedXML.writeElement(null, "displayname", XMLWriter.OPENING); generatedXML.writeData(resourceName); generatedXML.writeProperty (null, "getlastmodified", FastHttpDateFormat.formatDate (cacheEntry.attributes.getLastModified(), null)); generatedXML.writeProperty (null, "getcontentlength", String.valueOf(cacheEntry.attributes.getContentLength())); String contentType = getServletContext().getMimeType (cacheEntry.name); cacheEntry.attributes.getETag()); generatedXML.writeElement(null, "resourcetype", XMLWriter.NO_CONTENT); generatedXML.writeProperty (null, "creationdate", getISOCreationDate(cacheEntry.attributes.getCreation())); } else if (property.equals("displayname")) { generatedXML.writeElement generatedXML.writeProperty (null, "getcontentlength", (String.valueOf(cacheEntry.attributes.getContentLength()))); } else { generatedXML.writeProperty
/** * Check if the if-unmodified-since condition is satisfied. * * @param request The servlet request we are processing * @param response The servlet response we are creating * @param resourceInfo File object * @return boolean true if the resource meets the specified condition, * and false if the condition is not satisfied, in which case request * processing is stopped */ protected boolean checkIfUnmodifiedSince(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes) throws IOException { try { long lastModified = resourceAttributes.getLastModified(); long headerValue = request.getDateHeader("If-Unmodified-Since"); if (headerValue != -1) { if ( lastModified >= (headerValue + 1000)) { // The entity has not been modified since the date // specified by the client. This is not an error case. response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED); return false; } } } catch(IllegalArgumentException illegalArgument) { return true; } return true; }