use of io.undertow.server.handlers.resource.Resource in project spring-boot by spring-projects.
the class JarResourceManagerTests method emptyPathIsHandledCorrectly.
@Test
public void emptyPathIsHandledCorrectly() throws IOException {
Resource resource = this.resourceManager.getResource("");
assertThat(resource).isNotNull();
assertThat(resource.isDirectory()).isTrue();
}
use of io.undertow.server.handlers.resource.Resource in project kontraktor by RuedigerMoeller.
the class KontraktorServlet method doGet.
/**
* {
* facade = Actors.AsActor(ServletApp.class);
* ((ServletApp) facade).init();
* }*
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pathInfo = req.getPathInfo();
if (pathInfo == null) {
String dbg = req.getRequestURI();
resp.sendRedirect(dbg + "/");
return;
}
Resource resource = null;
if ("".equals(pathInfo) || "/".equals(pathInfo)) {
resource = dynamicResourceManager.getResource(pathInfo + "index.html");
} else {
resource = dynamicResourceManager.getResource(pathInfo);
}
if (resource != null) {
Long contentLength = resource.getContentLength();
if (contentLength != null) {
resp.setContentLength((int) contentLength.longValue());
}
byte[] bytes = null;
if (resource instanceof DynamicResourceManager.MyResource) {
bytes = ((DynamicResourceManager.MyResource) resource).getBytes();
} else if (resource.getFile() != null) {
bytes = FileUtil.readFully(resource.getFile());
}
// FIXME: mimetype, lastModified / 304
if (bytes != null) {
ServletOutputStream out = resp.getOutputStream();
out.write(bytes);
out.flush();
out.close();
return;
}
}
Log.Error(this, "Unhandled resource");
unhandledGet(req, resp);
}
use of io.undertow.server.handlers.resource.Resource in project undertow by undertow-io.
the class ServletContextImpl method getRealPath.
@Override
public String getRealPath(final String path) {
if (path == null) {
return null;
}
final DeploymentInfo deploymentInfo = getDeploymentInfo();
String canonicalPath = CanonicalPathUtils.canonicalize(path);
Resource resource;
try {
resource = deploymentInfo.getResourceManager().getResource(canonicalPath);
if (resource == null) {
// UNDERTOW-373 even though the resource does not exist we still need to return a path
Resource deploymentRoot = deploymentInfo.getResourceManager().getResource("/");
if (deploymentRoot == null) {
return null;
}
Path root = deploymentRoot.getFilePath();
if (root == null) {
return null;
}
if (!canonicalPath.startsWith("/")) {
canonicalPath = "/" + canonicalPath;
}
if (File.separatorChar != '/') {
canonicalPath = canonicalPath.replace('/', File.separatorChar);
}
return root.toAbsolutePath().toString() + canonicalPath;
}
} catch (IOException e) {
return null;
}
Path file = resource.getFilePath();
if (file == null) {
return null;
}
return file.toAbsolutePath().toString();
}
use of io.undertow.server.handlers.resource.Resource in project undertow by undertow-io.
the class ContentEncodedResourceManager method getResource.
/**
* 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) {
// 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);
}
}
use of io.undertow.server.handlers.resource.Resource in project undertow by undertow-io.
the class DirectoryPredicate method resolve.
@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;
}
return resource.isDirectory();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations