the list of licenses, organizations in which the user can publish and product profiles are preloaded and put into session by the ckan portlet

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/gcube-ckan-datacatalog@129806 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-07-02 21:41:11 +00:00
parent aa1a2868df
commit 40b6bb2ad9
7 changed files with 207 additions and 99 deletions

View File

@ -4,6 +4,9 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="ckan-util-library-1.0.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/ckan-util-library/ckan-util-library">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="context-root" value="gcube-ckan-datacatalog"/>
<property name="java-output-path" value="/gcube-ckan-datacatalog/target/gcube-ckan-datacatalog-1.0.0-SNAPSHOT/WEB-INF/classes"/>
</wb-module>

View File

@ -39,50 +39,11 @@ public class GCubeCkanDataCatalog implements EntryPoint {
@Override
public void onWindowClosing(ClosingEvent closingEvent) {
// eventManager.getEventBus().fireEvent(new NotifyLogoutEvent());
// invoke logout
frame.setUrl(GCubeCkanDataCatalog.CKAN_LOGUT_SERVICE);
/*RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, CKAN_LOGUT_SERVICE);
RequestCallback call = new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
// TODO Auto-generated method stub
}
@Override
public void onError(Request request, Throwable exception) {
// TODO Auto-generated method stub
}
};
builder.setCallback(call );
try {
builder.send();
}
catch (RequestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
});
// Window.addCloseHandler(new CloseHandler<Window>() {
//
// @Override
// public void onClose(CloseEvent<Window> event) {
//
// GCubeCkanDataCatalogPanel.print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
// CKanLeaveFrame frame = new CKanLeaveFrame(GCubeCkanDataCatalog.CKAN_LOGUT_SERVICE);
// DOM.appendChild(RootPanel.getBodyElement(), frame.getElement());
// }
// });
GCubeCkanDataCatalogPanel panel = new GCubeCkanDataCatalogPanel(RootPanel.get(DIV_PORTLET_ID), eventManager.getEventBus());
eventManager.setPanel(panel);

View File

@ -1,5 +1,6 @@
package org.gcube.portlets.gcubeckan.gcubeckandatacatalog.client.view;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Frame;
@ -21,7 +22,7 @@ public class CKanLeaveFrame extends Frame{
getElement().getStyle().setHeight(1, Unit.PX);
}catch(Exception e){
GCubeCkanDataCatalogPanel.print("exception " + e);
GWT.log("exception " + e);
}
}

View File

@ -188,7 +188,7 @@ public class CkanMetadataManagementPanel extends FlowPanel{
/**
* Show only home/statistics buttons
*/
public void doNotUserRelatedInfo(){
public void doNotShowUserRelatedInfo(){
insertMeta.setVisible(false);
editMeta.setVisible(false);

View File

@ -81,7 +81,7 @@ public class GCubeCkanDataCatalogPanel extends BaseViewTemplate{
if(result){
// the portlet is outside the portal and no user is logged in
// show only home and statistics
managementPanel.doNotUserRelatedInfo();
managementPanel.doNotShowUserRelatedInfo();
}else{
@ -139,7 +139,7 @@ public class GCubeCkanDataCatalogPanel extends BaseViewTemplate{
// the portlet is outside the portal and no user is logged in
// show only home and statistics
managementPanel.doNotUserRelatedInfo();
managementPanel.doNotShowUserRelatedInfo();
}
});
@ -159,10 +159,6 @@ public class GCubeCkanDataCatalogPanel extends BaseViewTemplate{
updateSize();
}
public static native void print(String msg)/*-{
console.log(msg);
}-*/;
/**
* Gets the top panel height.
*

View File

@ -41,16 +41,12 @@ import eu.trentorise.opendata.traceprov.internal.org.apache.commons.io.IOUtils;
@SuppressWarnings("serial")
public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implements GcubeCkanDataCatalogService {
/**
*
*/
public static final String UTF_8 = "UTF-8";
private static final String PORT_HTTP = ":80";
private static final String PORT_HTTPS = ":443";
private static final String HTTPS = "https";
private static final String HTTP = "http";
public static String CKANCONNECTORCONTEXT = "CkanConnectorContext";
// public static String CKANHIDEHEADER = "CkanHideHeader";
public static String CKANCONNECTORLOGOUT = "CkanConnectorLogout";
public static final String USERNAME_ATTRIBUTE = ScopeHelper.USERNAME_ATTRIBUTE;
private static Logger logger = LoggerFactory.getLogger(GcubeCkanDataCatalogServiceImpl.class);
@ -61,9 +57,13 @@ public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implem
public final static String TEST_MAIL = "test.user@test-com";
public final static String TEST_SEC_TOKEN = "4620e6d0-2313-4f48-9d54-eb3efd01a810";
public static final String CKAN_TOKEN_KEY = "ckanToken";
public static final String CKAN_ORGS_USER_KEY = "ckanOrgs";
public static final String CKAN_HIGHEST_ROLE = "ckanHigherRole"; // editor, member, admin, sysadmin
// ckan keys for ASL
private static final String CKAN_TOKEN_KEY = "ckanToken";
private static final String CKAN_ORGS_USER_KEY = "ckanOrgs"; // organizations to whom it belongs
private static final String CKAN_HIGHEST_ROLE = "ckanHighestRole"; // editor, member, admin, sysadmin
private static final String CKAN_LICENSES_KEY = "ckanLicenses"; // licenses
private static final String CKAN_ORGANIZATIONS_PUBLISH_KEY = "ckanOrganizationsPublish"; // here he can publish
private static final String CKAN_PROFILES_KEY = "ckanProfiles"; // product profiles
// ckan utils methods
private CKanUtilsImpl instance;
@ -289,7 +289,7 @@ public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implem
if(isSysAdmin){
logger.debug("The user is a sysadmin of the catalog -> he can edit/add");
logger.info("The user is a sysadmin of the catalog -> he can edit/add");
session.setAttribute(CKAN_HIGHEST_ROLE, CkanRole.SYSADMIN);
toReturn = CkanRole.SYSADMIN;
@ -297,7 +297,20 @@ public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implem
toReturn = UserUtil.getHighestRole(currentScope, username, groupName, ckanUtils);
session.setAttribute(CKAN_HIGHEST_ROLE, toReturn);
logger.debug("Set role " + toReturn + " into session for user " + username);
logger.info("Set role " + toReturn + " into session for user " + username);
}
// if he is not a member (so he can publish), preload:
// 1) organizations in which he can publish
// 2) the list of licenses
// 3) metadata profiles
// In this way the publish widget can simply retrieve those information from the ASL session
if(!toReturn.equals(CkanRole.MEMBER)){
UserUtil.getLicenses(session, CKAN_LICENSES_KEY, ckanUtils);
UserUtil.getUserOrganizationsList(session, CKAN_ORGANIZATIONS_PUBLISH_KEY, ckanUtils, getUserCKanTokenFromSession());
UserUtil.getMetadataProfilesList(session, CKAN_PROFILES_KEY, ckanUtils);
}
}
}
@ -445,7 +458,6 @@ public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implem
}
}
@SuppressWarnings("unchecked")
@Override
public Map<String, String> getCkanOrganizationsNamesAndUrlsForUser() {
@ -503,38 +515,4 @@ public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implem
return false;
}
}
/*
public static void main(String[] args) {
URL url;
try {
String deleteURI = "https://ckan-d-d4s.d4science.org:443/ckan-connector/gcube/service/disconnect?gcube-token=4620e6d0-2313-4f48-9d54-eb3efd01a810";
logger.debug("Perfoming HTTP delete to URI: "+deleteURI);
url = new URL(deleteURI);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
httpCon.connect();
if(httpCon.getResponseCode()==HttpStatus.SC_OK){
Map<String, List<String>> map = httpCon.getHeaderFields();
for (String key : map.keySet()) {
logger.trace("key: "+key +", value: "+map.get(key));
}
InputStream in = httpCon.getInputStream();
String encoding = httpCon.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
logger.debug("response: "+body);
logger.info("Logout Completed, response code: "+HttpStatus.SC_OK);
}else{
logger.warn("An error occurred during perfoming CKAN logout, Response status is: "+httpCon.getResponseCode());
}
}
catch (IOException e) {
logger.error("An error occured during performing Logout from CKAN", e);
}
}*/
}

View File

@ -4,11 +4,25 @@
package org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.gcube.application.framework.core.session.ASLSession;
import org.gcube.datacatalogue.ckanutillibrary.CKanUtilsImpl;
import org.gcube.datacatalogue.ckanutillibrary.models.RolesIntoOrganization;
import org.gcube.datacatalogue.metadatadiscovery.DataCalogueMetadataFormatReader;
import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataType;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataField;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataValidator;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataVocabulary;
import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.shared.CkanRole;
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.LicensesBean;
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.MetaDataProfileBean;
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.MetaDataTypeWrapper;
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.MetadataFieldWrapper;
import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.RoleManager;
import org.gcube.vomanagement.usermanagement.UserManager;
@ -67,7 +81,7 @@ public class UserUtil {
return null;
}
}
/**
* Retrieve the highest ckan role the user has.
* @param currentScope the current scope
@ -76,10 +90,10 @@ public class UserUtil {
* @param ckanUtils ckanUtils
*/
public static CkanRole getHighestRole(String currentScope, String username, String groupName, CKanUtilsImpl ckanUtils){
// base role as default value
CkanRole toReturn = CkanRole.MEMBER;
try{
// retrieve the liferay's roles for the user: we need to check every vre is the current scope is the root
// and just the current one if the scope is a vre, the vres inside the vo if it is a VO.
@ -92,7 +106,7 @@ public class UserUtil {
// retrieve current group id
long currentGroupId = groupManager.getGroupIdFromInfrastructureScope(currentScope);
logger.debug("Group id is " + currentGroupId + " and scope is " + currentScope);
// root
@ -209,7 +223,7 @@ public class UserUtil {
toReturn = ckanRoleMatched;
}
}
// set the role
logger.debug("Setting role " + toReturn + " into session for user " + username );
@ -249,10 +263,10 @@ public class UserUtil {
logger.error("Unable to retrieve the role information for this user. Returning member role", e);
toReturn = CkanRole.MEMBER;
}
return toReturn;
}
/**
* Map between roles.
*
@ -267,4 +281,159 @@ public class UserUtil {
default : return CkanRole.MEMBER;
}
}
/**
* Load the licenses list and put them into the asl session (the publisher widget will use it)
* @param session
* @param ckanLicensesKey
* @param ckanUtils
*/
public static void getLicenses(ASLSession session, String ckanLicensesKey, CKanUtilsImpl ckanUtils) {
try{
String username = session.getUsername();
logger.debug("User in session is " + username);
if(session.getAttribute(ckanLicensesKey) != null){
logger.debug("List of licenses was into session");
}
else{
List<String> titlesLicenses = ckanUtils.getLicenseTitles();
LicensesBean licensesBean = new LicensesBean(titlesLicenses);
session.setAttribute(ckanLicensesKey, licensesBean);
logger.info("List of licenses has been saved into session" + licensesBean);
}
}
catch(Exception e){
logger.error("Failed to preload licenses list", e);
}
}
/**
* Load the list of organizations in which he can publish and put them into the asl session (the publisher widget will use it)
* @param session
* @param ckanOrganizationsPublishKey
* @param ckanUtils
*/
public static void getUserOrganizationsList(ASLSession session,
String ckanOrganizationsPublishKey, CKanUtilsImpl ckanUtils, String token) {
try{
String username = session.getUsername();
logger.debug("Request for user " + username + " organizations list");
List<String> orgsName = new ArrayList<String>();
if(session.getAttribute(ckanOrganizationsPublishKey) != null){
logger.debug("List of organizations was into session");
}
else{
if(ckanUtils.isSysAdmin(username, token)){
logger.info("The user " + username + " is a sysadmin. He can publish everywhere");
orgsName = ckanUtils.getOrganizationsNames(); // get all organizations' names
}else{
// We need to retrieve orgs in which the user has the roles ADMIN or EDITOR
List<RolesIntoOrganization> rolesToMatch = new ArrayList<RolesIntoOrganization>();
rolesToMatch.add(RolesIntoOrganization.EDITOR);
rolesToMatch.add(RolesIntoOrganization.ADMIN);
Map<String, List<RolesIntoOrganization>> orgsAndRoles = ckanUtils.getGroupsAndRolesByUser(username, rolesToMatch);
logger.debug("Result is " + orgsAndRoles);
Iterator<Entry<String, List<RolesIntoOrganization>>> iterator = orgsAndRoles.entrySet().iterator();
// get the names
while (iterator.hasNext()) {
Map.Entry<String, List<RolesIntoOrganization>> entry = (Map.Entry<String, List<RolesIntoOrganization>>) iterator
.next();
orgsName.add(entry.getKey());
logger.debug("The user has a role ADMIN/EDITOR into org " + entry.getKey());
}
}
session.setAttribute(ckanOrganizationsPublishKey, orgsName);
logger.info("Organizations name for user " + username + " has been saved into session");
}
}catch(Exception e){
logger.error("Failed to preload list of organizations in which the user can publish", e);
}
}
/**
* Load the list of product profiles and put them into the asl session (the publisher widget will use it)
* @param session
* @param ckanOrganizationsPublishKey
* @param ckanUtils
*/
public static void getMetadataProfilesList(ASLSession session,
String ckanProfilesKey, CKanUtilsImpl ckanUtils) {
try{
String username = session.getUsername();
logger.debug("User in session is " + username);
if(session.getAttribute(ckanProfilesKey) != null){
logger.info("List of profiles was into session");
}
else{
List<MetaDataProfileBean> beans = new ArrayList<MetaDataProfileBean>();
try {
DataCalogueMetadataFormatReader reader = new DataCalogueMetadataFormatReader();
for (MetadataType mt : reader.getListOfMetadataTypes()) {
MetadataFormat metadata = reader.getMetadataFormatForMetadataType(mt);
// we need to wrap the list of metadata
List<MetadataFieldWrapper> wrapperList = new ArrayList<MetadataFieldWrapper>();
List<MetadataField> toWrap = metadata.getMetadataFields();
for(MetadataField metadataField: toWrap){
MetadataFieldWrapper wrapperObj = new MetadataFieldWrapper();
wrapperObj.setDefaulValue(metadataField.getDefaulValue());
wrapperObj.setFieldName(metadataField.getFieldName());
wrapperObj.setIsBoolean(metadataField.getIsBoolean());
wrapperObj.setMandatory(metadataField.getMandatory());
wrapperObj.setNote(metadataField.getNote());
MetadataValidator validator = metadataField.getValidator();
if(validator != null)
wrapperObj.setValidator(validator.getRegularExpression());
MetadataVocabulary vocabulary = metadataField.getVocabulary();
if(vocabulary != null)
wrapperObj.setVocabulary(vocabulary.getVocabularyFields());
// add to the list
wrapperList.add(wrapperObj);
}
// wrap the mt as well
MetaDataTypeWrapper typeWrapper = new MetaDataTypeWrapper();
typeWrapper.setDescription(mt.getDescription());
typeWrapper.setId(mt.getId());
typeWrapper.setName(mt.getName());
MetaDataProfileBean bean = new MetaDataProfileBean(typeWrapper, wrapperList);
beans.add(bean);
}
logger.info("List of beans is " + beans);
session.setAttribute(ckanProfilesKey, beans);
logger.debug("List of profiles has been saved into session");
} catch (Exception e) {
logger.error("Error while retrieving metadata beans ", e);
}
}
}
catch(Exception e){
logger.error("Failed to retrieve the list of product profiles", e);
}
}
}