From fcffda635ed58ade8bfe9e31f3e1983acfa9d3a4 Mon Sep 17 00:00:00 2001 From: ebocher Date: Tue, 2 Jul 2013 18:33:05 +0200 Subject: [PATCH 1/4] Improve url exceptions --- .../server/wms/GetCapabilitiesHandler.java | 14 ++++++++++--- .../orbisgis/server/wms/GetMapParameters.java | 21 ++++++++++++++++--- .../orbisgis/server/wms/ParametersTest.java | 14 +++++++++++++ .../java/org/orbisgis/server/wms/WMSTest.java | 10 ++++++--- 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/app/wms/src/main/java/org/orbisgis/server/wms/GetCapabilitiesHandler.java b/app/wms/src/main/java/org/orbisgis/server/wms/GetCapabilitiesHandler.java index ebc97a9..9fa427b 100644 --- a/app/wms/src/main/java/org/orbisgis/server/wms/GetCapabilitiesHandler.java +++ b/app/wms/src/main/java/org/orbisgis/server/wms/GetCapabilitiesHandler.java @@ -55,10 +55,9 @@ import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; import org.cts.crs.CRSException; import org.cts.crs.CoordinateReferenceSystem; +import org.cts.registry.RegistryException; /** * Creates the answer to a getCapabilities request and writes it into the output @@ -253,7 +252,13 @@ private OnlineResource buildOnlineResource(WMSResponse wmsResponse, String key, } catch (JAXBException ex) { throw new RuntimeException("Failed to build the JAXB Context, can't build the associated XML.", ex); } - Set codes = DataSourceFactory.getCRSFactory().getSupportedCodes("EPSG"); + Set codes = null; + try { + codes = DataSourceFactory.getCRSFactory().getSupportedCodes("EPSG"); + } catch (RegistryException ex) { + throw new RuntimeException("Cannot build the list of supported CRS codes.", ex); + } + LinkedList ll = new LinkedList(); for (String s : codes) { ll.add("EPSG:" + s); @@ -358,8 +363,11 @@ public void sourceAdded(SourceEvent e) { } layerMap.put(name, layer); } catch (NoSuchTableException ex) { + LOGGER.error("Cannot find the data "+ name, ex); } catch (DataSourceCreationException ex) { + LOGGER.error("Cannot find the data "+ name, ex); } catch (DriverException ex) { + LOGGER.error("Cannot acces to the data "+ name, ex); } } } diff --git a/app/wms/src/main/java/org/orbisgis/server/wms/GetMapParameters.java b/app/wms/src/main/java/org/orbisgis/server/wms/GetMapParameters.java index 42d6144..8871629 100644 --- a/app/wms/src/main/java/org/orbisgis/server/wms/GetMapParameters.java +++ b/app/wms/src/main/java/org/orbisgis/server/wms/GetMapParameters.java @@ -28,6 +28,7 @@ */ package org.orbisgis.server.wms; +import com.vividsolutions.jts.geom.Envelope; import java.util.*; /** @@ -102,6 +103,9 @@ public GetMapParameters(Map queryParameters) throws WMSExcepti bBox = parseBBox(qp.get(BBOX)[0]); width = parseInteger(qp.get(WIDTH)[0]); height = parseInteger(qp.get(HEIGHT)[0]); + if(width<=0 || height<=0){ + throw new WMSException("The width and height must be greater than 0."); + } layerList = parseLayers(qp.get(LAYERS)[0]); if (!qp.get(STYLES)[0].isEmpty()) { styleList = qp.get(STYLES)[0].split(","); @@ -110,13 +114,24 @@ public GetMapParameters(Map queryParameters) throws WMSExcepti } if (qp.containsKey("PIXELSIZE")) { - pixelSize = Double.valueOf(qp.get("PIXELSIZE")[0]); + try{ + pixelSize = Double.valueOf(qp.get("PIXELSIZE")[0]); + } catch(NumberFormatException nfe){ + throw new WMSException("The pixel size must be a double value.", nfe); + } + } + if (pixelSize<=0){ + throw new WMSException("The pixel siz must be greater than 0."); } imageFormat = qp.get(FORMAT)[0]; if (qp.containsKey(TRANSPARENT)) { - transparent = Boolean.valueOf(qp.get(TRANSPARENT)[0]); + try { + transparent = Boolean.valueOf(qp.get(TRANSPARENT)[0]); + } catch (NumberFormatException nfe) { + throw new WMSException("The transparent parameter must be expressed with true or false terms.", nfe); + } } if (qp.containsKey(BGCOLOR)) { @@ -169,7 +184,7 @@ protected final int parseInteger(String s) throws WMSException{ /** * Parses s as an array of comma separated doubles, transforming the potential NumberFormatException in a WMSException - * We want a BBOx, so there shall be exactly four double values. + * We want a BBox, so there shall be exactly four double values. * @param s The input String * @return The parsed array of double values * @throws WMSException diff --git a/app/wms/src/test/java/org/orbisgis/server/wms/ParametersTest.java b/app/wms/src/test/java/org/orbisgis/server/wms/ParametersTest.java index 40861f5..83352f5 100644 --- a/app/wms/src/test/java/org/orbisgis/server/wms/ParametersTest.java +++ b/app/wms/src/test/java/org/orbisgis/server/wms/ParametersTest.java @@ -181,6 +181,13 @@ public void testInvalidWidth() throws Exception { map.put("WIDTH", new String[]{"potato"}); mapBuildFail(map); } + + @Test + public void testInvalidWidth2() throws Exception { + Map map= getMapMap(); + map.put("WIDTH", new String[]{"0"}); + mapBuildFail(map); + } @Test public void testInvalidHeight() throws Exception { @@ -188,6 +195,13 @@ public void testInvalidHeight() throws Exception { map.put("HEIGHT", new String[]{"potato"}); mapBuildFail(map); } + + @Test + public void testInvalidHeight2() throws Exception { + Map map= getMapMap(); + map.put("HEIGHT", new String[]{"0"}); + mapBuildFail(map); + } @Test public void testInvalidLayerList() throws Exception { diff --git a/app/wms/src/test/java/org/orbisgis/server/wms/WMSTest.java b/app/wms/src/test/java/org/orbisgis/server/wms/WMSTest.java index fa2b23d..f7e475f 100644 --- a/app/wms/src/test/java/org/orbisgis/server/wms/WMSTest.java +++ b/app/wms/src/test/java/org/orbisgis/server/wms/WMSTest.java @@ -117,7 +117,7 @@ public void testReprojection() throws Exception { h.put("LAYERS", new String[]{"cantons"}); h.put("STYLES", new String[]{""}); h.put("CRS", new String[]{toCRS}); - h.put("BBOX", new String[]{"-5.372757617915,9.326100042301633,41.3630420705024,51.089386147807105"}); + h.put("BBOX", new String[]{"-6.022502267873058,10.239619617329726,46.52667702045907,56.28550253867566"}); h.put("WIDTH", new String[]{"874"}); h.put("HEIGHT", new String[]{"593"}); h.put("FORMAT", new String[]{"image/png"}); @@ -238,12 +238,16 @@ public void testImageFormat() throws Exception { h.put("LAYERS", new String[]{"cantons"}); h.put("STYLES", new String[]{""}); h.put("CRS", new String[]{"EPSG:27582"}); - h.put("BBOX", new String[]{"2677441.0,1197822.0,1620431.0,47680.0"}); + h.put("BBOX", new String[]{"47680,1197822, 1620431,2677441"}); h.put("WIDTH", new String[]{"874"}); h.put("HEIGHT", new String[]{"593"}); h.put("FORMAT", new String[]{"image/png"}); + + //h.put("BBOX", new String[]{"47680,1620431,1197822,2677441"}); + //POLYGON ((47680 1620431, 47680 2677441, 1197822 2677441, 1197822 1620431, 47680 1620431)) - ByteArrayOutputStream out = new ByteArrayOutputStream(); + FileOutputStream out = new FileOutputStream(new File("target/testwms.png")); + //ByteArrayOutputStream out = new ByteArrayOutputStream(); wms.processRequests(h, out, r); assertEquals(200, r.responseCode); From 32bdaece9141e84626f1fcc576c5808ea57cf7db Mon Sep 17 00:00:00 2001 From: ebocher Date: Fri, 5 Jul 2013 09:49:58 +0200 Subject: [PATCH 2/4] Add new tests and check to improve wms server --- .../server/wms/GetFeatureInfoHandler.java | 13 +- .../orbisgis/server/wms/GetMapParameters.java | 15 +- .../server/wms/GetFeatureInfoHandlerTest.java | 3 +- .../java/org/orbisgis/server/wms/WMSTest.java | 534 ++++++++++-------- .../org/orbisgis/server/wms/polygons.sld | 26 + 5 files changed, 360 insertions(+), 231 deletions(-) create mode 100644 app/wms/src/test/resources/org/orbisgis/server/wms/polygons.sld diff --git a/app/wms/src/main/java/org/orbisgis/server/wms/GetFeatureInfoHandler.java b/app/wms/src/main/java/org/orbisgis/server/wms/GetFeatureInfoHandler.java index a990fe9..1b5415b 100644 --- a/app/wms/src/main/java/org/orbisgis/server/wms/GetFeatureInfoHandler.java +++ b/app/wms/src/main/java/org/orbisgis/server/wms/GetFeatureInfoHandler.java @@ -84,9 +84,20 @@ public void getFeatureInfo(GetFeatureInfoParameters params, OutputStream output, params.getSld(), params.getExceptionsFormat(), output, wmsResponse, serverStyles); int width = params.getWidth(); int height = params.getHeight(); + int i = params.getI(); + int j = params.getJ(); + + if(i<=0 || i>=width){ + throw new WMSException("GetFeatureInfo request contains invalid I value. "); + } + + if(j<=0 || j>=height){ + throw new WMSException("GetFeatureInfo request contains invalid J value. "); + } + double[] bBox = params.getbBox(); DataSourceFactory dsf = Services.getService(DataManager.class).getDataSourceFactory(); - Envelope env = getEnvelopeRequest(bBox,width, height, params.getI(), params.getJ()); + Envelope env = getEnvelopeRequest(bBox,width, height, i, j); ILayer[] children = layers.getChildren(); IndexManager im = dsf.getIndexManager(); for(ILayer c : children){ diff --git a/app/wms/src/main/java/org/orbisgis/server/wms/GetMapParameters.java b/app/wms/src/main/java/org/orbisgis/server/wms/GetMapParameters.java index 8871629..ebeb22e 100644 --- a/app/wms/src/main/java/org/orbisgis/server/wms/GetMapParameters.java +++ b/app/wms/src/main/java/org/orbisgis/server/wms/GetMapParameters.java @@ -28,7 +28,6 @@ */ package org.orbisgis.server.wms; -import com.vividsolutions.jts.geom.Envelope; import java.util.*; /** @@ -104,9 +103,10 @@ public GetMapParameters(Map queryParameters) throws WMSExcepti width = parseInteger(qp.get(WIDTH)[0]); height = parseInteger(qp.get(HEIGHT)[0]); if(width<=0 || height<=0){ - throw new WMSException("The width and height must be greater than 0."); + throw new WMSException("The width and the height must be greater than 0."); } - layerList = parseLayers(qp.get(LAYERS)[0]); + layerList = parseLayers(qp.get(LAYERS)[0]); + if (!qp.get(STYLES)[0].isEmpty()) { styleList = qp.get(STYLES)[0].split(","); } else { @@ -179,12 +179,15 @@ protected final int parseInteger(String s) throws WMSException{ } catch (NumberFormatException nfe){ throw new WMSException("The given int value is not valid: "+s, nfe); } - } + } /** - * Parses s as an array of comma separated doubles, transforming the potential NumberFormatException in a WMSException - * We want a BBox, so there shall be exactly four double values. + * Parses s as an array of comma separated doubles, transforming the potential + * NumberFormatException in a WMSException + * We want a BBox, so there shall be exactly four double values + * that represent minx, miny, maxx, maxy. + * * @param s The input String * @return The parsed array of double values * @throws WMSException diff --git a/app/wms/src/test/java/org/orbisgis/server/wms/GetFeatureInfoHandlerTest.java b/app/wms/src/test/java/org/orbisgis/server/wms/GetFeatureInfoHandlerTest.java index fbcf0f0..6e10413 100644 --- a/app/wms/src/test/java/org/orbisgis/server/wms/GetFeatureInfoHandlerTest.java +++ b/app/wms/src/test/java/org/orbisgis/server/wms/GetFeatureInfoHandlerTest.java @@ -48,5 +48,6 @@ public void testComputeEnvelope() throws Exception { //Upper left corner as geographic coordinate (20,50) System.out.println(envelope); assertTrue(envelope.equals(new Envelope(27,28,43,42))); - } + } + } diff --git a/app/wms/src/test/java/org/orbisgis/server/wms/WMSTest.java b/app/wms/src/test/java/org/orbisgis/server/wms/WMSTest.java index f7e475f..31c6bba 100644 --- a/app/wms/src/test/java/org/orbisgis/server/wms/WMSTest.java +++ b/app/wms/src/test/java/org/orbisgis/server/wms/WMSTest.java @@ -58,236 +58,324 @@ */ public class WMSTest { - private File f; - private File fshp; - private File fshx; - private File fdbf; - private File fprj; - WMS wms = new WMS(); - - /** - * - * @throws Exception - */ - @Before - public void setUp() throws Exception { - f = File.createTempFile("wms", null); - f.delete(); - CoreWorkspace c = new CoreWorkspace(); - c.setWorkspaceFolder(f.getAbsolutePath()); - WMSProperties props = new WMSProperties(); - props.putProperty(WMSProperties.TITLE,"test"); - wms.init(c, Collections.emptyMap(), Collections.emptyMap(), props); - - fshp = File.createTempFile("gdms", ".shp"); - fshp.delete(); - FileUtils.copy(WMSTest.class.getResourceAsStream("cantons.shp"), fshp); - String name = FileUtils.getFileNameWithoutExtensionU(fshp); - fdbf = new File(fshp.getParentFile(), name + ".dbf"); - FileUtils.copy(WMSTest.class.getResourceAsStream("cantons.dbf"), fdbf); - fprj = new File(fshp.getParentFile(), name + ".prj"); - FileUtils.copy(WMSTest.class.getResourceAsStream("cantons.prj"), fprj); - fshx = new File(fshp.getParentFile(), name + ".shx"); - FileUtils.copy(WMSTest.class.getResourceAsStream("cantons.shx"), fshx); - - SourceManager sm = Services.getService(DataManager.class).getSourceManager(); - sm.register("cantons", fshp); + private File f; + private File fshp; + private File fshx; + private File fdbf; + private File fprj; + WMS wms = new WMS(); + + /** + * + * @throws Exception + */ + @Before + public void setUp() throws Exception { + f = File.createTempFile("wms", null); + f.delete(); + CoreWorkspace c = new CoreWorkspace(); + c.setWorkspaceFolder(f.getAbsolutePath()); + WMSProperties props = new WMSProperties(); + props.putProperty(WMSProperties.TITLE, "test"); + wms.init(c, Collections.emptyMap(), Collections.emptyMap(), props); + + fshp = File.createTempFile("gdms", ".shp"); + fshp.delete(); + FileUtils.copy(WMSTest.class.getResourceAsStream("cantons.shp"), fshp); + String name = FileUtils.getFileNameWithoutExtensionU(fshp); + fdbf = new File(fshp.getParentFile(), name + ".dbf"); + FileUtils.copy(WMSTest.class.getResourceAsStream("cantons.dbf"), fdbf); + fprj = new File(fshp.getParentFile(), name + ".prj"); + FileUtils.copy(WMSTest.class.getResourceAsStream("cantons.prj"), fprj); + fshx = new File(fshp.getParentFile(), name + ".shx"); + FileUtils.copy(WMSTest.class.getResourceAsStream("cantons.shx"), fshx); + + SourceManager sm = Services.getService(DataManager.class).getSourceManager(); + sm.register("cantons", fshp); + } + + /** + * + */ + @After + public void tearDown() { + wms.destroy(); + FileUtils.deleteDir(f); + FileUtils.deleteDir(fshp); + FileUtils.deleteDir(fdbf); + FileUtils.deleteDir(fshx); + FileUtils.deleteDir(fprj); + } + + @Test + public void testReprojection4326() throws Exception { + DummyResponse r = new DummyResponse("http://localhost:9000/wms/wms"); + HashMap h = new HashMap(); + final String toCRS = "EPSG:4326"; + h.put("REQUEST", new String[]{"GetMap"}); + h.put("SERVICE", new String[]{"WMS"}); + h.put("LAYERS", new String[]{"cantons"}); + h.put("STYLES", new String[]{""}); + h.put("CRS", new String[]{toCRS}); + h.put("BBOX", new String[]{"-6.022502267873058,46.52667702045907,10.239619617329726,56.28550253867566"}); + h.put("WIDTH", new String[]{"874"}); + h.put("HEIGHT", new String[]{"593"}); + h.put("FORMAT", new String[]{"image/png"}); + h.put("VERSION", new String[]{"1.3.0"}); + h.put("TRANSPARENT", new String[]{"TRUE"}); + // Get the original source + DataSource source = wms.getContext().getDataManager().getDataSource("cantons"); + source.open(); + Value geom; + try { + int geomIndex = MetadataUtilities.getGeometryFieldIndex(source.getMetadata()); + geom = source.getFieldValue(0, geomIndex); + } finally { + source.close(); } - - /** - * - */ - @After - public void tearDown() { - wms.destroy(); - FileUtils.deleteDir(f); - FileUtils.deleteDir(fshp); - FileUtils.deleteDir(fdbf); - FileUtils.deleteDir(fshx); - FileUtils.deleteDir(fprj); + FileOutputStream fileOutputStream = new FileOutputStream(new File("target/testReprojection4326.png")); + wms.processRequests(h, fileOutputStream, r); + // Get the projection source name + String sourceName = GetMapHandler.getProjectionSourceName("cantons", toCRS); + DataSource projSource = wms.getContext().getDataManager().getDataSource(sourceName); + projSource.open(); + try { + int geomIndex = MetadataUtilities.getGeometryFieldIndex(projSource.getMetadata()); + Geometry projGeom = projSource.getFieldValue(0, geomIndex).getAsGeometry(); + ST_Transform transformFunction = new ST_Transform(); + Value res = transformFunction.evaluate(wms.getContext().getDataSourceFactory(), geom, ValueFactory.createValue(toCRS)); + assertTrue(res.getAsGeometry().equals(projGeom)); + } finally { + projSource.close(); } - - @Test - public void testReprojection() throws Exception { - DummyResponse r = new DummyResponse("http://localhost:9000/wms/wms"); - HashMap h = new HashMap(); - final String toCRS = "EPSG:4326"; - h.put("REQUEST", new String[]{"GetMap"}); - h.put("SERVICE", new String[]{"WMS"}); - h.put("LAYERS", new String[]{"cantons"}); - h.put("STYLES", new String[]{""}); - h.put("CRS", new String[]{toCRS}); - h.put("BBOX", new String[]{"-6.022502267873058,10.239619617329726,46.52667702045907,56.28550253867566"}); - h.put("WIDTH", new String[]{"874"}); - h.put("HEIGHT", new String[]{"593"}); - h.put("FORMAT", new String[]{"image/png"}); - h.put("VERSION", new String[]{"1.3.0"}); - h.put("TRANSPARENT", new String[]{"TRUE"}); - // Get the original source - DataSource source = wms.getContext().getDataManager().getDataSource("cantons"); - source.open(); - Value geom; - try { - int geomIndex = MetadataUtilities.getGeometryFieldIndex(source.getMetadata()); - geom = source.getFieldValue(0, geomIndex); - } finally { - source.close(); - } - FileOutputStream fileOutputStream = new FileOutputStream(new File("target/testReprojection.png")); - wms.processRequests(h, fileOutputStream, r); - // Get the projection source name - String sourceName = GetMapHandler.getProjectionSourceName("cantons",toCRS); - DataSource projSource = wms.getContext().getDataManager().getDataSource(sourceName); - projSource.open(); - try { - int geomIndex = MetadataUtilities.getGeometryFieldIndex(projSource.getMetadata()); - Geometry projGeom = projSource.getFieldValue(0,geomIndex).getAsGeometry(); - ST_Transform transformFunction = new ST_Transform(); - Value res = transformFunction.evaluate(wms.getContext().getDataSourceFactory(),geom, ValueFactory.createValue(toCRS)); - assertTrue(res.getAsGeometry().equals(projGeom)); - } finally { - projSource.close(); - } - + } + + @Test + public void testReprojection3857() throws Exception { + DummyResponse r = new DummyResponse("http://localhost:9000/wms/wms"); + HashMap h = new HashMap(); + final String toCRS = "EPSG:3857"; + h.put("REQUEST", new String[]{"GetMap"}); + h.put("SERVICE", new String[]{"WMS"}); + h.put("LAYERS", new String[]{"cantons"}); + h.put("STYLES", new String[]{""}); + h.put("CRS", new String[]{toCRS}); + h.put("BBOX", new String[]{"-670421.885760964,5865155.04961359,1139869.24171796,7615462.350663964"}); + h.put("WIDTH", new String[]{"874"}); + h.put("HEIGHT", new String[]{"593"}); + h.put("FORMAT", new String[]{"image/png"}); + h.put("VERSION", new String[]{"1.3.0"}); + h.put("TRANSPARENT", new String[]{"TRUE"}); + // Get the original source + DataSource source = wms.getContext().getDataManager().getDataSource("cantons"); + source.open(); + Value geom; + try { + int geomIndex = MetadataUtilities.getGeometryFieldIndex(source.getMetadata()); + geom = source.getFieldValue(0, geomIndex); + } finally { + source.close(); } - /** - * Checks the error response for any missing parameter; - * - * @throws Exception - */ - @Test - public void testParameterErrors() throws Exception { - DummyResponse r = new DummyResponse("http://localhost:9000/wms/wms"); - HashMap h = new HashMap(); - - h.put("REQUEST", new String[]{"GetMap"}); - h.put("SERVICE", new String[]{"WMS"}); - h.put("LAYERS", new String[]{"cantons"}); - h.put("STYLES", new String[]{""}); - h.put("CRS", new String[]{"EPSG:27582"}); - h.put("BBOX", new String[]{"2677441.0,1197822.0,1620431.0,47680.0"}); - h.put("WIDTH", new String[]{"874"}); - h.put("HEIGHT", new String[]{"593"}); - h.put("FORMAT", new String[]{"image/png"}); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - - wms.processRequests(h, out, r); - assertEquals(400, r.responseCode); - assertEquals("text/xml;charset=UTF-8", r.contentType); - h.put("VERSION", new String[]{"1.3.0"}); - - h.remove("REQUEST"); - wms.processRequests(h, out, r); - assertEquals(400, r.responseCode); - assertEquals("text/xml;charset=UTF-8", r.contentType); - h.put("REQUEST", new String[]{"GetMap"}); - - h.remove("SERVICE"); - wms.processRequests(h, out, r); - assertEquals(400, r.responseCode); - assertEquals("text/xml;charset=UTF-8", r.contentType); - h.put("SERVICE", new String[]{"WMS"}); - - h.remove("CRS"); - wms.processRequests(h, out, r); - assertEquals(400, r.responseCode); - assertEquals("text/xml;charset=UTF-8", r.contentType); - h.put("CRS", new String[]{"EPSG:27582"}); - - h.remove("BBOX"); - wms.processRequests(h, out, r); - assertEquals(400, r.responseCode); - assertEquals("text/xml;charset=UTF-8", r.contentType); - h.put("BBOX", new String[]{"2677441.0,1197822.0,1620431.0,47680.0"}); - - h.remove("WIDTH"); - wms.processRequests(h, out, r); - assertEquals(400, r.responseCode); - assertEquals("text/xml;charset=UTF-8", r.contentType); - h.put("WIDTH", new String[]{"874"}); - - h.remove("HEIGHT"); - wms.processRequests(h, out, r); - assertEquals(400, r.responseCode); - assertEquals("text/xml;charset=UTF-8", r.contentType); - h.put("HEIGHT", new String[]{"593"}); - - h.remove("FORMAT"); - wms.processRequests(h, out, r); - assertEquals(400, r.responseCode); - assertEquals("text/xml;charset=UTF-8", r.contentType); + FileOutputStream fileOutputStream = new FileOutputStream(new File("target/testReprojection3857.png")); + wms.processRequests(h, fileOutputStream, r); + // Get the projection source name + String sourceName = GetMapHandler.getProjectionSourceName("cantons", toCRS); + DataSource projSource = wms.getContext().getDataManager().getDataSource(sourceName); + projSource.open(); + try { + int geomIndex = MetadataUtilities.getGeometryFieldIndex(projSource.getMetadata()); + Geometry projGeom = projSource.getFieldValue(0, geomIndex).getAsGeometry(); + ST_Transform transformFunction = new ST_Transform(); + Value res = transformFunction.evaluate(wms.getContext().getDataSourceFactory(), geom, ValueFactory.createValue(toCRS)); + assertTrue(res.getAsGeometry().equals(projGeom)); + } finally { + projSource.close(); } - - /** - * Checking the ability to display a map in any supported output format - * - * @throws Exception - */ - @Test - public void testImageFormat() throws Exception { - DummyResponse r = new DummyResponse("http://localhost:9000/wms/wms"); - HashMap h = new HashMap(); - - DataSourceFactory dsf = Services.getService(DataManager.class).getDataSourceFactory(); - dsf.getDataSource("cantons").open(); - - h.put("REQUEST", new String[]{"GetMap"}); - h.put("VERSION", new String[]{"1.3.0"}); - h.put("SERVICE", new String[]{"WMS"}); - h.put("LAYERS", new String[]{"cantons"}); - h.put("STYLES", new String[]{""}); - h.put("CRS", new String[]{"EPSG:27582"}); - h.put("BBOX", new String[]{"47680,1197822, 1620431,2677441"}); - h.put("WIDTH", new String[]{"874"}); - h.put("HEIGHT", new String[]{"593"}); - h.put("FORMAT", new String[]{"image/png"}); - - //h.put("BBOX", new String[]{"47680,1620431,1197822,2677441"}); - //POLYGON ((47680 1620431, 47680 2677441, 1197822 2677441, 1197822 1620431, 47680 1620431)) - - FileOutputStream out = new FileOutputStream(new File("target/testwms.png")); - //ByteArrayOutputStream out = new ByteArrayOutputStream(); - - wms.processRequests(h, out, r); - assertEquals(200, r.responseCode); - assertEquals("image/png", r.contentType); - - h.put("FORMAT", new String[]{"image/jpeg"}); - wms.processRequests(h, out, r); - assertEquals(200, r.responseCode); - assertEquals("image/jpeg", r.contentType); - - h.put("FORMAT", new String[]{"image/tiff"}); - wms.processRequests(h, out, r); - assertEquals(200, r.responseCode); - assertEquals("image/tiff", r.contentType); - + } + + /** + * Checks the error response for any missing parameter; + * + * @throws Exception + */ + @Test + public void testParameterErrors() throws Exception { + DummyResponse r = new DummyResponse("http://localhost:9000/wms/wms"); + HashMap h = new HashMap(); + + h.put("REQUEST", new String[]{"GetMap"}); + h.put("SERVICE", new String[]{"WMS"}); + h.put("LAYERS", new String[]{"cantons"}); + h.put("STYLES", new String[]{""}); + h.put("CRS", new String[]{"EPSG:27582"}); + h.put("BBOX", new String[]{"2677441.0,1197822.0,1620431.0,47680.0"}); + h.put("WIDTH", new String[]{"874"}); + h.put("HEIGHT", new String[]{"593"}); + h.put("FORMAT", new String[]{"image/png"}); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + wms.processRequests(h, out, r); + assertEquals(400, r.responseCode); + assertEquals("text/xml;charset=UTF-8", r.contentType); + h.put("VERSION", new String[]{"1.3.0"}); + + h.remove("REQUEST"); + wms.processRequests(h, out, r); + assertEquals(400, r.responseCode); + assertEquals("text/xml;charset=UTF-8", r.contentType); + h.put("REQUEST", new String[]{"GetMap"}); + + h.remove("SERVICE"); + wms.processRequests(h, out, r); + assertEquals(400, r.responseCode); + assertEquals("text/xml;charset=UTF-8", r.contentType); + h.put("SERVICE", new String[]{"WMS"}); + + h.remove("CRS"); + wms.processRequests(h, out, r); + assertEquals(400, r.responseCode); + assertEquals("text/xml;charset=UTF-8", r.contentType); + h.put("CRS", new String[]{"EPSG:27572"}); + + h.remove("BBOX"); + wms.processRequests(h, out, r); + assertEquals(400, r.responseCode); + assertEquals("text/xml;charset=UTF-8", r.contentType); + h.put("BBOX", new String[]{"2677441.0,1197822.0,1620431.0,47680.0"}); + + h.remove("WIDTH"); + wms.processRequests(h, out, r); + assertEquals(400, r.responseCode); + assertEquals("text/xml;charset=UTF-8", r.contentType); + h.put("WIDTH", new String[]{"874"}); + + h.remove("HEIGHT"); + wms.processRequests(h, out, r); + assertEquals(400, r.responseCode); + assertEquals("text/xml;charset=UTF-8", r.contentType); + h.put("HEIGHT", new String[]{"593"}); + + h.remove("FORMAT"); + wms.processRequests(h, out, r); + assertEquals(400, r.responseCode); + assertEquals("text/xml;charset=UTF-8", r.contentType); + } + + /** + * Checking the ability to display a map in any supported output format + * + * @throws Exception + */ + @Test + public void testImageFormat() throws Exception { + DummyResponse r = new DummyResponse("http://localhost:9000/wms/wms"); + HashMap h = new HashMap(); + + DataSourceFactory dsf = Services.getService(DataManager.class).getDataSourceFactory(); + dsf.getDataSource("cantons").open(); + + h.put("REQUEST", new String[]{"GetMap"}); + h.put("VERSION", new String[]{"1.3.0"}); + h.put("SERVICE", new String[]{"WMS"}); + h.put("LAYERS", new String[]{"cantons"}); + h.put("STYLES", new String[]{""}); + h.put("CRS", new String[]{"EPSG:27572"}); + h.put("BBOX", new String[]{"47680,1620431, 1197822,2677441"}); + h.put("WIDTH", new String[]{"874"}); + h.put("HEIGHT", new String[]{"593"}); + h.put("FORMAT", new String[]{"image/png"}); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + wms.processRequests(h, out, r); + assertEquals(200, r.responseCode); + assertEquals("image/png", r.contentType); + + h.put("FORMAT", new String[]{"image/jpeg"}); + wms.processRequests(h, out, r); + assertEquals(200, r.responseCode); + assertEquals("image/jpeg", r.contentType); + + h.put("FORMAT", new String[]{"image/tiff"}); + wms.processRequests(h, out, r); + assertEquals(200, r.responseCode); + assertEquals("image/tiff", r.contentType); + + } + + @Test + public void testGetFeatureInfoText() throws Exception { + DummyResponse r = new DummyResponse("http://localhost:9000/wms/wms"); + HashMap h = new HashMap(); + + DataSourceFactory dsf = Services.getService(DataManager.class).getDataSourceFactory(); + dsf.getDataSource("cantons").open(); + + h.put("REQUEST", new String[]{"GetFeatureInfo"}); + h.put("VERSION", new String[]{"1.3.0"}); + h.put("SERVICE", new String[]{"WMS"}); + h.put("LAYERS", new String[]{"cantons"}); + h.put("STYLES", new String[]{""}); + h.put("CRS", new String[]{"EPSG:27572"}); + h.put("BBOX", new String[]{"270213.9595191681,2293124.7156245164, 309188.95846363576,2257614.1610306683"}); + h.put("FORMAT", new String[]{"image/png"}); + h.put("WIDTH", new String[]{"874"}); + h.put("HEIGHT", new String[]{"593"}); + h.put("QUERY_LAYERS", new String[]{"cantons"}); + h.put("INFO_FORMAT", new String[]{"text/plain"}); + h.put("FEATURE_COUNT", new String[]{"10"}); + h.put("I", new String[]{"10"}); + h.put("J", new String[]{"12"}); + FileOutputStream out = new FileOutputStream(new File("target/featureinfo.txt")); + wms.processRequests(h, out, r); + } + + @Test + public void testGetMapWithSLD() throws Exception { + DummyResponse r = new DummyResponse("http://localhost:9000/wms/wms"); + HashMap h = new HashMap(); + + DataSourceFactory dsf = Services.getService(DataManager.class).getDataSourceFactory(); + dsf.getDataSource("cantons").open(); + + h.put("REQUEST", new String[]{"GetMap"}); + h.put("VERSION", new String[]{"1.3.0"}); + h.put("SERVICE", new String[]{"WMS"}); + h.put("CRS", new String[]{"EPSG:27582"}); + h.put("BBOX", new String[]{"270213.9595191681,2293124.7156245164, 309188.95846363576,2257614.1610306683"}); + h.put("FORMAT", new String[]{"image/png"}); + h.put("WIDTH", new String[]{"874"}); + h.put("HEIGHT", new String[]{"593"}); + h.put("SLD", new String[]{"/src/test/resources/org/orbisgis/server/wms/polygons.sld"}); + FileOutputStream out = new FileOutputStream(new File("target/sld.png")); + wms.processRequests(h, out, r); + + } + + private static class DummyResponse implements WMSResponse { + + private String contentType; + private String requestUrl; + private int responseCode; + + DummyResponse(String requestUrl) { + this.requestUrl = requestUrl; } - private static class DummyResponse implements WMSResponse { - - private String contentType; - private String requestUrl; - private int responseCode; - - DummyResponse(String requestUrl) { - this.requestUrl = requestUrl; - } - - @Override - public void setContentType(String contentType) { - this.contentType = contentType; - } + @Override + public void setContentType(String contentType) { + this.contentType = contentType; + } - @Override - public String getRequestUrl() { - return requestUrl; - } + @Override + public String getRequestUrl() { + return requestUrl; + } - @Override - public void setResponseCode(int code) { - responseCode = code; - } + @Override + public void setResponseCode(int code) { + responseCode = code; } + } } diff --git a/app/wms/src/test/resources/org/orbisgis/server/wms/polygons.sld b/app/wms/src/test/resources/org/orbisgis/server/wms/polygons.sld new file mode 100644 index 0000000..f4fa587 --- /dev/null +++ b/app/wms/src/test/resources/org/orbisgis/server/wms/polygons.sld @@ -0,0 +1,26 @@ + + + + cantons + + + + + Area symbolizer + + #0000ee + 0.6 + + + + #000000 + 1.0 + + 0.1 + + + + + + + From 57324326b3ef8caf2ed75100c5dd664aa4d00273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Gu=C3=A9ganno?= Date: Fri, 5 Jul 2013 14:13:38 +0200 Subject: [PATCH 3/4] iRelated to issue #14. I perform more accurate tests to define if an input request is valid ot not. I also fix two potential NPEs introduced while reworking the code. Finally, I fix the sample SLD as it is not valid. --- .../orbisgis/server/wms/GetMapParameters.java | 75 ++++++++++++++++--- .../orbisgis/server/wms/ParametersTest.java | 17 +++++ .../java/org/orbisgis/server/wms/WMSTest.java | 11 ++- .../org/orbisgis/server/wms/polygons.sld | 4 +- 4 files changed, 94 insertions(+), 13 deletions(-) diff --git a/app/wms/src/main/java/org/orbisgis/server/wms/GetMapParameters.java b/app/wms/src/main/java/org/orbisgis/server/wms/GetMapParameters.java index ebeb22e..460e761 100644 --- a/app/wms/src/main/java/org/orbisgis/server/wms/GetMapParameters.java +++ b/app/wms/src/main/java/org/orbisgis/server/wms/GetMapParameters.java @@ -35,33 +35,76 @@ * @author Alexis Guéganno. */ public class GetMapParameters { + /** + * Mandatory + */ public static final String VERSION = "VERSION"; + /** + * Mandatory + */ public static final String REQUEST = "REQUEST"; + /** + * Shall be present if SLD is absent, and absent if SLD is present + */ public static final String LAYERS = "LAYERS"; + /** + * Shall be present if SLD is absent, and absent if SLD is present + */ public static final String STYLES = "STYLES"; + /** + * Shall be present if STYLES and LAYERS are absent, and absent if STYLES and LAYERS are present + */ public static final String SLD = "SLD"; + /** + * Mandatory + */ public static final String CRS = "CRS"; + /** + * Mandatory + */ public static final String BBOX = "BBOX"; + /** + * Mandatory + */ public static final String WIDTH = "WIDTH"; + /** + * Mandatory + */ public static final String HEIGHT = "HEIGHT"; + /** + * Mandatory + */ public static final String FORMAT = "FORMAT"; + /** + * Optional + */ public static final String TRANSPARENT = "TRANSPARENT"; + /** + * Optional + */ public static final String BGCOLOR = "BGCOLOR"; + /** + * Optional + */ public static final String EXCEPTIONS = "EXCEPTIONS"; + /** + * Optional + */ public static final String TIME = "TIME"; public static final String ELEVATION = "ELEVATION"; public static final Set MANDATORY_PARAMETERS; static { Set temp = new HashSet(); - temp.add(LAYERS); - temp.add(STYLES); temp.add(BBOX); temp.add(WIDTH); temp.add(HEIGHT); temp.add(FORMAT); temp.add(CRS); MANDATORY_PARAMETERS = Collections.unmodifiableSet(temp); + for(String s : MANDATORY_PARAMETERS){ + System.out.println("Mandatory : "+s); + } } @@ -97,6 +140,15 @@ public GetMapParameters(Map queryParameters) throws WMSExcepti throw new WMSException("The following parameter is mandatory: "+s); } } + if(!qp.containsKey(SLD)){ + if(!qp.containsKey(LAYERS) || !qp.containsKey(STYLES)){ + throw new WMSException("Both layers and styles must be defined when SLD is absent"); + } + } else { + if(qp.containsKey(LAYERS) || qp.containsKey(STYLES)){ + throw new WMSException("Both layers and styles must not be defined when SLD is present"); + } + } crs = qp.get(CRS)[0]; bBox = parseBBox(qp.get(BBOX)[0]); @@ -105,12 +157,15 @@ public GetMapParameters(Map queryParameters) throws WMSExcepti if(width<=0 || height<=0){ throw new WMSException("The width and the height must be greater than 0."); } - layerList = parseLayers(qp.get(LAYERS)[0]); - - if (!qp.get(STYLES)[0].isEmpty()) { - styleList = qp.get(STYLES)[0].split(","); - } else { - styleList = new String[0]; + if(qp.containsKey(LAYERS)){ + layerList = parseLayers(qp.get(LAYERS)[0]); + } + if(qp.containsKey(STYLES)){ + if (!qp.get(STYLES)[0].isEmpty()) { + styleList = qp.get(STYLES)[0].split(","); + } else { + styleList = new String[0]; + } } if (qp.containsKey("PIXELSIZE")) { @@ -213,7 +268,7 @@ private double[] parseBBox(String s) throws WMSException{ * @return A copy of the array of layers that must be queried */ public String[] getLayerList() { - return Arrays.copyOf(layerList, layerList.length); + return layerList != null ? Arrays.copyOf(layerList, layerList.length) : new String[0]; } /** @@ -221,7 +276,7 @@ public String[] getLayerList() { * @return A copy of the array of style used to draw the layers that must be queried */ public String[] getStyleList() { - return Arrays.copyOf(styleList, styleList.length); + return styleList!= null ? Arrays.copyOf(styleList, styleList.length) : new String[0]; } /** diff --git a/app/wms/src/test/java/org/orbisgis/server/wms/ParametersTest.java b/app/wms/src/test/java/org/orbisgis/server/wms/ParametersTest.java index 83352f5..873860b 100644 --- a/app/wms/src/test/java/org/orbisgis/server/wms/ParametersTest.java +++ b/app/wms/src/test/java/org/orbisgis/server/wms/ParametersTest.java @@ -213,6 +213,23 @@ public void testInvalidLayerList() throws Exception { mapBuildFail(map); } + @Test + public void testMapSLDAndStyles(){ + Map map= getMapMap(); + map.put(GetMapParameters.SLD,new String[]{"sld"}); + mapBuildFail(map); + } + + @Test + public void testMapSLDAlone() throws Exception { + Map map= getMapMap(); + map.put(GetMapParameters.SLD,new String[]{"sld"}); + map.remove(GetMapParameters.LAYERS); + map.remove(GetMapParameters.STYLES); + GetMapParameters params = new GetMapParameters(map); + assertTrue(params.getSld().equals("sld")); + } + private void missingMapParameter(String param){ Map map= getMapMap(); map.remove(param); diff --git a/app/wms/src/test/java/org/orbisgis/server/wms/WMSTest.java b/app/wms/src/test/java/org/orbisgis/server/wms/WMSTest.java index 31c6bba..8433dc7 100644 --- a/app/wms/src/test/java/org/orbisgis/server/wms/WMSTest.java +++ b/app/wms/src/test/java/org/orbisgis/server/wms/WMSTest.java @@ -329,6 +329,7 @@ public void testGetFeatureInfoText() throws Exception { h.put("J", new String[]{"12"}); FileOutputStream out = new FileOutputStream(new File("target/featureinfo.txt")); wms.processRequests(h, out, r); + assertTrue(r.getResponseCode() == 200); } @Test @@ -347,9 +348,13 @@ public void testGetMapWithSLD() throws Exception { h.put("FORMAT", new String[]{"image/png"}); h.put("WIDTH", new String[]{"874"}); h.put("HEIGHT", new String[]{"593"}); - h.put("SLD", new String[]{"/src/test/resources/org/orbisgis/server/wms/polygons.sld"}); + String location = "file://"+System.getProperty("user.dir")+"/src/test/resources/org/orbisgis/server/wms/polygons.sld"; + System.out.println("Location is"+location); + h.put("SLD", new String[]{location}); FileOutputStream out = new FileOutputStream(new File("target/sld.png")); wms.processRequests(h, out, r); + System.out.println("Response code is "+r.getResponseCode()); + assertTrue(r.getResponseCode() == 200); } @@ -377,5 +382,9 @@ public String getRequestUrl() { public void setResponseCode(int code) { responseCode = code; } + + public int getResponseCode(){ + return responseCode; + } } } diff --git a/app/wms/src/test/resources/org/orbisgis/server/wms/polygons.sld b/app/wms/src/test/resources/org/orbisgis/server/wms/polygons.sld index f4fa587..87df341 100644 --- a/app/wms/src/test/resources/org/orbisgis/server/wms/polygons.sld +++ b/app/wms/src/test/resources/org/orbisgis/server/wms/polygons.sld @@ -1,6 +1,6 @@ - + cantons @@ -22,5 +22,5 @@ - + From ea7d51db80046427429cd81b074f72ee74e67b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Gu=C3=A9ganno?= Date: Fri, 5 Jul 2013 15:08:55 +0200 Subject: [PATCH 4/4] increases the version in order not to face caching issues... --- app/wms/pom.xml | 2 +- project/Build.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/wms/pom.xml b/app/wms/pom.xml index 7b388ae..4089ec6 100644 --- a/app/wms/pom.xml +++ b/app/wms/pom.xml @@ -6,7 +6,7 @@ org.orbisgis.server orbiswms-lib jar - 1.0-SNAPSHOT + 1.0.1-SNAPSHOT orbiswms-lib http://www.orbisgis.org diff --git a/project/Build.scala b/project/Build.scala index aaaa77c..a0c3915 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -8,7 +8,7 @@ object ApplicationBuild extends Build { val appVersion = "1.0-SNAPSHOT" val appDependencies = Seq( - "org.orbisgis.server" % "orbiswms-lib" % "1.0-SNAPSHOT" changing(), + "org.orbisgis.server" % "orbiswms-lib" % "1.0.1-SNAPSHOT" changing(), "com.sun.xml.bind" % "jaxb-impl" % "2.2.2", "com.kitfox.svg" % "svg-salamander" % "1.0" from "http://repo.orbisgis.org/com/kitfox/svg/svg-salamander/1.0/svg-salamander-1.0.jar", "javax.media" % "jai_core" % "1.1.3" from "http://repo.orbisgis.org/javax/media/jai_core/1.1.3/jai_core-1.1.3.jar",