/** Calls through to {@link #write(FeatureCollectionResponse, OutputStream, Operation)}. */ public void write(Object value, OutputStream output, Operation operation) throws IOException, ServiceException { // for WFS 2.0 we changed the input object type to be the request object adapter, but there // is other code (like WMS GetFeatureInfo) that passes in the old objects, so do a check if (value instanceof FeatureCollectionResponse) { write((FeatureCollectionResponse) value, output, operation); } else { write(FeatureCollectionResponse.adapt(value), output, operation); } }
@Override public FeatureCollectionResponse create() { return FeatureCollectionResponse.adapt( ((WfsFactory) getFactory()).createFeatureCollectionType()); }
@Override public FeatureCollectionResponse create() { return FeatureCollectionResponse.adapt( ((Wfs20Factory) getFactory()).createFeatureCollectionType()); }
/** * Saves the feature source contents into a zipped shapefile, returns the output as a byte array */ byte[] writeOut(FeatureCollection fc) throws IOException { ShapeZipOutputFormat zip = new ShapeZipOutputFormat(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(fc); zip.write(fct, bos, op); return bos.toByteArray(); }
@Test public void testSingle() throws Exception { FeatureSource<? extends FeatureType, ? extends Feature> source = getFeatureSource(MockData.SEVEN); FeatureCollection<? extends FeatureType, ? extends Feature> features = source.getFeatures(); FeatureCollectionResponse fcType = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fcType.getFeature().add(features); ByteArrayOutputStream output = new ByteArrayOutputStream(); producer().write(fcType, output, request(MockData.SEVEN)); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = docBuilder.parse(new ByteArrayInputStream(output.toByteArray())); assertEquals(7, document.getElementsByTagName("cdf:Seven").getLength()); }
@Test public void testMultipleSameNamespace() throws Exception { FeatureCollectionResponse fcType = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fcType.getFeature().add(getFeatureSource(MockData.SEVEN).getFeatures()); fcType.getFeature().add(getFeatureSource(MockData.FIFTEEN).getFeatures()); ByteArrayOutputStream output = new ByteArrayOutputStream(); producer().write(fcType, output, request(MockData.SEVEN, MockData.FIFTEEN)); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = docBuilder.parse(new ByteArrayInputStream(output.toByteArray())); assertEquals( 7 + 15, document.getElementsByTagName("cdf:Seven").getLength() + document.getElementsByTagName("cdf:Fifteen").getLength()); }
@Test public void testCharset() throws Exception { FeatureSource<? extends FeatureType, ? extends Feature> fs; fs = getFeatureSource(SystemTestData.BASIC_POLYGONS); ShapeZipOutputFormat zip = new ShapeZipOutputFormat(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(fs.getFeatures()); // add the charset Map options = new HashMap(); options.put("CHARSET", Charset.forName("ISO-8859-15")); gft.setFormatOptions(options); zip.write(fct, bos, op); checkShapefileIntegrity( new String[] {"BasicPolygons"}, new ByteArrayInputStream(bos.toByteArray())); assertEquals("ISO-8859-15", getCharset(new ByteArrayInputStream(bos.toByteArray()))); }
/** * Saves the feature source contents into a zipped shapefile, returns the output as a byte array */ byte[] writeOut(FeatureCollection fc, long maxShpSize, long maxDbfSize) throws IOException { ShapeZipOutputFormat zip = new ShapeZipOutputFormat(); zip.setMaxDbfSize(maxDbfSize); zip.setMaxShpSize(maxShpSize); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(fc); zip.write(fct, bos, op); return bos.toByteArray(); }
@Test public void testMultipleDifferentNamespace() throws Exception { FeatureCollectionResponse fcType = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fcType.getFeature().add(getFeatureSource(MockData.SEVEN).getFeatures()); fcType.getFeature().add(getFeatureSource(MockData.POLYGONS).getFeatures()); int npolys = getFeatureSource(MockData.POLYGONS).getFeatures().size(); ByteArrayOutputStream output = new ByteArrayOutputStream(); producer().write(fcType, output, request(MockData.SEVEN, MockData.POLYGONS)); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = docBuilder.parse(new ByteArrayInputStream(output.toByteArray())); assertEquals( 7 + npolys, document.getElementsByTagName("cdf:Seven").getLength() + document.getElementsByTagName("cgf:Polygons").getLength()); } }
@Test public void testESRIFormat() throws Exception { setupESRIPropertyFile(); FeatureSource<? extends FeatureType, ? extends Feature> fs; fs = getFeatureSource(SystemTestData.BASIC_POLYGONS); ShapeZipOutputFormat zip = new ShapeZipOutputFormat(getGeoServer(), getCatalog(), getResourceLoader()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(fs.getFeatures()); // add the charset Map options = new HashMap(); options.put("PRJFILEFORMAT", "ESRI"); gft.setFormatOptions(options); zip.write(fct, bos, op); byte[] byteArrayZip = bos.toByteArray(); checkShapefileIntegrity( new String[] {"BasicPolygons"}, new ByteArrayInputStream(byteArrayZip)); checkFileContent( "BasicPolygons.prj", new ByteArrayInputStream(byteArrayZip), get4326_ESRI_WKTContent()); }
@Test public void testTemplateSingleType() throws Exception { // copy the new template to the data dir WorkspaceInfo ws = getCatalog().getWorkspaceByName(SystemTestData.BASIC_POLYGONS.getPrefix()); getDataDirectory() .copyToWorkspaceDir( ws, getClass().getResourceAsStream("shapeziptest.ftl"), "shapezip.ftl"); // setup the request params SimpleFeatureCollection fc = getFeatureSource(SystemTestData.BASIC_POLYGONS).getFeatures(Filter.INCLUDE); ShapeZipOutputFormat zip = new ShapeZipOutputFormat(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(fc); // get the file name assertEquals("shapezip_BasicPolygons.zip", zip.getAttachmentFileName(fct, op)); // check the contents zip.write(fct, bos, op); byte[] zipBytes = bos.toByteArray(); checkShapefileIntegrity( new String[] {"theshape_BasicPolygons"}, new ByteArrayInputStream(zipBytes)); }
@Test public void testTemplateMultiGeomType() throws Exception { // copy the new template to the data dir WorkspaceInfo ws = getCatalog().getWorkspaceByName(ALL_DOTS.getPrefix()); getDataDirectory() .copyToWorkspaceDir( ws, getClass().getResourceAsStream("shapeziptest.ftl"), "shapezip.ftl"); // setup the request params ShapeZipOutputFormat zip = new ShapeZipOutputFormat(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(getFeatureSource(ALL_DOTS).getFeatures(Filter.INCLUDE)); // get the file name assertEquals("shapezip_All_Types_Dots.zip", zip.getAttachmentFileName(fct, op)); // check the contents zip.write(fct, bos, op); byte[] zipBytes = bos.toByteArray(); checkShapefileIntegrity( new String[] { "theshape_All_Types_DotsPoint", "theshape_All_Types_DotsMPoint", "theshape_All_Types_DotsPolygon", "theshape_All_Types_DotsLine" }, new ByteArrayInputStream(zipBytes)); }
@Test public void testESRIFormatMultiType() throws Exception { setupESRIPropertyFile(); ShapeZipOutputFormat zip = new ShapeZipOutputFormat(getGeoServer(), getCatalog(), getResourceLoader()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(getFeatureSource(ALL_TYPES).getFeatures()); Map options = new HashMap(); options.put("PRJFILEFORMAT", "ESRI"); gft.setFormatOptions(options); zip.write(fct, bos, op); byte[] byteArrayZip = bos.toByteArray(); final String[] expectedTypes = new String[] {"AllTypesPoint", "AllTypesMPoint", "AllTypesPolygon", "AllTypesLine"}; checkShapefileIntegrity(expectedTypes, new ByteArrayInputStream(byteArrayZip)); for (String fileName : expectedTypes) { checkFileContent( fileName + ".prj", new ByteArrayInputStream(byteArrayZip), get4326_ESRI_WKTContent()); } }
@Test public void testTemplateMultiType() throws Exception { // copy the new template to the data dir WorkspaceInfo ws = getCatalog().getWorkspaceByName(SystemTestData.BASIC_POLYGONS.getPrefix()); getDataDirectory() .copyToWorkspaceDir( ws, getClass().getResourceAsStream("shapeziptest.ftl"), "shapezip.ftl"); // setup the request params ShapeZipOutputFormat zip = new ShapeZipOutputFormat(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature() .add(getFeatureSource(SystemTestData.BASIC_POLYGONS).getFeatures(Filter.INCLUDE)); fct.getFeature().add(getFeatureSource(SystemTestData.BRIDGES).getFeatures(Filter.INCLUDE)); // get the file name assertEquals("shapezip_BasicPolygons.zip", zip.getAttachmentFileName(fct, op)); // check the contents zip.write(fct, bos, op); byte[] zipBytes = bos.toByteArray(); checkShapefileIntegrity( new String[] {"theshape_BasicPolygons", "theshape_Bridges"}, new ByteArrayInputStream(zipBytes)); }
@Test public void testESRIFormatFromDefaultValue() throws Exception { setupESRIPropertyFile(); final GeoServer geoServer = getGeoServer(); setupESRIFormatByDefault(geoServer, true); final FeatureSource fs = getFeatureSource(SystemTestData.BASIC_POLYGONS); final Catalog catalog = getCatalog(); final GeoServerResourceLoader resourceLoader = getResourceLoader(); ShapeZipOutputFormat zip = new ShapeZipOutputFormat(geoServer, catalog, resourceLoader); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(fs.getFeatures()); // add the charset Map options = new HashMap(); gft.setFormatOptions(options); zip.write(fct, bos, op); byte[] byteArrayZip = bos.toByteArray(); checkShapefileIntegrity( new String[] {"BasicPolygons"}, new ByteArrayInputStream(byteArrayZip)); checkFileContent( "BasicPolygons.prj", new ByteArrayInputStream(byteArrayZip), get4326_ESRI_WKTContent()); }
FeatureCollectionType response = WfsFactory.eINSTANCE.createFeatureCollectionType(); response.getFeature().add(decorated); FeatureCollectionResponse fcResponse = FeatureCollectionResponse.adapt(response);
@Test public void testOutputZipFileNameSpecifiedInFormatOptions() throws Exception { ShapeZipOutputFormat zip = new ShapeZipOutputFormat(getGeoServer(), getCatalog(), getResourceLoader()); FeatureCollectionResponse mockResult = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); mockResult.getFeature().add(getFeatureSource(ALL_DOTS).getFeatures(Filter.INCLUDE)); GetFeatureType mockRequest = WfsFactory.eINSTANCE.createGetFeatureType(); Operation mockOperation = new Operation( "GetFeature", getServiceDescriptor10(), null, new Object[] {mockRequest}); assertEquals("All_Types_Dots.zip", zip.getAttachmentFileName(mockResult, mockOperation)); mockRequest.getFormatOptions().put("FILENAME", "REQUEST_SUPPLIED_FILENAME.zip"); assertEquals( "REQUEST_SUPPLIED_FILENAME.zip", zip.getAttachmentFileName(mockResult, mockOperation)); }
ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(fs.getFeatures());