added StaticFileController

This commit is contained in:
Francesco Mangiacrapa 2024-05-02 12:35:38 +02:00
parent bc3b6c4c04
commit ebad8120df
5 changed files with 79 additions and 15 deletions

10
pom.xml
View File

@ -159,11 +159,11 @@
<artifactId>authorization-client</artifactId>
</dependency>
<!-- <dependency> -->
<!-- <groupId>com.fasterxml.jackson.core</groupId> -->
<!-- <artifactId>jackson-databind</artifactId> -->
<!-- <version>${jackson.version}</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>com.fasterxml.jackson.core</groupId> -->
<!-- <artifactId>jackson-databind</artifactId> -->
<!-- <version>${jackson.version}</version> -->
<!-- </dependency> -->
<dependency>
<groupId>org.gcube.common</groupId>

View File

@ -30,8 +30,7 @@ public class ConstantsResolver {
public static final String QUERY_PARAM_CONTENT_TYPE = "contentType";
public static final String QUERY_PARAM_FILE_NAME = "fileName";
//img is the path to refer images from https://data.dev.d4science.org/img/*
public static final String[] resourcesHardCoded = { "ctlg", "img" };
public static final String[] resourcesHardCoded = { "ctlg" };
// The default resource candidate to manage the input requests not matching any
// resolver services

View File

@ -14,12 +14,12 @@ import org.glassfish.jersey.server.ResourceConfig;
@ManagedBy(UriResolverSmartGearManagerInit.class)
public class UriResolver extends ResourceConfig {
public UriResolver() {
// Register all resources present under the package.
packages(CatalogueResolver.class.getPackage().getName(), RequestHandler.class.getPackage().getName(), BackCatalogueResolver.class.getPackage().getName());
packages(DataMinerInvocation.class.getPackage().getName());
//register(JspMvcFeature.class);
//property(JspMvcFeature.TEMPLATE_BASE_PATH, "/WEB-INF/jsp");
}
public UriResolver() {
// Register all resources present under the package.
packages(CatalogueResolver.class.getPackage().getName(), RequestHandler.class.getPackage().getName(), BackCatalogueResolver.class.getPackage().getName());
packages(DataMinerInvocation.class.getPackage().getName());
//
// register(JspMvcFeature.class);
// property(JspMvcFeature.TEMPLATE_BASE_PATH, "/WEB-INF/img");
}
}

View File

@ -0,0 +1,65 @@
/**
*
*/
package org.gcube.datatransfer.resolver.services;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class StaticFileController.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* May 2, 2024
*/
@Path("pub")
public class StaticFileController {
private static Logger logger = LoggerFactory.getLogger(StaticFileController.class);
/**
* To doc.
*
* @param req the req
* @return the input stream
* @throws WebApplicationException the web application exception
*/
@GET
@Path("/{any: .*}")
public InputStream toFile(@Context HttpServletRequest req) throws WebApplicationException {
logger.info(StaticFileController.class.getSimpleName() + " toFile called");
String pathInfo = req.getPathInfo();
logger.debug("pathInfo {}", pathInfo);
try {
logger.info("going to {}", pathInfo);
String realPath = req.getServletContext().getRealPath(pathInfo);
return new FileInputStream(new File(realPath));
} catch (Exception e) {
if (!(e instanceof WebApplicationException)) {
// UNEXPECTED EXCEPTION managing it as WebApplicationException
String error = pathInfo + " not found. Please, contact the support!";
throw ExceptionManager.internalErrorException(req, error, this.getClass(), null);
}
// ALREADY MANAGED AS WebApplicationException
logger.error("Exception:", e);
throw (WebApplicationException) e;
}
}
}

View File

Before

Width:  |  Height:  |  Size: 55 KiB

After

Width:  |  Height:  |  Size: 55 KiB