Commit a6210358 authored by Andrea Aime's avatar Andrea Aime
Browse files

[GEOS-9157] REST mosaic upload fails if using index store reference by name

parent 3c52e2ef
......@@ -2824,11 +2824,12 @@ public class ResourcePool {
}
/**
* Package private on purpose, using it for testing
* The catalog repository, used to gather store references by name by some GeoTools stores like
* pre-generalized or image mosaic
*
* @return
*/
CatalogRepository getRepository() {
public CatalogRepository getRepository() {
return repository;
}
}
......@@ -4,6 +4,7 @@
*/
package org.geoserver.rest.catalog;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
......@@ -15,6 +16,7 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.geoserver.catalog.Catalog;
import org.geoserver.catalog.CatalogBuilder;
import org.geoserver.catalog.CatalogRepository;
import org.geoserver.catalog.CoverageInfo;
import org.geoserver.catalog.CoverageStoreInfo;
import org.geoserver.catalog.LayerInfo;
......@@ -35,6 +37,7 @@ import org.geotools.coverage.grid.io.GridCoverage2DReader;
import org.geotools.coverage.grid.io.StructuredGridCoverage2DReader;
import org.geotools.util.URLs;
import org.geotools.util.factory.GeoTools;
import org.geotools.util.factory.Hints;
import org.opengis.coverage.grid.Format;
import org.opengis.coverage.grid.GridCoverageReader;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -230,10 +233,12 @@ public class CoverageStoreFileController extends AbstractStoreUploadController {
GridCoverage2DReader reader = null;
try {
reader = ((AbstractGridFormat) coverageFormat).getReader(uploadedFileURL);
CatalogRepository repository = catalog.getResourcePool().getRepository();
Hints hints = new Hints(new RenderingHints(Hints.REPOSITORY, repository));
reader = ((AbstractGridFormat) coverageFormat).getReader(uploadedFileURL, hints);
if (reader == null) {
throw new RestException(
"Could not aquire reader for coverage.", HttpStatus.INTERNAL_SERVER_ERROR);
"Could not acquire reader for coverage.", HttpStatus.INTERNAL_SERVER_ERROR);
}
// coverage read params
......
......@@ -5,7 +5,9 @@
*/
package org.geoserver.rest.catalog;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.File;
......@@ -21,6 +23,7 @@ import org.custommonkey.xmlunit.XMLAssert;
import org.geoserver.catalog.CatalogBuilder;
import org.geoserver.catalog.CoverageInfo;
import org.geoserver.catalog.CoverageStoreInfo;
import org.geoserver.catalog.DataStoreInfo;
import org.geoserver.catalog.WorkspaceInfo;
import org.geoserver.data.test.MockData;
import org.geoserver.data.test.SystemTestData;
......@@ -32,7 +35,10 @@ import org.geoserver.rest.util.IOUtils;
import org.geoserver.rest.util.RESTUtils;
import org.geotools.coverage.grid.io.GridCoverage2DReader;
import org.geotools.coverage.grid.io.StructuredGridCoverage2DReader;
import org.geotools.data.DataStore;
import org.geotools.data.DataUtilities;
import org.geotools.data.Query;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.gce.imagemosaic.ImageMosaicFormat;
import org.geotools.util.URLs;
import org.geotools.util.factory.GeoTools;
......@@ -47,6 +53,22 @@ import org.w3c.dom.Document;
public class CoverageStoreFileUploadTest extends CatalogRESTTestSupport {
@Override
protected void onSetUp(SystemTestData testData) throws Exception {
super.onSetUp(testData);
CatalogBuilder cb = new CatalogBuilder(getCatalog());
DataStoreInfo store = cb.buildDataStore("h2test");
store.getConnectionParameters().put("dbtype", "h2");
store.getConnectionParameters()
.put(
"database",
new File(getDataDirectory().findOrCreateDir("data"), "h2_test")
.getAbsolutePath());
store.getConnectionParameters().put("MVCC", true);
catalog.save(store);
}
@Before
public void cleanup() throws IOException {
// wipe out everything under "mosaic"
......@@ -101,14 +123,7 @@ public class CoverageStoreFileUploadTest extends CatalogRESTTestSupport {
@Test
public void testUploadImageMosaic() throws Exception {
URL zip = MockData.class.getResource("watertemp.zip");
InputStream is = null;
byte[] bytes;
try {
is = zip.openStream();
bytes = IOUtils.toByteArray(is);
} finally {
IOUtils.closeQuietly(is);
}
byte[] bytes = getBytes(zip);
MockHttpServletResponse response =
putAsServletResponse(
......@@ -134,9 +149,41 @@ public class CoverageStoreFileUploadTest extends CatalogRESTTestSupport {
}
@Test
public void testHarvestImageMosaic() throws Exception {
// Upload of the Mosaic via REST
URL zip = MockData.class.getResource("watertemp.zip");
public void testUploadImageMosaicRepoReference() throws Exception {
URL zip = CoverageStoreFileUploadTest.class.getResource("watertemp-repo.zip");
byte[] bytes = getBytes(zip);
MockHttpServletResponse response =
putAsServletResponse(
RestBaseController.ROOT_PATH
+ "/workspaces/gs/coveragestores/watertemp-repo/file.imagemosaic",
bytes,
"application/zip");
assertEquals(201, response.getStatus());
// check the response contents
String content = response.getContentAsString();
Document d = dom(new ByteArrayInputStream(content.getBytes()));
XMLAssert.assertXpathEvaluatesTo("watertemp-repo", "//coverageStore/name", d);
XMLAssert.assertXpathEvaluatesTo("ImageMosaic", "//coverageStore/type", d);
// check the coverage is actually there
CoverageStoreInfo storeInfo = getCatalog().getCoverageStoreByName("watertemp-repo");
assertNotNull(storeInfo);
CoverageInfo ci = getCatalog().getCoverageByName("watertemp-repo");
assertNotNull(ci);
assertEquals(storeInfo, ci.getStore());
// check harvesting happened as expected
DataStore ds = (DataStore) getCatalog().getDataStoreByName("h2test").getDataStore(null);
assertNotNull(ds);
SimpleFeatureSource fs = ds.getFeatureSource("watertemp-repo");
assertNotNull(fs);
assertEquals(4, fs.getCount(Query.ALL));
}
public byte[] getBytes(URL zip) throws IOException {
InputStream is = null;
byte[] bytes;
try {
......@@ -145,6 +192,15 @@ public class CoverageStoreFileUploadTest extends CatalogRESTTestSupport {
} finally {
IOUtils.closeQuietly(is);
}
return bytes;
}
@Test
public void testHarvestImageMosaic() throws Exception {
// Upload of the Mosaic via REST
URL zip = MockData.class.getResource("watertemp.zip");
byte[] bytes = getBytes(zip);
InputStream is;
MockHttpServletResponse response =
putAsServletResponse(
......@@ -209,14 +265,7 @@ public class CoverageStoreFileUploadTest extends CatalogRESTTestSupport {
// Harvesting of the Mosaic
URL zipHarvest = MockData.class.getResource("harvesting.zip");
// Extract a Byte array from the zip file
InputStream is = null;
byte[] bytes;
try {
is = zipHarvest.openStream();
bytes = IOUtils.toByteArray(is);
} finally {
IOUtils.closeQuietly(is);
}
byte[] bytes = getBytes(zipHarvest);
// Create the POST request
MockHttpServletRequest request =
createRequest(
......@@ -235,14 +284,7 @@ public class CoverageStoreFileUploadTest extends CatalogRESTTestSupport {
public void testHarvestImageMosaicWithDirectory() throws Exception {
// Upload of the Mosaic via REST
URL zip = MockData.class.getResource("watertemp.zip");
InputStream is = null;
byte[] bytes;
try {
is = zip.openStream();
bytes = IOUtils.toByteArray(is);
} finally {
IOUtils.closeQuietly(is);
}
byte[] bytes = getBytes(zip);
MockHttpServletResponse response =
putAsServletResponse(
......@@ -346,14 +388,7 @@ public class CoverageStoreFileUploadTest extends CatalogRESTTestSupport {
// Harvesting of the Mosaic
URL zipHarvest = MockData.class.getResource("harvesting.zip");
// Extract a Byte array from the zip file
InputStream is = null;
byte[] bytes;
try {
is = zipHarvest.openStream();
bytes = IOUtils.toByteArray(is);
} finally {
IOUtils.closeQuietly(is);
}
byte[] bytes = getBytes(zipHarvest);
// Create the POST request
MockHttpServletRequest request =
createRequest(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment