revisited the export method

This commit is contained in:
Francesco Mangiacrapa 2024-04-23 11:58:12 +02:00
parent f5c17150e6
commit 3fa182d803
1 changed files with 23 additions and 17 deletions

View File

@ -1,6 +1,7 @@
package org.gcube.datatransfer.resolver.services; package org.gcube.datatransfer.resolver.services;
import java.io.InputStream; import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -14,7 +15,6 @@ import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.StreamingOutput; import javax.ws.rs.core.StreamingOutput;
@ -97,8 +97,8 @@ public class GeoportalExporter {
String userAgentName = req.getHeader("User-Agent"); String userAgentName = req.getHeader("User-Agent");
LOG.info(this.getClass().getSimpleName() + " export - GET starts..."); LOG.info(this.getClass().getSimpleName() + " export - GET starts...");
LOG.info("Params: [" + EXPORT_TYPE + ": {}, " + PATH_USECASE_ID + ": {}, " + PATH_PROJECT_ID + ": {}, " + QUERY_PARAMETER_AS_URL + ": {}]", LOG.info("Params: [" + EXPORT_TYPE + ": {}, " + PATH_USECASE_ID + ": {}, " + PATH_PROJECT_ID + ": {}, "
export_type, ucdID, projectID, asURL); + QUERY_PARAMETER_AS_URL + ": {}]", export_type, ucdID, projectID, asURL);
checkPathParameterNotNull(req, EXPORT_TYPE, export_type); checkPathParameterNotNull(req, EXPORT_TYPE, export_type);
checkPathParameterNotNull(req, PATH_USECASE_ID, ucdID); checkPathParameterNotNull(req, PATH_USECASE_ID, ucdID);
@ -141,8 +141,20 @@ public class GeoportalExporter {
+ " seems to be not configured in the context: " + context, + " seems to be not configured in the context: " + context,
this.getClass(), helpURI); this.getClass(), helpURI);
} }
if (checked) { if (checked) {
if (getAsURL) {
try {
LOG.info("Serving request as getAsURL...");
FileReference pdfRef = exportAsPDF(req, ucdID, projectID, null, context, user);
return Response.ok(pdfRef.getStorageVolatileURL().toURI()).build();
} catch (URISyntaxException e) {
LOG.error("Error on performing export by url", e);
throw ExceptionManager.internalErrorException(req,
"Sorry, error occurred when generating the project URL", this.getClass(), helpURI);
}
}
try { try {
if (userAgentName != null) { if (userAgentName != null) {
LOG.info("Serving request as User-Agent {}", userAgentName); LOG.info("Serving request as User-Agent {}", userAgentName);
@ -178,21 +190,15 @@ public class GeoportalExporter {
} else { } else {
LOG.info("Serving request as client..."); LOG.info("Serving request as client...");
FileReference pdfRef = exportAsPDF(req, ucdID, projectID, null, context, user); FileReference pdfRef = exportAsPDF(req, ucdID, projectID, null, context, user);
// returning as stream
InputStream input = pdfRef.getStorageVolatileURL().openStream();
StreamingOutput so = new SingleFileStreamingOutput(input);
if (!getAsURL) { return Response.ok(so)
// returning as stream .header(ConstantsResolver.CONTENT_DISPOSITION,
InputStream input = pdfRef.getStorageVolatileURL().openStream(); "inline; filename=\"" + pdfRef.getFileName() + "\"")
StreamingOutput so = new SingleFileStreamingOutput(input); .header("Content-Type", pdfRef.getContentType()).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.ok(pdfRef.getStorageVolatileURL().toURI()).build();
}
} }
} catch (Exception e) { } catch (Exception e) {
LOG.error("Error on performing export", e); LOG.error("Error on performing export", e);