Added fallback on /geoserver/rest path [#28150#note-8]

This commit is contained in:
Francesco Mangiacrapa 2024-10-04 11:25:07 +02:00
parent b7cd1f8e20
commit 5a15af0036
2 changed files with 61 additions and 54 deletions

View File

@ -2,6 +2,7 @@
## [v1.1.4-SNAPSHOT]
- Improved logs
- Added fallback on /geoserver/rest path [#28150#note-8]
## [v1.1.3]
- Added apply regex business logic [#26322]

View File

@ -1,6 +1,5 @@
package org.gcube.application.cms.sdi.engine;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
@ -13,6 +12,7 @@ import org.gcube.data.transfer.library.DataTransferClient;
import org.gcube.spatial.data.gis.GISInterface;
import org.gcube.spatial.data.gis.is.AbstractGeoServerDescriptor;
import it.geosolutions.geoserver.rest.HTTPUtils;
import it.geosolutions.geoserver.rest.encoder.datastore.GSPostGISDatastoreEncoder;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@ -20,12 +20,9 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SDIManager {
public static final Pattern HOSTNAME_PATTERN=Pattern.compile("(?<=\\:\\/\\/)[^\\:]*");
public static final Pattern PORT_PATTERN=Pattern.compile("(?<=\\:)[\\d]+");
public static final Pattern DB_NAME_PATTERN=Pattern.compile("(?<=\\/)[^\\/]*(?=$)");
public static final Pattern HOSTNAME_PATTERN = Pattern.compile("(?<=\\:\\/\\/)[^\\:]*");
public static final Pattern PORT_PATTERN = Pattern.compile("(?<=\\:)[\\d]+");
public static final Pattern DB_NAME_PATTERN = Pattern.compile("(?<=\\/)[^\\/]*(?=$)");
private final GISInterface gis;
@Getter
@ -35,46 +32,52 @@ public class SDIManager {
@Getter
private final AbstractGeoServerDescriptor currentGeoserver;
private final String FALLBACK_GS_REST_INTERFACE = "/rest";
public SDIManager() throws SDIInteractionException {
try{
try {
log.debug("Initializing GIS Interface..");
gis=GISInterface.get();
currentGeoserver=gis.getCurrentGeoServer();
if(currentGeoserver==null)
gis = GISInterface.get();
currentGeoserver = gis.getCurrentGeoServer();
if (currentGeoserver == null)
throw new Exception("Unable to contact data transfer for geoserver ");
log.debug("Found geoserver descriptor "+currentGeoserver);
geoserverHostName=new URL(currentGeoserver.getUrl()).getHost();
log.debug("Found geoserver descriptor " + currentGeoserver);
geoserverHostName = new URL(currentGeoserver.getUrl()).getHost();
log.debug("Contacting Data Transfer from geoserver {} ",geoserverHostName);
String gsEP = "https://"+geoserverHostName;
dtGeoServer=DataTransferClient.getInstanceByEndpoint(gsEP);
if(!currentGeoserver.getReader().existGeoserver())
throw new Exception("Geoserver not reachable at "+gsEP);
}catch(Exception e) {
log.error("SDIManager init failed: ",e);
throw new SDIInteractionException("Unable to initialize SDI Manager",e);
log.debug("Contacting Data Transfer from geoserver {} ", geoserverHostName);
String gsEP = "https://" + geoserverHostName;
dtGeoServer = DataTransferClient.getInstanceByEndpoint(gsEP);
if (!currentGeoserver.getReader().existGeoserver()) {
// not a good but necessary solution here, see #28150#note-8
String fallbackGSRestURL = currentGeoserver.getUrl() + FALLBACK_GS_REST_INTERFACE;
log.warn("Geoserver rest interface not reachable at /rest/. Going to check "
+ fallbackGSRestURL);
if (!HTTPUtils.httpPing(fallbackGSRestURL, currentGeoserver.getUser(),
currentGeoserver.getPassword())) {
throw new Exception("Geoserver rest interface not reachable at " + fallbackGSRestURL);
}
}
} catch (Exception e) {
log.error("SDIManager init failed: ", e);
throw new SDIInteractionException("Unable to initialize SDI Manager", e);
}
}
public String createWorkspace(String toCreate) throws SDIInteractionException {
try {
if(!currentGeoserver.getReader().getWorkspaceNames().contains(toCreate)) {
log.debug("Creating workspace : "+toCreate);
if(!currentGeoserver.getPublisher().createWorkspace(toCreate))
throw new SDIInteractionException("Unable to create workspace "+toCreate);
}else log.debug("Workspace "+toCreate+" exists.");
if (!currentGeoserver.getReader().getWorkspaceNames().contains(toCreate)) {
log.debug("Creating workspace : " + toCreate);
if (!currentGeoserver.getPublisher().createWorkspace(toCreate))
throw new SDIInteractionException("Unable to create workspace " + toCreate);
} else
log.debug("Workspace " + toCreate + " exists.");
return toCreate;
} catch (IllegalArgumentException | MalformedURLException e) {
throw new SDIInteractionException("Unable to create workspace "+toCreate,e);
throw new SDIInteractionException("Unable to create workspace " + toCreate, e);
}
}
// protected static final String getToUseBaseLayerName(RegisteredFileSet fileset){
// // ******** IDENTIFY LAYER NAME correct layer name
// // Must be unique under same WS
@ -96,36 +99,38 @@ public class SDIManager {
// }
protected String createStore(GSPostGISDatastoreEncoder encoder, String workspace) throws SDIInteractionException {
String storeName=encoder.getName();
String storeName = encoder.getName();
try {
log.debug("Looking for datastore "+storeName+" under "+workspace);
log.debug("Looking for datastore " + storeName + " under " + workspace);
if(currentGeoserver.getReader().getDatastore(workspace,storeName)==null)
if (currentGeoserver.getReader().getDatastore(workspace, storeName) == null)
if(!currentGeoserver.getDataStoreManager().create(workspace, encoder))
throw new SDIInteractionException("Unable to create store "+storeName+" in "+workspace);
log.debug("Store "+storeName+" exists under "+workspace);
if (!currentGeoserver.getDataStoreManager().create(workspace, encoder))
throw new SDIInteractionException("Unable to create store " + storeName + " in " + workspace);
log.debug("Store " + storeName + " exists under " + workspace);
return storeName;
} catch (IllegalArgumentException | MalformedURLException e) {
throw new SDIInteractionException("Unable to create store "+storeName,e);
throw new SDIInteractionException("Unable to create store " + storeName, e);
}
}
protected String createStoreFromPostgisDB(String workspace, String storeName, DatabaseConnection connection)
throws SDIInteractionException {
String connectionUrl = connection.getUrl();
protected String createStoreFromPostgisDB(String workspace, String storeName, DatabaseConnection connection) throws SDIInteractionException{
String connectionUrl=connection.getUrl();
Matcher hostname=HOSTNAME_PATTERN.matcher(connectionUrl);
if (!hostname.find()) throw new SDIInteractionException("Unable to get Hostname from "+connection);
Matcher hostname = HOSTNAME_PATTERN.matcher(connectionUrl);
if (!hostname.find())
throw new SDIInteractionException("Unable to get Hostname from " + connection);
Matcher port = PORT_PATTERN.matcher(connectionUrl);
if (!port.find()) throw new SDIInteractionException("Unable to get PORT from "+connection);
if (!port.find())
throw new SDIInteractionException("Unable to get PORT from " + connection);
Matcher db = DB_NAME_PATTERN.matcher(connectionUrl);
if (!db.find()) throw new SDIInteractionException("Unable to get DB from "+connection);
if (!db.find())
throw new SDIInteractionException("Unable to get DB from " + connection);
GSPostGISDatastoreEncoder encoder=new GSPostGISDatastoreEncoder(storeName);
GSPostGISDatastoreEncoder encoder = new GSPostGISDatastoreEncoder(storeName);
encoder.setHost(hostname.group());
encoder.setPort(Integer.parseInt(port.group()));
encoder.setDatabase(db.group());
@ -138,19 +143,20 @@ public class SDIManager {
encoder.setFetchSize(1000);
encoder.setValidateConnections(true);
return createStore(encoder,workspace);
return createStore(encoder, workspace);
}
protected String publishStyle(File sldFile, String name) throws SDIInteractionException {
try {
if(!currentGeoserver.getReader().existsStyle(name)) {
log.debug("Registering style "+name);
if(!currentGeoserver.getPublisher().publishStyle(sldFile, name))
throw new SDIInteractionException("Unable to register style "+name);
}else log.debug("Style "+name+" already existing");
if (!currentGeoserver.getReader().existsStyle(name)) {
log.debug("Registering style " + name);
if (!currentGeoserver.getPublisher().publishStyle(sldFile, name))
throw new SDIInteractionException("Unable to register style " + name);
} else
log.debug("Style " + name + " already existing");
return name;
} catch (IllegalArgumentException | MalformedURLException e) {
throw new SDIInteractionException("Unable to create style "+name,e);
throw new SDIInteractionException("Unable to create style " + name, e);
}
}