2012-12-07 17:43:49 +01:00
|
|
|
package org.gcube.applicationsupportlayer.social;
|
2012-12-07 17:47:02 +01:00
|
|
|
|
2013-09-30 15:43:29 +02:00
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
|
|
|
|
2012-12-20 14:14:21 +01:00
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.IOException;
|
2012-12-19 16:39:27 +01:00
|
|
|
import java.io.InputStream;
|
2012-12-08 19:49:21 +01:00
|
|
|
import java.util.Date;
|
2013-09-30 15:43:29 +02:00
|
|
|
import java.util.List;
|
2012-12-08 19:49:21 +01:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2012-12-20 14:14:21 +01:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.commons.net.ftp.FTP;
|
|
|
|
import org.apache.commons.net.ftp.FTPClient;
|
2016-02-25 18:57:25 +01:00
|
|
|
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser;
|
2013-09-30 15:43:29 +02:00
|
|
|
import org.gcube.common.encryption.StringEncrypter;
|
|
|
|
import org.gcube.common.portal.PortalContext;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2012-12-08 19:49:21 +01:00
|
|
|
import org.gcube.portal.databook.shared.Feed;
|
|
|
|
import org.gcube.portal.databook.shared.FeedType;
|
2012-12-20 16:52:10 +01:00
|
|
|
import org.gcube.portal.databook.shared.ImageType;
|
2012-12-08 19:49:21 +01:00
|
|
|
import org.gcube.portal.databook.shared.PrivacyLevel;
|
2013-09-30 15:43:29 +02:00
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2012-12-12 18:17:32 +01:00
|
|
|
|
2012-12-07 17:47:02 +01:00
|
|
|
|
2012-12-07 17:43:49 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author Massimiliano Assante, ISTI-CNR
|
|
|
|
*
|
2012-12-20 14:14:21 +01:00
|
|
|
* use to share updates from within your applicationProfile, the update will be published in the Users News Feed belonging to the VRE your applicationProfile runs into
|
2012-12-07 17:43:49 +01:00
|
|
|
*/
|
2012-12-12 17:16:21 +01:00
|
|
|
public class ApplicationNewsManager extends SocialPortalBridge implements NewsManager {
|
2013-01-03 16:28:36 +01:00
|
|
|
|
2013-09-30 15:43:29 +02:00
|
|
|
private static final Logger _log = LoggerFactory.getLogger(ApplicationNewsManager.class);
|
2012-12-20 14:14:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the FTP Server RuntimeResource coordinates
|
|
|
|
*/
|
|
|
|
private static String RUNTIME_RESOURCE_NAME = "SocialPortalStorage";
|
|
|
|
private static String CATEGORY_NAME = "FTPServer";
|
2016-02-25 18:57:25 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param scope the current scope
|
|
|
|
* @param currUser an instance of {@link SocialNetworkingUser} filled with the required user data
|
|
|
|
* @param portletClassName your portlet class name will be used ad unique identifier for your applicationProfile
|
|
|
|
*/
|
|
|
|
public ApplicationNewsManager(String scope, SocialNetworkingUser currUser, String portletClassName) {
|
|
|
|
super(scope, currUser, portletClassName);
|
|
|
|
}
|
2012-12-07 18:05:51 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
2012-12-12 18:17:32 +01:00
|
|
|
public boolean shareApplicationUpdate(String feedText) {
|
|
|
|
return getStoreInstance().saveAppFeed(buildFeed(feedText, "", "", "", ""));
|
2012-12-07 18:05:51 +01:00
|
|
|
}
|
2012-12-07 17:43:49 +01:00
|
|
|
/**
|
2012-12-07 18:02:20 +01:00
|
|
|
* {@inheritDoc}
|
2012-12-07 17:43:49 +01:00
|
|
|
*/
|
|
|
|
@Override
|
2012-12-13 16:43:42 +01:00
|
|
|
public boolean shareApplicationUpdate(String feedText, String uriParams) {
|
|
|
|
return getStoreInstance().saveAppFeed(buildFeed(feedText, uriParams, "", "", ""));
|
2012-12-07 17:43:49 +01:00
|
|
|
}
|
|
|
|
/**
|
2012-12-07 18:02:20 +01:00
|
|
|
* {@inheritDoc}
|
2012-12-07 17:43:49 +01:00
|
|
|
*/
|
|
|
|
@Override
|
2012-12-13 16:43:42 +01:00
|
|
|
public boolean shareApplicationUpdate(String feedText, String uriParams, String previewTitle, String previewDescription, String previewThumbnailUrl) {
|
|
|
|
return getStoreInstance().saveAppFeed(buildFeed(feedText, uriParams, previewTitle, previewDescription, previewThumbnailUrl));
|
2012-12-07 17:43:49 +01:00
|
|
|
}
|
2012-12-13 23:20:29 +01:00
|
|
|
/**
|
2012-12-20 14:14:21 +01:00
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean shareApplicationUpdate(String feedText, String uriParams, String previewTitle, String previewDescription, InputStream previewThumbnailInputStream, ImageType imageExtension) {
|
|
|
|
String httpImageUrl = uploadImageOnFTPServer(previewThumbnailInputStream, imageExtension);
|
|
|
|
return shareApplicationUpdate(feedText, uriParams, previewTitle, previewDescription, httpImageUrl);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* buid a an ApplicationProfile Feed
|
2012-12-13 23:20:29 +01:00
|
|
|
*
|
|
|
|
* @param description add a description for the update you are sharing
|
2012-12-20 14:14:21 +01:00
|
|
|
* @param uriParams the additional parameteres your applicationProfile needs to open the subject of this update e.g. id=12345&type=foo
|
2012-12-13 23:20:29 +01:00
|
|
|
* @param previewTitle the title to show in the preview
|
|
|
|
* @param previewDescription the description to show in the preview
|
|
|
|
* @param previewThumbnailUrl the image url to show in the preview
|
|
|
|
* @return a feed instance ready to be written
|
|
|
|
*/
|
2012-12-13 16:43:42 +01:00
|
|
|
private Feed buildFeed(String description, String uriParams, String previewTitle, String previewDescription, String previewThumbnailUrl) {
|
2012-12-12 17:16:21 +01:00
|
|
|
String descToAdd = escapeHtml(description);
|
2012-12-20 14:14:21 +01:00
|
|
|
|
|
|
|
String uri = applicationProfile.getUrl();
|
2012-12-13 16:43:42 +01:00
|
|
|
//add the GET params if necessary
|
|
|
|
if (uriParams != null && uriParams.compareTo("") != 0)
|
|
|
|
uri += "?"+uriParams;
|
2016-02-25 18:57:25 +01:00
|
|
|
String scope = currScope;
|
2012-12-12 17:16:21 +01:00
|
|
|
Feed toReturn = new Feed(
|
|
|
|
UUID.randomUUID().toString(),
|
|
|
|
FeedType.PUBLISH,
|
2012-12-20 14:14:21 +01:00
|
|
|
applicationProfile.getKey(),
|
2012-12-12 17:16:21 +01:00
|
|
|
new Date(),
|
2012-12-13 11:29:28 +01:00
|
|
|
scope,
|
2012-12-12 17:16:21 +01:00
|
|
|
uri,
|
|
|
|
previewThumbnailUrl,
|
|
|
|
descToAdd,
|
|
|
|
PrivacyLevel.SINGLE_VRE,
|
2012-12-20 14:14:21 +01:00
|
|
|
applicationProfile.getName(),
|
2012-12-12 17:16:21 +01:00
|
|
|
"no-email",
|
2012-12-20 14:14:21 +01:00
|
|
|
applicationProfile.getImageUrl(),
|
2012-12-12 17:16:21 +01:00
|
|
|
previewTitle,
|
|
|
|
previewDescription,
|
2012-12-13 11:29:28 +01:00
|
|
|
"",
|
|
|
|
true);
|
2012-12-12 17:16:21 +01:00
|
|
|
return toReturn;
|
2012-12-20 14:14:21 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param previewThumbnailInputStream .
|
|
|
|
* @param imageExtension .
|
|
|
|
* @return the http url of the image uploaded on the ftp server
|
|
|
|
*/
|
|
|
|
private String uploadImageOnFTPServer(InputStream previewThumbnailInputStream, ImageType imageExtension) {
|
|
|
|
FTPClient client = new FTPClient( );
|
|
|
|
InputStream inputStream = previewThumbnailInputStream;
|
|
|
|
|
|
|
|
String ftpUrl = "";
|
|
|
|
String user = "";
|
|
|
|
String pwd = "";
|
|
|
|
String httpBaseURL = "";
|
2012-12-20 16:37:56 +01:00
|
|
|
String fileName = UUID.randomUUID() + "." + imageExtension.toString().toLowerCase();
|
2012-12-20 14:14:21 +01:00
|
|
|
try {
|
2013-09-30 15:43:29 +02:00
|
|
|
ServiceEndpoint res = getConfigurationFromIS();
|
|
|
|
AccessPoint ac = res.profile().accessPoints().iterator().next();
|
|
|
|
ftpUrl = ac.address();
|
|
|
|
httpBaseURL = res.profile().runtime().hostedOn();
|
|
|
|
user = ac.username();
|
|
|
|
pwd = StringEncrypter.getEncrypter().decrypt(ac.password());
|
2012-12-20 14:14:21 +01:00
|
|
|
|
|
|
|
// Connect to the FTP server as anonymous
|
|
|
|
client.connect(ftpUrl);
|
|
|
|
client.login(user, pwd);
|
|
|
|
|
|
|
|
client.setFileType(FTP.BINARY_FILE_TYPE);
|
|
|
|
client.enterLocalPassiveMode();
|
|
|
|
|
|
|
|
BufferedInputStream bis = new BufferedInputStream(inputStream);
|
|
|
|
client.storeFile(fileName, bis);
|
|
|
|
bis.close();
|
|
|
|
client.logout();
|
|
|
|
} catch(IOException ioe) {
|
|
|
|
ioe.printStackTrace();
|
|
|
|
_log.error( "Error communicating with FTP server." );
|
|
|
|
} catch (Exception e) {
|
|
|
|
_log.error( "Probably sth wrong in fetching FTP Server RuntimeResource from IS" );
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
IOUtils.closeQuietly( inputStream );
|
|
|
|
try {
|
|
|
|
client.disconnect( );
|
|
|
|
} catch (IOException e) {
|
|
|
|
_log.error( "Problem disconnecting from FTP server" );
|
|
|
|
}
|
|
|
|
}
|
2013-05-10 19:05:27 +02:00
|
|
|
StringBuilder sb = new StringBuilder().append(httpBaseURL).append("/").append(fileName);
|
2012-12-20 14:14:21 +01:00
|
|
|
_log.info( "Uploaded file FTP server: http url: " + sb );
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return the runtime resource of the FTP Server node
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2013-09-30 15:43:29 +02:00
|
|
|
private ServiceEndpoint getConfigurationFromIS() throws Exception {
|
|
|
|
|
|
|
|
//set the scope
|
|
|
|
String currScope = ScopeProvider.instance.get();
|
|
|
|
|
|
|
|
ScopeProvider.instance.set("/"+PortalContext.getConfiguration().getInfrastructureName());
|
|
|
|
|
|
|
|
SimpleQuery query = queryFor(ServiceEndpoint.class);
|
|
|
|
query.addCondition("$resource/Profile/Name/text() eq '"+ RUNTIME_RESOURCE_NAME +"'");
|
|
|
|
query.addCondition("$resource/Profile/Category/text() eq '"+ CATEGORY_NAME +"'");
|
|
|
|
|
|
|
|
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
|
|
|
|
|
|
|
|
List<ServiceEndpoint> conf = client.submit(query);
|
|
|
|
ScopeProvider.instance.set(currScope);
|
|
|
|
return conf.get(0);
|
2012-12-20 14:14:21 +01:00
|
|
|
}
|
2012-12-07 17:43:49 +01:00
|
|
|
}
|