//package org.gcube.portlets.user.geoportaldataviewer.server; // //import static org.gcube.application.geoportal.client.GeoportalAbstractPlugin.statefulMongoConcessioni; // //import javax.servlet.http.HttpServletRequest; // //import org.gcube.application.geoportal.client.legacy.ConcessioniManagerI; //import org.gcube.application.geoportal.common.model.legacy.Concessione; //import org.gcube.common.authorization.library.provider.AccessTokenProvider; //import org.gcube.common.keycloak.KeycloakClientFactory; //import org.gcube.common.keycloak.model.TokenResponse; //import org.gcube.common.scope.api.ScopeProvider; //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 ConcessioniMongoService2 { // // private static final Logger LOG = LoggerFactory.getLogger(ConcessioniMongoService2.class); // // /** // * Instantiates a new concessioni mongo service, interface for client // * statefulMongoConcessioni. // */ // public ConcessioniMongoService2() { // } // // /** // * Gets the item by id. // * // * @param request the request // * @param mongoItemId the mongo item id // * @return the item by id // * @throws Exception the exception // */ // public Concessione getItemById(HttpServletRequest request, String mongoItemId) throws Exception { // LOG.info("called getItemById: " + mongoItemId); // SessionUtil.getCurrentContext(request, true); // SessionUtil.getCurrentToken(request, true); // // Obtain the client // ConcessioniManagerI concessioniManager = statefulMongoConcessioni().build(); // // Returning item by Id // return concessioniManager.getById(mongoItemId); // } // // /** // * Gets the item by id. // * // * @param request the request // * @param mongoItemId the mongo item id // * @return the item by id // * @throws Exception the exception // */ // public Concessione getItemById(String mongoItemId) throws Exception { // LOG.info("called getItemById: " + mongoItemId); // ConcessioniManagerI concessioniManager = statefulMongoConcessioni().build(); // // Returning item by Id // return concessioniManager.getById(mongoItemId); // } // // /** // * Iam client get item by. // * // * @param scope the scope // * @param mongoItemId the mongo item id // * @param clientId the client id // * @param clientSecret the secret // * @return the concessione // * @throws Exception the exception // */ // public Concessione iamClientGetItemBy(String scope, String mongoItemId, String clientId, String clientSecret) // throws Exception { // LOG.info("called IAM Client getItemById: " + mongoItemId); // // ScopeProvider.instance.set(scope); // // if (clientId == null) { // throw new Exception("Invalid parameter 'clientId'"); // } // // if (clientSecret == null) { // throw new Exception("Invalid parameter 'secret'"); // } // // String umaAcessToken = null; // try { // // LOG.info("Querying KeycloakClientFactory to get UMA token.."); // TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(clientId, clientSecret, scope, null); // umaAcessToken = tr.getAccessToken(); // if (umaAcessToken != null && !umaAcessToken.isEmpty()) { // LOG.info("UMA Access Token read correctly"); // } else { // LOG.error("UMA Access Token NOT RETRIEVED!!!"); // throw new Exception("UMA Access Token is null or empty"); // } // } catch (Exception e2) { // throw new Exception("Error occurred on reading UMA access token:", e2); // } // // String previousUMAToken = null; // try { // // try { // // Here the previousUMAToken should be null // previousUMAToken = AccessTokenProvider.instance.get(); // } catch (Exception e) { // // catching excpetion to be sure // // silent // } // // LOG.debug("JWT token: " + umaAcessToken.substring(0, 20) + "_MASKED_TOKEN_"); // LOG.info("Setting clientId '" + clientId + "' identity by JWT token in the " // + AccessTokenProvider.class.getSimpleName()); // AccessTokenProvider.instance.set(umaAcessToken); // ConcessioniManagerI concessioniManager = statefulMongoConcessioni().build(); // // Returning item by Id // return concessioniManager.getById(mongoItemId); // // } catch (Exception e) { // LOG.error(e.getMessage(), e); // throw new Exception(e.getMessage()); // } finally { // try { // AccessTokenProvider.instance.set(previousUMAToken); // LOG.info("Done reset to prevoius UMA token into " + AccessTokenProvider.class.getSimpleName()); // } catch (Exception e) { // } // } // } // // /** // * To concessione. // * // * @param jsonString the json string // * @return the concessione // */ // public Concessione toConcessione(String jsonString) { // LOG.info("toConcessione called"); // try { // return org.gcube.application.geoportal.client.utils.Serialization.read(jsonString, Concessione.class); // } catch (Exception e) { // LOG.warn("Error on serializing: ", e); // return null; // } // } // // /* // * For testing public Concessione iamClientGetItemBy(HttpServletRequest request, // * String mongoItemId, String clientId, String secret) throws Exception { String // * scope = SessionUtil.getCurrentContext(request, true); return // * iamClientGetItemBy(scope, mongoItemId, clientId, secret); } // */ // //}