2014-10-10 14:53:39 +02:00
package org.gcube.datatransfer.resolver.gis ;
2016-06-09 11:36:25 +02:00
import org.gcube.common.scope.api.ScopeProvider ;
2014-10-14 18:36:13 +02:00
import org.gcube.datatransfer.resolver.gis.exception.GeonetworkInstanceException ;
2014-10-10 14:53:39 +02:00
import org.gcube.spatial.data.geonetwork.GeoNetwork ;
import org.gcube.spatial.data.geonetwork.GeoNetworkPublisher ;
import org.gcube.spatial.data.geonetwork.LoginLevel ;
2017-05-15 14:45:20 +02:00
import org.gcube.spatial.data.geonetwork.configuration.Configuration ;
import org.gcube.spatial.data.geonetwork.model.Account ;
import org.gcube.spatial.data.geonetwork.model.Account.Type ;
2016-06-09 11:36:25 +02:00
import org.gcube.spatial.data.geonetwork.model.faults.AuthorizationException ;
import org.gcube.spatial.data.geonetwork.model.faults.MissingConfigurationException ;
import org.gcube.spatial.data.geonetwork.model.faults.MissingServiceEndpointException ;
2014-10-10 14:53:39 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2016-06-09 11:36:25 +02:00
2017-05-16 12:57:46 +02:00
2014-10-10 14:53:39 +02:00
/ * *
2016-06-09 11:36:25 +02:00
* The Class GeonetworkInstance .
2014-10-10 14:53:39 +02:00
*
2016-06-09 11:36:25 +02:00
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
2017-05-16 12:57:46 +02:00
* May 16 , 2017
2014-10-10 14:53:39 +02:00
* /
public class GeonetworkInstance {
private GeoNetworkPublisher geonetworkPublisher = null ;
2016-06-09 11:36:25 +02:00
/ * *
* Instantiates a new geonetwork instance .
* /
2014-10-10 14:53:39 +02:00
public GeonetworkInstance ( ) { } //FOR SERIALIZATION
2016-06-09 11:36:25 +02:00
2014-10-10 14:53:39 +02:00
private Logger logger = LoggerFactory . getLogger ( GeonetworkInstance . class ) ;
2016-06-09 11:36:25 +02:00
private String scope ;
2017-05-15 14:45:20 +02:00
private Account account ;
private LoginLevel level ;
private Type type ;
2014-10-10 14:53:39 +02:00
/ * *
2016-06-09 11:36:25 +02:00
* Instantiates a new geonetwork instance .
*
* @param scope the scope
* @throws GeonetworkInstanceException the geonetwork instance exception
2014-10-10 14:53:39 +02:00
* /
2016-06-09 11:36:25 +02:00
public GeonetworkInstance ( String scope ) throws GeonetworkInstanceException {
2017-05-15 14:45:20 +02:00
this ( scope , false , null , null ) ;
2014-10-10 14:53:39 +02:00
}
2017-09-05 11:25:26 +02:00
// /**
// * Creates a new configuration for Gis publisher with parameter passed in input. Executes the login on geonetwork instance if authenticate param is true, no otherwise
// * Use scope found in ScopeProvider
// *
// * @param authenticate the authenticate
// * @param level the level
// * @throws Exception the exception
// */
// public GeonetworkInstance(boolean authenticate, LoginLevel level) throws Exception {
// try {
// this.level = level;
// createInstanceGeonetworkPublisher(authenticate, level);
// } catch (Exception e) {
// logger.error("Sorry, an error occurred in getting geonetwork instance",e);
// throw new Exception("Sorry, an error occurred in getting geonetwork instance",e);
// }
// }
2014-10-10 14:53:39 +02:00
2017-05-15 14:45:20 +02:00
2014-10-10 14:53:39 +02:00
/ * *
2016-06-09 11:36:25 +02:00
* Instantiates a new geonetwork instance .
2017-05-15 14:45:20 +02:00
*
2016-06-09 11:36:25 +02:00
* @param scope the scope
* @param authenticate the authenticate
* @param level the level
2017-05-15 14:45:20 +02:00
* @param type the type
2016-06-09 11:36:25 +02:00
* @throws GeonetworkInstanceException the geonetwork instance exception
2014-10-10 14:53:39 +02:00
* /
2017-05-15 14:45:20 +02:00
public GeonetworkInstance ( String scope , boolean authenticate , LoginLevel level , Type type ) throws GeonetworkInstanceException {
this . scope = scope ;
this . level = level ;
this . type = type ;
2017-03-24 17:38:36 +01:00
String originalScope = ScopeProvider . instance . get ( ) ;
2016-06-09 12:54:23 +02:00
logger . debug ( " Instancing GeonetworkInstance with scope: " + scope + " , authenticate: " + authenticate + " , login level: " + level ) ;
2014-10-10 14:53:39 +02:00
try {
2016-06-09 11:36:25 +02:00
ScopeProvider . instance . set ( scope ) ;
logger . info ( " setting scope " + scope ) ;
2017-05-15 16:59:27 +02:00
createInstanceGeonetworkPublisher ( authenticate , level ) ;
2017-05-15 14:45:20 +02:00
if ( this . type ! = null ) {
Configuration config = geonetworkPublisher . getConfiguration ( ) ;
this . account = config . getScopeConfiguration ( ) . getAccounts ( ) . get ( type ) ;
}
//logger.info("Admin: "+config.getAdminAccount().getUser()+", Pwd: "+config.getAdminAccount().getPassword());
2014-10-10 14:53:39 +02:00
} catch ( Exception e ) {
String message = " Sorry, an error occurred in instancing geonetwork " ;
logger . warn ( message , e ) ;
2014-10-14 18:36:13 +02:00
throw new GeonetworkInstanceException ( message ) ;
2016-06-09 11:36:25 +02:00
} finally {
2017-03-24 17:38:36 +01:00
if ( originalScope ! = null ) {
2016-09-07 15:07:46 +02:00
ScopeProvider . instance . set ( originalScope ) ;
2017-03-24 17:38:36 +01:00
logger . info ( " scope provider set to orginal scope: " + originalScope ) ;
2016-09-07 15:07:46 +02:00
} else {
ScopeProvider . instance . reset ( ) ;
logger . info ( " scope provider reset " ) ;
}
2014-10-10 14:53:39 +02:00
}
}
/ * *
2016-06-09 11:36:25 +02:00
* Creates the instance geonetwork publisher .
*
* @param authenticate the authenticate
* @param level the level
* @throws Exception the exception
2014-10-10 14:53:39 +02:00
* /
2017-05-15 16:59:27 +02:00
private void createInstanceGeonetworkPublisher ( boolean authenticate , LoginLevel level ) throws Exception {
2016-06-09 12:54:23 +02:00
logger . debug ( " creating new geonetworkPublisher.. " ) ;
2016-06-09 11:36:25 +02:00
this . geonetworkPublisher = GeoNetwork . get ( ) ;
2017-05-15 14:45:20 +02:00
if ( authenticate & & this . level ! = null )
2017-05-15 16:59:27 +02:00
authenticateOnGeoenetwork ( level ) ;
2014-10-10 14:53:39 +02:00
}
2016-06-09 11:36:25 +02:00
2014-10-10 14:53:39 +02:00
/ * *
2016-06-09 11:36:25 +02:00
* Authenticate on geoenetwork .
*
* @param level the level
* @throws GeonetworkInstanceException the geonetwork instance exception
2014-10-10 14:53:39 +02:00
* /
2016-06-09 11:36:25 +02:00
private void authenticateOnGeoenetwork ( LoginLevel level ) throws GeonetworkInstanceException {
2014-10-10 14:53:39 +02:00
logger . trace ( " authenticating.. geonetworkPublisher is null? " + ( this . geonetworkPublisher = = null ) ) ;
if ( geonetworkPublisher = = null ) {
logger . trace ( " skipping authentication.. please createInstace " ) ;
return ;
}
2016-06-09 11:36:25 +02:00
try {
2014-10-14 18:36:13 +02:00
try {
2016-06-09 11:59:12 +02:00
logger . info ( " Authenticating with login level: " + level ) ;
2016-06-09 11:36:25 +02:00
this . geonetworkPublisher . login ( level ) ;
}
catch ( MissingConfigurationException | MissingServiceEndpointException e ) {
logger . error ( " MissingConfigurationException | MissingServiceEndpointException exception " , e ) ;
2014-10-15 11:01:04 +02:00
throw new GeonetworkInstanceException ( " Geonetwork authentication failed " ) ;
2014-10-14 18:36:13 +02:00
}
2016-06-09 11:36:25 +02:00
} catch ( AuthorizationException e ) {
logger . error ( " AuthorizationException " , e ) ;
throw new GeonetworkInstanceException ( " Geonetwork authentication failed " ) ;
2014-10-10 14:53:39 +02:00
}
2016-06-09 11:36:25 +02:00
logger . trace ( " authentication on geonetwork completed " ) ;
2014-10-10 14:53:39 +02:00
}
2016-06-09 11:36:25 +02:00
/ * *
* Gets the geonetwork publisher .
*
* @return the geonetwork publisher
* /
2014-10-10 14:53:39 +02:00
public GeoNetworkPublisher getGeonetworkPublisher ( ) {
return geonetworkPublisher ;
}
2017-05-15 14:45:20 +02:00
/ * *
2017-05-16 12:57:46 +02:00
* Gets the account .
*
2017-05-15 14:45:20 +02:00
* @return the account
* /
public Account getAccount ( ) {
return account ;
}
2014-10-10 14:53:39 +02:00
2016-06-09 11:36:25 +02:00
/ * *
* Gets the scope .
*
* @return the scope
* /
2014-10-10 14:53:39 +02:00
public String getScope ( ) {
2016-06-09 11:36:25 +02:00
return scope ;
2014-10-10 14:53:39 +02:00
}
2016-06-09 11:36:25 +02:00
/ * ( non - Javadoc )
* @see java . lang . Object # toString ( )
* /
2014-10-10 14:53:39 +02:00
@Override
public String toString ( ) {
2016-06-09 11:36:25 +02:00
2014-10-10 14:53:39 +02:00
StringBuilder builder = new StringBuilder ( ) ;
builder . append ( " GeonetworkInstance [geonetworkPublisher= " ) ;
builder . append ( geonetworkPublisher ) ;
2017-05-15 14:45:20 +02:00
builder . append ( " , logger= " ) ;
builder . append ( logger ) ;
2014-10-10 14:53:39 +02:00
builder . append ( " , scope= " ) ;
builder . append ( scope ) ;
2017-05-15 14:45:20 +02:00
builder . append ( " , account= " ) ;
builder . append ( account ) ;
builder . append ( " , level= " ) ;
builder . append ( level ) ;
builder . append ( " , type= " ) ;
builder . append ( type ) ;
2014-10-10 14:53:39 +02:00
builder . append ( " ] " ) ;
return builder . toString ( ) ;
}
}