forked from geoserver/geoserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request geoserver#4479 from dromagnoli/GEOS-9733
[GEOS-9733]: Importer, stop scanning the whole directory when looking for supplemental files
- Loading branch information
Showing
7 changed files
with
285 additions
and
28 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
.../core/src/main/java/org/geoserver/importer/DefaultSupplementalFileExtensionsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* (c) 2020 Open Source Geospatial Foundation - all rights reserved | ||
* This code is licensed under the GPL 2.0 license, available at the root | ||
* application directory. | ||
*/ | ||
package org.geoserver.importer; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import org.apache.commons.lang.StringUtils; | ||
|
||
/** Default implementation of a SupplementalFileExtensionsProvider */ | ||
public class DefaultSupplementalFileExtensionsProvider | ||
implements SupplementalFileExtensionsProvider { | ||
private Set<String> acceptedInputExtensions; | ||
private Set<String> supplementalExtensions; | ||
private Set<String> upperCaseSupplementalExtensions; | ||
|
||
public DefaultSupplementalFileExtensionsProvider( | ||
Set<String> acceptedInputExtensions, Set<String> supplementalExtensions) { | ||
this.acceptedInputExtensions = Collections.unmodifiableSet(acceptedInputExtensions); | ||
this.supplementalExtensions = Collections.unmodifiableSet(supplementalExtensions); | ||
Set<String> upperCase = new HashSet<>(); | ||
supplementalExtensions.stream().forEach(e -> upperCase.add(e.toUpperCase())); | ||
upperCaseSupplementalExtensions = Collections.unmodifiableSet(upperCase); | ||
} | ||
|
||
private boolean isSupportedInputExtension(String extension) { | ||
return extension != null && acceptedInputExtensions.contains(extension.toLowerCase()); | ||
} | ||
|
||
public boolean canHandle(String baseExtension) { | ||
return baseExtension != null | ||
&& acceptedInputExtensions.contains(baseExtension.toLowerCase()); | ||
} | ||
|
||
public Set<String> getExtensions(String baseExtension) { | ||
if (!isSupportedInputExtension(baseExtension)) return Collections.emptySet(); | ||
// some data providers produce tiff files being stored as .TIF | ||
// we can reasonably suppose that supplemental files will be upper case too | ||
// i.e. .PRJ, .XML | ||
return StringUtils.isAllUpperCase(baseExtension) | ||
? upperCaseSupplementalExtensions | ||
: supplementalExtensions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...ion/importer/core/src/main/java/org/geoserver/importer/SpatialFileExtensionsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* (c) 2020 Open Source Geospatial Foundation - all rights reserved | ||
* This code is licensed under the GPL 2.0 license, available at the root | ||
* application directory. | ||
*/ | ||
package org.geoserver.importer; | ||
|
||
import java.util.*; | ||
|
||
/** A Class reporting a Set of default file extensions for some commonly used spatial files */ | ||
public class SpatialFileExtensionsProvider implements SupplementalFileExtensionsProvider { | ||
|
||
public SpatialFileExtensionsProvider() {}; | ||
|
||
static class JPEGFileExtensionsProvider extends DefaultSupplementalFileExtensionsProvider { | ||
|
||
JPEGFileExtensionsProvider() { | ||
super( | ||
new HashSet<String>(Arrays.asList("jpeg", "jpg")), | ||
new HashSet<String>(Arrays.asList("jpw", "wld", "prj"))); | ||
} | ||
}; | ||
|
||
static class TIFFFileExtensionsProvider extends DefaultSupplementalFileExtensionsProvider { | ||
|
||
TIFFFileExtensionsProvider() { | ||
super( | ||
new HashSet<String>(Arrays.asList("tif", "tiff")), | ||
new HashSet<String>( | ||
Arrays.asList( | ||
"tfw", "wld", "aux", "rrd", "xml", "tif.aux.xml", "prj"))); | ||
} | ||
}; | ||
|
||
static class PNGFileExtensionsProvider extends DefaultSupplementalFileExtensionsProvider { | ||
|
||
PNGFileExtensionsProvider() { | ||
super( | ||
new HashSet<String>(Arrays.asList("png")), | ||
new HashSet<String>(Arrays.asList("pnw", "wld", "aux.xml", "xml", "prj"))); | ||
} | ||
}; | ||
|
||
static class NetCDFFileExtensionsProvider extends DefaultSupplementalFileExtensionsProvider { | ||
|
||
NetCDFFileExtensionsProvider() { | ||
super( | ||
new HashSet<String>(Arrays.asList("nc")), | ||
new HashSet<String>(Arrays.asList("ncx", "aux.xml", "xml", "prj"))); | ||
} | ||
}; | ||
|
||
static class GribFileExtensionsProvider extends DefaultSupplementalFileExtensionsProvider { | ||
|
||
GribFileExtensionsProvider() { | ||
super( | ||
new HashSet<String>(Arrays.asList("grib", "grb", "grib2", "grb2")), | ||
new HashSet<String>( | ||
Arrays.asList( | ||
"grb2.ncx3", | ||
"gbx9", | ||
"ncx3", | ||
"gbx9.ncx3", | ||
"aux.xml", | ||
"xml", | ||
"prj"))); | ||
} | ||
}; | ||
|
||
static class ShapeFileExtensionsProvider extends DefaultSupplementalFileExtensionsProvider { | ||
|
||
ShapeFileExtensionsProvider() { | ||
super( | ||
new HashSet<String>(Arrays.asList("shp")), | ||
new HashSet<String>( | ||
Arrays.asList( | ||
"shx", | ||
"dbf", | ||
"aux.xml", | ||
"idx", | ||
"sbx", | ||
"sbn", | ||
"shp.ed.lock", | ||
"shp.xml", | ||
"prj"))); | ||
} | ||
}; | ||
|
||
private static final ShapeFileExtensionsProvider SHAPEFILE_PROVIDER = | ||
new ShapeFileExtensionsProvider(); | ||
private static final TIFFFileExtensionsProvider TIF_PROVIDER = new TIFFFileExtensionsProvider(); | ||
private static final JPEGFileExtensionsProvider JPEG_PROVIDER = | ||
new JPEGFileExtensionsProvider(); | ||
private static final PNGFileExtensionsProvider PNG_PROVIDER = new PNGFileExtensionsProvider(); | ||
private static final NetCDFFileExtensionsProvider NETCDF_PROVIDER = | ||
new NetCDFFileExtensionsProvider(); | ||
private static final GribFileExtensionsProvider GRIB_PROVIDER = | ||
new GribFileExtensionsProvider(); | ||
|
||
static Map<String, SupplementalFileExtensionsProvider> PROVIDERS = new HashMap(); | ||
|
||
{ | ||
// Providers being setup on the available tests | ||
PROVIDERS.put("tif", TIF_PROVIDER); | ||
PROVIDERS.put("tiff", TIF_PROVIDER); | ||
PROVIDERS.put("jpeg", JPEG_PROVIDER); | ||
PROVIDERS.put("jpg", JPEG_PROVIDER); | ||
PROVIDERS.put("png", PNG_PROVIDER); | ||
PROVIDERS.put("shp", SHAPEFILE_PROVIDER); | ||
PROVIDERS.put("nc", NETCDF_PROVIDER); | ||
PROVIDERS.put("grib", GRIB_PROVIDER); | ||
PROVIDERS.put("grib2", GRIB_PROVIDER); | ||
} | ||
|
||
public Set<String> getExtensions(String baseExtension) { | ||
if (canHandle(baseExtension)) { | ||
return PROVIDERS.get(baseExtension.toLowerCase()).getExtensions(baseExtension); | ||
} | ||
return Collections.emptySet(); | ||
} | ||
|
||
public boolean canHandle(String baseExtension) { | ||
return baseExtension != null && PROVIDERS.containsKey(baseExtension.toLowerCase()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...mporter/core/src/main/java/org/geoserver/importer/SupplementalFileExtensionsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* (c) 2020 Open Source Geospatial Foundation - all rights reserved | ||
* This code is licensed under the GPL 2.0 license, available at the root | ||
* application directory. | ||
*/ | ||
package org.geoserver.importer; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* A Class reporting a Set of file extensions for a given base extension. Additional implementations | ||
* may return additional file extensions (i.e. getting them from a Datadir or external file | ||
* definition). | ||
*/ | ||
public interface SupplementalFileExtensionsProvider { | ||
|
||
/** | ||
* Return the set of supplemental file extensions available for the given base input extension | ||
*/ | ||
Set<String> getExtensions(String baseExtension); | ||
|
||
/** Check if this provider can handle the specified base input extension */ | ||
boolean canHandle(String baseExtension); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...sion/importer/core/src/test/java/org/geoserver/importer/SupplementalFileProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* (c) 2020 Open Source Geospatial Foundation - all rights reserved | ||
* This code is licensed under the GPL 2.0 license, available at the root | ||
* application directory. | ||
*/ | ||
package org.geoserver.importer; | ||
|
||
import java.util.Set; | ||
import junit.framework.TestCase; | ||
|
||
public class SupplementalFileProviderTest extends TestCase { | ||
|
||
public void testSupportedSupplementalFiles() throws Exception { | ||
|
||
SpatialFileExtensionsProvider provider = new SpatialFileExtensionsProvider(); | ||
|
||
// Test some unsupported base extensions | ||
assertFalse(provider.canHandle("txt")); | ||
assertFalse(provider.canHandle("pdf")); | ||
|
||
// Test some supported extensions | ||
assertTrue(provider.canHandle("tif")); | ||
Set<String> extensions = provider.getExtensions("tif"); | ||
assertTrue(extensions.contains("tfw")); | ||
assertTrue(extensions.contains("prj")); | ||
assertTrue(extensions.contains("wld")); | ||
assertTrue(extensions.contains("rrd")); | ||
|
||
assertTrue(provider.canHandle("jpg")); | ||
extensions = provider.getExtensions("jpg"); | ||
assertTrue(extensions.contains("jpw")); | ||
assertTrue(extensions.contains("prj")); | ||
assertTrue(extensions.contains("wld")); | ||
|
||
// Test the UPPERCASE support | ||
extensions = provider.getExtensions("SHP"); | ||
assertTrue(extensions.contains("DBF")); | ||
assertTrue(extensions.contains("SHX")); | ||
} | ||
} |