Search in sources :

Example 1 with ETag

use of io.undertow.util.ETag in project undertow by undertow-io.

the class DefaultServlet method serveFileBlocking.

private void serveFileBlocking(final HttpServletRequest req, final HttpServletResponse resp, final Resource resource) 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)) {
            resp.setStatus(StatusCodes.NOT_MODIFIED);
            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)) {
        HttpServerExchange exchange = SecurityActions.requireCurrentServletRequestContext().getOriginalRequest().getExchange();
        if (rangeResponse == null) {
            resource.serve(exchange.getResponseSender(), exchange, completionCallback(include));
        } else {
            ((RangeAwareResource) resource).serveRange(exchange.getResponseSender(), exchange, start, end, completionCallback(include));
        }
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) ETag(io.undertow.util.ETag) ByteRange(io.undertow.util.ByteRange) RangeAwareResource(io.undertow.server.handlers.resource.RangeAwareResource) Date(java.util.Date)

Example 2 with ETag

use of io.undertow.util.ETag in project keycloak by keycloak.

the class SecureServerDefinition method createHttpManagementConfigContextService.

private static Service<Void> createHttpManagementConfigContextService(final String factoryName, final InjectedValue<ExtensibleHttpManagement> httpConfigContext) {
    final String contextName = "/keycloak/adapter/" + factoryName + "/";
    return new Service<Void>() {

        public void start(StartContext startContext) throws StartException {
            ExtensibleHttpManagement extensibleHttpManagement = (ExtensibleHttpManagement) httpConfigContext.getValue();
            extensibleHttpManagement.addStaticContext(contextName, new ResourceManager() {

                public Resource getResource(final String path) throws IOException {
                    KeycloakAdapterConfigService adapterConfigService = KeycloakAdapterConfigService.getInstance();
                    final String config = adapterConfigService.getJSON(factoryName);
                    if (config == null) {
                        return null;
                    }
                    return new Resource() {

                        public String getPath() {
                            return null;
                        }

                        public Date getLastModified() {
                            return null;
                        }

                        public String getLastModifiedString() {
                            return null;
                        }

                        public ETag getETag() {
                            return null;
                        }

                        public String getName() {
                            return null;
                        }

                        public boolean isDirectory() {
                            return false;
                        }

                        public List<Resource> list() {
                            return Collections.emptyList();
                        }

                        public String getContentType(MimeMappings mimeMappings) {
                            return "application/json";
                        }

                        public void serve(Sender sender, HttpServerExchange exchange, IoCallback completionCallback) {
                            sender.send(config);
                        }

                        public Long getContentLength() {
                            return Long.valueOf((long) config.length());
                        }

                        public String getCacheKey() {
                            return null;
                        }

                        public File getFile() {
                            return null;
                        }

                        public Path getFilePath() {
                            return null;
                        }

                        public File getResourceManagerRoot() {
                            return null;
                        }

                        public Path getResourceManagerRootPath() {
                            return null;
                        }

                        public URL getUrl() {
                            return null;
                        }
                    };
                }

                public boolean isResourceChangeListenerSupported() {
                    return false;
                }

                public void registerResourceChangeListener(ResourceChangeListener listener) {
                }

                public void removeResourceChangeListener(ResourceChangeListener listener) {
                }

                public void close() throws IOException {
                }
            });
        }

        public void stop(StopContext stopContext) {
            ((ExtensibleHttpManagement) httpConfigContext.getValue()).removeContext(contextName);
        }

        public Void getValue() throws IllegalStateException, IllegalArgumentException {
            return null;
        }
    };
}
Also used : Path(java.nio.file.Path) StopContext(org.jboss.msc.service.StopContext) Resource(io.undertow.server.handlers.resource.Resource) Service(org.jboss.msc.service.Service) ResourceManager(io.undertow.server.handlers.resource.ResourceManager) IOException(java.io.IOException) MimeMappings(io.undertow.util.MimeMappings) Date(java.util.Date) URL(java.net.URL) Sender(io.undertow.io.Sender) HttpServerExchange(io.undertow.server.HttpServerExchange) StartContext(org.jboss.msc.service.StartContext) ETag(io.undertow.util.ETag) IoCallback(io.undertow.io.IoCallback) ResourceChangeListener(io.undertow.server.handlers.resource.ResourceChangeListener) List(java.util.List) ExtensibleHttpManagement(org.jboss.as.server.mgmt.domain.ExtensibleHttpManagement) File(java.io.File)

Example 3 with ETag

use of io.undertow.util.ETag in project divolte-collector by divolte.

the class TrackingJavaScriptResourceTest method testETagIsValid.

@Test
public void testETagIsValid() throws IOException {
    final ETag eTag = trackingJavaScript.getEntityBody().getETag();
    validateEtag(eTag);
}
Also used : ETag(io.undertow.util.ETag) Test(org.junit.Test)

Example 4 with ETag

use of io.undertow.util.ETag in project divolte-collector by divolte.

the class TrackingJavaScriptResourceTest method testGzippedETagIsValid.

@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()))));
}
Also used : ETag(io.undertow.util.ETag) Test(org.junit.Test)

Example 5 with ETag

use of io.undertow.util.ETag in project undertow by undertow-io.

the class PathResourceManagerTestCase method testETagFunction.

@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);
}
Also used : Path(java.nio.file.Path) ETag(io.undertow.util.ETag) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) ResourceManager(io.undertow.server.handlers.resource.ResourceManager) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Aggregations

ETag (io.undertow.util.ETag)11 HttpServerExchange (io.undertow.server.HttpServerExchange)4 Date (java.util.Date)4 ByteRange (io.undertow.util.ByteRange)3 ByteBuffer (java.nio.ByteBuffer)3 Test (org.junit.Test)3 HttpHandler (io.undertow.server.HttpHandler)2 RangeAwareResource (io.undertow.server.handlers.resource.RangeAwareResource)2 ResourceManager (io.undertow.server.handlers.resource.ResourceManager)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Splitter (com.google.common.base.Splitter)1 Iterables (com.google.common.collect.Iterables)1 GzippableHttpBody (io.divolte.server.js.GzippableHttpBody)1 HttpBody (io.divolte.server.js.HttpBody)1 JavaScriptResource (io.divolte.server.js.JavaScriptResource)1 IoCallback (io.undertow.io.IoCallback)1 Sender (io.undertow.io.Sender)1 ResponseCache (io.undertow.server.handlers.cache.ResponseCache)1 ContentEncodedResource (io.undertow.server.handlers.encoding.ContentEncodedResource)1