Java Code Examples for io.undertow.server.handlers.resource.Resource#getContentLength()
The following examples show how to use
io.undertow.server.handlers.resource.Resource#getContentLength() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UndertowResolver.java From quarkus-http with Apache License 2.0 | 6 votes |
public boolean resolveResource(int type, String name) { Resource resource; try { resource = servletRequestContext.getDeployment().getDeploymentInfo().getResourceManager().getResource(name); } catch (IOException e) { throw new RuntimeException(e); } switch (type) { case 0: return (resource == null); case 1: return (resource != null); case 2: return (resource != null && resource.getContentLength() > 0); default: return false; } }
Example 2
Source File: UndertowResolver.java From lams with GNU General Public License v2.0 | 6 votes |
public boolean resolveResource(int type, String name) { Resource resource; try { resource = servletRequestContext.getDeployment().getDeploymentInfo().getResourceManager().getResource(name); } catch (IOException e) { throw new RuntimeException(e); } switch (type) { case 0: return (resource == null); case 1: return (resource != null); case 2: return (resource != null && resource.getContentLength() > 0); default: return false; } }
Example 3
Source File: FilePredicate.java From quarkus-http with Apache License 2.0 | 5 votes |
@Override public boolean resolve(final HttpServerExchange value) { String location = this.location.readAttribute(value); ServletRequestContext src = value.getAttachment(ServletRequestContext.ATTACHMENT_KEY); if(src == null) { return false; } ResourceManager manager = src.getDeployment().getDeploymentInfo().getResourceManager(); if(manager == null) { return false; } try { Resource resource = manager.getResource(location); if(resource == null) { return false; } if(resource.isDirectory()) { return false; } if(requireContent){ return resource.getContentLength() != null && resource.getContentLength() > 0; } else { return true; } } catch (IOException e) { throw new RuntimeException(e); } }
Example 4
Source File: FilePredicate.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public boolean resolve(final HttpServerExchange value) { String location = this.location.readAttribute(value); ServletRequestContext src = value.getAttachment(ServletRequestContext.ATTACHMENT_KEY); if(src == null) { return false; } ResourceManager manager = src.getDeployment().getDeploymentInfo().getResourceManager(); if(manager == null) { return false; } try { Resource resource = manager.getResource(location); if(resource == null) { return false; } if(resource.isDirectory()) { return false; } if(requireContent){ return resource.getContentLength() != null && resource.getContentLength() > 0; } else { return true; } } catch (IOException e) { throw new RuntimeException(e); } }
Example 5
Source File: DefaultServlet.java From quarkus-http with Apache License 2.0 | 4 votes |
private void serveFileBlocking(final HttpServletRequest req, final HttpServletResponse resp, final Resource resource, HttpServerExchange exchange) throws IOException { final ETag etag = resource.getETag(); final Date lastModified = resource.getLastModified(); if (req.getDispatcherType() != DispatcherType.INCLUDE) { if (!ETagUtils.handleIfMatch(req.getHeader(HttpHeaderNames.IF_MATCH), etag, false) || !DateUtils.handleIfUnmodifiedSince(req.getHeader(HttpHeaderNames.IF_UNMODIFIED_SINCE), lastModified)) { resp.setStatus(StatusCodes.PRECONDITION_FAILED); return; } if (!ETagUtils.handleIfNoneMatch(req.getHeader(HttpHeaderNames.IF_NONE_MATCH), etag, true) || !DateUtils.handleIfModifiedSince(req.getHeader(HttpHeaderNames.IF_MODIFIED_SINCE), lastModified)) { if (req.getMethod().equals(HttpMethodNames.GET) || req.getMethod().equals(HttpMethodNames.HEAD)) { resp.setStatus(StatusCodes.NOT_MODIFIED); } else { resp.setStatus(StatusCodes.PRECONDITION_FAILED); } return; } } //we are going to proceed. Set the appropriate headers if (resp.getContentType() == null) { if (!resource.isDirectory()) { final String contentType = deployment.getServletContext().getMimeType(resource.getName()); if (contentType != null) { resp.setContentType(contentType); } else { resp.setContentType("application/octet-stream"); } } } if (lastModified != null) { resp.setHeader(HttpHeaderNames.LAST_MODIFIED, resource.getLastModifiedString()); } if (etag != null) { resp.setHeader(HttpHeaderNames.ETAG, etag.toString()); } ByteRange.RangeResponseResult rangeResponse = null; long start = -1, end = -1; try { //only set the content length if we are using a stream //if we are using a writer who knows what the length will end up being //todo: if someone installs a filter this can cause problems //not sure how best to deal with this //we also can't deal with range requests if a writer is in use Long contentLength = resource.getContentLength(); if (contentLength != null) { resp.getOutputStream(); if (contentLength > Integer.MAX_VALUE) { resp.setContentLengthLong(contentLength); } else { resp.setContentLength(contentLength.intValue()); } if (resource instanceof RangeAwareResource && ((RangeAwareResource) resource).isRangeSupported() && resource.getContentLength() != null) { resp.setHeader(HttpHeaderNames.ACCEPT_RANGES, "bytes"); //TODO: figure out what to do with the content encoded resource manager final ByteRange range = ByteRange.parse(req.getHeader(HttpHeaderNames.RANGE)); if (range != null) { rangeResponse = range.getResponseResult(resource.getContentLength(), req.getHeader(HttpHeaderNames.IF_RANGE), resource.getLastModified(), resource.getETag() == null ? null : resource.getETag().getTag()); if (rangeResponse != null) { start = rangeResponse.getStart(); end = rangeResponse.getEnd(); resp.setStatus(rangeResponse.getStatusCode()); resp.setHeader(HttpHeaderNames.CONTENT_RANGE, rangeResponse.getContentRange()); long length = rangeResponse.getContentLength(); if (length > Integer.MAX_VALUE) { resp.setContentLengthLong(length); } else { resp.setContentLength((int) length); } if (rangeResponse.getStatusCode() == StatusCodes.REQUEST_RANGE_NOT_SATISFIABLE) { return; } } } } } } catch (IllegalStateException e) { } final boolean include = req.getDispatcherType() == DispatcherType.INCLUDE; if (!req.getMethod().equals(HttpMethodNames.HEAD)) { if (rangeResponse == null) { resource.serveBlocking(exchange.getOutputStream(), exchange); } else { ((RangeAwareResource) resource).serveRangeBlocking(exchange.getOutputStream(), exchange, start, end); } } }
Example 6
Source File: DefaultServlet.java From lams with GNU General Public License v2.0 | 4 votes |
private void serveFileBlocking(final HttpServletRequest req, final HttpServletResponse resp, final Resource resource, HttpServerExchange exchange) throws IOException { final ETag etag = resource.getETag(); final Date lastModified = resource.getLastModified(); if(req.getDispatcherType() != DispatcherType.INCLUDE) { if (!ETagUtils.handleIfMatch(req.getHeader(Headers.IF_MATCH_STRING), etag, false) || !DateUtils.handleIfUnmodifiedSince(req.getHeader(Headers.IF_UNMODIFIED_SINCE_STRING), lastModified)) { resp.setStatus(StatusCodes.PRECONDITION_FAILED); return; } if (!ETagUtils.handleIfNoneMatch(req.getHeader(Headers.IF_NONE_MATCH_STRING), etag, true) || !DateUtils.handleIfModifiedSince(req.getHeader(Headers.IF_MODIFIED_SINCE_STRING), lastModified)) { if(req.getMethod().equals(Methods.GET_STRING) || req.getMethod().equals(Methods.HEAD_STRING)) { resp.setStatus(StatusCodes.NOT_MODIFIED); } else { resp.setStatus(StatusCodes.PRECONDITION_FAILED); } return; } } //we are going to proceed. Set the appropriate headers if(resp.getContentType() == null) { if(!resource.isDirectory()) { final String contentType = deployment.getServletContext().getMimeType(resource.getName()); if (contentType != null) { resp.setContentType(contentType); } else { resp.setContentType("application/octet-stream"); } } } if (lastModified != null) { resp.setHeader(Headers.LAST_MODIFIED_STRING, resource.getLastModifiedString()); } if (etag != null) { resp.setHeader(Headers.ETAG_STRING, etag.toString()); } ByteRange.RangeResponseResult rangeResponse = null; long start = -1, end = -1; try { //only set the content length if we are using a stream //if we are using a writer who knows what the length will end up being //todo: if someone installs a filter this can cause problems //not sure how best to deal with this //we also can't deal with range requests if a writer is in use Long contentLength = resource.getContentLength(); if (contentLength != null) { resp.getOutputStream(); if(contentLength > Integer.MAX_VALUE) { resp.setContentLengthLong(contentLength); } else { resp.setContentLength(contentLength.intValue()); } if(resource instanceof RangeAwareResource && ((RangeAwareResource)resource).isRangeSupported() && resource.getContentLength() != null) { resp.setHeader(Headers.ACCEPT_RANGES_STRING, "bytes"); //TODO: figure out what to do with the content encoded resource manager final ByteRange range = ByteRange.parse(req.getHeader(Headers.RANGE_STRING)); if(range != null) { rangeResponse = range.getResponseResult(resource.getContentLength(), req.getHeader(Headers.IF_RANGE_STRING), resource.getLastModified(), resource.getETag() == null ? null : resource.getETag().getTag()); if(rangeResponse != null){ start = rangeResponse.getStart(); end = rangeResponse.getEnd(); resp.setStatus(rangeResponse.getStatusCode()); resp.setHeader(Headers.CONTENT_RANGE_STRING, rangeResponse.getContentRange()); long length = rangeResponse.getContentLength(); if(length > Integer.MAX_VALUE) { resp.setContentLengthLong(length); } else { resp.setContentLength((int) length); } if(rangeResponse.getStatusCode() == StatusCodes.REQUEST_RANGE_NOT_SATISFIABLE) { return; } } } } } } catch (IllegalStateException e) { } final boolean include = req.getDispatcherType() == DispatcherType.INCLUDE; if (!req.getMethod().equals(Methods.HEAD_STRING)) { if(rangeResponse == null) { resource.serve(exchange.getResponseSender(), exchange, completionCallback(include)); } else { ((RangeAwareResource)resource).serveRange(exchange.getResponseSender(), exchange, start, end, completionCallback(include)); } } }
Example 7
Source File: ContentEncodedResourceManager.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Gets a pre-encoded resource. * <p> * TODO: blocking / non-blocking semantics * * @param resource * @param exchange * @return * @throws IOException */ public ContentEncodedResource getResource(final Resource resource, final HttpServerExchange exchange) throws IOException { final String path = resource.getPath(); Path file = resource.getFilePath(); if (file == null) { return null; } if (minResourceSize > 0 && resource.getContentLength() < minResourceSize || maxResourceSize > 0 && resource.getContentLength() > maxResourceSize || !(encodingAllowed == null || encodingAllowed.resolve(exchange))) { return null; } AllowedContentEncodings encodings = contentEncodingRepository.getContentEncodings(exchange); if (encodings == null || encodings.isNoEncodingsAllowed()) { return null; } EncodingMapping encoding = encodings.getEncoding(); if (encoding == null || encoding.getName().equals(ContentEncodingRepository.IDENTITY)) { return null; } String newPath = path + ".undertow.encoding." + encoding.getName(); Resource preCompressed = encoded.getResource(newPath); if (preCompressed != null) { return new ContentEncodedResource(preCompressed, encoding.getName()); } final LockKey key = new LockKey(path, encoding.getName()); if (fileLocks.putIfAbsent(key, this) != null) { //another thread is already compressing //we don't do anything fancy here, just return and serve non-compressed content return null; } FileChannel targetFileChannel = null; FileChannel sourceFileChannel = null; try { //double check, the compressing thread could have finished just before we acquired the lock preCompressed = encoded.getResource(newPath); if (preCompressed != null) { return new ContentEncodedResource(preCompressed, encoding.getName()); } final Path finalTarget = encodedResourcesRoot.resolve(newPath); final Path tempTarget = encodedResourcesRoot.resolve(newPath); //horrible hack to work around XNIO issue OutputStream tmp = Files.newOutputStream(tempTarget); try { tmp.close(); } finally { IoUtils.safeClose(tmp); } targetFileChannel = FileChannel.open(tempTarget, StandardOpenOption.READ, StandardOpenOption.WRITE); sourceFileChannel = FileChannel.open(file, StandardOpenOption.READ); StreamSinkConduit conduit = encoding.getEncoding().getResponseWrapper().wrap(new ImmediateConduitFactory<StreamSinkConduit>(new FileConduitTarget(targetFileChannel, exchange)), exchange); final ConduitStreamSinkChannel targetChannel = new ConduitStreamSinkChannel(null, conduit); long transferred = sourceFileChannel.transferTo(0, resource.getContentLength(), targetChannel); targetChannel.shutdownWrites(); org.xnio.channels.Channels.flushBlocking(targetChannel); if (transferred != resource.getContentLength()) { UndertowLogger.REQUEST_LOGGER.failedToWritePreCachedFile(); } Files.move(tempTarget, finalTarget); encoded.invalidate(newPath); final Resource encodedResource = encoded.getResource(newPath); return new ContentEncodedResource(encodedResource, encoding.getName()); } finally { IoUtils.safeClose(targetFileChannel); IoUtils.safeClose(sourceFileChannel); fileLocks.remove(key); } }