Skip to content

Commit

Permalink
[GEOS-9157] REST mosaic upload fails if using index store reference b…
Browse files Browse the repository at this point in the history
…y name
  • Loading branch information
aaime committed Mar 19, 2019
1 parent 6632079 commit 1098c9e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2825,11 +2825,12 @@ static CoverageInfo getCoverageInfo(String coverageName, CoverageStoreInfo store
}

/**
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package org.geoserver.rest.catalog;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
Expand All @@ -15,6 +16,7 @@
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;
Expand All @@ -35,6 +37,7 @@
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;
Expand Down Expand Up @@ -230,10 +233,12 @@ public RestWrapper<CoverageStoreInfo> coverageStorePut(

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +23,7 @@
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;
Expand All @@ -32,7 +35,10 @@
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;
Expand All @@ -47,6 +53,22 @@

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"
Expand Down Expand Up @@ -101,14 +123,7 @@ public void testUploadWithSpaces() throws Exception {
@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(
Expand All @@ -134,9 +149,41 @@ public void testUploadImageMosaic() throws Exception {
}

@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 {
Expand All @@ -145,6 +192,15 @@ public void testHarvestImageMosaic() throws Exception {
} 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(
Expand Down Expand Up @@ -209,14 +265,7 @@ public void testHarvestNotAllowedOnSimpleCoverageStore() throws Exception {
// 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(
Expand All @@ -235,14 +284,7 @@ public void testHarvestNotAllowedOnSimpleCoverageStore() throws Exception {
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(
Expand Down Expand Up @@ -346,14 +388,7 @@ public void testHarvestExternalImageMosaic() throws Exception {
// 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(
Expand Down
Binary file not shown.

0 comments on commit 1098c9e

Please sign in to comment.