Added SmartGear Init implementing ApplicationManager

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@174158 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-11-15 11:18:03 +00:00
parent 26ca78b976
commit 0e571e0963
14 changed files with 168 additions and 94 deletions

17
pom.xml
View File

@ -54,13 +54,6 @@
<artifactId>common-authorization</artifactId>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.apache.tomcat</groupId> -->
<!-- <artifactId>catalina</artifactId> -->
<!-- <version>6.0.16</version> -->
<!-- </dependency> -->
<dependency>
<groupId>org.gcube.contentmanagement</groupId>
<artifactId>storage-manager-wrapper</artifactId>
@ -103,11 +96,11 @@
</dependency>
<!-- jersey -->
<!-- <dependency> -->
<!-- <groupId>org.glassfish.jersey.ext</groupId> -->
<!-- <artifactId>jersey-mvc-jsp</artifactId> -->
<!-- <version>2.13</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.glassfish.jersey.ext</groupId> -->
<!-- <artifactId>jersey-mvc-jsp</artifactId> -->
<!-- <version>2.13</version> -->
<!-- </dependency> -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>

View File

@ -10,6 +10,14 @@ import javax.ws.rs.core.StreamingOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class SingleFileStreamingOutput.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Nov 15, 2018
*/
public class SingleFileStreamingOutput implements StreamingOutput {
private static final Logger log = LoggerFactory.getLogger(SingleFileStreamingOutput.class);
@ -17,6 +25,11 @@ public class SingleFileStreamingOutput implements StreamingOutput {
InputStream streamToWrite;
/**
* Instantiates a new single file streaming output.
*
* @param streamToWrite the stream to write
*/
public SingleFileStreamingOutput(InputStream streamToWrite) {
super();
this.streamToWrite = streamToWrite;
@ -24,9 +37,10 @@ public class SingleFileStreamingOutput implements StreamingOutput {
/**
* Overriding the write method to write request data directly to Jersey outputStream .
* @param outputStream
* @throws IOException
* @throws WebApplicationException
*
* @param outputStream the output stream
* @throws IOException Signals that an I/O exception has occurred.
* @throws WebApplicationException the web application exception
*/
@Override
public void write(OutputStream outputStream) throws IOException, WebApplicationException {
@ -35,6 +49,13 @@ public class SingleFileStreamingOutput implements StreamingOutput {
log.debug("StreamOutput written");
}
/**
* Copy.
*
* @param in the in
* @param out the out
* @throws IOException Signals that an I/O exception has occurred.
*/
private void copy(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[2048];
int readcount = 0;

View File

@ -2,6 +2,8 @@ package org.gcube.datatransfer.resolver;
import javax.ws.rs.Path;
import org.gcube.datatransfer.resolver.requesthandler.TokenSetter;
import org.gcube.datatransfer.resolver.services.CatalogueResolver;
import org.glassfish.jersey.server.ResourceConfig;
@Path("uri-resolver")
@ -9,9 +11,7 @@ public class UriResolver extends ResourceConfig {
public UriResolver() {
// Register all resources present under the package.
packages("org.gcube.datatransfer.resolver.services","org.gcube.datatransfer.resolver.requesthandler");
// register(JspMvcFeature.class);
// property(MvcFeature.TEMPLATE_BASE_PATH, "/WEB-INF/jsp");
packages(CatalogueResolver.class.getPackage().getName(), TokenSetter.class.getPackage().getName());
}
}

View File

@ -7,7 +7,7 @@ package org.gcube.datatransfer.resolver.caches;
import java.util.concurrent.TimeUnit;
import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileReader;
import org.gcube.datatransfer.resolver.listeners.UriResolverStartupListener;
import org.gcube.datatransfer.resolver.init.UriResolverSmartGearInit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -80,7 +80,7 @@ public class LoadingGeoExplorerApplicationURLCache {
if (scope == null || scope.isEmpty())
logger.warn("Scope is null or ermpty, skipping loadGisViewerApplicationURL");
ApplicationProfileReader reader = new ApplicationProfileReader(scope, UriResolverStartupListener.getGeoExplorerProfile().getGenericResource(), UriResolverStartupListener.getGeoExplorerProfile().getAppId(), false);
ApplicationProfileReader reader = new ApplicationProfileReader(scope, UriResolverSmartGearInit.getGeoExplorerProfile().getGenericResource(), UriResolverSmartGearInit.getGeoExplorerProfile().getAppId(), false);
String url = reader.getApplicationProfile().getUrl();
logger.info("With scope "+scope+" loaded the GeoExplorer Application URL "+url);
return url;

View File

@ -7,7 +7,7 @@ package org.gcube.datatransfer.resolver.caches;
import java.util.concurrent.TimeUnit;
import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileReader;
import org.gcube.datatransfer.resolver.listeners.UriResolverStartupListener;
import org.gcube.datatransfer.resolver.init.UriResolverSmartGearInit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -80,10 +80,10 @@ public class LoadingGisViewerApplicationURLCache {
if (scope == null || scope.isEmpty())
logger.warn("Scope is null or ermpty, skipping loadGisViewerApplicationURL");
ApplicationProfileReader reader = new ApplicationProfileReader(scope, UriResolverStartupListener.getGisViewerProfile().getGenericResource(), UriResolverStartupListener.getGisViewerProfile().getAppId(), false);
ApplicationProfileReader reader = new ApplicationProfileReader(scope, UriResolverSmartGearInit.getGisViewerProfile().getGenericResource(), UriResolverSmartGearInit.getGisViewerProfile().getAppId(), false);
if(reader.getApplicationProfile()==null){
logger.error("NO Appllication Profile "+UriResolverStartupListener.getGisViewerProfile().getAppId()+" found in the scope: "+scope+", returning null!");
logger.error("NO Appllication Profile "+UriResolverSmartGearInit.getGisViewerProfile().getAppId()+" found in the scope: "+scope+", returning null!");
return null;
}
String url = reader.getApplicationProfile().getUrl();

View File

@ -8,7 +8,7 @@ import java.util.concurrent.TimeUnit;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datatransfer.resolver.catalogue.resource.GetAllInfrastructureVREs;
import org.gcube.datatransfer.resolver.listeners.UriResolverStartupListener;
import org.gcube.datatransfer.resolver.init.UriResolverSmartGearInit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -76,8 +76,8 @@ public class LoadingVREsScopeCache {
try{
//POPULATE THE CACHE READING THE RESOURCE "CATALOGUE-RESOLVER"
logger.info("Trying to pre-populate VRE Names to Scope cache");
ScopeProvider.instance.set(UriResolverStartupListener.getRootContextScope());
Map<String, String> map = GetAllInfrastructureVREs.loadMapOFVreNameToScope(UriResolverStartupListener.getRootContextScope());
ScopeProvider.instance.set(UriResolverSmartGearInit.getRootContextScope());
Map<String, String> map = GetAllInfrastructureVREs.loadMapOFVreNameToScope(UriResolverSmartGearInit.getRootContextScope());
vresNameToScope.asMap().putAll(map);
logger.info("Cache populated with: "+vresNameToScope.asMap().toString());
//logger.info("Pre-Loaded CatalogueApplicationProfiles cache is: "+catalogueApplicationProfiles.asMap().toString());

View File

@ -1,7 +1,7 @@
/**
*
*/
package org.gcube.datatransfer.resolver;
package org.gcube.datatransfer.resolver.catalogue;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,7 +1,7 @@
/**
*
*/
package org.gcube.datatransfer.resolver;
package org.gcube.datatransfer.resolver.gis.geonetwork;
/**
@ -14,6 +14,4 @@ public interface GeonetworkRequestFilterParameters {
public static enum MODE {HARVEST, VRE};
public static enum VISIBILITY {PUB, PRV};
public static String REQUEST_DELIMITIER = "/$$";
public static String OWNER_PARAM = "OWNER";
}

View File

@ -0,0 +1,65 @@
/**
*
*/
package org.gcube.datatransfer.resolver.init;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The listener interface for receiving uriResolverServletContext events.
* The class that is interested in processing a uriResolverServletContext
* event implements this interface, and the object created
* with that class is registered with a component using the
* component's <code>addUriResolverServletContextListener<code> method. When
* the uriResolverServletContext event occurs, that object's appropriate
* method is invoked.
*
* @see UriResolverServletContextEvent
*/
@WebListener
public class UriResolverServletContextListener implements ServletContextListener {
private static Logger log = LoggerFactory.getLogger(UriResolverServletContextListener.class);
private static ServletContext servletContext;
/* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
*/
@Override
public void contextInitialized(ServletContextEvent event) {
servletContext = event.getServletContext();
log.info("Context Initialized at context path: "+servletContext.getContextPath());
}
/* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
*/
@Override
public void contextDestroyed(ServletContextEvent event) {
// Perform action during application's shutdown
}
/**
* Gets the servlet context.
*
* @return the servlet context
*/
public static ServletContext getServletContext() {
return servletContext;
}
}

View File

@ -1,7 +1,7 @@
/**
*
*/
package org.gcube.datatransfer.resolver.listeners;
package org.gcube.datatransfer.resolver.init;
import java.io.File;
import java.io.FileInputStream;
@ -10,40 +10,30 @@ import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebListener;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type;
import org.gcube.datatransfer.resolver.caches.LoadingGeonetworkInstanceCache;
import org.gcube.datatransfer.resolver.caches.LoadingGisViewerApplicationURLCache;
import org.gcube.datatransfer.resolver.caches.LoadingVREsScopeCache;
import org.gcube.datatransfer.resolver.gis.property.ApplicationProfilePropertyReader;
import org.gcube.datatransfer.resolver.gis.property.PropertyFileNotFoundException;
import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.configuration.container.ContainerConfiguration;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.smartgears.context.container.ContainerContext;
import org.gcube.smartgears.ApplicationManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The listener interface for receiving startup events.
* The class that is interested in processing a startup
* event implements this interface, and the object created
* with that class is registered with a component using the
* component's <code>addStartupListener<code> method. When
* the startup event occurs, that object's appropriate
* method is invoked.
* The Class UriResolverSmartGearInit.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Nov 2, 2018
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Nov 15, 2018
*/
@WebListener
public class UriResolverStartupListener implements ServletContextListener {
public class UriResolverSmartGearInit implements ApplicationManager {
private static Logger logger = LoggerFactory.getLogger(UriResolverStartupListener.class);
private static Logger logger = LoggerFactory.getLogger(UriResolverSmartGearInit.class);
public static final String GIS_VIEWER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES = "gisviewerappgenericresource.properties";
public static final String GEO_EXPLORER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES = "geoexplorerappgenericresource.properties";
@ -53,57 +43,67 @@ public class UriResolverStartupListener implements ServletContextListener {
public static final String ENV_SCOPE = "SCOPE"; //Environment Variable
private static String rootContextScope = null;
private static ApplicationProfilePropertyReader gisViewerProfile;
private static ApplicationProfilePropertyReader geoExplorerProfile;
/* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
*/
@Override
public void contextInitialized(ServletContextEvent event) {
try {
rootContextScope = loadScopeFromEnvironment();
ApplicationContext ctx = ContextProvider.get();
ContainerContext container = ctx.container();
logger.info("Is ContainerContext null? "+(container==null));
ContainerConfiguration contConfiguration = container.configuration();
logger.info("Is ContainerConfiguration null? "+(contConfiguration==null));
/* (non-Javadoc)
* @see org.gcube.smartgears.ApplicationManager#onInit()
*/
@Override
public void onInit() {
String rootScope = ctx.container().configuration().infrastructure();
logger.info("Is infrastructure null? "+(rootScope==null));
try {
logger.info("The ContextProvider read the infrastructure name: "+rootScope);
if(rootContextScope==null){
logger.info("The RootContextScope is null, getting it from ScopeProvider");
String scope = ScopeProvider.instance.get();
ScopeBean theScopeBean = new ScopeBean(scope);
logger.info("The ScopeBean is: "+theScopeBean.toString());
if(theScopeBean.is(Type.INFRASTRUCTURE)){
rootContextScope = theScopeBean.name();
rootContextScope = rootContextScope.startsWith("/")?rootContextScope:"/"+rootContextScope;
logger.info("The RootContextScope has value: "+rootContextScope);
}
}else{
//THE ROOT SCOPE has been initialized
gisViewerProfile = loadApplicationProfile(UriResolverServletContextListener.getServletContext(), GIS_VIEWER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES);
geoExplorerProfile = loadApplicationProfile(UriResolverServletContextListener.getServletContext(), GEO_EXPLORER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES);
//init the caches
new LoadingGeonetworkInstanceCache();
new LoadingGisViewerApplicationURLCache();
new LoadingVREsScopeCache();
//new LoadingCatalogueApplicationProfilesCache();
logger.info("Context initialized with: ");
logger.info("Scope: "+rootContextScope);
logger.info("GisViewerProfile [ID: "+gisViewerProfile.getAppId() + ", Generic Resource Type: "+gisViewerProfile.getGenericResource()+"]");
logger.info("GeoExplorerProfile [ID: "+geoExplorerProfile. getAppId() + ", Generic Resource Type: "+geoExplorerProfile.getGenericResource()+"]");
}
}
catch (Exception e) {
//
logger.error(e.getMessage(), e);
}
gisViewerProfile = loadApplicationProfile(event.getServletContext(), GIS_VIEWER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES);
geoExplorerProfile = loadApplicationProfile(event.getServletContext(), GEO_EXPLORER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES);
//init the caches
new LoadingGeonetworkInstanceCache();
new LoadingGisViewerApplicationURLCache();
new LoadingVREsScopeCache();
//new LoadingCatalogueApplicationProfilesCache();
logger.info("Context initialized with: ");
logger.info("Scope: "+rootContextScope);
logger.info("GisViewerProfile [ID: "+gisViewerProfile.getAppId() + ", Generic Resource Type: "+gisViewerProfile.getGenericResource()+"]");
logger.info("GeoExplorerProfile [ID: "+geoExplorerProfile. getAppId() + ", Generic Resource Type: "+geoExplorerProfile.getGenericResource()+"]");
}
/* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
*/
@Override
public void contextDestroyed(ServletContextEvent event) {
// Perform action during application's shutdown
}
/* (non-Javadoc)
* @see org.gcube.smartgears.ApplicationManager#onShutdown()
*/
@Override
public void onShutdown() {
// TODO Auto-generated method stub
}
/**
* Gets the currency.
@ -143,7 +143,7 @@ public class UriResolverStartupListener implements ServletContextListener {
String scopeFromEnv = System.getenv(ENV_SCOPE);
if(scopeFromEnv == null || scopeFromEnv.isEmpty())
throw new ServletException(UriResolverStartupListener.class.getName() +" cannot read scope from Environment Variable: "+ENV_SCOPE+", It is null or empty");
throw new ServletException(UriResolverServletContextListener.class.getName() +" cannot read scope from Environment Variable: "+ENV_SCOPE+", It is null or empty");
logger.info("Read scope: "+scopeFromEnv+" from Environment Variable: "+ENV_SCOPE);
return scopeFromEnv;
@ -183,5 +183,4 @@ public class UriResolverStartupListener implements ServletContextListener {
}
}
}

View File

@ -17,9 +17,9 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datatransfer.resolver.ResourceCatalogueCodes;
import org.gcube.datatransfer.resolver.caches.LoadingVREsScopeCache;
import org.gcube.datatransfer.resolver.catalogue.CatalogueRequest;
import org.gcube.datatransfer.resolver.catalogue.ResourceCatalogueCodes;
import org.gcube.datatransfer.resolver.catalogue.resource.CkanCatalogueConfigurationsReader;
import org.gcube.datatransfer.resolver.catalogue.resource.GatewayCKANCatalogueReference;
import org.gcube.datatransfer.resolver.catalogue.resource.GetAllInfrastructureVREs;

View File

@ -26,8 +26,6 @@ import javax.ws.rs.core.StreamingOutput;
import org.apache.commons.io.IOUtils;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datatransfer.resolver.GeonetworkRequestFilterParameters.MODE;
import org.gcube.datatransfer.resolver.GeonetworkRequestFilterParameters.VISIBILITY;
import org.gcube.datatransfer.resolver.SingleFileStreamingOutput;
import org.gcube.datatransfer.resolver.gis.GeonetworkAccessParameter;
import org.gcube.datatransfer.resolver.gis.GeonetworkInstance;
@ -35,6 +33,8 @@ import org.gcube.datatransfer.resolver.gis.GeonetworkServiceInterface;
import org.gcube.datatransfer.resolver.gis.exception.GeonetworkInstanceException;
import org.gcube.datatransfer.resolver.gis.geonetwork.FilterGetRecords;
import org.gcube.datatransfer.resolver.gis.geonetwork.GNAuthentication;
import org.gcube.datatransfer.resolver.gis.geonetwork.GeonetworkRequestFilterParameters.MODE;
import org.gcube.datatransfer.resolver.gis.geonetwork.GeonetworkRequestFilterParameters.VISIBILITY;
import org.gcube.datatransfer.resolver.gis.geonetwork.ReusableInputStream;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException;

View File

@ -62,7 +62,6 @@ public class StorageIDResolver {
*/
@GET
@Path("{storage-id}")
//@Path("{storage-id}")
public Response getStorageId(@Context HttpServletRequest httpRequest, @PathParam(STORAGE_ID) String storageId, @QueryParam(ConstantsResolver.QUERY_PARAM_FILE_NAME) String fileName, @QueryParam(ConstantsResolver.QUERY_PARAM_CONTENT_TYPE) String contentType, @QueryParam(ConstantsResolver.QUERY_PARAM_VALIDATION) Boolean validation) {
logger.info(this.getClass().getSimpleName()+" GET starts...");
//Checking mandatory parameter storageId

View File

@ -19,7 +19,6 @@ import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
import org.gcube.datatransfer.resolver.util.HTTPCallsUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -254,7 +253,7 @@ public class TestResolvers {
*
* @throws Exception the exception
*/
@Test
//Test
public void testCatalogueResolverCreatePubrivatItemURL() throws Exception{
String entityName = "dynamic_reporting";