diff --git a/src/main/java/org/gcube/spatial/data/gis/GISInterface.java b/src/main/java/org/gcube/spatial/data/gis/GISInterface.java index 9c40095..a974ca4 100644 --- a/src/main/java/org/gcube/spatial/data/gis/GISInterface.java +++ b/src/main/java/org/gcube/spatial/data/gis/GISInterface.java @@ -1,7 +1,5 @@ package org.gcube.spatial.data.gis; -import it.geosolutions.geonetwork.exception.GNException; -import it.geosolutions.geonetwork.exception.GNServerException; import it.geosolutions.geonetwork.util.GNInsertConfiguration; import it.geosolutions.geoserver.rest.GeoServerRESTManager; import it.geosolutions.geoserver.rest.GeoServerRESTPublisher; @@ -10,7 +8,6 @@ import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder.ProjectionPolicy import java.io.File; import java.io.FileNotFoundException; -import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; @@ -19,18 +16,17 @@ import java.util.NoSuchElementException; import java.util.SortedSet; import java.util.concurrent.ConcurrentSkipListSet; -import javax.xml.bind.JAXBException; - import org.gcube.common.scope.api.ScopeProvider; import org.gcube.spatial.data.geonetwork.GeoNetwork; import org.gcube.spatial.data.geonetwork.GeoNetworkPublisher; import org.gcube.spatial.data.geonetwork.GeoNetworkReader; -import org.gcube.spatial.data.geonetwork.configuration.AuthorizationException; +import org.gcube.spatial.data.gis.is.GeoServerDescriptor; +import org.gcube.spatial.data.gis.is.InfrastructureCrawler; import org.gcube.spatial.data.gis.meta.MetadataEnricher; -import org.gcube.spatial.data.gis.model.GeoServerDescriptor; -import org.gcube.spatial.data.gis.model.InfrastructureCrawler; -import org.gcube.spatial.data.gis.model.PublishResponse; -import org.gcube.spatial.data.gis.model.PublishResponse.PublishedState; +import org.gcube.spatial.data.gis.model.report.DeleteReport; +import org.gcube.spatial.data.gis.model.report.PublishResponse; +import org.gcube.spatial.data.gis.model.report.Report; +import org.gcube.spatial.data.gis.model.report.Report.OperationState; import org.opengis.metadata.Metadata; import org.opengis.metadata.citation.DateType; import org.slf4j.Logger; @@ -92,10 +88,11 @@ public class GISInterface { GeoServerRESTPublisher publisher=null; try{ GeoServerDescriptor desc=getGeoServerSet(false).last(); + logger.debug("Using "+desc); publisher=getGeoServerManager(desc.getUrl(), desc.getUser(), desc.getPassword()).getPublisher(); publishResult=publisher.publishGeoTIFF(workspace, storeName, coverageName, geotiff, srs, policy, defaultStyle, bbox); if(publishResult){ - toReturn.setDataPublishResult(PublishedState.COMPLETE); + toReturn.setDataOperationResult(Report.OperationState.COMPLETE); ArrayList distributionUris=new ArrayList(); // distributionUris.add(desc.getUrl()+"gwc/service/wms") @@ -103,29 +100,68 @@ public class GISInterface { enricher.addDate(new Date(System.currentTimeMillis()), DateType.CREATION); enricher.addPreview(distributionUris.get(0)); enricher.setdistributionURIs(distributionUris); - toReturn.getMetaPublishMessages().addAll(enricher.getMessages()); + toReturn.getMetaOperationMessages().addAll(enricher.getMessages()); + if(enricher.getMessages().size()>0)toReturn.setMetaOperationResult(OperationState.WARN); long returnedId=getGN().insertMetadata(config,enricher.getEnriched()); toReturn.setReturnedMetaId(returnedId); - } + toReturn.setMetaOperationResult(OperationState.COMPLETE); + }else toReturn.getDataOperationMessages().add("Publish operation returned false, unable to publish data"); }catch(NoSuchElementException e){ - toReturn.getDataPublishMessages().add("No GeoServer Found under scope "+ScopeProvider.instance.get()); + toReturn.getDataOperationMessages().add("No GeoServer Found under scope "+ScopeProvider.instance.get()); } catch (IllegalArgumentException e) { - toReturn.getDataPublishMessages().add("Unable to instatiate GeoServerRESTPublisher, cause :"+e.getMessage()); + if(publisher==null) + toReturn.getDataOperationMessages().add("Unable to instatiate GeoServerRESTPublisher, cause :"+e.getMessage()); + else toReturn.getDataOperationMessages().add("Unable to publish data, cause :"+e.getMessage()); } catch (MalformedURLException e) { - toReturn.getDataPublishMessages().add("Unable to instatiate GeoServerRESTPublisher, cause :"+e.getMessage()); + toReturn.getDataOperationMessages().add("Unable to instatiate GeoServerRESTPublisher, cause :"+e.getMessage()); } catch (FileNotFoundException e) { - toReturn.getDataPublishMessages().add("Unable to publish data GeoServerRESTPublisher, cause :"+e.getMessage()); + toReturn.getDataOperationMessages().add("Unable to publish data, cause :"+e.getMessage()); } catch (Exception e) { // exceptions raised by publishing metadata, need to clean up - toReturn.getMetaPublishMessages().add("Unable to publish metadata, cause :"+e.getMessage()); - //TODO rollback - } + toReturn.getMetaOperationMessages().add("Unable to publish metadata, cause :"+e.getMessage()); + DeleteReport delRep=deleteStore(workspace,storeName,null); + if(!delRep.getDataOperationResult().equals(OperationState.COMPLETE)){ + toReturn.setDataOperationResult(OperationState.WARN); + toReturn.getDataOperationMessages().add("Unable to rollback data publishing, following messages from delete operation (state : "+delRep.getDataOperationResult()+")"); + toReturn.getDataOperationMessages().addAll(delRep.getDataOperationMessages()); + } + } return toReturn; } // ********************* DELETE Logic + public DeleteReport deleteStore(String workspace,String storeName,Long metadataUUID){ + DeleteReport toReturn=new DeleteReport(); + GeoServerRESTPublisher publisher=null; + try{ + GeoServerDescriptor desc=getGeoServerSet(false).last(); + publisher=getGeoServerManager(desc.getUrl(), desc.getUser(), desc.getPassword()).getPublisher(); + boolean removed=publisher.removeDatastore(workspace, storeName,true); + if(removed){ + toReturn.setDataOperationResult(Report.OperationState.COMPLETE); + if(metadataUUID!=null){ + getGN().deleteMetadata(metadataUUID); + }else { + toReturn.setMetaOperationResult(OperationState.WARN); + toReturn.getMetaOperationMessages().add("Passed meta UUID is null, no metadata deleted"); + } + }else toReturn.getDataOperationMessages().add("Remove data operation returned false, unable to delete Store"); + }catch(NoSuchElementException e){ + toReturn.getDataOperationMessages().add("No GeoServer Found under scope "+ScopeProvider.instance.get()); + } catch (IllegalArgumentException e) { + if(publisher==null) + toReturn.getDataOperationMessages().add("Unable to instatiate GeoServerRESTPublisher, cause :"+e.getMessage()); + else toReturn.getDataOperationMessages().add("Unable to publish data, cause :"+e.getMessage()); + } catch (MalformedURLException e) { + toReturn.getDataOperationMessages().add("Unable to instatiate GeoServerRESTPublisher, cause :"+e.getMessage()); + } catch (Exception e) { + toReturn.getMetaOperationMessages().add("Unable to delete metadata, cause :"+e.getMessage()); + } + return toReturn; + } + //************ PRIVATE private GeoNetworkPublisher geoNetwork=null; diff --git a/src/main/java/org/gcube/spatial/data/gis/model/GeoServerDescriptor.java b/src/main/java/org/gcube/spatial/data/gis/is/GeoServerDescriptor.java similarity index 98% rename from src/main/java/org/gcube/spatial/data/gis/model/GeoServerDescriptor.java rename to src/main/java/org/gcube/spatial/data/gis/is/GeoServerDescriptor.java index f74b4ca..7987ed5 100644 --- a/src/main/java/org/gcube/spatial/data/gis/model/GeoServerDescriptor.java +++ b/src/main/java/org/gcube/spatial/data/gis/is/GeoServerDescriptor.java @@ -1,4 +1,4 @@ -package org.gcube.spatial.data.gis.model; +package org.gcube.spatial.data.gis.is; public class GeoServerDescriptor implements Comparable{ diff --git a/src/main/java/org/gcube/spatial/data/gis/model/InfrastructureCrawler.java b/src/main/java/org/gcube/spatial/data/gis/is/InfrastructureCrawler.java similarity index 67% rename from src/main/java/org/gcube/spatial/data/gis/model/InfrastructureCrawler.java rename to src/main/java/org/gcube/spatial/data/gis/is/InfrastructureCrawler.java index cc7c09e..0005b89 100644 --- a/src/main/java/org/gcube/spatial/data/gis/model/InfrastructureCrawler.java +++ b/src/main/java/org/gcube/spatial/data/gis/is/InfrastructureCrawler.java @@ -1,4 +1,4 @@ -package org.gcube.spatial.data.gis.model; +package org.gcube.spatial.data.gis.is; import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; @@ -8,18 +8,23 @@ import java.util.List; import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.utils.encryption.StringEncrypter; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.spatial.data.gis.GISInterface; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class InfrastructureCrawler { + final static Logger logger= LoggerFactory.getLogger(InfrastructureCrawler.class); public static List queryforGeoServer(){ List toReturn=new ArrayList(); SimpleQuery query = queryFor(ServiceEndpoint.class); query.addCondition("$resource/Profile/Category/text() eq 'Gis'") - .addCondition("$resource/Profile/Platform/Name/text() eq 'geonetwork'") + .addCondition("$resource/Profile/Platform/Name/text() eq 'GeoServer'") .setResult("$resource/Profile/AccessPoint"); DiscoveryClient client = clientFor(AccessPoint.class); @@ -27,7 +32,11 @@ public class InfrastructureCrawler { List accesspoints = client.submit(query); // if(accesspoints.size()==0) throw new Exception("No Resource found under current scope "+ScopeProvider.instance.get()); for (AccessPoint point : accesspoints) { - toReturn.add(new GeoServerDescriptor(point.address(),point.username(),point.password(),0l)); + try{ + toReturn.add(new GeoServerDescriptor(point.address(),point.username(),StringEncrypter.getEncrypter().decrypt(point.password()),0l)); + }catch(Exception e){ + logger.warn("Unable to decript password for "+point.username()+" in access point "+point.address()+", access to modify methods may fail"); + } // url=point.address(); // user=point.username(); // pwd=point.password(); diff --git a/src/main/java/org/gcube/spatial/data/gis/meta/MetadataEnricher.java b/src/main/java/org/gcube/spatial/data/gis/meta/MetadataEnricher.java index be8d2db..7845bc4 100644 --- a/src/main/java/org/gcube/spatial/data/gis/meta/MetadataEnricher.java +++ b/src/main/java/org/gcube/spatial/data/gis/meta/MetadataEnricher.java @@ -8,7 +8,7 @@ import java.util.Date; import java.util.List; import java.util.UUID; -import org.gcube.spatial.data.gis.model.PublishResponse.PublishedState; +import org.gcube.spatial.data.gis.model.report.Report.OperationState; import org.geotoolkit.metadata.iso.DefaultMetadata; import org.geotoolkit.metadata.iso.citation.DefaultCitation; import org.geotoolkit.metadata.iso.citation.DefaultCitationDate; diff --git a/src/main/java/org/gcube/spatial/data/gis/model/Costants.java b/src/main/java/org/gcube/spatial/data/gis/model/Costants.java new file mode 100644 index 0000000..a572a03 --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/gis/model/Costants.java @@ -0,0 +1,9 @@ +package org.gcube.spatial.data.gis.model; + +public class Costants { + + public static final double[] WORLD_BOUNDING_BOX=new double[]{ + -180.0,-90.0,180.0,90.0 + }; + +} diff --git a/src/main/java/org/gcube/spatial/data/gis/model/EnrichmentResponse.java b/src/main/java/org/gcube/spatial/data/gis/model/EnrichmentResponse.java deleted file mode 100644 index c6b4f05..0000000 --- a/src/main/java/org/gcube/spatial/data/gis/model/EnrichmentResponse.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.gcube.spatial.data.gis.model; - -import java.util.ArrayList; - -import org.geotoolkit.metadata.iso.DefaultMetadata; - -public class EnrichmentResponse { - - private DefaultMetadata enrichedMeta; - private DefaultMetadata passedMeta; - private ArrayList warnMessages=new ArrayList(); - - public EnrichmentResponse(DefaultMetadata passed) { - // TODO Auto-generated constructor stub - } - - public DefaultMetadata getEnrichedMeta() { - return enrichedMeta; - } - public void setEnrichedMeta(DefaultMetadata enrichedMeta) { - this.enrichedMeta = enrichedMeta; - } - public DefaultMetadata getPassedMeta() { - return passedMeta; - } - public ArrayList getWarnMessages() { - return warnMessages; - } -} diff --git a/src/main/java/org/gcube/spatial/data/gis/model/PublishResponse.java b/src/main/java/org/gcube/spatial/data/gis/model/PublishResponse.java deleted file mode 100644 index 0f6ca1c..0000000 --- a/src/main/java/org/gcube/spatial/data/gis/model/PublishResponse.java +++ /dev/null @@ -1,132 +0,0 @@ -package org.gcube.spatial.data.gis.model; - -import java.util.ArrayList; - -import org.geotoolkit.metadata.iso.DefaultMetadata; -import org.opengis.metadata.Metadata; - -public class PublishResponse { - - public static enum PublishedState{ - COMPLETE,ERROR,WARN - } - - private Metadata passedMetadata; - private Metadata publishedMetadata; - private PublishedState dataPublishResult=PublishedState.ERROR; - private PublishedState metaPublishResult=PublishedState.ERROR; - private ArrayList dataPublishMessages=new ArrayList(); - private ArrayList metaPublishMessages=new ArrayList(); - private long returnedMetaId; - - public PublishResponse() { - // TODO Auto-generated constructor stub - } - - public PublishResponse(Metadata passedMetadata){ - setPassedMetadata(passedMetadata); - } - - - /** - * @return the passedMetadata - */ - public Metadata getPassedMetadata() { - return passedMetadata; - } - - /** - * @param passedMetadata the passedMetadata to set - */ - public void setPassedMetadata(Metadata passedMetadata) { - this.passedMetadata = new DefaultMetadata(passedMetadata); - } - - /** - * @return the publishedMetadata - */ - public Metadata getPublishedMetadata() { - return publishedMetadata; - } - - /** - * @param publishedMetadata the publishedMetadata to set - */ - public void setPublishedMetadata(Metadata publishedMetadata) { - this.publishedMetadata = publishedMetadata; - } - - /** - * @return the dataPublishResult - */ - public PublishedState getDataPublishResult() { - return dataPublishResult; - } - - /** - * @param dataPublishResult the dataPublishResult to set - */ - public void setDataPublishResult(PublishedState dataPublishResult) { - this.dataPublishResult = dataPublishResult; - } - - /** - * @return the metaPublishResult - */ - public PublishedState getMetaPublishResult() { - return metaPublishResult; - } - - /** - * @param metaPublishResult the metaPublishResult to set - */ - public void setMetaPublishResult(PublishedState metaPublishResult) { - this.metaPublishResult = metaPublishResult; - } - - /** - * @return the dataPublishMessages - */ - public ArrayList getDataPublishMessages() { - return dataPublishMessages; - } - - /** - * @param dataPublishMessages the dataPublishMessages to set - */ - public void setDataPublishMessages(ArrayList dataPublishMessages) { - this.dataPublishMessages = dataPublishMessages; - } - - /** - * @return the metaPublishMessages - */ - public ArrayList getMetaPublishMessages() { - return metaPublishMessages; - } - - /** - * @param metaPublishMessages the metaPublishMessages to set - */ - public void setMetaPublishMessages(ArrayList metaPublishMessages) { - this.metaPublishMessages = metaPublishMessages; - } - - /** - * @return the returnedMetaId - */ - public long getReturnedMetaId() { - return returnedMetaId; - } - - /** - * @param returnedMetaId the returnedMetaId to set - */ - public void setReturnedMetaId(long returnedMetaId) { - this.returnedMetaId = returnedMetaId; - } - - - - -} diff --git a/src/main/java/org/gcube/spatial/data/gis/model/report/DeleteReport.java b/src/main/java/org/gcube/spatial/data/gis/model/report/DeleteReport.java new file mode 100644 index 0000000..4b7182d --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/gis/model/report/DeleteReport.java @@ -0,0 +1,8 @@ +package org.gcube.spatial.data.gis.model.report; + +public class DeleteReport extends Report { + + private String metadataUUID; + + +} diff --git a/src/main/java/org/gcube/spatial/data/gis/model/report/PublishResponse.java b/src/main/java/org/gcube/spatial/data/gis/model/report/PublishResponse.java new file mode 100644 index 0000000..69cb4df --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/gis/model/report/PublishResponse.java @@ -0,0 +1,86 @@ +package org.gcube.spatial.data.gis.model.report; + + +import org.geotoolkit.metadata.iso.DefaultMetadata; +import org.opengis.metadata.Metadata; + +public class PublishResponse extends Report{ + + private Metadata passedMetadata; + private Metadata publishedMetadata; + private long returnedMetaId; + + + public PublishResponse() { + // TODO Auto-generated constructor stub + } + + public PublishResponse(Metadata passedMetadata){ + setPassedMetadata(passedMetadata); + } + + + /** + * @return the passedMetadata + */ + public Metadata getPassedMetadata() { + return passedMetadata; + } + + /** + * @param passedMetadata the passedMetadata to set + */ + public void setPassedMetadata(Metadata passedMetadata) { + this.passedMetadata = new DefaultMetadata(passedMetadata); + } + + /** + * @return the publishedMetadata + */ + public Metadata getPublishedMetadata() { + return publishedMetadata; + } + + /** + * @param publishedMetadata the publishedMetadata to set + */ + public void setPublishedMetadata(Metadata publishedMetadata) { + this.publishedMetadata = publishedMetadata; + } + + /** + * @return the returnedMetaId + */ + public long getReturnedMetaId() { + return returnedMetaId; + } + + /** + * @param returnedMetaId the returnedMetaId to set + */ + public void setReturnedMetaId(long returnedMetaId) { + this.returnedMetaId = returnedMetaId; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("PublishResponse [passedMetadata="); + builder.append(passedMetadata); + builder.append(", publishedMetadata="); + builder.append(publishedMetadata); + builder.append(", returnedMetaId="); + builder.append(returnedMetaId); + builder.append(", report="); + builder.append(super.toString()); + builder.append("]"); + return builder.toString(); + } + + + + +} diff --git a/src/main/java/org/gcube/spatial/data/gis/model/report/Report.java b/src/main/java/org/gcube/spatial/data/gis/model/report/Report.java new file mode 100644 index 0000000..b753f7b --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/gis/model/report/Report.java @@ -0,0 +1,83 @@ +package org.gcube.spatial.data.gis.model.report; + +import java.util.ArrayList; + +public class Report { + + public static enum OperationState{ + COMPLETE,ERROR,WARN + } + + private Report.OperationState dataOperationResult = Report.OperationState.ERROR; + private Report.OperationState metaOperationResult = Report.OperationState.ERROR; + private ArrayList dataOperationMessages = new ArrayList(); + private ArrayList metaOperationMessages = new ArrayList(); + /** + * @return the dataOperationResult + */ + public Report.OperationState getDataOperationResult() { + return dataOperationResult; + } + /** + * @param dataOperationResult the dataOperationResult to set + */ + public void setDataOperationResult(Report.OperationState dataOperationResult) { + this.dataOperationResult = dataOperationResult; + } + /** + * @return the metaOperationResult + */ + public Report.OperationState getMetaOperationResult() { + return metaOperationResult; + } + /** + * @param metaOperationResult the metaOperationResult to set + */ + public void setMetaOperationResult(Report.OperationState metaOperationResult) { + this.metaOperationResult = metaOperationResult; + } + /** + * @return the dataOperationMessages + */ + public ArrayList getDataOperationMessages() { + return dataOperationMessages; + } + /** + * @param dataOperationMessages the dataOperationMessages to set + */ + public void setDataOperationMessages(ArrayList dataOperationMessages) { + this.dataOperationMessages = dataOperationMessages; + } + /** + * @return the metaOperationMessages + */ + public ArrayList getMetaOperationMessages() { + return metaOperationMessages; + } + /** + * @param metaOperationMessages the metaOperationMessages to set + */ + public void setMetaOperationMessages(ArrayList metaOperationMessages) { + this.metaOperationMessages = metaOperationMessages; + } + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Report [dataOperationResult="); + builder.append(dataOperationResult); + builder.append(", metaOperationResult="); + builder.append(metaOperationResult); + builder.append(", dataOperationMessages="); + builder.append(dataOperationMessages); + builder.append(", metaOperationMessages="); + builder.append(metaOperationMessages); + builder.append("]"); + return builder.toString(); + } + + + +}