refactoring

This commit is contained in:
Francesco Mangiacrapa 2021-09-24 11:13:25 +02:00
parent 85230de9bb
commit 46ae55e8dd
4 changed files with 279 additions and 177 deletions

View File

@ -7,8 +7,12 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.application.geoportal.common.model.legacy.LayerConcessione;
import org.gcube.application.geoportal.common.model.legacy.UploadedImage;
import org.gcube.application.geoportalcommon.ConvertToDataViewModel;
import org.gcube.application.geoportalcommon.GeoNaDataViewerProfileReader;
import org.gcube.application.geoportalcommon.GeoportalCommon;
@ -26,7 +30,6 @@ import org.gcube.application.geoportalcommon.util.URLParserUtil;
import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerService;
import org.gcube.portlets.user.geoportaldataviewer.server.gis.FeatureParser;
import org.gcube.portlets.user.geoportaldataviewer.server.gis.WMSUrlValidator;
import org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.ConcessioneImageUtil;
import org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.ConcessioniMongoServiceIdentityProxy;
import org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.accesspolicy.GeoNACheckAccessPolicy;
import org.gcube.portlets.user.geoportaldataviewer.server.util.SessionUtil;
@ -36,6 +39,7 @@ import org.gcube.portlets.user.geoportaldataviewer.shared.gis.wfs.FeatureRow;
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.wms.GeoInformationForWMSRequest;
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.wms.Styles;
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.wms.ZAxis;
import org.gcube.portlets.user.urlshortener.UrlShortener;
import org.gcube.spatial.data.geoutility.GeoNcWMSMetadataUtility;
import org.gcube.spatial.data.geoutility.bean.LayerStyles;
import org.gcube.spatial.data.geoutility.bean.LayerZAxis;
@ -60,6 +64,8 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme
/** The Constant LOG. */
private static final Logger LOG = LoggerFactory.getLogger(GeoportalDataViewerServiceImpl.class);
private static final String CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE = "MAP_IMAGE_PREVIEW_FOR_CONCESSIONE";
/**
* Parses the wms request.
*
@ -152,8 +158,8 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme
if (concessioneIds != null && concessioneIds.size() > 0) {
String cId = concessioneIds.get(0);
try {
UploadedImageDV uplImg = SessionUtil
.getPreviewImageForConcessione(this.getThreadLocalRequest(), "Concessione", cId);
UploadedImageDV uplImg = sessionloadPreviewImageForConcessione(this.getThreadLocalRequest(),
"Concessione", cId);
// List<UploadedImageDV> listUI = getUploadedImagesForId("Concessione", cId, 1);
Map<String, List<UploadedImageDV>> mapImages = new LinkedHashMap<String, List<UploadedImageDV>>();
mapImages.put(cId, Arrays.asList(uplImg));
@ -188,8 +194,7 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme
throws Exception {
LOG.info("getUploadedImagesForId [itemId: " + itemId + ", itemType: " + itemType + "] called");
return new ConcessioneImageUtil().getUploadedImagesForId(this.getThreadLocalRequest(), itemType, itemId,
maxImages);
return getUploadedImagesForId(this.getThreadLocalRequest(), itemType, itemId, maxImages);
}
/**
@ -551,4 +556,109 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme
return listDAO;
}
/**
* Gets the preview image for concessione from http session. It is the first
* image retrieved from mongoService for mongoConcessioneId. Caching it into
* session
*
* @param httpServletRequest the http servlet request
* @param itemType the item type
* @param mongoConcessioneId the mongo concessione id
* @return the preview image for concessione
*/
private UploadedImageDV sessionloadPreviewImageForConcessione(HttpServletRequest httpServletRequest,
String itemType, String mongoConcessioneId) {
LOG.info("sessionloadPreviewImageForConcessione [mongoConcessioneId: " + mongoConcessioneId + ", itemType: "
+ itemType + "] called");
HttpSession session = httpServletRequest.getSession();
Map<String, List<UploadedImageDV>> mapImages = null;
List<UploadedImageDV> lUI = null;
try {
mapImages = (LinkedHashMap) session.getAttribute(CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE);
if (mapImages == null) {
mapImages = new LinkedHashMap<String, List<UploadedImageDV>>();
}
List<UploadedImageDV> imagePreviewForConcessione = mapImages.get(mongoConcessioneId);
if (imagePreviewForConcessione == null || imagePreviewForConcessione.size() == 0) {
LOG.info("Into " + CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE + " object session the mongoConcessioneId "
+ mongoConcessioneId + " is empty or null, loading from service and filling it");
lUI = getUploadedImagesForId(httpServletRequest, itemType, mongoConcessioneId, 1);
mapImages.put(mongoConcessioneId, lUI);
}
lUI = mapImages.get(mongoConcessioneId);
LOG.info("From " + CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE + " object session read image: " + lUI);
session.setAttribute(CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE, mapImages);
} catch (Exception e) {
LOG.warn("Error occurred when instancing the " + UrlShortener.class.getSimpleName(), e);
}
if (lUI == null || lUI.isEmpty())
return null;
return lUI.get(0);
}
/**
* Gets the uploaded images for id.
*
* @param httpServletRequest the http servlet request
* @param itemType the item type
* @param itemId the item id
* @param maxImages the max images
* @return the uploaded images for id
* @throws Exception the exception
*/
private List<UploadedImageDV> getUploadedImagesForId(HttpServletRequest httpServletRequest, String itemType,
String itemId, Integer maxImages) throws Exception {
LOG.info("getUploadedImagesForId [itemId: " + itemId + ", itemType: " + itemType + "] called");
if (itemType == null)
throw new Exception("Invalid parameter. The itemType is null");
if (itemId == null)
throw new Exception("Invalid parameter. The itemId is null");
List<UploadedImageDV> listUI = null;
try {
if (itemType.equalsIgnoreCase("concessione")) {
LOG.info("Trying to get concessione for id " + itemId);
ConcessioniMongoServiceIdentityProxy cms = new ConcessioniMongoServiceIdentityProxy(httpServletRequest);
Concessione concessione = cms.getItemById(itemId);
if (concessione != null) {
LOG.info("For id " + itemId + ", got concessione " + concessione.getNome() + " from service");
List<UploadedImage> images = concessione.getImmaginiRappresentative();
if (images != null) {
listUI = new ArrayList<UploadedImageDV>();
int max = maxImages < images.size() ? maxImages : images.size();
for (int i = 0; i < max; i++) {
UploadedImageDV ui = ConvertToDataViewModel.toUploadedImage(images.get(i));
listUI.add(ui);
}
LOG.info("For id " + itemId + ", got " + listUI.size() + " image/s");
}
} else
throw new Exception("Concessione with id '" + itemId + "' not available");
}
return listUI;
} catch (Exception e) {
String erroMsg = UploadedImage.class.getSimpleName() + " not available for "
+ Concessione.class.getSimpleName() + " with id " + itemId;
LOG.error(erroMsg, e);
throw new Exception(erroMsg);
}
}
}

View File

@ -1,83 +1,83 @@
package org.gcube.portlets.user.geoportaldataviewer.server.mongoservice;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.application.geoportal.common.model.legacy.UploadedImage;
import org.gcube.application.geoportalcommon.ConvertToDataViewModel;
import org.gcube.application.geoportalcommon.shared.products.model.UploadedImageDV;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class ConcessioneImageUtil.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Sep 7, 2021
*/
public class ConcessioneImageUtil {
private static final Logger LOG = LoggerFactory.getLogger(ConcessioneImageUtil.class);
/**
* Gets the uploaded images for id.
*
* @param httpServletRequest the http servlet request
* @param itemType the item type
* @param itemId the item id
* @param maxImages the max images
* @return the uploaded images for id
* @throws Exception the exception
*/
public List<UploadedImageDV> getUploadedImagesForId(HttpServletRequest httpServletRequest, String itemType,
String itemId, Integer maxImages) throws Exception {
LOG.info("getUploadedImagesForId [itemId: " + itemId + ", itemType: " + itemType + "] called");
if (itemType == null)
throw new Exception("Invalid parameter. The itemType is null");
if (itemId == null)
throw new Exception("Invalid parameter. The itemId is null");
List<UploadedImageDV> listUI = null;
try {
if (itemType.equalsIgnoreCase("concessione")) {
LOG.info("Trying to get concessione for id " + itemId);
ConcessioniMongoServiceIdentityProxy cms = new ConcessioniMongoServiceIdentityProxy(httpServletRequest);
Concessione concessione = cms.getItemById(itemId);
if (concessione != null) {
LOG.info("For id " + itemId + ", got concessione " + concessione.getNome() + " from service");
List<UploadedImage> images = concessione.getImmaginiRappresentative();
if (images != null) {
listUI = new ArrayList<UploadedImageDV>();
int max = maxImages < images.size() ? maxImages : images.size();
for (int i = 0; i < max; i++) {
UploadedImageDV ui = ConvertToDataViewModel.toUploadedImage(images.get(i));
listUI.add(ui);
}
LOG.info("For id " + itemId + ", got " + listUI.size() + " image/s");
}
} else
throw new Exception("Concessione with id '" + itemId + "' not available");
}
return listUI;
} catch (Exception e) {
String erroMsg = UploadedImage.class.getSimpleName() + " not available for "
+ Concessione.class.getSimpleName() + " with id " + itemId;
LOG.error(erroMsg, e);
throw new Exception(erroMsg);
}
}
}
//package org.gcube.portlets.user.geoportaldataviewer.server.mongoservice;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import javax.servlet.http.HttpServletRequest;
//
//import org.gcube.application.geoportal.common.model.legacy.Concessione;
//import org.gcube.application.geoportal.common.model.legacy.UploadedImage;
//import org.gcube.application.geoportalcommon.ConvertToDataViewModel;
//import org.gcube.application.geoportalcommon.shared.products.model.UploadedImageDV;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
///**
// * The Class ConcessioneImageUtil.
// *
// * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
// *
// * Sep 7, 2021
// */
//public class ConcessioneImageUtil {
//
// private static final Logger LOG = LoggerFactory.getLogger(ConcessioneImageUtil.class);
//
// /**
// * Gets the uploaded images for id.
// *
// * @param httpServletRequest the http servlet request
// * @param itemType the item type
// * @param itemId the item id
// * @param maxImages the max images
// * @return the uploaded images for id
// * @throws Exception the exception
// */
// public List<UploadedImageDV> getUploadedImagesForId(HttpServletRequest httpServletRequest, String itemType,
// String itemId, Integer maxImages) throws Exception {
// LOG.info("getUploadedImagesForId [itemId: " + itemId + ", itemType: " + itemType + "] called");
//
// if (itemType == null)
// throw new Exception("Invalid parameter. The itemType is null");
//
// if (itemId == null)
// throw new Exception("Invalid parameter. The itemId is null");
//
// List<UploadedImageDV> listUI = null;
//
// try {
//
// if (itemType.equalsIgnoreCase("concessione")) {
//
// LOG.info("Trying to get concessione for id " + itemId);
// ConcessioniMongoServiceIdentityProxy cms = new ConcessioniMongoServiceIdentityProxy(httpServletRequest);
// Concessione concessione = cms.getItemById(itemId);
// if (concessione != null) {
// LOG.info("For id " + itemId + ", got concessione " + concessione.getNome() + " from service");
// List<UploadedImage> images = concessione.getImmaginiRappresentative();
//
// if (images != null) {
// listUI = new ArrayList<UploadedImageDV>();
// int max = maxImages < images.size() ? maxImages : images.size();
// for (int i = 0; i < max; i++) {
// UploadedImageDV ui = ConvertToDataViewModel.toUploadedImage(images.get(i));
// listUI.add(ui);
// }
// LOG.info("For id " + itemId + ", got " + listUI.size() + " image/s");
// }
// } else
// throw new Exception("Concessione with id '" + itemId + "' not available");
// }
//
// return listUI;
//
// } catch (Exception e) {
// String erroMsg = UploadedImage.class.getSimpleName() + " not available for "
// + Concessione.class.getSimpleName() + " with id " + itemId;
// LOG.error(erroMsg, e);
// throw new Exception(erroMsg);
// }
//
// }
//
//}

View File

@ -1,6 +1,7 @@
package org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.accessidentities;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.gcube.common.authorization.library.provider.AccessTokenProvider;
import org.gcube.common.keycloak.KeycloakClientFactory;
@ -16,15 +17,17 @@ public class IAMClientIdentity implements GcubeIdentity {
private String previousUMAToken = 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 = SessionUtil.getIAMClientCredentials(httpRequest);
IAMClientCredentials credentials = sessionGetIAMClientCredentials(httpRequest);
try {
if(credentials==null) {
credentials = IAMClientCredentialsReader.getCredentials();
SessionUtil.setIAMClientCredentials(httpRequest,credentials);
sessionSetIAMClientCredentials(httpRequest,credentials);
}
} catch (Exception e) {
LOG.error("Error on discovering IAM Client credentials", e);
@ -80,4 +83,35 @@ public class IAMClientIdentity implements GcubeIdentity {
}
/**
* Gets the IAM client credentials.
*
* @param httpRequest the http request
* @return the IAM client credentials
*/
public static IAMClientCredentials sessionGetIAMClientCredentials(HttpServletRequest httpRequest) {
HttpSession session = httpRequest.getSession();
try {
return (IAMClientCredentials) session.getAttribute(IAM_CLIENT_CREDENTIALS);
} catch (Exception e) {
LOG.warn("Error occurred when reading " + IAM_CLIENT_CREDENTIALS + " from session");
return null;
}
}
/**
* Sets the IAM client credentials.
*
* @param httpRequest the http request
* @param iamCC the iam CC
*/
public static void sessionSetIAMClientCredentials(HttpServletRequest httpRequest, IAMClientCredentials iamCC) {
HttpSession session = httpRequest.getSession();
try {
session.setAttribute(IAM_CLIENT_CREDENTIALS, iamCC);
} catch (Exception e) {
LOG.warn("Error occurred when setting " + IAM_CLIENT_CREDENTIALS + " into session");
}
}
}

View File

@ -3,19 +3,12 @@
*/
package org.gcube.portlets.user.geoportaldataviewer.server.util;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.gcube.application.geoportalcommon.shared.products.model.UploadedImageDV;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.ConcessioneImageUtil;
import org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.accessidentities.IAMClientCredentials;
import org.gcube.portlets.user.urlshortener.UrlShortener;
import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault;
@ -42,10 +35,6 @@ public class SessionUtil {
private static final String URL_SHORTENER_SERVICE = "URL_SHORTENER_SERVICE";
private static final String CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE = "MAP_IMAGE_PREVIEW_FOR_CONCESSIONE";
private static final String IAM_CLIENT_CREDENTIALS = "IAM_CLIENT_CREDENTIALS";
/**
* Checks if is into portal.
*
@ -180,80 +169,49 @@ public class SessionUtil {
return shortener;
}
/**
* Gets the preview image for concessione. It is the first image retrieved from
* mongoService for mongoConcessioneId. Caching it into session
*
* @param httpServletRequest the http servlet request
* @param itemType the item type
* @param mongoConcessioneId the mongo concessione id
* @return the preview image for concessione
*/
public static UploadedImageDV getPreviewImageForConcessione(HttpServletRequest httpServletRequest, String itemType,
String mongoConcessioneId) {
HttpSession session = httpServletRequest.getSession();
Map<String, List<UploadedImageDV>> mapImages = null;
List<UploadedImageDV> lUI = null;
try {
mapImages = (LinkedHashMap) session.getAttribute(CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE);
if (mapImages == null) {
mapImages = new LinkedHashMap<String, List<UploadedImageDV>>();
}
List<UploadedImageDV> imagePreviewForConcessione = mapImages.get(mongoConcessioneId);
if (imagePreviewForConcessione == null || imagePreviewForConcessione.size() == 0) {
LOG.info("Into " + CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE + " object session the mongoConcessioneId "
+ mongoConcessioneId + " is empty or null, loading from service and filling it");
lUI = new ConcessioneImageUtil().getUploadedImagesForId(httpServletRequest, itemType,
mongoConcessioneId, 1);
mapImages.put(mongoConcessioneId, lUI);
}
lUI = mapImages.get(mongoConcessioneId);
LOG.info("From " + CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE + " object session read image: " + lUI);
session.setAttribute(CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE, mapImages);
} catch (Exception e) {
LOG.warn("Error occurred when instancing the " + UrlShortener.class.getSimpleName(), e);
}
if (lUI == null || lUI.isEmpty())
return null;
return lUI.get(0);
}
/**
* Gets the IAM client credentials.
*
* @param httpRequest the http request
* @return the IAM client credentials
*/
public static IAMClientCredentials getIAMClientCredentials(HttpServletRequest httpRequest) {
HttpSession session = httpRequest.getSession();
try {
return (IAMClientCredentials) session.getAttribute(IAM_CLIENT_CREDENTIALS);
} catch (Exception e) {
LOG.warn("Error occurred when reading " + IAM_CLIENT_CREDENTIALS + " from session");
return null;
}
}
/**
* Sets the IAM client credentials.
*
* @param httpRequest the http request
* @param iamCC the iam CC
*/
public static void setIAMClientCredentials(HttpServletRequest httpRequest, IAMClientCredentials iamCC) {
HttpSession session = httpRequest.getSession();
try {
session.setAttribute(IAM_CLIENT_CREDENTIALS, iamCC);
} catch (Exception e) {
LOG.warn("Error occurred when setting " + IAM_CLIENT_CREDENTIALS + " into session");
}
}
// /**
// * Gets the preview image for concessione. It is the first image retrieved from
// * mongoService for mongoConcessioneId. Caching it into session
// *
// * @param httpServletRequest the http servlet request
// * @param itemType the item type
// * @param mongoConcessioneId the mongo concessione id
// * @return the preview image for concessione
// */
// public static UploadedImageDV getPreviewImageForConcessione(HttpServletRequest httpServletRequest, String itemType,
// String mongoConcessioneId) {
//
// HttpSession session = httpServletRequest.getSession();
// Map<String, List<UploadedImageDV>> mapImages = null;
// List<UploadedImageDV> lUI = null;
// try {
// mapImages = (LinkedHashMap) session.getAttribute(CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE);
//
// if (mapImages == null) {
// mapImages = new LinkedHashMap<String, List<UploadedImageDV>>();
// }
//
// List<UploadedImageDV> imagePreviewForConcessione = mapImages.get(mongoConcessioneId);
//
// if (imagePreviewForConcessione == null || imagePreviewForConcessione.size() == 0) {
// LOG.info("Into " + CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE + " object session the mongoConcessioneId "
// + mongoConcessioneId + " is empty or null, loading from service and filling it");
// lUI = new ConcessioneImageUtil().getUploadedImagesForId(httpServletRequest, itemType,
// mongoConcessioneId, 1);
// mapImages.put(mongoConcessioneId, lUI);
// }
//
// lUI = mapImages.get(mongoConcessioneId);
// LOG.info("From " + CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE + " object session read image: " + lUI);
// session.setAttribute(CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE, mapImages);
//
// } catch (Exception e) {
// LOG.warn("Error occurred when instancing the " + UrlShortener.class.getSimpleName(), e);
// }
//
// if (lUI == null || lUI.isEmpty())
// return null;
//
// return lUI.get(0);
// }
}