added application profile reader for "Gis Viewer Application"

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@100652 82a268e6-3cf1-43bd-a215-b396298e98cf
feature/18038
Francesco Mangiacrapa 10 years ago
parent 67e92e38ba
commit aa3e2cabae

@ -15,6 +15,7 @@
<distroDirectory>distro</distroDirectory>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@ -74,6 +75,12 @@
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- DOM -->
<dependency>
<groupId>org.w3c</groupId>

@ -0,0 +1,76 @@
package org.gcube.datatransfer.resolver.applicationprofile;
import java.io.Serializable;
public class ApplicationProfile implements Serializable {
/**
*
*/
private static final long serialVersionUID = 7308377041723589760L;
private String key;
private String name;
private String description;
private String imageUrl;
private String scope;
private String url;
public ApplicationProfile() {
super();
}
public ApplicationProfile(String key, String name, String description, String imageUrl, String scope, String url) {
super();
this.key = key;
this.name = name;
this.description = description;
this.imageUrl = imageUrl;
this.scope = scope;
this.url = url;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return "ApplicationProfile [key=" + key + ", name=" + name + ", description="
+ description + ", imageUrl=" + imageUrl + ", scope=" + scope
+ ", url=" + url + "]";
}
}

@ -0,0 +1,8 @@
package org.gcube.datatransfer.resolver.applicationprofile;
@SuppressWarnings("serial")
public class ApplicationProfileNotFoundException extends Exception {
public ApplicationProfileNotFoundException(String message) {
super(message);
}
}

@ -0,0 +1,203 @@
package org.gcube.datatransfer.resolver.applicationprofile;
import static org.gcube.resources.discovery.icclient.ICFactory.client;
import java.io.StringReader;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.gcube.common.resources.gcore.utils.XPathHelper;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.Query;
import org.gcube.resources.discovery.client.queries.impl.QueryBox;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
/**
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* @Oct 13, 2014
*
*/
public class ApplicationProfileReader {
protected static final String RESOURCE_PROFILE_BODY_END_POINT_URL_TEXT = "/Resource/Profile/Body/EndPoint/URL/text()";
protected static final String RESOURCE_PROFILE_BODY_END_POINT_SCOPE_TEXT = "/Resource/Profile/Body/EndPoint/Scope/text()";
protected static final String RESOURCE_PROFILE_BODY_TEXT = "/Resource/Profile/Body/text()";
protected static final String RESOURCE_PROFILE_BODY_THUMBNAIL_URL_TEXT = "/Resource/Profile/Body/ThumbnailURL/text()";
protected static final String RESOURCE_PROFILE_BODY_APP_ID_TEXT = "/Resource/Profile/Body/AppId/text()";
protected static final String RESOURCE_PROFILE_DESCRIPTION_TEXT = "/Resource/Profile/Description/text()";
protected static final String RESOURCE_PROFILE_NAME_TEXT = "/Resource/Profile/Name/text()";
private Logger logger = Logger.getLogger(ApplicationProfileReader.class);
private String secondaryType;
private String appId;
private String scope;
private ApplicationProfile applicationProfile;
/**
*
* @param scope - the scope to be searched
* @param genericResource - the name of generic resource
* @param portletClassName - the AppId of generic resource
*/
public ApplicationProfileReader(String scope, String secondaryType, String portletClassName) {
this.scope = scope;
this.secondaryType = secondaryType;
this.appId = portletClassName;
this.applicationProfile = readProfileFromInfrastrucure();
}
public ApplicationProfile getApplicationProfile() {
return applicationProfile;
}
/**
* this method looks up the applicationProfile profile among the ones available in the infrastructure
* @param portletClassName your servlet class name will be used ad unique identifier for your applicationProfile
* @return the applicationProfile profile
*/
private ApplicationProfile readProfileFromInfrastrucure() {
ApplicationProfile appProf = new ApplicationProfile();
String queryString = GcubeQuery.getGcubeGenericQueryString(secondaryType, appId);
logger.info("Trying to fetch applicationProfile profile from the infrastructure for " + secondaryType + " scope: " + scope);
try {
String infra = ScopeUtil.getInfrastructureNameFromScope(this.scope);
ScopeProvider.instance.set(infra);
logger.info("scope provider set instance: "+infra);
Query q = new QueryBox(queryString);
DiscoveryClient<String> client = client();
List<String> appProfile = client.submit(q);
if (appProfile == null || appProfile.size() == 0)
throw new ApplicationProfileNotFoundException("Your applicationProfile is not registered in the infrastructure");
else {
String elem = appProfile.get(0);
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Node node = docBuilder.parse(new InputSource(new StringReader(elem))).getDocumentElement();
XPathHelper helper = new XPathHelper(node);
List<String> currValue = null;
currValue = helper.evaluate(RESOURCE_PROFILE_NAME_TEXT);
if (currValue != null && currValue.size() > 0) {
appProf.setName(currValue.get(0));
}
else throw new ApplicationProfileNotFoundException("Your applicationProfile NAME was not found in the profile");
currValue = helper.evaluate(RESOURCE_PROFILE_DESCRIPTION_TEXT);
if (currValue != null && currValue.size() > 0) {
appProf.setDescription(currValue.get(0));
}
else logger.warn("No Description exists for " + appProf.getName());
currValue = helper.evaluate(RESOURCE_PROFILE_BODY_APP_ID_TEXT);
if (currValue != null && currValue.size() > 0) {
appProf.setKey(currValue.get(0));
}
else throw new ApplicationProfileNotFoundException("Your applicationProfile ID n was not found in the profile, consider adding <AppId> element in <Body>");
currValue = helper.evaluate(RESOURCE_PROFILE_BODY_THUMBNAIL_URL_TEXT);
if (currValue != null && currValue.size() > 0) {
appProf.setImageUrl(currValue.get(0));
}
else{
logger.warn("Null or empty <ThumbnailURL> element in <Body>" + appProf.getName());
}
currValue = helper.evaluate("/Resource/Profile/Body/EndPoint[Scope='"+scope.toString()+"']/Scope/text()");
if (currValue != null && currValue.size() > 0) {
List<String> scopes = currValue;
String currentScope = scopes.get(0);
int slashCount = StringUtils.countMatches(currentScope, "/");
if(slashCount < 3){//CASE not VRE - set session scope
logger.info("Scope "+ scope.toString() + " is not a VRE");
List<String> listSessionScope = helper.evaluate("/Resource/Profile/Body/EndPoint[Scope='"+scope.toString()+"']/Sessionscope/text()"); //get session scope of i+1-mo scope
if(listSessionScope!=null && listSessionScope.size()>0){ //If sessions scope exists
logger.trace("setting session scope "+ listSessionScope.get(0));
appProf.setScope(listSessionScope.get(0));
}
else{
logger.trace("session scope not exists setting scope "+ scope.toString());
appProf.setScope(scope.toString());
}
}
else{ //CASE IS A VRE
logger.info("Scope "+ scope.toString() + " is a VRE");
appProf.setScope(scope.toString());
}
//RETRIEVE URL
currValue = helper.evaluate("/Resource/Profile/Body/EndPoint[Scope='"+scope.toString()+"']/URL/text()");
if (currValue != null && currValue.size() > 0) {
String url = currValue.get(0);
// System.out.println("URL "+url);
if(url!=null)
appProf.setUrl(url);
else
throw new ApplicationProfileNotFoundException("Your applicationProfile URL was not found in the profile for Scope: " + scope.toString());
}
else throw new ApplicationProfileNotFoundException("Your applicationProfile URL was not found in the profile for Scope: " + scope.toString());
}
else throw new ApplicationProfileNotFoundException("Your applicationProfile with scope "+scope.toString()+" was not found in the profile, consider adding <EndPoint><Scope> element in <Body>");
return appProf;
}
} catch (Exception e) {
logger.error("Error while trying to fetch applicationProfile profile from the infrastructure");
e.printStackTrace();
return null;
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("ApplicationProfileReader [secondaryType=");
builder.append(secondaryType);
builder.append(", appId=");
builder.append(appId);
builder.append(", scope=");
builder.append(scope);
builder.append(", applicationProfile=");
builder.append(applicationProfile);
builder.append("]");
return builder.toString();
}
/*
public static void main(String[] args) {
String portletClassName = "org.gcube.portlets.user.gisviewerapp.server.GisViewerAppServiceImpl";
String scope ="/gcube";
String secondaryType = "ApplicationProfile";
ApplicationProfileReader reader = new ApplicationProfileReader(scope, secondaryType, portletClassName);
System.out.println(reader);
}*/
}

@ -0,0 +1,14 @@
package org.gcube.datatransfer.resolver.applicationprofile;
public class GcubeQuery {
public static String getGcubeGenericQueryString(String secondaryType, String appId){
return "for $profile in collection('/db/Profiles/GenericResource')//Resource " +
"where $profile/Profile/SecondaryType/string() eq '"+secondaryType+"' and $profile/Profile/Body/AppId/string() " +
" eq '" + appId + "'" +
"return $profile";
}
}

@ -0,0 +1,43 @@
/**
*
*/
package org.gcube.datatransfer.resolver.applicationprofile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* @Oct 13, 2014
*
*/
public class ScopeUtil {
private static final String SCOPE_SEPARATOR = "/";
public static final Logger logger = LoggerFactory.getLogger(ScopeUtil.class);
public static String getInfrastructureNameFromScope(String scope) throws Exception{
if(scope==null || scope.isEmpty()){
throw new Exception("Scope is null or empty");
}
if(!scope.startsWith(SCOPE_SEPARATOR)){
logger.warn("Input scope: "+scope+" not have / is a really scope?");
scope = SCOPE_SEPARATOR+scope;
logger.warn("Tentative as scope: "+scope);
}
String[] splitScope = scope.split(SCOPE_SEPARATOR);
String rootScope = SCOPE_SEPARATOR + splitScope[1];
if(rootScope.length()<2){
throw new Exception("Infrastructure name not found in "+scope);
}
return rootScope;
}
}

@ -4,6 +4,8 @@
package org.gcube.datatransfer.resolver.gis;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.net.URLEncoder;
import java.util.HashMap;
@ -17,8 +19,12 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileReader;
import org.gcube.datatransfer.resolver.applicationprofile.ScopeUtil;
import org.gcube.datatransfer.resolver.gis.GeoProfileReader.GEO_SERVICE;
import org.gcube.datatransfer.resolver.gis.entity.ServerParameters;
import org.gcube.datatransfer.resolver.gis.property.GisViewerAppGenericResourcePropertyReader;
import org.gcube.datatransfer.resolver.gis.property.PropertyFileNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -29,6 +35,14 @@ import org.slf4j.LoggerFactory;
*/
public class GisResolver extends HttpServlet{
/**
*
*/
private static final String UTF_8 = "UTF-8";
/**
*
*/
private static final String TEXT_PLAIN = "text/plain";
/**
*
*/
@ -46,12 +60,14 @@ public class GisResolver extends HttpServlet{
/** The logger. */
private static final Logger logger = LoggerFactory.getLogger(GisResolver.class);
protected Map<String, ServerParameters> cachedServerParams;
protected Map<String, ServerParameters> cachedServerParams; //A cache scope - geonetwork paramerteres
protected Map<String, String> cachedGisViewerApplHostname; //A cache scope - GisViewerApp hostname
private Timer timer;
private GisViewerAppGenericResourcePropertyReader gisViewerAppPropertyReader;
//FIVE MINUTES
public static final long CACHE_RESET_TIME = 5*60*1000;
public static final long CACHE_RESET_TIME = 10*60*1000;
@Override
public void init() throws ServletException {
@ -62,15 +78,16 @@ public class GisResolver extends HttpServlet{
public void run() {
logger.info("Timer resetting cache..");
reseCacheServerParameters();
reseGisViewerAppEndPoint();
reseCacheGisViewerApplicationHostname();
}
}, 0, CACHE_RESET_TIME);
}
protected ServerParameters getCachedServerParameters(String scope) throws Exception{
if(cachedServerParams==null){
if(cachedServerParams==null)
reseCacheServerParameters();
}
logger.info("Tentative to recovering gis server param from cache to scope "+scope);
ServerParameters serverParam = cachedServerParams.get(scope);
@ -81,7 +98,7 @@ public class GisResolver extends HttpServlet{
try {
serverParam = reader.retrieveGisParameters(scope, GEO_SERVICE.GEONETWORK);
cachedServerParams.put(scope, serverParam);
logger.info("Updated cache! Scope "+scope+" linking "+serverParam);
logger.info("Updated Gis server cache! Scope "+scope+" linking "+serverParam);
} catch (Exception e) {
logger.error("An error occurred on reading application profile to "+GEO_SERVICE.GEONETWORK, e);
throw new Exception("Sorry, An error occurred on reading configuration to "+GEO_SERVICE.GEONETWORK);
@ -99,6 +116,46 @@ public class GisResolver extends HttpServlet{
logger.info("Cache server params reset!");
}
private void reseCacheGisViewerApplicationHostname(){
cachedGisViewerApplHostname = new HashMap<String, String>();
logger.info("Cache Gis Viewer Hostname reset!");
}
private void reseGisViewerAppEndPoint(){
try {
gisViewerAppPropertyReader = new GisViewerAppGenericResourcePropertyReader();
logger.info("GisViewerApp end point updated!");
} catch (PropertyFileNotFoundException e) {
logger.error("Error on reset GisViewerAppEndPoint ",e);
}
}
protected String getGisViewerApplicationURL(String scope) throws Exception{
if(cachedGisViewerApplHostname==null)
reseCacheGisViewerApplicationHostname();
String infra = ScopeUtil.getInfrastructureNameFromScope(scope);
logger.info("Tentative to recovering gis viewer application hostname from cache to scope "+scope);
String gisViewerAppHostname = cachedGisViewerApplHostname.get(infra);
if(gisViewerAppHostname==null){
logger.info("Gis viewer application hostname is null, reading from application profile..");
if(gisViewerAppPropertyReader==null)
gisViewerAppPropertyReader = new GisViewerAppGenericResourcePropertyReader();
ApplicationProfileReader reader = new ApplicationProfileReader(infra, gisViewerAppPropertyReader.getGenericResource(), gisViewerAppPropertyReader.getAppId());
String url = reader.getApplicationProfile().getUrl();
cachedGisViewerApplHostname.put(infra, url);
logger.info("Updated GisViewerApplication cache! Scope "+scope+" linking "+url);
return url;
}else
logger.info("Cache Gis viewer application is not null using it");
return gisViewerAppHostname;
}
/* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@ -130,17 +187,26 @@ public class GisResolver extends HttpServlet{
ServerParameters geonetworkParams = getCachedServerParameters(scope);
String wmsRequest = getLayerWmsRequest(scope, gisUUID, geonetworkParams);
logger.info("wms url is: " + wmsRequest);
wmsRequest = URLEncoder.encode(wmsRequest, UTF_8);
logger.info("encoded WMS url is: " + wmsRequest);
String gisPortletUrl = getGisViewerApplicationURL(scope);
logger.info("Gis Viewer Application url is: " + gisPortletUrl);
// logger.info("WmsRequest is: " + wmsRequest);
// wmsRequest = encodeURLWithParamDelimiter(wmsRequest);
// logger.info("Encoded url is: " + wmsRequest);
// wmsRequest = appendParamReplacement(wmsRequest);
logger.info("wms url is: " + wmsRequest);
wmsRequest = URLEncoder.encode(wmsRequest, "UTF-8");
logger.info("encoded WMS url is: " + wmsRequest);
String gisPortletUrl = "http://pc-mangiacrapa.isti.cnr.it:8080/gis-viewer-app-1.0.0-SNAPSHOT/?";
gisPortletUrl+="wmsrequest="+wmsRequest;
urlRedirect(req, resp, gisPortletUrl);
gisPortletUrl+="?wmsrequest="+wmsRequest;
resp.setContentType(TEXT_PLAIN);
resp.setCharacterEncoding(UTF_8);
PrintWriter out = resp.getWriter();
out.println(gisPortletUrl);
out.close();
// urlRedirect(req, resp, gisPortletUrl);
} catch (Exception e) {
logger.error("Exception:", e);
String error = "Sorry, an error occurred on resolving request with UUID "+gisUUID+" and scope "+scope+". Please, contact support!";
@ -261,12 +327,17 @@ public class GisResolver extends HttpServlet{
ServerParameters geonetworkParams = gisResolver.getCachedServerParameters(scope);
String wmsRequest = gisResolver.getLayerWmsRequest(scope, UUID, geonetworkParams);
logger.info("Final url is: " + wmsRequest);
wmsRequest = URLEncoder.encode(wmsRequest, "UTF-8");
wmsRequest = URLEncoder.encode(wmsRequest, UTF_8);
logger.info("Encoded WMS request is: " + wmsRequest);
String gisPortletUrl = "http://127.0.0.1:8080/gis-viewer-app-1.0.0-SNAPSHOT/?";
gisPortletUrl+="wmsrequest="+wmsRequest;
logger.info("gisPortletUrl: " + gisPortletUrl);
String gisPortletUrl = gisResolver.getGisViewerApplicationURL(scope);
logger.info("Gis Viewer Application url is: " + gisPortletUrl);
// logger.info("WmsRequest is: " + wmsRequest);
// wmsRequest = encodeURLWithParamDelimiter(wmsRequest);
// logger.info("Encoded url is: " + wmsRequest);
// wmsRequest = appendParamReplacement(wmsRequest);
gisPortletUrl+="?wmsrequest="+wmsRequest;
System.out.println(gisPortletUrl);
// urlRedirect(req, resp, gisPortletUrl);
} catch (Exception e) {
// TODO Auto-generated catch block

@ -0,0 +1,61 @@
package org.gcube.datatransfer.resolver.gis.property;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
public class GisViewerAppGenericResourcePropertyReader {
protected static final String GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES = "gisviewerappgenericresource.properties";
protected static final String SECONDARY_TYPE = "SECONDARY_TYPE";
protected static final String APP_ID = "APP_ID";
private String appId;
private String genericResource;
private Logger logger = Logger.getLogger(GisViewerAppGenericResourcePropertyReader.class);
public GisViewerAppGenericResourcePropertyReader() throws PropertyFileNotFoundException {
Properties prop = new Properties();
try {
InputStream in = (InputStream) GisViewerAppGenericResourcePropertyReader.class.getResourceAsStream(GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES);
// load a properties file
prop.load(in);
// get the property value - the application Id
this.appId = prop.getProperty(APP_ID);
this.genericResource = prop.getProperty(SECONDARY_TYPE);
} catch (IOException e) {
logger.error("An error occurred on read property file "+e, e);
throw new PropertyFileNotFoundException("An error occurred on read property file "+e);
}
}
public String getAppId() {
return appId;
}
public String getGenericResource() {
return genericResource;
}
public static void main(String[] args) {
try {
GisViewerAppGenericResourcePropertyReader reader = new GisViewerAppGenericResourcePropertyReader();
System.out.println(reader.getAppId());
System.out.println(reader.getGenericResource());
} catch (PropertyFileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

@ -0,0 +1,8 @@
package org.gcube.datatransfer.resolver.gis.property;
@SuppressWarnings("serial")
public class PropertyFileNotFoundException extends Exception {
public PropertyFileNotFoundException(String message) {
super(message);
}
}

@ -0,0 +1,11 @@
# Property files
#
# author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
# created 02/2013
#
# The generic resource that describes the properties to open
# an item from workspace
#
SECONDARY_TYPE = ApplicationProfile
APP_ID = org.gcube.portlets.user.gisviewerapp.server.GisViewerAppServiceImpl

@ -1,5 +1,5 @@
# Set root category priority to WARN and its only appender to A1.
log4j.rootCategory=DEBUG, A0
log4j.rootCategory=ERROR, A0
log4j.appender.A0=org.apache.log4j.ConsoleAppender
log4j.appender.A0.layout=org.apache.log4j.PatternLayout
@ -9,5 +9,5 @@ log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern= %d{HH:mm:ss,SSS} %-5p %c{2} [%t,%M:%L] %m%n
#log4j.category.org.gcube=TRACE,A1
log4j.category.org.gcube=DEBUG,A1
log4j.category.org.gcube=INFO,A1
log4j.additivity.org.gcube=false
Loading…
Cancel
Save