updated GeoportalExporter

This commit is contained in:
Francesco Mangiacrapa 2024-04-19 14:58:59 +02:00
parent 324099ceb9
commit 06f87b7956
8 changed files with 133 additions and 97 deletions

12
pom.xml
View File

@ -137,12 +137,12 @@
<version>${jersey.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-mvc-jsp</artifactId>
<version>${jersey.version}</version>
<scope>compile</scope>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.glassfish.jersey.ext</groupId> -->
<!-- <artifactId>jersey-mvc-jsp</artifactId> -->
<!-- <version>${jersey.version}</version> -->
<!-- <scope>compile</scope> -->
<!-- </dependency> -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>

View File

@ -9,7 +9,6 @@ import org.gcube.datatransfer.resolver.services.CatalogueResolver;
import org.gcube.datatransfer.resolver.services.tobackward.BackCatalogueResolver;
import org.gcube.smartgears.annotations.ManagedBy;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;
@Path("uri-resolver")
@ManagedBy(UriResolverSmartGearManagerInit.class)
@ -19,8 +18,8 @@ public class UriResolver extends ResourceConfig {
// 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");
//register(JspMvcFeature.class);
//property(JspMvcFeature.TEMPLATE_BASE_PATH, "/WEB-INF/jsp");
}
}

View File

@ -35,7 +35,7 @@ public class UriResolverServletContextListener implements ServletContextListener
public void contextInitialized(ServletContextEvent event) {
servletContext = event.getServletContext();
log.info("Context Initialized at context path: "+servletContext.getContext("/"));
log.info("Context Initialized at context path: "+servletContext.getContextPath());
}
@Override

View File

@ -14,7 +14,6 @@ import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.glassfish.jersey.server.mvc.Viewable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -39,7 +38,7 @@ public class DocsGenerator {
*/
@GET
@Path("/{any: .*}")
public Viewable toDoc(@Context HttpServletRequest req) throws WebApplicationException {
public InputStream toDoc(@Context HttpServletRequest req) throws WebApplicationException {
logger.info(DocsGenerator.class.getSimpleName() + " toDoc called");
String pathInfo = req.getPathInfo();
@ -52,10 +51,8 @@ public class DocsGenerator {
logger.info("going to {}", pathInfo);
//String realPath = req.getServletContext().getRealPath(pathInfo);
//return new FileInputStream(new File(realPath));
return new Viewable(pathInfo);
String realPath = req.getServletContext().getRealPath(pathInfo);
return new FileInputStream(new File(realPath));
} catch (Exception e) {

View File

@ -1,7 +1,6 @@
package org.gcube.datatransfer.resolver.services;
import java.io.InputStream;
import java.net.URL;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
@ -29,10 +28,11 @@ import org.gcube.datatransfer.resolver.ConstantsResolver;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
import org.gcube.datatransfer.resolver.util.SingleFileStreamingOutput;
import org.glassfish.jersey.server.mvc.Viewable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonObject;
/**
* The Class GeoportalExporter.
*
@ -43,6 +43,8 @@ import org.slf4j.LoggerFactory;
@Path("geoportal")
public class GeoportalExporter {
private static final String PDF_CODE = "pdfCode";
public static final String EXPORT_TYPE = "type";
public static final String PATH_PROJECT_ID = GeoportalResolver.PATH_PROJECT_ID;
@ -102,7 +104,9 @@ public class GeoportalExporter {
if (checked) {
if (userAgentName != null) {
LOG.info("Serving request as User-Agent {}", userAgentName);
String entity = entityHTMLMessage("Exporting as PDF...", "The project with " + projectID, true);
String pollingCode = ucdID+"_"+projectID+"_"+System.currentTimeMillis();
String viewPdfURL = req.getServletContext()+"/view/"+pollingCode;
String entity = entityHTMLMessage("Exporting as PDF...", "The project with " + projectID, true, viewPdfURL);
return Response.ok(entity).encoding("UTF-8").header(ConstantsResolver.CONTENT_TYPE, "text/html")
.build();
} else {
@ -132,67 +136,66 @@ public class GeoportalExporter {
}
/**
* Resolve geoportal no app def.
*
* @param req the req
* @param vreName the vre name
* @param exportType the export type
* @param ucdID the ucd ID
* @param projectID the project ID
* @return the response
* @throws WebApplicationException the web application exception
*/
@GET
@Path("/export/{type}/view/{usecase_id}/{project_id}")
@Produces({ MediaType.TEXT_HTML })
public Viewable exportView(@Context HttpServletRequest req, @PathParam(EXPORT_TYPE) String export_type,
@PathParam(PATH_USECASE_ID) String ucdID, @PathParam(PATH_PROJECT_ID) String projectID)
throws WebApplicationException {
String userAgentName = req.getHeader("User-Agent");
LOG.info(this.getClass().getSimpleName() + " export view - GET starts...");
LOG.debug("params are: exportType: {}, ucdID: {}, projectID: {}", export_type, ucdID, projectID);
checkPathParameterNotNull(req, EXPORT_TYPE, export_type);
checkPathParameterNotNull(req, PATH_USECASE_ID, ucdID);
checkPathParameterNotNull(req, PATH_PROJECT_ID, projectID);
checkExportType(req, export_type);
try {
SecretManager cm = SecretManagerProvider.instance.get();
String context = cm.getContext();
if (context == null) {
throw ExceptionManager.forbiddenException(req, "Cannot determine context (the scope)", this.getClass(),
helpURI);
}
org.gcube.common.authorization.utils.user.User user = cm.getUser();
LOG.info("Identified caller {} in context {}", user.getUsername(), context);
Geoportal_PDF_Exporter pdfExporter = new Geoportal_PDF_Exporter();
boolean checked = pdfExporter.checkConfig();
if (checked) {
LOG.info("Serving request as User-Agent {}", userAgentName);
//String entity = entityHTMLMessage("Exporting as PDF...", "The project with " + projectID, true);
// /**
// * Resolve geoportal no app def.
// *
// * @param req the req
// * @param vreName the vre name
// * @param exportType the export type
// * @param ucdID the ucd ID
// * @param projectID the project ID
// * @return the response
// * @throws WebApplicationException the web application exception
// */
// @GET
// @Path("/export/{type}/view/{usecase_id}/{project_id}")
// @Produces({ MediaType.TEXT_HTML })
// public Response exportView(@Context HttpServletRequest req, @PathParam(EXPORT_TYPE) String export_type,
// @PathParam(PATH_USECASE_ID) String ucdID, @PathParam(PATH_PROJECT_ID) String projectID)
// throws WebApplicationException {
//
// String userAgentName = req.getHeader("User-Agent");
//
// LOG.info(this.getClass().getSimpleName() + " export view - GET starts...");
// LOG.debug("params are: exportType: {}, ucdID: {}, projectID: {}", export_type, ucdID, projectID);
//
// checkPathParameterNotNull(req, EXPORT_TYPE, export_type);
// checkPathParameterNotNull(req, PATH_USECASE_ID, ucdID);
// checkPathParameterNotNull(req, PATH_PROJECT_ID, projectID);
//
// checkExportType(req, export_type);
//
// try {
//
// SecretManager cm = SecretManagerProvider.instance.get();
// String context = cm.getContext();
// if (context == null) {
// throw ExceptionManager.forbiddenException(req, "Cannot determine context (the scope)", this.getClass(),
// helpURI);
// }
// org.gcube.common.authorization.utils.user.User user = cm.getUser();
// LOG.info("Identified caller {} in context {}", user.getUsername(), context);
//
// Geoportal_PDF_Exporter pdfExporter = new Geoportal_PDF_Exporter();
// boolean checked = pdfExporter.checkConfig();
//
// if (checked) {
// LOG.info("Serving request as User-Agent {}", userAgentName);
// String entity = entityHTMLMessage("Exporting as PDF...", "The project with " + projectID, true);
// return Response.ok(entity).encoding("UTF-8").header(ConstantsResolver.CONTENT_TYPE, "text/html")
// .build();
return new Viewable("/geoportal_exporter");
} else {
throw ExceptionManager.notFoundException(req, this.getClass().getSimpleName()+ " not found in the context: "+context,
this.getClass(), helpURI);
}
} catch (Exception e) {
LOG.error("Error on performing export", e);
throw ExceptionManager.internalErrorException(req, "Sorry, error occurred when exporting the project",
this.getClass(), helpURI);
}
}
// } else {
// throw ExceptionManager.notFoundException(req, this.getClass().getSimpleName()+ " not found in the context: "+context,
// this.getClass(), helpURI);
// }
//
// } catch (Exception e) {
// LOG.error("Error on performing export", e);
// throw ExceptionManager.internalErrorException(req, "Sorry, error occurred when exporting the project",
// this.getClass(), helpURI);
// }
//
// }
@GET
@Path("/export/{type}/healthcheck")
@ -238,6 +241,33 @@ public class GeoportalExporter {
this.getClass(), helpURI);
}
}
@GET
@Path("/view/{pdfCode}")
@Produces({ MediaType.TEXT_PLAIN })
public Response viewPDF(@Context HttpServletRequest req, @PathParam(PDF_CODE) String pdfCode)
throws WebApplicationException {
LOG.info(this.getClass().getSimpleName() + " viewPDF - GET starts...");
LOG.debug("param is: pdfCode: {}", pdfCode);
checkPathParameterNotNull(req, EXPORT_TYPE, pdfCode);
try {
} catch (Exception e) {
LOG.error("Error on performing healthcheck", e);
throw ExceptionManager.internalErrorException(req,
"Error when performing " + GeoportalExporter.class.getSimpleName() + " healthcheck",
this.getClass(), helpURI);
}
JsonObject jsonObject = new JsonObject();
jsonObject.add("url", null);
String json = jsonObject.toString();
return Response.ok(json).build();
}
public void checkPathParameterNotNull(HttpServletRequest req, String parameter, String value)
throws BadRequestException {
@ -332,7 +362,7 @@ public class GeoportalExporter {
* @param waiting the waiting
* @return the string
*/
protected String entityHTMLMessage(String action, String message, boolean waiting) {
protected String entityHTMLMessage(String action, String message, boolean waiting, String viewPdfURL) {
String html = "<html>" + "<head>" + "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">"
+ "<style>" + "html, body {\n" + " margin: 10px;\n" + " width: 100%;\n" + " height: 100%;\n"
@ -352,7 +382,25 @@ public class GeoportalExporter {
}
html += "<br/><p id=\"message\">" + message + "</p>";
html += "</div></body></html>";
html += "</div>"
+ ""
+ "<script>"
+ ""
+ "async function fetchPDF_URL() {\n"
+ " const response = await fetch("+viewPdfURL+");\n"
+ " return response.json();\n"
+ "}"
+ "async function playPDFPoll() { "
+ "const pdf_json = await fetchPDF_URL(); "
+ "if (!(pdf_json.url === null || pdf_json.url === undefined)) {\n"
+ " window.location.replace(pdf_json.url);\n"
+ " } else {\n"
+ " setTimeout(() => {\n"
+ " playPDFPoll();\n"
+ " }, 2000);\n"
+ " }\n"
+ "}"
+ "</script></body></html>";
return html;
}

View File

@ -16,7 +16,6 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.glassfish.jersey.server.mvc.Viewable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -31,8 +30,6 @@ import org.slf4j.LoggerFactory;
public class UriResolverIndex {
private static Logger logger = LoggerFactory.getLogger(UriResolverIndex.class);
private String indexFile = "/index";
/**
* Index.
@ -44,15 +41,14 @@ public class UriResolverIndex {
@GET
@Produces({ MediaType.TEXT_HTML })
@Path("")
public Viewable index(@Context HttpServletRequest req) throws WebApplicationException {
public InputStream index(@Context HttpServletRequest req) throws WebApplicationException {
//String indexFile = "/WEB-INF/jsp/index.jsp";
String indexFile = "/WEB-INF/jsp/index.jsp";
try {
logger.info(UriResolverIndex.class.getSimpleName() + " called");
//String realPath = req.getServletContext().getRealPath(indexFile);
return new Viewable(indexFile);
//return new FileInputStream(new File(realPath));
String realPath = req.getServletContext().getRealPath(indexFile);
return new FileInputStream(new File(realPath));
} catch (Exception e) {
if (!(e instanceof WebApplicationException)) {

View File

@ -13,8 +13,6 @@ import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.server.mvc.Viewable;
/**
* The UriResolverInfo get index.
*
@ -35,7 +33,7 @@ public class UriResolverInfo {
@GET
@Produces({ MediaType.TEXT_HTML })
@Path("")
public Viewable info(@Context HttpServletRequest req) throws WebApplicationException {
public InputStream info(@Context HttpServletRequest req) throws WebApplicationException {
return new UriResolverIndex().index(req);
}
}

View File

@ -32,7 +32,6 @@ import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.gcube.datatransfer.resolver.services.exceptions.NotFoundException;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.glassfish.jersey.server.mvc.Viewable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -191,15 +190,14 @@ public class WekeoResolver {
@GET
@Produces({ MediaType.TEXT_HTML })
@Path("")
public Viewable index(@Context HttpServletRequest req) throws WebApplicationException {
public InputStream index(@Context HttpServletRequest req) throws WebApplicationException {
String indexFile = "/WEB-INF/jsp/wekeo.jsp";
try {
// logger.info(this.getClass().getSimpleName() + " index called");
// String realPath = req.getServletContext().getRealPath(indexFile);
// return new FileInputStream("/wekeo");
return new Viewable(indexFile);
logger.info(this.getClass().getSimpleName() + " index called");
String realPath = req.getServletContext().getRealPath(indexFile);
return new FileInputStream(new File(realPath));
} catch (Exception e) {
if (!(e instanceof WebApplicationException)) {