DSGeoServerAction extends BaseAction<EventObject> { @Override public Queue<EventObject> execute(Queue<EventObject> events) throws ActionException { listenerForwarder.started(); final Queue<EventObject> outputEvents = new LinkedList<EventObject>(); updateTask("Check GeoServer configuration"); final String url = conf.getGeoserverURL(); final String user = conf.getGeoserverUID(); final String password = conf.getGeoserverPWD(); GeoServerRESTManager gsMan = null; try { gsMan = new GeoServerRESTManager(new URL(url), user, password); } catch (MalformedURLException e) { failAction("Wrong GeoServer URL"); } catch(IllegalArgumentException e){ failAction("Unable to create the GeoServer Manager using a null argument"); } listenerForwarder.progressing(5,"GeoServer configuration checked"); updateTask("Check operation"); String op = conf.getOperation(); if(op == null || !(op.equalsIgnoreCase("PUBLISH") || op.equalsIgnoreCase("REMOVE"))){ failAction("Bad operation: " + op + " in configuration"); } listenerForwarder.progressing(10,"Operation checked"); updateTask("Check workspace configuration"); String ws = conf.getDefaultNamespace(); String wsUri = conf.getDefaultNamespaceUri(); Boolean existWS = false; synchronized(existWS){ existWS = gsMan.getReader().getWorkspaceNames().contains(ws); if(!existWS){ boolean createWS = conf.getCreateNameSpace(); if(createWS){ updateTask("Create workspace "+ws+" in GeoServer"); boolean created = false; if(wsUri == null){ created = gsMan.getPublisher().createWorkspace(ws); }else{ try { created = gsMan.getPublisher().createWorkspace(ws, new URI(wsUri)); } catch (URISyntaxException e) { failAction("Invalid NameSpace URI "+wsUri+" in configuration"); } } if(!created){ failAction("FATAL: unable to create workspace "+ws+" in GeoServer"); } }else{ failAction("Bad workspace (namespace): "+ ws +" in configuration"); } } } listenerForwarder.progressing(25,"GeoServer workspace checked"); while (events.size() > 0) { final EventObject ev; try { if ((ev = events.remove()) != null) { updateTask("Working on incoming event: " + ev.getSource()); updateTask("Check acceptable file"); FileSystemEvent fileEvent = (FileSystemEvent) ev; updateTask("Set Feature Configuration"); this.createFeatureConfiguration(fileEvent); FeatureConfiguration featureConfig = conf.getFeatureConfiguration(); updateTask("Check Feature Configuration"); if(featureConfig.getTypeName() == null){ failAction("feature typeName cannot be null"); } updateTask("Check datastore configuration"); String ds = conf.getStoreName(); Boolean existDS = false; synchronized(existDS){ existDS = gsMan.getReader().getDatastores(ws).getNames().contains(ds); if(!existDS){ boolean createDS = conf.getCreateDataStore(); if(createDS){ updateTask("Create datastore in GeoServer"); Map<String,Object> datastore = this.deserialize(featureConfig.getDataStore()); String dbType = (String) datastore.get("dbtype"); boolean created = false; if(dbType.equalsIgnoreCase("postgis")){ GSPostGISDatastoreEncoder encoder = new GSPostGISDatastoreEncoder(ds); encoder.setName(ds); encoder.setEnabled(true); encoder.setHost((String) datastore.get("host")); encoder.setPort(Integer.parseInt((String) datastore.get("port"))); encoder.setDatabase((String) datastore.get("database")); encoder.setSchema((String) datastore.get("schema")); encoder.setUser((String) datastore.get("user")); encoder.setPassword((String) datastore.get("passwd")); created = gsMan.getStoreManager().create(ws, encoder); if(!created){ failAction("FATAL: unable to create PostGIS datastore "+ds+" in GeoServer"); } }else if(dbType.equalsIgnoreCase("oracle")){ String dbname = (String) datastore.get("database"); GSOracleNGDatastoreEncoder encoder = new GSOracleNGDatastoreEncoder(ds, dbname); encoder.setName(ds); encoder.setEnabled(true); encoder.setHost((String) datastore.get("host")); encoder.setPort(Integer.parseInt((String) datastore.get("port"))); encoder.setDatabase(dbname); encoder.setSchema((String) datastore.get("schema")); encoder.setUser((String) datastore.get("user")); encoder.setPassword((String) datastore.get("passwd")); created = gsMan.getStoreManager().create(ws, encoder); if(!created){ failAction("FATAL: unable to create Oracle NG datastore "+ds+" in GeoServer"); } }else{ failAction("The datastore type "+dbType+" is not supported"); } }else{ failAction("Bad datastore:"+ ds +" in configuration. Datastore "+ds+" doesn't exist in workspace (namespace) "+ws); } } } listenerForwarder.progressing(50,"Check GeoServer datastore"); boolean done = false; if(op.equalsIgnoreCase("PUBLISH")){ if(!gsMan.getReader().getLayers().getNames().contains(featureConfig.getTypeName())){ updateTask("Publish DBLayer "+featureConfig.getTypeName()+" in GeoServer"); final GSFeatureTypeEncoder fte = new GSFeatureTypeEncoder(); fte.setName(featureConfig.getTypeName()); fte.setTitle(featureConfig.getTypeName()); String crs = featureConfig.getCrs(); if(crs != null){ fte.setSRS(featureConfig.getCrs()); }else{ fte.setSRS("EPSG:4326"); } fte.setProjectionPolicy(ProjectionPolicy.FORCE_DECLARED); final GSLayerEncoder layerEncoder = new GSLayerEncoder(); layerEncoder.setDefaultStyle(this.defineLayerStyle(featureConfig, gsMan)); if(conf.getStyles() != null){ for(String style : conf.getStyles()){ layerEncoder.addStyle(style); } } done = gsMan.getPublisher().publishDBLayer(ws, ds, fte, layerEncoder); if(!done){ failAction("Impossible to publish DBLayer "+featureConfig.getTypeName()+" in GeoServer"); } } }else if(op.equalsIgnoreCase("REMOVE")){ if(gsMan.getReader().getLayers().getNames().contains(featureConfig.getTypeName())){ updateTask("Remove DBLayer "+featureConfig.getTypeName()+" from GeoServer"); done = gsMan.getPublisher().unpublishFeatureType(ws, ds, featureConfig.getTypeName()); if(!done){ failAction("Impossible to remove DBLayer "+featureConfig.getTypeName()+" in GeoServer"); } } } listenerForwarder.progressing(100F, "Successful Geoserver "+op+" operation"); listenerForwarder.completed(); outputEvents.add(ev); } else { if (LOGGER.isErrorEnabled()) { LOGGER.error("Encountered a NULL event: SKIPPING..."); } continue; } } catch (Exception ioe) { failAction("Unable to produce the output: " + ioe.getLocalizedMessage(),ioe); } } return outputEvents; } DSGeoServerAction(DSGeoServerConfiguration actionConfiguration); @Override @CheckConfiguration boolean checkConfiguration(); @Override Queue<EventObject> execute(Queue<EventObject> events);  }
@Test public void testGeomDataPublication() throws Exception { if(enabled){ configuration.setFeatureConfiguration(FeatureConfiguration.fromXML( new FileInputStream(getResourceFile("states.xml")))); Queue<EventObject> events = new LinkedList<EventObject>(); events.add(new FileSystemEvent(getResourceFile("states.xml"), FileSystemEventType.FILE_ADDED)); DSGeoServerAction action = new DSGeoServerAction(configuration); action.setTempDir(new File("/tmp")); action.execute(events); } }
@Test public void testGeomlessDataPublication() throws Exception { if(enabled){ configuration.setFeatureConfiguration(FeatureConfiguration.fromXML( new FileInputStream(getResourceFile("states_stats.xml")))); Queue<EventObject> events = new LinkedList<EventObject>(); events.add(new FileSystemEvent(getResourceFile("states_stats.xml"), FileSystemEventType.FILE_ADDED)); DSGeoServerAction action = new DSGeoServerAction(configuration); action.setTempDir(new File("/tmp")); action.execute(events); } }