spark.resource.ExternalResourceHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spark-core Show documentation
Show all versions of spark-core Show documentation
A micro framework for creating web applications in Kotlin and Java 8 with minimal effort
The newest version!
//
// ========================================================================
// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package spark.resource;
import java.net.MalformedURLException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.staticfiles.DirectoryTraversal;
import spark.utils.Assert;
/**
* Locates resources from external folder
* Code snippets copied from Eclipse Jetty source. Modifications made by Per Wendel.
*/
public class ExternalResourceHandler extends AbstractResourceHandler {
private static final Logger LOG = LoggerFactory.getLogger(ExternalResourceHandler.class);
private final String baseResource;
private String welcomeFile;
/**
* Constructor
*
* @param baseResource the base resource path
*/
public ExternalResourceHandler(String baseResource) {
this(baseResource, null);
}
/**
* Constructor
*
* @param baseResource the base resource path
* @param welcomeFile the welcomeFile
*/
public ExternalResourceHandler(String baseResource, String welcomeFile) {
Assert.notNull(baseResource);
this.baseResource = baseResource;
this.welcomeFile = welcomeFile;
}
@Override
protected AbstractFileResolvingResource getResource(String path) throws MalformedURLException {
if (path == null || !path.startsWith("/")) {
throw new MalformedURLException(path);
}
try {
path = UriPath.canonical(path);
final String addedPath = addPaths(baseResource, path);
ExternalResource resource = new ExternalResource(addedPath);
if (resource.exists() && resource.isDirectory()) {
if (welcomeFile != null) {
resource = new ExternalResource(addPaths(resource.getPath(), welcomeFile));
} else {
// No welcome file configured, serve nothing since it's a directory
resource = null;
}
}
if (resource != null && resource.exists()) {
DirectoryTraversal.protectAgainstForExternal(resource.getPath(), baseResource);
return resource;
} else {
return null;
}
} catch (DirectoryTraversal.DirectoryTraversalDetection directoryTraversalDetection) {
throw directoryTraversalDetection;
} catch (Exception e) {
if (LOG.isDebugEnabled()) {
LOG.debug(e.getClass().getSimpleName() + " when trying to get resource. " + e.getMessage());
}
}
return null;
}
}