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));
}
}
}
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;
}
};
}
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);
}
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()))));
}
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);
}
Aggregations