geoportal-data-viewer-app/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/mongoservice/ConcessioniMongoServiceIden...

78 lines
2.3 KiB
Java

package org.gcube.portlets.user.geoportaldataviewer.server.mongoservice;
import javax.servlet.http.HttpServletRequest;
import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.common.portal.PortalContext;
import org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.accessidentities.GcubeIdentity;
import org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.accessidentities.IAMClientIdentity;
import org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.accessidentities.UserIdentity;
import org.gcube.portlets.user.geoportaldataviewer.server.util.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class ConcessioniMongoServiceIdentityProxy.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Sep 23, 2021
*/
public class ConcessioniMongoServiceIdentityProxy {
private static final Logger LOG = LoggerFactory.getLogger(ConcessioniMongoServiceIdentityProxy.class);
private GcubeIdentity gCubeIdentity;
/**
* Instantiates a new concessioni mongo service proxy.
*
* @param request the request
* @throws Exception the exception
*/
public ConcessioniMongoServiceIdentityProxy(HttpServletRequest request) throws Exception {
SessionUtil.getCurrentContext(request, true);
PortalContext pContext = PortalContext.getConfiguration();
String username = null;
try {
username = pContext.getCurrentUser(request).getUsername();
}catch (Exception e) {
LOG.info("Username not detected in session");
}
if (username != null) {
LOG.info("User detected, using its identity");
gCubeIdentity = new UserIdentity();
} else {
LOG.info("User not detected, using IAM Client identity");
gCubeIdentity = new IAMClientIdentity();
}
gCubeIdentity.setIdentity(request);
}
/**
* Gets the item by id.
*
* @param mongoItemId the mongo item id
* @return the item by id
* @throws Exception the exception
*/
public Concessione getItemById(String mongoItemId) throws Exception {
try {
// Obtain the client
ConcessioniMongoService concessioniManager = new ConcessioniMongoService();
// Returning item by Id
return concessioniManager.getItemById(mongoItemId);
}catch(Exception e) {
LOG.error("Error on reading itemById: "+mongoItemId, e);
throw(e);
}finally {
gCubeIdentity.resetIdentity();
}
}
}