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

83 lines
3.2 KiB
Java

package org.gcube.portlets.user.geoportaldataviewer.server.mongoservice;
import javax.servlet.http.HttpServletRequest;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportalcommon.ConvertToDataValueObjectModel;
import org.gcube.application.geoportalcommon.ProjectDVBuilder;
import org.gcube.application.geoportalcommon.geoportal.GeoportalClientCaller;
import org.gcube.application.geoportalcommon.geoportal.ProjectsCaller;
import org.gcube.application.geoportalcommon.shared.geoportal.project.ProjectDV;
import org.gcube.common.portal.PortalContext;
import org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.accessidentity.GcubeIdentity;
import org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.accessidentity.IAMClientIdentity;
import org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.accessidentity.UserIdentity;
import org.gcube.portlets.user.geoportaldataviewer.server.util.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class GeoportalServiceIdentityProxy.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Sep 23, 2021
*/
public class GeoportalServiceIdentityProxy {
private static final Logger LOG = LoggerFactory.getLogger(GeoportalServiceIdentityProxy.class);
private GcubeIdentity gCubeIdentity;
/**
* Instantiates a new concessioni mongo service proxy.
*
* @param request the request
* @throws Exception the exception
*/
public GeoportalServiceIdentityProxy(HttpServletRequest request) throws Exception {
SessionUtil.getCurrentContext(request, true);
PortalContext pContext = PortalContext.getConfiguration();
String username = null;
try {
username = pContext.getCurrentUser(request).getUsername();
if (username == null || username.isEmpty())
throw new Exception("Invalid username");
} catch (Exception e) {
LOG.info("Username not detected in session");
username = null; // to be sure that is null and not empty string
}
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 project by ID.
*
* @param profileID the profile ID
* @param projectID the project ID
* @return the project by ID
* @throws Exception the exception
*/
public ProjectDV getProjectByID(String profileID, String projectID) throws Exception {
ProjectsCaller clientPrj = GeoportalClientCaller.projects();
Project theProject = clientPrj.getProjectByID(profileID, projectID);
ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(true);
return ConvertToDataValueObjectModel.toProjectDV(theProject, projectBuilder);
/*
* 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(); }
*/
}
}