Skip to content

Commit

Permalink
Replacing deprecated url method in GeoServerResourceLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
aaime committed Jun 1, 2019
1 parent 35d91a2 commit 1414daf
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.geoserver.catalog.DataStoreInfo;
import org.geoserver.platform.GeoServerExtensions;
import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.resource.Files;
import org.geoserver.platform.resource.Resources;
import org.geotools.data.DataStore;
import org.geotools.data.Repository;
import org.opengis.feature.type.Name;
Expand All @@ -35,7 +37,10 @@ public class DSFinderRepository extends org.geotools.data.gen.DSFinderRepository

protected URL getURLForLocation(String location) throws IOException {
GeoServerResourceLoader loader = GeoServerExtensions.bean(GeoServerResourceLoader.class);
File f = loader.url(location);
File f =
Resources.find(
Resources.fromURL(Files.asResource(loader.getBaseDirectory()), location),
true);
URL url = null;
if (f.exists()) {
url = f.toURI().toURL();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.net.URLDecoder;
import org.geoserver.platform.GeoServerExtensions;
import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.resource.Files;
import org.geoserver.platform.resource.Resources;

/**
* The default implementation for GeneralizationInfosProvider, reading the info from an XML file.
Expand All @@ -32,7 +34,10 @@ protected URL deriveURLFromSourceObject(Object source) throws IOException {

GeoServerResourceLoader loader =
GeoServerExtensions.bean(GeoServerResourceLoader.class);
File f = loader.url(path);
File f =
Resources.find(
Resources.fromURL(Files.asResource(loader.getBaseDirectory()), path),
true);
URL url = null;
if (f != null && f.exists()) {
url = f.toURI().toURL();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.resource.FileLockProvider;
import org.geoserver.platform.resource.FileSystemResourceStore;
import org.geoserver.platform.resource.Files;
import org.geoserver.platform.resource.Resource;
import org.geoserver.platform.resource.Resources;
import org.geoserver.wps.executor.DefaultProcessManager;
import org.geoserver.wps.executor.WPSExecutionManager;
import org.geoserver.wps.resource.DefaultProcessArtifactsStore;
Expand Down Expand Up @@ -122,7 +124,12 @@ void initWPS(WPSInfo info, GeoServer geoServer) {
File storage = new File(outputStorageDirectory);
// if it's a path relative to the data directory, make it absolute
if (!storage.isAbsolute()) {
storage = resourceLoader.url(outputStorageDirectory);
storage =
Resources.find(
Resources.fromURL(
Files.asResource(resourceLoader.getBaseDirectory()),
outputStorageDirectory),
true);
}
if (storage.exists() && !storage.isDirectory()) {
throw new IllegalArgumentException(
Expand Down
27 changes: 23 additions & 4 deletions src/main/src/main/java/org/geoserver/catalog/ResourcePool.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
import org.geoserver.platform.GeoServerExtensions;
import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.ServiceException;
import org.geoserver.platform.resource.Files;
import org.geoserver.platform.resource.Resource;
import org.geoserver.platform.resource.ResourceListener;
import org.geoserver.platform.resource.ResourceNotification;
import org.geoserver.platform.resource.Resources;
import org.geoserver.util.EntityResolverProvider;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.io.AbstractGridFormat;
Expand Down Expand Up @@ -674,13 +676,22 @@ public static <K, V> Map<K, V> getParams(Map<K, V> m, GeoServerResourceLoader lo
String path = (String) value;

if (path.startsWith("file:")) {
File fixedPath = loader.url(path);
File fixedPath =
Resources.find(
Resources.fromURL(
Files.asResource(loader.getBaseDirectory()), path),
true);
URL url = URLs.fileToUrl(fixedPath);
entry.setValue((V) url.toExternalForm());
}
} else if (value instanceof URL && ((URL) value).getProtocol().equals("file")) {
URL url = (URL) value;
File fixedPath = loader.url(url.toString());
File fixedPath =
Resources.find(
Resources.fromURL(
Files.asResource(loader.getBaseDirectory()),
url.toString()),
true);
entry.setValue((V) URLs.fileToUrl(fixedPath));
} else if ((key != null)
&& (key.equals("directory") || key.equals("database"))
Expand All @@ -689,7 +700,11 @@ public static <K, V> Map<K, V> getParams(Map<K, V> m, GeoServerResourceLoader lo
// if a url is used for a directory (for example property store), convert it to path

if (path.startsWith("file:")) {
File fixedPath = loader.url(path);
File fixedPath =
Resources.find(
Resources.fromURL(
Files.asResource(loader.getBaseDirectory()), path),
true);
entry.setValue((V) fixedPath.toString());
}
}
Expand Down Expand Up @@ -1593,7 +1608,11 @@ private Object getObjectToRead(String urlString) {

if (isFile) {
GeoServerResourceLoader loader = catalog.getResourceLoader();
final File readerFile = loader.url(urlString);
final File readerFile =
Resources.find(
Resources.fromURL(
Files.asResource(loader.getBaseDirectory()), urlString),
true);
if (readerFile != null) {
readObject = readerFile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.geoserver.data.test.SystemTestData;
import org.geoserver.data.test.TestData;
import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.resource.Files;
import org.geoserver.platform.resource.Resources;
import org.geoserver.test.GeoServerSystemTestSupport;
import org.geoserver.test.SystemTest;
import org.junit.Test;
Expand Down Expand Up @@ -52,7 +54,12 @@ protected void onSetUp(SystemTestData testData) throws Exception {
@Test
public void testFindDataFile() throws IOException {
GeoServerResourceLoader loader = getResourceLoader();
final File file = loader.url("file:" + RAIN_DATA_PATH);
final File file =
Resources.find(
Resources.fromURL(
Files.asResource(loader.getBaseDirectory()),
"file:" + RAIN_DATA_PATH),
true);
assertNotNull(file);
}

Expand All @@ -61,14 +68,23 @@ public void testFindDataFileForAbsolutePath() throws IOException {
GeoServerResourceLoader loader = getResourceLoader();
final File dataDir = loader.getBaseDirectory();
final String absolutePath = dataDir.getCanonicalPath() + SEPARATOR_CHAR + RAIN_DATA_PATH;
final File file = loader.url(absolutePath);
final File file =
Resources.find(
Resources.fromURL(
Files.asResource(loader.getBaseDirectory()), absolutePath),
true);
assertNotNull(file);
}

@Test
public void testFindDataFileForCustomUrl() throws IOException {
GeoServerResourceLoader loader = getResourceLoader();
final File file = loader.url("sde://user:password@server:port");
final File file =
Resources.find(
Resources.fromURL(
Files.asResource(loader.getBaseDirectory()),
"sde://user:password@server:port"),
true);
assertNull(file); // Before GEOS-5931 it would have been returned a file again
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import org.geoserver.platform.resource.FileSystemResourceStore;
import org.geoserver.platform.resource.Files;
import org.geoserver.platform.resource.Paths;
import org.geoserver.platform.resource.Resource;
import org.geoserver.platform.resource.ResourceNotificationDispatcher;
Expand Down Expand Up @@ -205,12 +204,6 @@ public Resource fromPath(String path) {
return Resources.fromPath(path, resources.get(Paths.BASE));
}

/** @deprecated use {@link Resources#fromURL(Resource, String)} */
@Deprecated
public File url(String url) {
return Resources.find(Resources.fromURL(get(Paths.BASE), url), true);
}

/**
* Performs file lookup.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.geoserver.data.test.SystemTestData;
import org.geoserver.platform.GeoServerExtensions;
import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.resource.Files;
import org.geoserver.platform.resource.Resources;
import org.geoserver.rest.RestBaseController;
import org.junit.Before;
import org.junit.Ignore;
Expand Down Expand Up @@ -545,7 +547,12 @@ private void purgeRequest(final String purge, final int expectedFiles) throws Ex

GeoServerResourceLoader loader = GeoServerExtensions.bean(GeoServerResourceLoader.class);

final File storeDir = loader.url("data/wcs/mosaicfordelete");
final File storeDir =
Resources.find(
Resources.fromURL(
Files.asResource(loader.getBaseDirectory()),
"data/wcs/mosaicfordelete"),
true);
File[] content = storeDir.listFiles();
assertThat(content.length, anyOf(equalTo(9), equalTo(10), equalTo(11)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.geoserver.catalog.CoverageInfo;
import org.geoserver.platform.GeoServerExtensions;
import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.resource.Files;
import org.geoserver.platform.resource.Resources;
import org.geotools.util.logging.Logging;

/**
Expand All @@ -32,7 +34,12 @@ public String getMimeType(CoverageInfo cInfo) throws IOException {
// no mapping let's go with the ImageIO reader code
GeoServerResourceLoader loader = GeoServerExtensions.bean(GeoServerResourceLoader.class);

final File sourceFile = loader.url(cInfo.getStore().getURL());
final File sourceFile =
Resources.find(
Resources.fromURL(
Files.asResource(loader.getBaseDirectory()),
cInfo.getStore().getURL()),
true);
if (sourceFile == null) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Original source is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ public void setObject(String location) {
}
location = "file:" + path;
} else {
File dataFile = Resources.find(
Resources.fromURL(Files.asResource(rootDir), location), true);
File dataFile =
Resources.find(
Resources.fromURL(Files.asResource(rootDir), location), true);
if (dataFile == null || dataFile.equals(file)) {
// not relative to the data directory, does not need fixing
location = "file://" + file.getAbsolutePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,15 @@ public void validate(IValidatable<String> validatable) {
GeoServerResourceLoader loader = GeoServerExtensions.bean(GeoServerResourceLoader.class);
if (baseDirectory != null) {
// local to provided baseDirectory
relFile = Resources.find(Resources.fromURL(Files.asResource(baseDirectory), uriSpec),
true);
relFile =
Resources.find(
Resources.fromURL(Files.asResource(baseDirectory), uriSpec), true);
} else if (loader != null) {
// local to data directory?
relFile = loader.url(uriSpec);
relFile =
Resources.find(
Resources.fromURL(Files.asResource(loader.getBaseDirectory()), uriSpec),
true);
}

if (relFile == null || !relFile.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.apache.wicket.model.Model;
import org.geoserver.platform.GeoServerExtensions;
import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.resource.Files;
import org.geoserver.platform.resource.Resources;
import org.geoserver.web.wicket.ParamResourceModel;
import org.geotools.util.logging.Logging;

Expand Down Expand Up @@ -85,7 +87,12 @@ public GeoServerFileChooser(String id, IModel<File> file, boolean hideFileSystem

// first check if the file is a relative reference into the data dir
if (selection != null) {
File relativeToDataDir = loader.url(selection.getPath());
File relativeToDataDir =
Resources.find(
Resources.fromURL(
Files.asResource(loader.getBaseDirectory()),
selection.getPath()),
true);
if (relativeToDataDir != null) {
selection = relativeToDataDir;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import javax.imageio.ImageIO;
import org.geoserver.platform.GeoServerExtensions;
import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.resource.Files;
import org.geoserver.platform.resource.Resource;
import org.geoserver.platform.resource.Resources;
import org.geoserver.wms.WMSMapContent;
import org.geotools.util.SoftValueHashMap;
import org.geotools.util.URLs;
Expand Down Expand Up @@ -99,7 +101,11 @@ protected BufferedImage getLogo() throws IOException {
url = new URL(imageURL);

if (url.getProtocol() == null || url.getProtocol().equals("file")) {
File file = loader.url(imageURL);
File file =
Resources.find(
Resources.fromURL(
Files.asResource(loader.getBaseDirectory()), imageURL),
true);
if (file.exists()) {
url = URLs.fileToUrl(file);
}
Expand Down

0 comments on commit 1414daf

Please sign in to comment.