From 9e49f6f281138781870a8d5996d5d4ef9702cb92 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Wed, 24 Apr 2024 14:46:34 +0200 Subject: [PATCH] integrated the GeoportalExporterActionServlet --- .classpath | 5 + .settings/org.eclipse.wst.common.component | 220 +++++++++++++++++- pom.xml | 7 +- .../client/GeoportalDataViewerConstants.java | 16 +- .../client/GeoportalDataViewerService.java | 4 +- .../GeoportalDataViewerServiceAsync.java | 4 +- .../client/ui/cms/project/ProjectViewer.java | 57 ++--- .../GeoportalDataViewerServiceImpl.java | 88 ------- .../GeoportalExporterActionServlet.java | 197 ++++++++++++++++ .../server/WatingServerActionServlet.java | 137 ----------- .../GeoportalServiceIdentityProxy.java | 20 +- .../accessidentity/GcubeIdentity.java | 29 ++- .../accessidentity/IAMClientIdentity.java | 33 ++- .../accessidentity/UserIdentity.java | 23 ++ src/main/webapp/WEB-INF/web.xml | 24 +- .../GeoportalExporterActionServletTest.java | 42 ++++ 16 files changed, 614 insertions(+), 292 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/GeoportalExporterActionServlet.java delete mode 100644 src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/WatingServerActionServlet.java create mode 100644 src/test/java/org/gcube/portlets/user/geoportaldataviewer/GeoportalExporterActionServletTest.java diff --git a/.classpath b/.classpath index 90eb7e4..aa74096 100644 --- a/.classpath +++ b/.classpath @@ -30,5 +30,10 @@ + + + + + diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 67a2a15..b83d38d 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -1,5 +1,25 @@ - + + + + + + + + + + + + + + + + + + + + + @@ -109,7 +129,27 @@ - + + + + + + + + + + + + + + + + + + + + + @@ -219,7 +259,27 @@ - + + + + + + + + + + + + + + + + + + + + + @@ -329,7 +389,27 @@ - + + + + + + + + + + + + + + + + + + + + + @@ -439,7 +519,27 @@ - + + + + + + + + + + + + + + + + + + + + + @@ -549,7 +649,49 @@ - + + + + + + + + + + + + + + + + + + uses + + + uses + + + + + + + + + + + + + + + + + + + + + + @@ -659,7 +801,27 @@ - + + + + + + + + + + + + + + + + + + + + + @@ -769,7 +931,27 @@ - + + + + + + + + + + + + + + + + + + + + + @@ -879,7 +1061,27 @@ - + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 7911e90..9b9b837 100644 --- a/pom.xml +++ b/pom.xml @@ -175,8 +175,13 @@ org.gcube.common keycloak-client - [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + [2.0.0, 3.0.0-SNAPSHOT) + + + + + com.liferay.portal portal-service diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerConstants.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerConstants.java index 4538a3a..25c0436 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerConstants.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerConstants.java @@ -16,12 +16,18 @@ import com.google.gwt.i18n.client.DateTimeFormat; */ public class GeoportalDataViewerConstants { +// //WAITING_SERVER_ACTION_SERVLET +// public static final String WAITING_PARAMETER = "waiting"; +// public static final String ACTION_TITLE_PARAMETER = "action"; +// public static final String MESSAGE_PARAMETER = "message"; +// public static final String WAITING_SERVER_ACTION_SERVLET = GWT.getModuleBaseURL() +// + "geoportaldataviewerwaitingservice"; + //WAITING_SERVER_ACTION_SERVLET - public static final String WAITING_PARAMETER = "waiting"; - public static final String ACTION_TITLE_PARAMETER = "action"; - public static final String MESSAGE_PARAMETER = "message"; - public static final String WAITING_SERVER_ACTION_SERVLET = GWT.getModuleBaseURL() - + "geoportaldataviewerwaitingservice"; + public static final String UCD_ID_PARAMETER = "ucdID"; + public static final String PROIECT_ID_PARAMETER = "projectID"; + public static final String GEOPORTAL_EXPORT_ACTION_SERVLET = GWT.getModuleBaseURL() + + "geoportalexporterservice"; public static final int TABLET_WIDTH = 1601; diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerService.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerService.java index a13f569..2f036cf 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerService.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerService.java @@ -231,7 +231,7 @@ public interface GeoportalDataViewerService extends RemoteService { Boolean checkExportAsPDFConfig(); - String exportAsPDF(String profileID, String projectID, String profileTitle, - GeoportalItemReferences geoportalItemReferences); +// String exportAsPDF(String profileID, String projectID, String profileTitle, +// GeoportalItemReferences geoportalItemReferences); } diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerServiceAsync.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerServiceAsync.java index 37cdb3c..0db918c 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerServiceAsync.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/GeoportalDataViewerServiceAsync.java @@ -95,7 +95,7 @@ public interface GeoportalDataViewerServiceAsync { void checkExportAsPDFConfig(AsyncCallback callback); - void exportAsPDF(String profileID, String projectID, String profileTitle, - GeoportalItemReferences geoportalItemReferences, AsyncCallback callback); +// void exportAsPDF(String profileID, String projectID, String profileTitle, +// GeoportalItemReferences geoportalItemReferences, AsyncCallback callback); } diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/project/ProjectViewer.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/project/ProjectViewer.java index 283619b..04c8aaa 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/project/ProjectViewer.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/cms/project/ProjectViewer.java @@ -212,39 +212,42 @@ public class ProjectViewer extends Composite { @Override public void onClick(ClickEvent event) { - String theProjectName =""; + String theProjectName = ""; try { - theProjectName = (String) projectView.getTheProjectDV().getTheDocument().getFirstEntryOfMap().getValue(); - theProjectName = theProjectName!=null?theProjectName: "Project ID: " + projectView.getTheProjectDV().getId(); - }catch (Exception e) { + theProjectName = (String) projectView.getTheProjectDV().getTheDocument().getFirstEntryOfMap() + .getValue(); + theProjectName = theProjectName != null ? theProjectName + : "Project ID: " + projectView.getTheProjectDV().getId(); + } catch (Exception e) { // TODO: handle exception } - + StringBuilder builder = new StringBuilder(); - builder.append(GeoportalDataViewerConstants.WAITING_SERVER_ACTION_SERVLET+"?"); - builder.append(GeoportalDataViewerConstants.ACTION_TITLE_PARAMETER+"=Exporting as PDF...&"); - builder.append(GeoportalDataViewerConstants.MESSAGE_PARAMETER+"="+theProjectName+"&"); - builder.append(GeoportalDataViewerConstants.WAITING_PARAMETER+"=true"); + builder.append(GeoportalDataViewerConstants.GEOPORTAL_EXPORT_ACTION_SERVLET + "?"); + builder.append(GeoportalDataViewerConstants.UCD_ID_PARAMETER + "=" + + projectView.getTheProjectDV().getProfileID() + "&"); + builder.append(GeoportalDataViewerConstants.PROIECT_ID_PARAMETER + "=" + + projectView.getTheProjectDV().getId()); - String theWaitingServletRequest = builder.toString(); - final NewBrowserWindow ref = NewBrowserWindow.open(theWaitingServletRequest, "_blank", null); - - GeoportalDataViewerServiceAsync.Util.getInstance().exportAsPDF(projectView.getTheProjectDV().getProfileID(), - projectView.getTheProjectDV().getId(), theTitle, geoportalItemReferences, - new AsyncCallback() { + String geoportalExportActionServlet = builder.toString(); + final NewBrowserWindow ref = NewBrowserWindow.open(geoportalExportActionServlet, "_blank", null); - @Override - public void onFailure(Throwable caught) { - // TODO Auto-generated method stub - - } - - @Override - public void onSuccess(String resultURL) { - ref.setUrl(resultURL); - - } - }); +// GeoportalDataViewerServiceAsync.Util.getInstance().exportAsPDF( +// projectView.getTheProjectDV().getProfileID(), projectView.getTheProjectDV().getId(), theTitle, +// geoportalItemReferences, new AsyncCallback() { +// +// @Override +// public void onFailure(Throwable caught) { +// // TODO Auto-generated method stub +// +// } +// +// @Override +// public void onSuccess(String resultURL) { +// ref.setUrl(resultURL); +// +// } +// }); } }); diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/GeoportalDataViewerServiceImpl.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/GeoportalDataViewerServiceImpl.java index 873e857..3377c89 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/GeoportalDataViewerServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/GeoportalDataViewerServiceImpl.java @@ -70,8 +70,6 @@ import org.gcube.application.geoportalcommon.util.URLParserUtil; import org.gcube.application.geoportaldatamapper.GcubeProfilesPerUCDIdCache; import org.gcube.application.geoportaldatamapper.Geoportal_JSON_Mapper; import org.gcube.application.geoportaldatamapper.exporter.Geoportal_PDF_Exporter; -import org.gcube.application.geoportaldatamapper.shared.ExporterProjectSource; -import org.gcube.common.scope.api.ScopeProvider; import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerConstants; import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerService; import org.gcube.portlets.user.geoportaldataviewer.server.gis.FeatureParser; @@ -620,56 +618,6 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme } -// public Map> getAvaiableCrossFilteringLayers() { -// -// Map> mapProfileIDOvelayLayers = new LinkedHashMap<>(); -// -// // Reading UCDs Collections from the session -// Map ucdCollections = SessionUtil.getAvailableCollections(getThreadLocalRequest()); -// -// for (String profileId : ucdCollections.keySet()) { -// -// UseCaseDescriptor u = ucdCollections.get(profileId); -// -// // Loading geoportal_grouped_overlay_layers if exists -// GEOPORTAL_DATA_HANDLER theHandler = GEOPORTAL_DATA_HANDLER.geoportal_grouped_cross_filtering; -// List handlers = u.getHandlersByType(theHandler.getType()); -// -// if (handlers != null && handlers.size() > 0) { -// // Loading Handler gcube_profiles -// HandlerDeclaration handler = handlers.get(0); -// List listGroupedCustomLayers = null; -// try { -// -// HandlerDeclarationDV handlerDV = ConvertToDataValueObjectModel.toHandlerDeclarationDV(handler, u, -// GEOPORTAL_CONFIGURATION_TYPE.grouped_overlay_layers); -// -// if (handler != null) { -// ConfigurationDV config = handlerDV.getConfiguration(); -// switch (config.getConfigurationType()) { -// case grouped_overlay_layers: -// listGroupedCustomLayers = (List) config.getConfiguration(); -// break; -// default: -// break; -// } -// } -// } catch (Exception e) { -// LOG.error("Error on loading {} config for ucd Id {}. Returning null", -// GEOPORTAL_CONFIGURATION_TYPE.grouped_overlay_layers, u.getId()); -// } -// -// mapProfileIDOvelayLayers.put(profileId, listGroupedCustomLayers); -// } else { -// LOG.warn("No handler " + theHandler + "found into UCD " + u.getId() + ", continue..."); -// mapProfileIDOvelayLayers.put(profileId, null); -// } -// } -// -// return mapProfileIDOvelayLayers; -// -// } - /** * Gets the layer index. * @@ -1901,40 +1849,4 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme return configChecked; } - @Override - public String exportAsPDF(String profileID, String projectID, String profileTitle, - GeoportalItemReferences geoportalItemReferences) { - LOG.debug("exportAsPDF for profileID: " + profileID + ", projectID: " + projectID + "called"); - - String pdfURL = null; - try { - - GeoportalServiceIdentityProxy identity = new GeoportalServiceIdentityProxy(this.getThreadLocalRequest()); - - Geoportal_PDF_Exporter gpdfe = new Geoportal_PDF_Exporter(); - ExporterProjectSource exportSource = new ExporterProjectSource(); - exportSource.setProfileID(profileID); - exportSource.setProfileTitle(profileTitle); - exportSource.setProjectID(projectID); - exportSource.setScope(ScopeProvider.instance.get()); - - GeoportalItemReferences gir = getPublicLinksFor(geoportalItemReferences); - - if (identity.isUser()) { - exportSource.setGisLink(gir.getRestrictedLink().getShortURL()); - GCubeUser user = SessionUtil.getCurrentUser(this.getThreadLocalRequest()); - exportSource.setAccountname(user.getUsername()); - } else { - exportSource.setGisLink(gir.getOpenLink().getShortURL()); - exportSource.setAccountname(null); - } - - pdfURL = gpdfe.createPDFFile(exportSource); - - } catch (Exception e1) { - // silent - } - return pdfURL; - } - } diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/GeoportalExporterActionServlet.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/GeoportalExporterActionServlet.java new file mode 100644 index 0000000..5b7a5cc --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/GeoportalExporterActionServlet.java @@ -0,0 +1,197 @@ +/** + * + */ +package org.gcube.portlets.user.geoportaldataviewer.server; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Enumeration; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.io.IOUtils; +import org.apache.http.client.methods.HttpGet; +import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerConstants; +import org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.GeoportalServiceIdentityProxy; +import org.gcube.portlets.user.uriresolvermanager.geoportal.GeoportalExporterAPI; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Class GeoportalExporterActionServlet. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Apr 23, 2024 + */ +public class GeoportalExporterActionServlet extends HttpServlet { + + /** + * + */ + private static final long serialVersionUID = 6037090280489238565L; + protected static Logger logger = LoggerFactory.getLogger(GeoportalExporterActionServlet.class); + + /** + * {@inheritDoc} + */ + @Override + public void init() throws ServletException { + super.init(); + logger.trace(GeoportalExporterActionServlet.class.getSimpleName() + " ready."); + } + + /** + * Do get. + * + * @param req the req + * @param resp the resp + * @throws IOException Signals that an I/O exception has occurred. + * @throws ServletException the servlet exception + */ + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { +// setSecretManager(); + String projectID = req.getParameter(GeoportalDataViewerConstants.PROIECT_ID_PARAMETER); + String ucdID = req.getParameter(GeoportalDataViewerConstants.UCD_ID_PARAMETER); + + logger.info("doGet called with Input Params [projectID: " + projectID + ", ucdID: " + ucdID + "]"); + + try { + + GeoportalServiceIdentityProxy identity = new GeoportalServiceIdentityProxy(req); + String theToken = identity.getToken(); + String theIdentity = identity.getIdentity(); + + GeoportalExporterAPI geoportalExporterAPI = new GeoportalExporterAPI(); + URL urlRequest = geoportalExporterAPI.exportProject("pdf", ucdID, projectID, false); + String urlToRedirect = urlRequest.toString(); + logger.info("Performing request to {} with identity {}", urlToRedirect, identity.getDescription()); + logger.info("Token is {}", theToken.substring(0, 20) + "_MASKED_TOKEN"); + + InputStream is = performHttpRequestToSGService(urlToRedirect, theIdentity, theToken); + IOUtils.copy(is, resp.getOutputStream()); + resp.flushBuffer(); + + } catch (Exception e) { + logger.error("Error occurred when exporting the Project with id: " + projectID, e); + sendError(resp, "Error occurred when exporting the Project. Error is: " + e.getMessage()); + return; + } + + } + + public static InputStream performHttpRequestToSGService(String urlToService, String identity, String token) + throws IOException { + logger.debug("performHttpRequestToSGService called"); + try { + + URL url = new URL(urlToService); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + // timeout at 60sec + conn.setConnectTimeout(6000); + conn.setDoOutput(true); + + if (token.length() > 50) { + // is JWT TOKEN + conn.setRequestProperty("Authorization", "Bearer " + token); + } else { + // is legacy Token + conn.setRequestProperty("gcube-token", token); + } + + conn.setRequestProperty("Content-Type", "text/html"); + conn.setRequestMethod("GET"); + logger.debug("performHttpRequestToSGService done, returning"); + return conn.getInputStream(); + } catch (IOException e) { + logger.error("Error on performing request to url " + urlToService, e); + throw e; + } + } + + /** + * Sets the headers to target. + * + * @param httpSource the http source + * @param response the response + * @return the http servlet response + */ + public static HttpServletResponse setHeadersToTarget(HttpServletRequest httpSource, HttpServletResponse response) { + + Enumeration headersNames = httpSource.getHeaderNames(); + while (headersNames.hasMoreElements()) { + String headerName = (String) headersNames.nextElement(); + Enumeration values = httpSource.getHeaders(headerName); + while (values.hasMoreElements()) { + String value = values.nextElement(); + logger.info("Copying header {} with value {}", headerName, value); + response.addHeader(headerName, value); + } + } + return response; + } + + /** + * Sets the headers to target. + * + * @param httpSource the http source + * @param httpTarget the http target + * @return the http get + */ + public static HttpGet setHeadersToTarget(HttpServletRequest httpSource, HttpGet httpTarget) { + + Enumeration headersNames = httpSource.getHeaderNames(); + while (headersNames.hasMoreElements()) { + String headerName = (String) headersNames.nextElement(); + Enumeration values = httpSource.getHeaders(headerName); + while (values.hasMoreElements()) { + String value = values.nextElement(); + logger.info("Copying header {} with value {}", headerName, value); + httpTarget.addHeader(headerName, value); + } + } + return httpTarget; + } + + /** + * Sets the headers to response. + * + * @param httpSource the http source + * @param httpTarget the http target + * @return the http get + */ + public static HttpGet setHeadersToResponse(HttpServletRequest httpSource, HttpGet httpTarget) { + + Enumeration headersNames = httpSource.getHeaderNames(); + while (headersNames.hasMoreElements()) { + String headerName = (String) headersNames.nextElement(); + Enumeration values = httpSource.getHeaders(headerName); + while (values.hasMoreElements()) { + String value = values.nextElement(); + logger.info("Copying header {} with value {}", headerName, value); + httpTarget.addHeader(headerName, value); + } + } + return httpTarget; + } + + /** + * Send response. + * + * @param response the response + * @param message the message + * @throws IOException Signals that an I/O exception has occurred. + */ + protected void sendError(HttpServletResponse response, String message) throws IOException { + response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + response.setContentType("text/html"); + response.getWriter().write("Error" + message + ""); + response.flushBuffer(); + } + +} diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/WatingServerActionServlet.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/WatingServerActionServlet.java deleted file mode 100644 index f6a2309..0000000 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/WatingServerActionServlet.java +++ /dev/null @@ -1,137 +0,0 @@ -/** - * - */ -package org.gcube.portlets.user.geoportaldataviewer.server; - -import java.io.IOException; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerConstants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * The Class WatingServerActionServlet. - * - * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it - * - * Nov 22, 2023 - */ -public class WatingServerActionServlet extends HttpServlet { - - /** - * - */ - private static final long serialVersionUID = -1058074450240555475L; - protected static Logger logger = LoggerFactory.getLogger(WatingServerActionServlet.class); - - /** - * {@inheritDoc} - */ - @Override - public void init() throws ServletException { - super.init(); - logger.trace("Workspace WatingServerActionServlet ready."); - } - - /** - * Do get. - * - * @param req the req - * @param resp the resp - * @throws IOException Signals that an I/O exception has occurred. - */ - /* - * (non-Javadoc) - * - * @see - * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, - * javax.servlet.http.HttpServletResponse) - */ - public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { - - String action = req.getParameter(GeoportalDataViewerConstants.ACTION_TITLE_PARAMETER); - String message = req.getParameter(GeoportalDataViewerConstants.MESSAGE_PARAMETER); - boolean waiting = req.getParameter(GeoportalDataViewerConstants.WAITING_PARAMETER) == null ? true - : req.getParameter(GeoportalDataViewerConstants.WAITING_PARAMETER).equals("true"); - - logger.debug("Input Params [action: " + action + ", message: "+ message +", waiting: " + waiting + "]"); - - action = action == null || action.isEmpty() ? "Loading" : action; - - message = message == null || message.isEmpty() ? "" : message; - - sendResponse(resp, action, message, waiting); - } - - - /** - * Send response. - * - * @param response the response - * @param action the action - * @param message the message - * @param waiting the waiting - * @throws IOException Signals that an I/O exception has occurred. - */ - protected void sendResponse(HttpServletResponse response, String action, String message, boolean waiting) throws IOException { - response.setStatus(HttpServletResponse.SC_OK); - response.setContentType("text/html"); - response.getWriter().write(setHTMLMessage(action, message, waiting)); - response.flushBuffer(); - } - - /** - * Sets the HTML message. - * - * @param action the action - * @param message the message - * @param waiting the waiting - * @return the string - */ - protected String setHTMLMessage(String action, String message, boolean waiting) { - - String html = "" + "" + "" - + "" - + "D4Science Geoportal - Action" + "" + ""; - - - html += "\"D4Science
"; - - html += "
"; - html += "

" + action + "

"; - - if (waiting) { - html += "\"D4Science
"; - } - - html += "

" + message + "

"; - html += "
"; - - return html; - } - -} diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/GeoportalServiceIdentityProxy.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/GeoportalServiceIdentityProxy.java index 33d9ebb..b955e51 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/GeoportalServiceIdentityProxy.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/GeoportalServiceIdentityProxy.java @@ -22,9 +22,9 @@ public class GeoportalServiceIdentityProxy { private static final Logger LOG = LoggerFactory.getLogger(GeoportalServiceIdentityProxy.class); private GcubeIdentity gCubeIdentity; - + private boolean isUser = false; - + private boolean isIAMClient = false; /** @@ -57,12 +57,24 @@ public class GeoportalServiceIdentityProxy { gCubeIdentity.setIdentity(request); } - + public boolean isUser() { return isUser; } - + public boolean isIAMClient() { return isIAMClient; } + + public String getDescription() { + return gCubeIdentity.getIdentityDescription(); + } + + public String getToken() { + return gCubeIdentity.getToken(); + } + + public String getIdentity() { + return gCubeIdentity.getIdentity(); + } } diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/accessidentity/GcubeIdentity.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/accessidentity/GcubeIdentity.java index ad1e4e4..15e4f8c 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/accessidentity/GcubeIdentity.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/accessidentity/GcubeIdentity.java @@ -2,16 +2,15 @@ package org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.accessid import javax.servlet.http.HttpServletRequest; - /** * The Interface GcubeIdentity. * * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * - * Sep 23, 2021 + * Sep 23, 2021 */ public interface GcubeIdentity { - + /** * Sets the identity. * @@ -19,10 +18,32 @@ public interface GcubeIdentity { * @throws Exception the exception */ public void setIdentity(HttpServletRequest httpRequest) throws Exception; - + /** * Reset identity. */ public void resetIdentity(); + /** + * Gets the identity description. + * + * @return the identity description + */ + public String getIdentityDescription(); + + + /** + * Gets the identity. + * + * @return the identity + */ + public String getIdentity(); + + /** + * Gets the token. + * + * @return the token + */ + public String getToken(); + } diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/accessidentity/IAMClientIdentity.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/accessidentity/IAMClientIdentity.java index 45dbc63..ca51ff2 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/accessidentity/IAMClientIdentity.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/accessidentity/IAMClientIdentity.java @@ -16,18 +16,22 @@ public class IAMClientIdentity implements GcubeIdentity { private static final Logger LOG = LoggerFactory.getLogger(IAMClientIdentity.class); private String previousUMAToken = null; + + private String currentUMAToken = null; + private String clientId = null; + private static final String IAM_CLIENT_CREDENTIALS = "IAM_CLIENT_CREDENTIALS"; @Override public void setIdentity(HttpServletRequest httpRequest) throws Exception { LOG.info("setIdentity called"); String currentScope = SessionUtil.getCurrentContext(httpRequest, true); - IAMClientCredentials credentials = sessionGetIAMClientCredentials(httpRequest); + IAMClientCredentials credentials = sessionGetIAMClientCredentials(httpRequest); try { - if(credentials==null) { + if (credentials == null) { credentials = IAMClientCredentialsReader.getCredentials(); - sessionSetIAMClientCredentials(httpRequest,credentials); + sessionSetIAMClientCredentials(httpRequest, credentials); } } catch (Exception e) { LOG.error("Error on discovering IAM Client credentials", e); @@ -36,7 +40,7 @@ public class IAMClientIdentity implements GcubeIdentity { LOG.trace("Read credentials: " + credentials); String umaAcessToken = null; - String clientId = credentials.getClientId(); + clientId = credentials.getClientId(); String clientSecret = credentials.getClientSecret(); try { @@ -46,6 +50,7 @@ public class IAMClientIdentity implements GcubeIdentity { umaAcessToken = tr.getAccessToken(); if (umaAcessToken != null && !umaAcessToken.isEmpty()) { LOG.info("UMA Access Token read correctly"); + currentUMAToken = umaAcessToken; } else { LOG.error("UMA Access Token NOT RETRIEVED!!!"); throw new Exception("UMA Access Token is null or empty"); @@ -75,6 +80,11 @@ public class IAMClientIdentity implements GcubeIdentity { } + @Override + public String getToken() { + return currentUMAToken; + } + @Override public void resetIdentity() { LOG.info("resetIdentity called"); @@ -82,7 +92,7 @@ public class IAMClientIdentity implements GcubeIdentity { LOG.info("resetIdentity to previous AccessToken"); } - + /** * Gets the IAM client credentials. * @@ -103,7 +113,7 @@ public class IAMClientIdentity implements GcubeIdentity { * Sets the IAM client credentials. * * @param httpRequest the http request - * @param iamCC the iam CC + * @param iamCC the iam CC */ public static void sessionSetIAMClientCredentials(HttpServletRequest httpRequest, IAMClientCredentials iamCC) { HttpSession session = httpRequest.getSession(); @@ -114,4 +124,15 @@ public class IAMClientIdentity implements GcubeIdentity { } } + @Override + public String getIdentityDescription() { + return "ClientId: "+ clientId; + + } + + @Override + public String getIdentity() { + return clientId; + } + } diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/accessidentity/UserIdentity.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/accessidentity/UserIdentity.java index f2ee8f9..3a6f79f 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/accessidentity/UserIdentity.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/accessidentity/UserIdentity.java @@ -3,6 +3,7 @@ package org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.accessid import javax.servlet.http.HttpServletRequest; import org.gcube.portlets.user.geoportaldataviewer.server.util.SessionUtil; +import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,13 +11,19 @@ public class UserIdentity implements GcubeIdentity { private static final Logger LOG = LoggerFactory.getLogger(UserIdentity.class); + private HttpServletRequest httpRequest; + + private GCubeUser user; + public UserIdentity() { } @Override public void setIdentity(HttpServletRequest httpRequest) throws Exception { LOG.info("setIdentity called"); + this.httpRequest = httpRequest; SessionUtil.getCurrentToken(httpRequest, true); + user = SessionUtil.getCurrentUser(httpRequest); } @@ -25,4 +32,20 @@ public class UserIdentity implements GcubeIdentity { LOG.info("resetIdentity called, doing nothing"); // doing nothing } + + @Override + public String getToken() { + return SessionUtil.getCurrentToken(httpRequest, true); + } + + @Override + public String getIdentityDescription() { + return user != null ? "user: " + user.getUsername() : "null"; + } + + @Override + public String getIdentity() { + return user.getUsername(); + } + } diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 81b416b..70095f1 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -16,11 +16,16 @@ org.gcube.portlets.user.geoportaldataviewer.server.GeoportalDataViewerServiceImpl - - geoportalWaitingServerActionServlet - org.gcube.portlets.user.geoportaldataviewer.server.WatingServerActionServlet + + + + + + + geoportalExporterServerActionServlet + org.gcube.portlets.user.geoportaldataviewer.server.GeoportalExporterActionServlet - + mpformbuilderServlet org.gcube.portlets.widgets.mpformbuilder.server.MetadataProfileFormBuilderServiceImpl @@ -37,9 +42,14 @@ /GeoportalDataViewer/geoportaldataviewerservice - - geoportalWaitingServerActionServlet - /GeoportalDataViewer/geoportaldataviewerwaitingservice + + + + + + + geoportalExporterServerActionServlet + /GeoportalDataViewer/geoportalexporterservice diff --git a/src/test/java/org/gcube/portlets/user/geoportaldataviewer/GeoportalExporterActionServletTest.java b/src/test/java/org/gcube/portlets/user/geoportaldataviewer/GeoportalExporterActionServletTest.java new file mode 100644 index 0000000..0c8fd1b --- /dev/null +++ b/src/test/java/org/gcube/portlets/user/geoportaldataviewer/GeoportalExporterActionServletTest.java @@ -0,0 +1,42 @@ +/** + * + */ +package org.gcube.portlets.user.geoportaldataviewer; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; +import org.gcube.portlets.user.geoportaldataviewer.server.GeoportalExporterActionServlet; + +/** + * The Class GeoportalExporterActionServletTest. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Apr 24, 2024 + */ +public class GeoportalExporterActionServletTest { + + /** + * The main method. + * + * @param args the arguments + */ + public static void main(String[] args) { + String url = "https://data.dev.d4science.org/geoportal/export/pdf/profiledConcessioni/661d2c6f8804530afb90b132"; + String identity = "francesco.mangiacrapa"; + String token = ""; + System.out.println("Start"); + InputStream response; + try { + response = GeoportalExporterActionServlet.performHttpRequestToSGService(url, identity, token); + System.out.println(IOUtils.toString(response)); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println("End"); + } + +}