@ -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