ShapeFileAction extends BaseAction<EventObject> { public Queue<EventObject> execute(Queue<EventObject> events) throws ActionException { listenerForwarder.setTask("config"); listenerForwarder.started(); try { GeoServerActionConfiguration configuration=getConfiguration(); if (configuration == null) { throw new IllegalStateException("ActionConfig is null."); } final int inputSize = events.size(); final EventObject event = events.peek(); String[] shapeNames; File zippedFile = null; it.geosolutions.geobatch.geoserver.UploadMethod transferMethod = it.geosolutions.geobatch.geoserver.UploadMethod.valueOf(configuration.getDataTransferMethod()); if (transferMethod == null) { transferMethod =it.geosolutions.geobatch.geoserver.UploadMethod.getDefault(); } File[] files=null; File tmpDirFile=null; Integer epsgCode=null; GeometryDescriptor descriptor=null; CoordinateReferenceSystem crs=null; if (inputSize == 1) { zippedFile = toFile(event); if (LOGGER.isDebugEnabled()) LOGGER.debug("Testing for compressed file: " + zippedFile); tmpDirFile = Extract.extract(zippedFile,getTempDir(),false); listenerForwarder.progressing(5, "File extracted"); if(tmpDirFile==null){ throw new IllegalStateException("Not valid input: we need a zip file "); } if (!tmpDirFile.isDirectory()) { if (!tmpDirFile.isFile()) { throw new IllegalStateException( "Not valid input: we need a zip file "); }else{ tmpDirFile = tmpDirFile.getParentFile(); } } final Collector c = new Collector(FileFilterUtils.notFileFilter(FileFilterUtils.nameFileFilter(tmpDirFile.getName()))); final List<File> fileList = c.collect(tmpDirFile); files = fileList.toArray(new File[1]); shapeNames = acceptable(files); } else { if (LOGGER.isTraceEnabled()) LOGGER.trace("Checking input collection..."); listenerForwarder.progressing(5, "Checking input collection..."); files = new File[events.size()]; int i = 0; for (EventObject ev : events) { files[i++] = toFile(ev); } tmpDirFile = new File(FilenameUtils.getFullPath(files[0].getAbsolutePath())); shapeNames = acceptable(files); if(transferMethod!=it.geosolutions.geobatch.geoserver.UploadMethod.EXTERNAL) { zippedFile = Compressor.deflate(getTempDir(),shapeNames[0], files); if (zippedFile == null) { throw new IllegalStateException("Unable to create the zip file"); } } } if (shapeNames == null) { final String message = "Input is not a zipped file nor a valid collection of files"; if (LOGGER.isErrorEnabled()) LOGGER.error(message); throw new IllegalStateException(message); } for(String shape: shapeNames) { FileDataStore store = null; try{ store=Utils.SHP_FACTORY.createDataStore(new File(tmpDirFile,shape+".shp").toURI().toURL()); crs = store.getSchema().getCoordinateReferenceSystem(); epsgCode= crs!=null?CRS.lookupEpsgCode(crs, false):null; descriptor = store.getSchema().getGeometryDescriptor(); } finally { if(store!=null){ try{ store.dispose(); }catch (Exception e) { if(LOGGER.isTraceEnabled()){ LOGGER.trace(e.getLocalizedMessage(),e); } } } } } listenerForwarder.progressing(10, "In progress"); GeoServerRESTReader reader = new GeoServerRESTReader(configuration.getGeoserverURL(), configuration.getGeoserverUID(), configuration.getGeoserverPWD()); GeoServerRESTPublisher publisher = new GeoServerRESTPublisher( configuration.getGeoserverURL(), configuration.getGeoserverUID(), configuration.getGeoserverPWD()); WorkspaceUtils.createWorkspace(reader, publisher, configuration.getDefaultNamespace(), configuration.getDefaultNamespaceUri()); String nativeCRS=null; ProjectionPolicy projectionPolicy = ProjectionPolicy.NONE; final String defaultCRS = configuration.getCrs(); String finalEPSGCode = defaultCRS; if (epsgCode == null) { if (finalEPSGCode == null) { final String message = "Input file has no CRS neither the configuration provides a default one"; final ActionException ae = new ActionException(this, message); if (LOGGER.isErrorEnabled()) LOGGER.error(message, ae); listenerForwarder.failed(ae); throw ae; } if(crs!=null){ nativeCRS=crs.toWKT(); projectionPolicy = ProjectionPolicy.REPROJECT_TO_DECLARED; } else { projectionPolicy = ProjectionPolicy.FORCE_DECLARED; } } else { finalEPSGCode = "EPSG:" + epsgCode; nativeCRS=finalEPSGCode; } String defaultStyle=configuration.getDefaultStyle(); if(defaultStyle==null || defaultStyle.isEmpty()){ final GeometryType geometryType = descriptor.getType(); Class clazz=geometryType.getBinding(); if(clazz.isAssignableFrom(Point.class)||clazz.isAssignableFrom(MultiPoint.class)){ defaultStyle=Utils.DEFAULT_POINT_STYLE; } else if(clazz.isAssignableFrom(LineString.class)||clazz.isAssignableFrom(MultiLineString.class)){ defaultStyle=Utils.DEFAULT_LINE_STYLE; } else if(clazz.isAssignableFrom(Polygon.class)||clazz.isAssignableFrom(MultiPolygon.class)){ defaultStyle=Utils.DEFAULT_POLYGON_STYLE; } } UploadMethod uMethod=null; switch (transferMethod) { case DIRECT: uMethod=UploadMethod.FILE; break; case EXTERNAL: uMethod=UploadMethod.EXTERNAL; break; default: throw new IllegalArgumentException("Unsupported transfer method: "+configuration.getDataTransferMethod()); } String wsName = configuration.getDefaultNamespace(); String dsName = configuration.getStoreName() == null? shapeNames[0] : configuration.getStoreName(); String lyrName = configuration.getLayerName() == null? shapeNames[0] : configuration.getLayerName(); String styleName = defaultStyle; boolean success = false; if(shapeNames.length==1) { success = publisher.publishShp(wsName, dsName, null, lyrName, uMethod, zippedFile.toURI(), finalEPSGCode, nativeCRS, projectionPolicy, styleName); } else { success = publisher.publishShpCollection(wsName, dsName, zippedFile.toURI()); } if (success) { final String message = "Shape file SUCCESFULLY sent"; if (LOGGER.isInfoEnabled()) LOGGER.info(message); listenerForwarder.progressing(90, message); } else { final String message = "Shape file FAILED to be sent"; final ActionException ae = new ActionException(this, message); if (LOGGER.isErrorEnabled()) LOGGER.error(message, ae); listenerForwarder.failed(ae); throw ae; } if (configuration instanceof GeoServerShapeActionConfiguration) { if (LOGGER.isInfoEnabled()) LOGGER.info("Configuring shape datastore connection parameters"); GeoServerShapeActionConfiguration shpConfig = (GeoServerShapeActionConfiguration)configuration; GeoServerRESTManager manager = new GeoServerRESTManager(new URL(shpConfig.getGeoserverURL()), shpConfig.getGeoserverUID(), shpConfig.getGeoserverPWD()); GeoServerRESTStoreManager dsManager = manager.getStoreManager(); RESTDataStore dsRead = manager.getReader().getDatastore(wsName, dsName); GSShapefileDatastoreEncoder dsWrite = new GSShapefileDatastoreEncoder(dsRead); if (shpConfig.getUrl() != null) dsWrite.setUrl(shpConfig.getUrl()); if (shpConfig.getCharset() != null) dsWrite.setCharset(shpConfig.getCharset()); if (shpConfig.getCreateSpatialIndex() != null) dsWrite.setCreateSpatialIndex(shpConfig.getCreateSpatialIndex()); if (shpConfig.getMemoryMappedBuffer() != null) dsWrite.setMemoryMappedBuffer(shpConfig.getMemoryMappedBuffer()); if (shpConfig.getCacheAndReuseMemoryMaps() != null) dsWrite.setCacheAndReuseMemoryMaps(shpConfig.getCacheAndReuseMemoryMaps()); success = dsManager.update(wsName, dsWrite); if (success) { String message = "Shape datastore SUCCESFULLY configured"; if (LOGGER.isInfoEnabled()) LOGGER.info(message); listenerForwarder.progressing(100, message); } else { String message = "Shape datastore FAILED to be configured"; final ActionException ae = new ActionException(this, message); if (LOGGER.isErrorEnabled()) LOGGER.error(message, ae); listenerForwarder.failed(ae); throw ae; } } return events; } catch (Throwable t) { final ActionException ae = new ActionException(this, t.getMessage(), t); if (LOGGER.isErrorEnabled()) LOGGER.error(ae.getLocalizedMessage(), ae); listenerForwarder.failed(ae); throw ae; } } ShapeFileAction(final GeoServerShapeActionConfiguration configuration); @Override @CheckConfiguration boolean checkConfiguration(); Queue<EventObject> execute(Queue<EventObject> events);  }
@Test public void testExecuteMultiShpZip() throws Exception { if (skipTest()) { LOGGER.warn("Skipping test."); return; } configuration.setGeoserverPWD(getPwd()); configuration.setGeoserverUID(getUid()); configuration.setGeoserverURL(getUrl()); File zipFile = new File(getClass().getResource(MULTI_SHP_ZIP).toURI()); Queue<EventObject> events = new LinkedList<EventObject>(); events.add(new FileSystemEvent(zipFile, FileSystemEventType.FILE_ADDED)); ShapeFileAction action = new ShapeFileAction(configuration); action.setTempDir(new File("/tmp")); action.execute(events); }