io.undertow.util.ETag Java Examples

The following examples show how to use io.undertow.util.ETag. 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: PathResourceManagerTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Test
public void testETagFunction() throws Exception {
    final String fileName = "page.html";
    final Path rootPath = Paths.get(getClass().getResource(fileName).toURI()).getParent();
    final ResourceManager resourceManager = PathResourceManager.builder()
            .setBase(rootPath)
            .setETagFunction(new PathResourceManager.ETagFunction() {
                @Override
                public ETag generate(Path path) {
                    return new ETag(true, path.getFileName().toString());
                }
            })
            .build();
    ETag expected = new ETag(true, fileName);
    ETag actual = resourceManager.getResource("page.html").getETag();
    Assert.assertEquals(expected, actual);
}
 
Example #2
Source File: TrackingJavaScriptResourceTest.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
private static void validateEtag(final ETag eTag) {
    assertThat(eTag, is(notNullValue()));
    assertThat(eTag.isWeak(), is(false));
    assertThat(eTag.getTag(), not(isEmptyOrNullString()));
    assertThat(eTag.toString(), startsWith("\""));
    assertThat(eTag.toString(), endsWith("\""));
    assertThat(eTag.toString().length(), is(greaterThan(2)));
}
 
Example #3
Source File: DirectoryUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Serve static resource for the directory listing
 *
 * @param exchange The exchange
 * @return true if resources were served
 */
public static boolean sendRequestedBlobs(HttpServerExchange exchange) {
    ByteBuffer buffer = null;
    String type = null;
    String etag = null;
    String quotedEtag = null;
    if ("css".equals(exchange.getQueryString())) {
        buffer = Blobs.FILE_CSS_BUFFER.duplicate();
        type = "text/css";
        etag = Blobs.FILE_CSS_ETAG;
        quotedEtag = Blobs.FILE_CSS_ETAG_QUOTED;
    } else if ("js".equals(exchange.getQueryString())) {
        buffer = Blobs.FILE_JS_BUFFER.duplicate();
        type = "application/javascript";
        etag = Blobs.FILE_JS_ETAG;
        quotedEtag = Blobs.FILE_JS_ETAG_QUOTED;
    }

    if (buffer != null) {

        if(!ETagUtils.handleIfNoneMatch(exchange, new ETag(false, etag), false)) {
            exchange.setStatusCode(StatusCodes.NOT_MODIFIED);
            return true;
        }

        exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, String.valueOf(buffer.limit()));
        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, type);
        exchange.getResponseHeaders().put(Headers.ETAG, quotedEtag);
        if (Methods.HEAD.equals(exchange.getRequestMethod())) {
            exchange.endExchange();
            return true;
        }
        exchange.getResponseSender().send(buffer);

        return true;
    }

    return false;
}
 
Example #4
Source File: GzippableHttpBody.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
public GzippableHttpBody(final ByteBuffer data, final ETag eTag) {
    super(data, eTag);
    logger.debug("Compressing resource.");
    final Optional<ByteBuffer> gzippedData = Gzip.compress(data);
    if (gzippedData.isPresent()) {
        logger.info("Compressed resource: {} -> {}",
                    data.remaining(), gzippedData.get().remaining());
        gzippedBody = Optional.of(new HttpBody(gzippedData.get(),
                                               new ETag(eTag.isWeak(), "gz+" + eTag.getTag())));
    } else {
        logger.info("Resource not compressable.");
        gzippedBody = Optional.empty();
    }
}
 
Example #5
Source File: DirectoryUtils.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
/**
 * Serve static resource for the directory listing
 *
 * @param exchange The exchange
 * @return true if resources were served
 */
public static boolean sendRequestedBlobs(HttpServerExchange exchange) {
    ByteBuf buffer = null;
    String type = null;
    String etag = null;
    String quotedEtag = null;
    if ("css".equals(exchange.getQueryString())) {
        buffer = Blobs.FILE_CSS_BUFFER.duplicate();
        type = "text/css";
        etag = Blobs.FILE_CSS_ETAG;
        quotedEtag = Blobs.FILE_CSS_ETAG_QUOTED;
    } else if ("js".equals(exchange.getQueryString())) {
        buffer = Blobs.FILE_JS_BUFFER.duplicate();
        type = "application/javascript";
        etag = Blobs.FILE_JS_ETAG;
        quotedEtag = Blobs.FILE_JS_ETAG_QUOTED;
    }

    if (buffer != null) {

        if (!ETagUtils.handleIfNoneMatch(exchange, new ETag(false, etag), false)) {
            exchange.setStatusCode(StatusCodes.NOT_MODIFIED);
            return true;
        }

        exchange.setResponseHeader(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(buffer.readableBytes()));
        exchange.setResponseHeader(HttpHeaderNames.CONTENT_TYPE, type);
        exchange.setResponseHeader(HttpHeaderNames.ETAG, quotedEtag);
        if (HttpMethodNames.HEAD.equals(exchange.getRequestMethod())) {
            exchange.endExchange();
            return true;
        }
        exchange.writeAsync(buffer.duplicate(), true, IoCallback.END_EXCHANGE, null);

        return true;
    }

    return false;
}
 
Example #6
Source File: TrackingJavaScriptResourceTest.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
@Test
public void testGzippedETagIsValid() {
    final Optional<HttpBody> gzippedBody = trackingJavaScript.getEntityBody().getGzippedBody();
    assertThat(gzippedBody.isPresent(), is(true));
    final ETag eTag = gzippedBody.get().getETag();
    validateEtag(eTag);
    assertThat(eTag, is(not(equalTo(trackingJavaScript.getEntityBody().getETag()))));
}
 
Example #7
Source File: HttpBody.java    From divolte-collector with Apache License 2.0 4 votes vote down vote up
public ETag getETag() {
    return eTag;
}
 
Example #8
Source File: HttpBody.java    From divolte-collector with Apache License 2.0 4 votes vote down vote up
public HttpBody(final ByteBuffer body, final ETag eTag) {
    this.body = Objects.requireNonNull(body.asReadOnlyBuffer());
    this.eTag = Objects.requireNonNull(eTag);
}
 
Example #9
Source File: JavaScriptResource.java    From divolte-collector with Apache License 2.0 4 votes vote down vote up
private static ETag generateETag(final byte[] entityBytes) {
    final MessageDigest digester = createDigester();
    final byte[] digest = digester.digest(entityBytes);
    return new ETag(false, Base64.getEncoder().encodeToString(digest));
}
 
Example #10
Source File: TrackingJavaScriptResourceTest.java    From divolte-collector with Apache License 2.0 4 votes vote down vote up
@Test
public void testETagIsValid() throws IOException {
    final ETag eTag = trackingJavaScript.getEntityBody().getETag();
    validateEtag(eTag);
}
 
Example #11
Source File: JavaScriptHandler.java    From divolte-collector with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("Requested received for {} from {}",
                     resource.getResourceName(), exchange.getSourceAddress().getHostString());
    }
    // Start with headers that we always set the same way.
    final HeaderMap responseHeaders = exchange.getResponseHeaders();
    responseHeaders.put(Headers.CACHE_CONTROL, CACHE_CONTROL_HEADER_VALUE);

    // Figure out if we possibly need to deal with a compressed response,
    // based on client capability.
    final GzippableHttpBody uncompressedBody = resource.getEntityBody();
    final Optional<HttpBody> gzippedBody = uncompressedBody.getGzippedBody();
    final HttpBody bodyToSend;
    if (gzippedBody.isPresent()) {
        /*
         * Compressed responses can use Content-Encoding and/or Transfer-Encoding.
         * The semantics differ slightly, but it is suffice to say that most user
         * agents don't advertise their Transfer-Encoding support.
         * So for now we only support the Content-Encoding mechanism.
         * Some other notes:
         *  - Some clients implement 'deflate' incorrectly. Hence we only support 'gzip',
         *    despite it having slightly more overhead.
         *  - We don't use Undertow's built-in compression support because we've
         *    pre-calculated the compressed response and expect to serve it up
         *    repeatedly, instead of calculating it on-the-fly for every request.
         */
        responseHeaders.put(Headers.VARY, Headers.ACCEPT_ENCODING_STRING);
        final HeaderValues acceptEncoding =
                exchange.getRequestHeaders().get(Headers.ACCEPT_ENCODING);
        if (null != acceptEncoding &&
                acceptEncoding.stream()
                              .anyMatch((header) -> Iterables.contains(HEADER_SPLITTER.split(header), "gzip"))) {
            responseHeaders.put(Headers.CONTENT_ENCODING, "gzip");
            bodyToSend = gzippedBody.get();
        } else {
            bodyToSend = uncompressedBody;
        }
    } else {
        bodyToSend = uncompressedBody;
    }

    // Now we know which version of the entity is visible to this user-agent,
    // figure out if the client already has the current version or not.
    final ETag eTag = bodyToSend.getETag();
    responseHeaders.put(Headers.ETAG, eTag.toString());
    if (ETagUtils.handleIfNoneMatch(exchange, eTag, true)) {
        final ByteBuffer entityBody = bodyToSend.getBody();
        responseHeaders.put(Headers.CONTENT_TYPE, "application/javascript");
        exchange.getResponseSender().send(entityBody);
    } else {
        exchange.setStatusCode(StatusCodes.NOT_MODIFIED);
        exchange.endExchange();
    }
}
 
Example #12
Source File: OperationParameter.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public ETag getEtag() {
    return etag;
}
 
Example #13
Source File: CustomResourceHandler.java    From PYX-Reloaded with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public ETag generate(Path path) {
    if (!cacheEnabled) return null;
    return new ETag(false, serverTag);
}
 
Example #14
Source File: CachedHttpRequest.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public ETag getEtag() {
    return etag;
}
 
Example #15
Source File: PathResource.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public ETag getETag() {
    return eTag;
}
 
Example #16
Source File: PathResourceManager.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public ETag generate(Path path) {
    return null;
}
 
Example #17
Source File: PathResource.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public PathResource(final Path file, final PathResourceManager manager, String path, ETag eTag) {
    this.file = file;
    this.path = path;
    this.manager = manager;
    this.eTag = eTag;
}
 
Example #18
Source File: DefaultServlet.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
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 #19
Source File: URLResource.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public ETag getETag() {
    return null;
}
 
Example #20
Source File: CachedResource.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public ETag getETag() {
    return eTag;
}
 
Example #21
Source File: DefaultServlet.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
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 #22
Source File: KnownPathResourceManager.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public ETag getETag() {
    return null;
}
 
Example #23
Source File: PathResource.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public ETag getETag() {
    return eTag;
}
 
Example #24
Source File: PathResource.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
public PathResource(final Path file, final PathResourceManager manager, String path, ETag eTag) {
    this.file = file;
    this.path = path;
    this.manager = manager;
    this.eTag = eTag;
}
 
Example #25
Source File: PathResourceManager.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public ETag generate(Path path) {
    return null;
}
 
Example #26
Source File: URLResource.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public ETag getETag() {
    return null;
}
 
Example #27
Source File: CachedResource.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public ETag getETag() {
    return eTag;
}
 
Example #28
Source File: TestResourceLoader.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public ETag getETag() {
    return delegate.getETag();
}
 
Example #29
Source File: PathResourceManager.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Generates an {@link ETag} for the provided {@link Path}.
 *
 * @param path Path for which to generate an ETag
 * @return ETag representing the provided path, or null
 */
ETag generate(Path path);
 
Example #30
Source File: Resource.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * @return The resources etags
 */
ETag getETag();