mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-02-02 04:39:14 +01:00
do not return directories from WebResourceLoader
This commit is contained in:
@@ -39,19 +39,18 @@ import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableList.Builder;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.util.HttpUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Default implementation of the {@link UberWebResourceLoader}.
|
||||
@@ -134,7 +133,7 @@ public class DefaultUberWebResourceLoader implements UberWebResourceLoader
|
||||
|
||||
try
|
||||
{
|
||||
URL ctxResource = servletContext.getResource(path);
|
||||
URL ctxResource = nonDirectory(servletContext.getResource(path));
|
||||
|
||||
if (ctxResource != null)
|
||||
{
|
||||
@@ -144,7 +143,7 @@ public class DefaultUberWebResourceLoader implements UberWebResourceLoader
|
||||
|
||||
for (PluginWrapper wrapper : plugins)
|
||||
{
|
||||
URL resource = wrapper.getWebResourceLoader().getResource(path);
|
||||
URL resource = nonDirectory(wrapper.getWebResourceLoader().getResource(path));
|
||||
|
||||
if (resource != null)
|
||||
{
|
||||
@@ -190,13 +189,13 @@ public class DefaultUberWebResourceLoader implements UberWebResourceLoader
|
||||
|
||||
try
|
||||
{
|
||||
resource = servletContext.getResource(path);
|
||||
resource = nonDirectory(servletContext.getResource(path));
|
||||
|
||||
if (resource == null)
|
||||
{
|
||||
for (PluginWrapper wrapper : plugins)
|
||||
{
|
||||
resource = wrapper.getWebResourceLoader().getResource(path);
|
||||
resource = nonDirectory(wrapper.getWebResourceLoader().getResource(path));
|
||||
|
||||
if (resource != null)
|
||||
{
|
||||
@@ -219,6 +218,29 @@ public class DefaultUberWebResourceLoader implements UberWebResourceLoader
|
||||
return resource;
|
||||
}
|
||||
|
||||
private URL nonDirectory(URL url) {
|
||||
if (url == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isDirectory(url)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
private boolean isDirectory(URL url) {
|
||||
if ("file".equals(url.getProtocol())) {
|
||||
try {
|
||||
return Files.isDirectory(Paths.get(url.toURI()));
|
||||
} catch (URISyntaxException ex) {
|
||||
throw Throwables.propagate(ex);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
|
||||
@@ -93,7 +93,7 @@ public class PathWebResourceLoader implements WebResourceLoader
|
||||
URL resource = null;
|
||||
Path file = directory.resolve(filePath(path));
|
||||
|
||||
if (Files.exists(file))
|
||||
if (Files.exists(file) && ! Files.isDirectory(file))
|
||||
{
|
||||
logger.trace("found path {} at {}", path, file);
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ package sonia.scm.plugin;
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -52,9 +53,8 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
@@ -143,8 +143,7 @@ public class DefaultUberWebResourceLoaderTest extends WebResourceLoaderTestBase
|
||||
when(servletContext.getResource("/myresource")).thenReturn(SCM_MANAGER);
|
||||
|
||||
WebResourceLoader resourceLoader =
|
||||
new DefaultUberWebResourceLoader(servletContext,
|
||||
new ArrayList<PluginWrapper>());
|
||||
new DefaultUberWebResourceLoader(servletContext, new ArrayList<>());
|
||||
URL resource = resourceLoader.getResource("/myresource");
|
||||
|
||||
assertSame(SCM_MANAGER, resource);
|
||||
@@ -173,6 +172,50 @@ public class DefaultUberWebResourceLoaderTest extends WebResourceLoaderTestBase
|
||||
containsInAnyOrder(file.toURI().toURL(), BITBUCKET));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForDirectoryFromServletContext() throws IOException {
|
||||
URL url = temp.newFolder().toURI().toURL();
|
||||
when(servletContext.getResource("/myresource")).thenReturn(url);
|
||||
|
||||
WebResourceLoader resourceLoader =
|
||||
new DefaultUberWebResourceLoader(servletContext, new ArrayList<>());
|
||||
|
||||
assertNull(resourceLoader.getResource("/myresource"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForDirectoryFromPlugin() throws IOException {
|
||||
URL url = temp.newFolder().toURI().toURL();
|
||||
WebResourceLoader loader = mock(WebResourceLoader.class);
|
||||
when(loader.getResource("/myresource")).thenReturn(url);
|
||||
|
||||
PluginWrapper pluginWrapper = mock(PluginWrapper.class);
|
||||
when(pluginWrapper.getWebResourceLoader()).thenReturn(loader);
|
||||
|
||||
WebResourceLoader resourceLoader =
|
||||
new DefaultUberWebResourceLoader(servletContext, Lists.newArrayList(pluginWrapper));
|
||||
|
||||
assertNull(resourceLoader.getResource("/myresource"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForDirectories() throws IOException {
|
||||
URL url = temp.newFolder().toURI().toURL();
|
||||
when(servletContext.getResource("/myresource")).thenReturn(url);
|
||||
|
||||
WebResourceLoader loader = mock(WebResourceLoader.class);
|
||||
when(loader.getResource("/myresource")).thenReturn(url);
|
||||
|
||||
PluginWrapper pluginWrapper = mock(PluginWrapper.class);
|
||||
when(pluginWrapper.getWebResourceLoader()).thenReturn(loader);
|
||||
|
||||
UberWebResourceLoader resourceLoader =
|
||||
new DefaultUberWebResourceLoader(servletContext, Lists.newArrayList(pluginWrapper));
|
||||
|
||||
List<URL> resources = resourceLoader.getResources("/myresource");
|
||||
Assertions.assertThat(resources).isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -54,15 +54,18 @@ import java.net.URL;
|
||||
public class PathWebResourceLoaderTest extends WebResourceLoaderTestBase
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testGetResource() throws IOException
|
||||
{
|
||||
public void testGetNullForDirectories() throws IOException {
|
||||
File directory = temp.newFolder();
|
||||
assertTrue(new File(directory, "awesome").mkdir());
|
||||
|
||||
WebResourceLoader resourceLoader = new PathWebResourceLoader(directory.toPath());
|
||||
assertNull(resourceLoader.getResource("awesome"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGetResource() throws IOException {
|
||||
File directory = temp.newFolder();
|
||||
URL url = file(directory, "myresource").toURI().toURL();
|
||||
|
||||
@@ -74,7 +77,4 @@ public class PathWebResourceLoaderTest extends WebResourceLoaderTestBase
|
||||
assertNull(resourceLoader.getResource("other"));
|
||||
}
|
||||
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user