@ -14,6 +14,7 @@ import org.gcube.portlets.user.uriresolvermanager.entity.Resolver;
import org.gcube.portlets.user.uriresolvermanager.entity.ServiceAccessPoint ;
import org.gcube.portlets.user.uriresolvermanager.entity.ServiceParameter ;
import org.gcube.portlets.user.uriresolvermanager.exception.IllegalArgumentException ;
import org.gcube.portlets.user.uriresolvermanager.exception.NotImplementedException ;
import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapException ;
import org.gcube.portlets.user.uriresolvermanager.readers.RuntimeResourceReader ;
import org.gcube.portlets.user.uriresolvermanager.readers.UriResolverMapReader ;
@ -25,21 +26,10 @@ import org.slf4j.LoggerFactory;
/ * *
* The Class UriResolverManager .
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
* Sep 6 , 2016
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it Sep 6 , 2016
* /
public class UriResolverManager {
/ * *
* Time to reload Runtime Resource Configuration
* /
//public static int RESET_DELAY = 15*60*1000; //15 MINUTES
/ * *
* Time to reload Runtime Resource Configuration
* /
//public static int RESET_TIME = RESET_DELAY; //15 MINUTES
private UriResolverMapReader uriResolverMapReader ;
private Map < String , Resolver > applicationTypes ;
private String applicationType ;
@ -77,34 +67,36 @@ public class UriResolverManager {
return usingReader ;
}
public static final Logger logger = LoggerFactory . getLogger ( UriResolverManager . class ) ;
public static final Logger LOG = LoggerFactory . getLogger ( UriResolverManager . class ) ;
/ * *
* Instantiates a new uri resolver manager .
* Precondition : set the scope into ScopeProvider { @link ScopeProvider # get ( ) }
* The scope is used to look up the generic resource with name : { @link UriResolverMapReader # URI_RESOLVER_MAP_RESOURCE_NAME } , secondary type : { @link UriResolverMapReader # URIRESOLVERMAP_SECONDARY_TYPE } from IS to map ApplicationType with its Resolver
* Instantiates a new uri resolver manager . Precondition : set the scope into
* ScopeProvider { @link ScopeProvider # get ( ) } The scope is used to look up the
* generic resource with name :
* { @link UriResolverMapReader # URI_RESOLVER_MAP_RESOURCE_NAME } , secondary type :
* { @link UriResolverMapReader # URIRESOLVERMAP_SECONDARY_TYPE } from IS to map
* ApplicationType with its Resolver
*
* @throws UriResolverMapException the uri resolver map exception
* @throws UriResolverMapException the uri resolver map exception
* @throws IllegalArgumentException the illegal argument exception
* /
public UriResolverManager ( ) throws UriResolverMapException , IllegalArgumentException {
public UriResolverManager ( ) throws UriResolverMapException , IllegalArgumentException {
try {
String scope = ScopeProvider . instance . get ( ) ;
logger . info ( "UriResolverManager is using scope: " + scope + ", read from ScopeProvider" ) ;
LOG . info ( "UriResolverManager is using scope: " + scope + ", read from ScopeProvider" ) ;
if ( scope = = null )
if ( scope = = null )
throw new UriResolverMapException ( "Scope is null, set scope into ScopeProvider!" ) ;
this . uriResolverMapReader = new UriResolverMapReader ( ) ;
this . applicationTypes = uriResolverMapReader . getApplicationTypes ( ) ;
// this.setTimerUriResolverReader(RESET_DELAY, RESET_TIME);
} catch ( UriResolverMapException e ) {
logger . error ( "UriResolverMapException: " , e ) ;
// this.setTimerUriResolverReader(RESET_DELAY, RESET_TIME);
} catch ( UriResolverMapException e ) {
LOG . error ( "UriResolverMapException: " , e ) ;
throw e ;
} catch ( Exception e ) {
logger . error ( "UriResolverManager: " , e ) ;
LOG . error ( "UriResolverManager: " , e ) ;
throw new UriResolverMapException ( "Map Application Type - Resources not found in IS" ) ;
}
}
@ -116,40 +108,42 @@ public class UriResolverManager {
* @throws IllegalArgumentException the illegal argument exception
* /
public void setApplicationType ( String applicationType ) throws IllegalArgumentException {
if ( ! this . applicationTypes . containsKey ( applicationType ) ) {
throw new IllegalArgumentException ( "Application type '" + applicationType + "' not found in Application Types: " + getApplicationTypes ( ) ) ;
if ( ! this . applicationTypes . containsKey ( applicationType ) ) {
throw new IllegalArgumentException ( "Application type '" + applicationType
+ "' not found in Application Types: " + getApplicationTypes ( ) ) ;
}
this . applicationType = applicationType ;
}
/ * *
* Instance a UriResolverManager
* Precondition : set the scope provider { @link ScopeProvider . instance . get ( ) }
* The scope is used to look up the generic resource { @link UriResolverMapReader # URI_RESOLVER_MAP } available in the infrastructure to map ApplicationType with its Resolver
* Instance a UriResolverManager Precondition : set the scope provider
* { @link ScopeProvider . instance . get ( ) } The scope is used to look up the generic
* resource { @link UriResolverMapReader # URI_RESOLVER_MAP } available in the
* infrastructure to map ApplicationType with its Resolver
*
* @param applicationType a ( valid ) key Application Type { @link UriResolverManager # getApplicationTypes ( ) }
* @throws UriResolverMapException the uri resolver map exception
* @param applicationType a ( valid ) key Application Type
* { @link UriResolverManager # getApplicationTypes ( ) }
* @throws UriResolverMapException the uri resolver map exception
* @throws IllegalArgumentException the illegal argument exception
* /
public UriResolverManager ( String applicationType ) throws UriResolverMapException , IllegalArgumentException {
public UriResolverManager ( String applicationType ) throws UriResolverMapException , IllegalArgumentException {
this ( ) ;
setApplicationType ( applicationType ) ;
}
/ * *
* Gets the link .
*
* @param applicationType the application type
* @param parameters the map of the parameters sent as HTTP query string
* @param shortLink if true the link is shorted otherwise none
* @param parameters the map of the parameters sent as HTTP query string
* @param shortLink if true the link is shorted otherwise none
* @return the link
* @throws IllegalArgumentException the illegal argument exception
* @throws UriResolverMapException the uri resolver map exception
* @throws UriResolverMapException the uri resolver map exception
* /
public String getLink ( String applicationType , Map < String , String > parameters , boolean shortLink ) throws IllegalArgumentException , UriResolverMapException {
public String getLink ( String applicationType , Map < String , String > parameters , boolean shortLink )
throws IllegalArgumentException , UriResolverMapException {
this . applicationType = applicationType ;
return getLink ( parameters , shortLink ) ;
}
@ -158,113 +152,147 @@ public class UriResolverManager {
* Gets the link .
*
* @param parameters the map of the parameters sent as HTTP query string
* @param shortLink if true the link is shorted otherwise none
* @param shortLink if true the link is shorted otherwise none
* @return the link
* @throws IllegalArgumentException the illegal argument exception
* @throws UriResolverMapException the uri resolver map exception
* @throws UriResolverMapException the uri resolver map exception
* /
public String getLink ( Map < String , String > parameters , boolean shortLink ) throws IllegalArgumentException , UriResolverMapException {
public String getLink ( Map < String , String > parameters , boolean shortLink )
throws IllegalArgumentException , UriResolverMapException {
if ( applicationType = = null )
if ( applicationType = = null )
throw new IllegalArgumentException ( "Application type is null" ) ;
Resolver resolver = this . applicationTypes . get ( applicationType ) ;
String link ;
String link = null ;
if ( parameters = = null )
if ( parameters = = null )
throw new IllegalArgumentException ( "Input Map parameters is null" ) ;
try {
// lockReader();
// lockReader();
if ( reader = = null ) {
logger . info ( "Runtime Resource Reader is null, istancing..." ) ;
if ( reader = = null ) {
LOG . info ( "Runtime Resource Reader is null, istancing..." ) ;
reader = new RuntimeResourceReader ( resolver . getResourceName ( ) ) ;
}
if ( resolver . getEntryName ( ) = = null | | resolver . getEntryName ( ) . isEmpty ( ) ) {
logger . warn ( "The entryname to " + resolver . getResourceName ( ) + " is null or empty, reading first Access Point!!" ) ;
if ( resolver . getEntryName ( ) = = null | | resolver . getEntryName ( ) . isEmpty ( ) ) {
LOG . warn ( "The entryname to " + resolver . getResourceName ( )
+ " is null or empty, reading first Access Point!!" ) ;
serviceAccessPoint = reader . getServiceAccessPoints ( ) . get ( 0 ) ;
} else {
logger . warn ( "Reading Access Point for Entry Name: " + resolver . getEntryName ( ) ) ;
} else {
LOG . warn ( "Reading Access Point for Entry Name: " + resolver . getEntryName ( ) ) ;
serviceAccessPoint = reader . getServiceAccessPointForEntryName ( resolver . getEntryName ( ) ) ;
if ( serviceAccessPoint = = null )
throw new UriResolverMapException ( "Entry Name " + resolver . getEntryName ( ) + " not found in Resource name: " + resolver . getResourceName ( ) ) ;
if ( serviceAccessPoint = = null )
throw new UriResolverMapException ( "Entry Name " + resolver . getEntryName ( )
+ " not found in Resource name: " + resolver . getResourceName ( ) ) ;
}
List < ServiceParameter > resourceParameters = serviceAccessPoint . getServiceParameters ( ) ;
// CHECK PARAMETERS
LOG . debug ( "Service parameters are: " + resourceParameters ) ;
// CHECK PARAMETERS
for ( ServiceParameter serviceParameter : resourceParameters ) {
if ( serviceParameter . isMandatory ( ) ) {
if ( ! parameters . containsKey ( serviceParameter . getKey ( ) ) ) {
throw new IllegalArgumentException ( "Mandatory service key (parameter) '" + serviceParameter . getKey ( ) + "' not found into input map" ) ;
if ( serviceParameter . isMandatory ( ) ) {
if ( ! parameters . containsKey ( serviceParameter . getKey ( ) ) ) {
throw new IllegalArgumentException ( "Mandatory service key (parameter) '"
+ serviceParameter . getKey ( ) + "' not found into input map" ) ;
}
}
}
String baseURI = serviceAccessPoint . getServiceUrl ( ) ;
//Encoding only the query string
try {
link = resolver . getLink ( baseURI , parameters ) ;
if ( shortLink )
return shortTheLink ( link ) ;
return link ;
} catch ( NotImplementedException e ) {
LOG . info ( "Specialized getLink not implemented, going to default implementation" ) ;
}
String linkDecoded = null ;
String queryString = null ;
if ( ! shortLink ) {
//not shortening so returning the link with the query string with only the parameters encoded
if ( ! shortLink ) {
// not shortening so returning the link with the query string with only the
// parameters encoded
LOG . info ( "getLink implemented via GET request and encoded query-string" ) ;
queryString = UrlEncoderUtil . encodeQuery ( parameters ) ;
String toReturn = String . format ( "%s?%s" , baseURI , queryString ) ;
logger . info ( "returning link with encoded parameters in the query string: " + toReturn ) ;
String toReturn = String . format ( "%s?%s" , baseURI , queryString ) ;
LOG . info ( "returning link with encoded parameters in the query string: " + toReturn ) ;
return toReturn ;
}
// Not specialized getLink has been implemented
// Short link required
// Going to build the GET request with query string decoded
LOG . info ( "getLink implemented via GET request and (decoded) query-string" ) ;
queryString = UrlEncoderUtil . toQueryString ( parameters ) ;
linkDecoded = String . format ( "%s?%s" , baseURI , queryString ) ;
linkDecoded = String . format ( "%s?%s" , baseURI , queryString ) ;
link = linkDecoded ;
logger . info ( "Created HTTP URI request (link): " + link ) ;
if ( shortLink ) {
try {
LOG . info ( "Created HTTP link: " + link ) ;
if ( shortLink ) {
try {
LOG . info ( "Short link requested, so encoding query string is required..." ) ;
String queryStringEncoded = UrlEncoderUtil . encodeString ( queryString ) ;
link = String . format ( "%s?%s" , baseURI , queryStringEncoded ) ;
logger . info ( "Encoded link is: " + link ) ;
logger . info ( "Shortner start.." ) ;
UrlShortener shortener = new UrlShortener ( ) ;
String shortedLink = shortener . shorten ( link ) ;
logger . info ( "Shorted link is: " + shortedLink ) ;
if ( shortedLink ! = null & & shortedLink . equals ( link ) ) {
//here the short link and the input link are identical
//so the shortening did not work
//I'm returning the decoded link because it is directly consumable via browser
logger . debug ( "Shorted link is equal to input link, returning decoded link: " + linkDecoded ) ;
link = String . format ( "%s?%s" , baseURI , queryStringEncoded ) ;
LOG . info ( "Encoded link is: " + link ) ;
LOG . info ( "Shortner starts.." ) ;
String shortedLink = shortTheLink ( link ) ;
LOG . info ( "Shorted link is: " + shortedLink ) ;
if ( shortedLink ! = null & & shortedLink . equals ( link ) ) {
// here the short link and the input link are identical
// so the shortening did not work
// I'm returning the decoded link because it is directly consumable via browser
LOG . debug ( "Shorted link is equal to input link, returning decoded link: " + linkDecoded ) ;
link = linkDecoded ;
} else {
// here the link is really shorted
logger . debug ( "The link is really shorted, returning it" ) ;
} else {
// here the link is really shorted
LOG . debug ( "The link is really shorted, returning it" ) ;
link = shortedLink ;
}
} catch ( Exception e ) {
logger . warn ( "An error occurred during link shortening: " , e ) ;
// here I'm returning the decoded link in case of error on shortening it
} catch ( Exception e ) {
LOG . warn ( "An error occurred during link shortening: " , e ) ;
// here I'm returning the decoded link in case of error on shortening it
link = linkDecoded ;
}
}
} catch ( IllegalArgumentException e ) {
logger . error ( "Uri Resolver IllegalArgumentException: " , e ) ;
} catch ( IllegalArgumentException e ) {
LOG . error ( "Uri Resolver IllegalArgumentException: " , e ) ;
throw e ;
} catch ( Exception e ) {
logger . error ( "Uri Resolver Exception: " , e ) ;
throw new UriResolverMapException ( "Uri Resolver error: " + e . getMessage ( ) ) ;
LOG . error ( "Uri Resolver Exception: " , e ) ;
throw new UriResolverMapException ( "Uri Resolver error: " + e . getMessage ( ) ) ;
}
return link ;
}
private String shortTheLink ( String link ) {
String toReturnLink = link ;
try {
UrlShortener shortener = new UrlShortener ( ) ;
String shortedLink = shortener . shorten ( link ) ;
LOG . info ( "Shorted link is: " + shortedLink ) ;
toReturnLink = shortedLink ;
} catch ( Exception e ) {
LOG . warn ( "Returning source link, an error occurred during link shortening: " , e ) ;
}
return toReturnLink ;
}
/ * *
* Gets the application types .
*
* @return the Application Types available
* /
public Set < String > getApplicationTypes ( ) {
public Set < String > getApplicationTypes ( ) {
return this . applicationTypes . keySet ( ) ;
}
@ -274,37 +302,40 @@ public class UriResolverManager {
* @param resolver the resolver
* @return the list
* @throws IllegalArgumentException the illegal argument exception
* @throws Exception the exception
* @throws Exception the exception
* /
public List < ServiceParameter > discoveryServiceParameters ( Resolver resolver ) throws IllegalArgumentException , Exception {
public List < ServiceParameter > discoveryServiceParameters ( Resolver resolver )
throws IllegalArgumentException , Exception {
try {
String scope = ScopeProvider . instance . get ( ) ;
logger . info ( "SiscoveryServiceParameters is using scope: " + scope + ", read from ScopeProvider" ) ;
LOG . info ( "SiscoveryServiceParameters is using scope: " + scope + ", read from ScopeProvider" ) ;
if ( scope = = null )
if ( scope = = null )
throw new UriResolverMapException ( "Scope is null, set scope into ScopeProvider!" ) ;
if ( resolver = = null )
if ( resolver = = null )
throw new IllegalArgumentException ( "Resolver is null, set Resolver" ) ;
RuntimeResourceReader reader = new RuntimeResourceReader ( resolver . getResourceName ( ) ) ;
ServiceAccessPoint serviceAccessPoint = null ;
if ( resolver . getEntryName ( ) = = null | | resolver . getEntryName ( ) . isEmpty ( ) ) {
logger . warn ( "The entryname to " + resolver . getResourceName ( ) + " is null or empty, reading first Access Point!!" ) ;
if ( resolver . getEntryName ( ) = = null | | resolver . getEntryName ( ) . isEmpty ( ) ) {
LOG . warn ( "The entryname to " + resolver . getResourceName ( )
+ " is null or empty, reading first Access Point!!" ) ;
serviceAccessPoint = reader . getServiceAccessPoints ( ) . get ( 0 ) ;
} else {
logger . info ( "Reading Access Point for entryname: " + resolver . getEntryName ( ) ) ;
} else {
LOG . info ( "Reading Access Point for entryname: " + resolver . getEntryName ( ) ) ;
serviceAccessPoint = reader . getServiceAccessPointForEntryName ( resolver . getEntryName ( ) ) ;
if ( serviceAccessPoint = = null )
throw new UriResolverMapException ( "Entry Name " + resolver . getEntryName ( ) + " not found in Resource name: " + resolver . getResourceName ( ) ) ;
if ( serviceAccessPoint = = null )
throw new UriResolverMapException ( "Entry Name " + resolver . getEntryName ( )
+ " not found in Resource name: " + resolver . getResourceName ( ) ) ;
}
return serviceAccessPoint . getServiceParameters ( ) ;
} catch ( Exception e ) {
logger . error ( "Uri Resolver error: " , e ) ;
throw new UriResolverMapException ( "Uri Resolver error: " + e . getMessage ( ) ) ;
LOG . error ( "Uri Resolver error: " , e ) ;
throw new UriResolverMapException ( "Uri Resolver error: " + e . getMessage ( ) ) ;
}
}
@ -314,7 +345,7 @@ public class UriResolverManager {
* @param applicationType the application type
* @return the resolver
* /
public Resolver getResolver ( String applicationType ) {
public Resolver getResolver ( String applicationType ) {
return this . applicationTypes . get ( applicationType ) ;
}
@ -323,14 +354,14 @@ public class UriResolverManager {
*
* @return a map Application Type - Resolver
* /
public Map < String , Resolver > getCapabilities ( ) {
public Map < String , Resolver > getCapabilities ( ) {
return this . applicationTypes ;
}
/ * *
* Sets the timer uri resolver reader .
*
* @param delay the delay
* @param delay the delay
* @param period the period
* /
public void setTimerUriResolverReader ( long delay , long period ) {
@ -342,45 +373,31 @@ public class UriResolverManager {
timer . schedule ( new TimerTask ( ) {
@Override
public void run ( ) {
logger . info ( "Timer Reset Runtime Resource running.." ) ;
LOG . info ( "Timer Reset Runtime Resource running.." ) ;
int counters = countReaders ( ) ;
if ( counters = = 0 ) {
logger . info ( "Reader not locked, resetting" ) ;
if ( counters = = 0 ) {
LOG . info ( "Reader not locked, resetting" ) ;
reader = null ;
} else
logger . info ( "Reader locked, counters is/are:" + counters + ", skipping" ) ;
} else
LOG . info ( "Reader locked, counters is/are:" + counters + ", skipping" ) ;
}
} , delay , period ) ;
}
/ * *
* Cancel timer uri resolver reader .
* /
public void cancelTimerUriResolverReader ( ) {
if ( timer ! = null )
public void cancelTimerUriResolverReader ( ) {
if ( timer ! = null )
timer . cancel ( ) ;
}
/ * *
* Invalid uri resolver reader .
* /
public void invalidUriResolverReader ( ) {
public void invalidUriResolverReader ( ) {
reader = null ;
}
/ *
public static void main ( String [ ] args ) {
try {
UriResolverManager manager = new UriResolverManager ( ) ;
System . out . println ( manager . getCapabilities ( ) ) ;
System . out . println ( manager . getApplicationTypes ( ) ) ;
} catch ( Exception e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
} * /
}