Added query parameter as_url

This commit is contained in:
Francesco Mangiacrapa 2024-04-22 15:31:09 +02:00
parent a2216d237d
commit dc3ea29ff3
2 changed files with 91 additions and 16 deletions

View File

@ -75,7 +75,7 @@ public class HTML_Page {
+ " }\n"
+ " } catch (error) {\n"
+ " console.log(\"no json response \" + response_object);\n"
+ " console.log(\"error \" + error);\n"
+ " //console.log(\"error \" + error);\n"
+ " showError(\"Error when exporting PDF :-(\");\n"
+ " }\n"
+ "\n"

View File

@ -3,11 +3,13 @@ package org.gcube.datatransfer.resolver.services;
import java.io.InputStream;
import java.util.LinkedHashMap;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
@ -56,23 +58,31 @@ public class GeoportalExporter {
public static final String PATH_USECASE_ID = GeoportalResolver.PATH_USECASE_ID;
public static final String PATH_VRE_NAME = GeoportalResolver.PATH_VRE_NAME;
public static final String QUERY_PARAMETER_AS_URL = "as_url";
private static final Logger LOG = LoggerFactory.getLogger(GeoportalExporter.class);
private static String helpURI = "https://wiki.gcube-system.org/gcube/URI_Resolver#Geoportal_Resolver";
private static LinkedHashMap<String, FetchPDF> map = new LinkedHashMap<String, FetchPDF>();
/**
* The Enum ACCEPTED_EXPORT_TYPE.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Apr 22, 2024
*/
public enum ACCEPTED_EXPORT_TYPE {
pdf
}
/**
* Resolve geoportal no app def.
* Export.
*
* @param req the req
* @param vreName the vre name
* @param exportType the export type
* @param ucdID the ucd ID
* @param projectID the project ID
* @param req the req
* @param export_type the export type
* @param ucdID the ucd ID
* @param projectID the project ID
* @return the response
* @throws WebApplicationException the web application exception
*/
@ -80,8 +90,8 @@ public class GeoportalExporter {
@Path("/export/{type}/{usecase_id}/{project_id}")
@Produces({ MediaType.TEXT_HTML, MediaType.TEXT_PLAIN, MediaType.APPLICATION_OCTET_STREAM })
public Response export(@Context HttpServletRequest req, @PathParam(EXPORT_TYPE) String export_type,
@PathParam(PATH_USECASE_ID) String ucdID, @PathParam(PATH_PROJECT_ID) String projectID)
throws WebApplicationException {
@PathParam(PATH_USECASE_ID) String ucdID, @PathParam(PATH_PROJECT_ID) String projectID,
@QueryParam(QUERY_PARAMETER_AS_URL) @Nullable String asURL) throws WebApplicationException {
String userAgentName = req.getHeader("User-Agent");
@ -92,6 +102,13 @@ public class GeoportalExporter {
checkPathParameterNotNull(req, PATH_USECASE_ID, ucdID);
checkPathParameterNotNull(req, PATH_PROJECT_ID, projectID);
boolean getAsURL = false;
try {
getAsURL = Boolean.parseBoolean(asURL);
} catch (Exception e) {
// TODO: handle exception
}
checkExportType(req, export_type);
boolean checked = false;
@ -159,14 +176,20 @@ public class GeoportalExporter {
LOG.info("Serving request as client...");
FileReference pdfRef = exportAsPDF(req, ucdID, projectID, null, context, user);
InputStream input = pdfRef.getStorageVolatileURL().openStream();
StreamingOutput so = new SingleFileStreamingOutput(input);
if (!getAsURL) {
//returning as stream
InputStream input = pdfRef.getStorageVolatileURL().openStream();
StreamingOutput so = new SingleFileStreamingOutput(input);
ResponseBuilder response = Response.ok(so)
.header(ConstantsResolver.CONTENT_DISPOSITION,
"inline; filename=\"" + pdfRef.getFileName() + "\"")
.header("Content-Type", pdfRef.getContentType());
return response.build();
ResponseBuilder response = Response.ok(so)
.header(ConstantsResolver.CONTENT_DISPOSITION,
"inline; filename=\"" + pdfRef.getFileName() + "\"")
.header("Content-Type", pdfRef.getContentType());
return response.build();
}else {
//returning as URI;
return Response.seeOther(pdfRef.getStorageVolatileURL().toURI()).build();
}
}
} catch (Exception e) {
LOG.error("Error on performing export", e);
@ -182,6 +205,14 @@ public class GeoportalExporter {
}
/**
* Healthcheck.
*
* @param req the req
* @param export_type the export type
* @return the response
* @throws WebApplicationException the web application exception
*/
@GET
@Path("/export/{type}/healthcheck")
@Produces({ MediaType.TEXT_HTML, MediaType.TEXT_PLAIN })
@ -224,6 +255,14 @@ public class GeoportalExporter {
}
}
/**
* View PDF.
*
* @param req the req
* @param pdfCode the pdf code
* @return the response
* @throws WebApplicationException the web application exception
*/
@GET
@Path("/view/{pdfCode}")
@Produces({ MediaType.TEXT_PLAIN, MediaType.TEXT_HTML })
@ -309,6 +348,14 @@ public class GeoportalExporter {
}
/**
* Check path parameter not null.
*
* @param req the req
* @param parameter the parameter
* @param value the value
* @throws BadRequestException the bad request exception
*/
public void checkPathParameterNotNull(HttpServletRequest req, String parameter, String value)
throws BadRequestException {
if (value == null || value.isEmpty()) {
@ -318,6 +365,14 @@ public class GeoportalExporter {
}
}
/**
* Check export type.
*
* @param req the req
* @param export_type the export type
* @return the accepted export type
* @throws BadRequestException the bad request exception
*/
public ACCEPTED_EXPORT_TYPE checkExportType(HttpServletRequest req, String export_type) throws BadRequestException {
ACCEPTED_EXPORT_TYPE exportType;
try {
@ -333,6 +388,18 @@ public class GeoportalExporter {
return exportType;
}
/**
* Export as PDF.
*
* @param req the req
* @param profileID the profile ID
* @param projectID the project ID
* @param profileTitle the profile title
* @param context the context
* @param user the user
* @return the file reference
* @throws WebApplicationException the web application exception
*/
public FileReference exportAsPDF(HttpServletRequest req, String profileID, String projectID, String profileTitle,
String context, User user) throws WebApplicationException {
LOG.info("exportAsPDF for profileID: " + profileID + ", projectID: " + projectID + "called");
@ -370,6 +437,14 @@ public class GeoportalExporter {
return pdfRef;
}
/**
* Gets the public links for.
*
* @param item the item
* @param context the context
* @return the public links for
* @throws Exception the exception
*/
public GeoportalItemReferences getPublicLinksFor(GeoportalItemReferences item, String context) throws Exception {
LOG.info("getPublicLinksFor called for: " + item);