minor fix for support of #11139

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/widgets/ckan-metadata-publisher-widget@163125 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2018-02-09 14:40:21 +00:00
parent c425ed2c92
commit 60d3b61185
3 changed files with 43 additions and 50 deletions

View File

@ -370,7 +370,8 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
datasetUrl,
false, // send notification to other people
toCreate.getTags(),
GenericUtils.getCurrentUser(getThreadLocalRequest()).getFullname()
GenericUtils.getCurrentUser(getThreadLocalRequest()).getFullname(),
GenericUtils.getCurrentClientUrl(getThreadLocalRequest())
);
threadWritePost.start();

View File

@ -72,45 +72,6 @@ public class AssociationToGroupAndNotifyThread extends Thread {
logger.info("Association thread started to put the dataset with id = "+ datasetId + " into group with title " + groupTitle + " for user " + username);
// if(groupTitle != null){
// try{
//
// // create the group
// CkanGroup group = catalogue.createGroup(groupTitle, groupTitle, "");
//
// if(group == null){
//
// logger.warn("The group doesn't exist! Unable to perform such association");
//
// }else{
//
// logger.debug("Group exists, going to add the user " + username + " as its admin...");
//
// // retrieve the role to be assigned according the one the user has into the organization of the dataset
// RolesCkanGroupOrOrg role = RolesCkanGroupOrOrg.valueOf(catalogue.getRoleOfUserInOrganization(username, organization, catalogue.getApiKeyFromUsername(username)).toUpperCase());
//
// if(!role.equals(RolesCkanGroupOrOrg.ADMIN))
// role = RolesCkanGroupOrOrg.MEMBER; // decrease the role to member if it is not an admin
//
// boolean assigned = catalogue.checkRoleIntoGroup(username, groupTitle, role);
//
// if(assigned){
//
// logger.debug("Admin/editor role was assigned for this group, going to associate the product to the group");
// boolean putIntoGroup = catalogue.assignDatasetToGroup(groupTitle, datasetId, catalogue.getApiKeyFromUsername(username));
// logger.info("Was product put into group? " + putIntoGroup);
//
// if(putIntoGroup)
// notifyGroupAdmins(catalogue, group.getName(), group.getTitle(), username);
//
// }
// }
//
// }catch(Exception e){
// logger.warn("Something went wrong when tried to add the group " + groupTitle, e);
// }
// }
// force creation of groups if needed
if(groupsForceCreation != null){
logger.info("Groups that must be created before association are " + groupsForceCreation);

View File

@ -2,10 +2,13 @@ package org.gcube.portlets.widgets.ckandatapublisherwidget.server.threads;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Base64;
import java.util.List;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.portal.GCubePortalConstants;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portal.databook.client.GCubeSocialNetworking;
import org.gcube.portlets.widgets.ckandatapublisherwidget.server.utils.GCoreEndPointReaderSocial;
import org.gcube.portlets.widgets.ckandatapublisherwidget.server.utils.GenericUtils;
import org.json.simple.JSONObject;
@ -34,8 +37,8 @@ public class WritePostCatalogueManagerThread extends Thread {
private static final String APPLICATION_ID_CATALOGUE_MANAGER = "org.gcube.datacatalogue.ProductCatalogue";
private static final String NOTIFICATION_MESSAGE = "Dear members,<br>The item '$PRODUCT_TITLE' has been just published by $USER_FULLNAME.<br>You can find it here: $PRODUCT_URL <br>";
private static final String SOCIAL_SERVICE_APPLICATION_TOKEN = "/2/tokens/generate-application-token";
private static final String SOCIAL_SERVICE_WRITE_APPLICATION_POST = "/2/posts/write-post-app";
private static final String SOCIAL_SERVICE_APPLICATION_TOKEN = "2/tokens/generate-application-token";
private static final String SOCIAL_SERVICE_WRITE_APPLICATION_POST = "2/posts/write-post-app";
private static final String MEDIATYPE_JSON = "application/json";
private static final Log logger = LogFactoryUtil.getLog(WritePostCatalogueManagerThread.class);
// private static Logger logger = LoggerFactory.getLogger(WritePostCatalogueManagerThread.class);
@ -46,6 +49,7 @@ public class WritePostCatalogueManagerThread extends Thread {
private boolean enableNotification;
private List<String> hashtags;
private String userFullName;
private String userCurrentUrl;
/**
* @param token
@ -59,7 +63,7 @@ public class WritePostCatalogueManagerThread extends Thread {
public WritePostCatalogueManagerThread(
String username, String scope,
String productTitle, String productUrl, boolean enableNotification,
List<String> hashtags, String userFullName) {
List<String> hashtags, String userFullName, String userCurrentUrl) {
super();
this.username = username;
this.scope = scope;
@ -68,6 +72,7 @@ public class WritePostCatalogueManagerThread extends Thread {
this.enableNotification = enableNotification;
this.hashtags = hashtags;
this.userFullName = userFullName;
this.userCurrentUrl = userCurrentUrl;
}
@Override
@ -90,6 +95,13 @@ public class WritePostCatalogueManagerThread extends Thread {
ScopeProvider.instance.set(scope);
SecurityTokenProvider.instance.set(token);
final String profilePageURL = GCubePortalConstants.PREFIX_GROUP_URL + extractOrgFriendlyURL(userCurrentUrl) + GCubePortalConstants.USER_PROFILE_FRIENDLY_URL;
userFullName = "<a class=\"link\" href=\"" + profilePageURL + "?"+
Base64.getEncoder().encodeToString(GCubeSocialNetworking.USER_PROFILE_OID.getBytes())+"="+
Base64.getEncoder().encodeToString(username.getBytes())+"\">"+userFullName+
"</a> ";
// write
writeProductPost(
productTitle,
@ -107,6 +119,21 @@ public class WritePostCatalogueManagerThread extends Thread {
}
}
public static String extractOrgFriendlyURL(String portalURL) {
String groupRegEx = "/group/";
if (portalURL.contains(groupRegEx)) {
String[] splits = portalURL.split(groupRegEx);
String friendlyURL = splits[1];
if (friendlyURL.contains("/")) {
friendlyURL = friendlyURL.split("/")[0];
} else {
friendlyURL = friendlyURL.split("\\?")[0].split("\\#")[0];
}
return "/"+friendlyURL;
}
return null;
}
/**
* Send notification to vre members about the created product by writing a post.
* @param productName the title of the product
@ -158,7 +185,9 @@ public class WritePostCatalogueManagerThread extends Thread {
String token = null;
try{
HttpResponse response = performRequest(client, path, "{\"app_id\":\"" + APPLICATION_ID_CATALOGUE_MANAGER + "\"}");
JSONObject request = new JSONObject();
request.put("app_id", APPLICATION_ID_CATALOGUE_MANAGER);
HttpResponse response = performRequest(client, path, request.toJSONString());
int statusTokenGenerate = response.getStatusLine().getStatusCode();
@ -218,9 +247,11 @@ public class WritePostCatalogueManagerThread extends Thread {
message += " #" + modifiedHashtag; // ckan accepts tag with empty spaces, we don't
}
logger.info("The post that is going to be written is -> " + message);
HttpResponse response = performRequest(client, path, "{\"text\":\"" + message + "\", \"enable_notification\" : "+ enablePostNotification+ "}");
JSONObject request = new JSONObject();
request.put("text", message);
request.put("enable_notification", enablePostNotification);
logger.info("The post that is going to be written is ->\n" + request.toJSONString());
HttpResponse response = performRequest(client, path, request.toJSONString());
int statusWritePost = response.getStatusLine().getStatusCode();
if(statusWritePost == HttpURLConnection.HTTP_CREATED){
@ -244,14 +275,14 @@ public class WritePostCatalogueManagerThread extends Thread {
writePost(client, realLocation, productName, productUrl, userFullname, hashtags, enablePostNotification);
}else
throw new RuntimeException("Failed to write the post");
throw new RuntimeException("Failed to write the post ");
}catch(Exception e){
logger.error("Failed to retrieve application token", e);
logger.error("Failed to write the post ", e);
}
}
/**
* Convert the json response to a map
* @param response