270 lines
11 KiB
Java
270 lines
11 KiB
Java
package org.gcube.applicationsupportlayer.social;
|
|
|
|
import java.io.BufferedInputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.apache.commons.net.ftp.FTP;
|
|
import org.apache.commons.net.ftp.FTPClient;
|
|
import org.gcube.application.framework.core.session.ASLSession;
|
|
import org.gcube.applicationsupportlayer.social.ex.ApplicationProfileNotFoundException;
|
|
import org.gcube.common.core.contexts.GHNContext;
|
|
import org.gcube.common.core.informationsystem.client.AtomicCondition;
|
|
import org.gcube.common.core.informationsystem.client.ISClient;
|
|
import org.gcube.common.core.informationsystem.client.XMLResult;
|
|
import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericQuery;
|
|
import org.gcube.common.core.informationsystem.client.queries.GCUBERuntimeResourceQuery;
|
|
import org.gcube.common.core.resources.GCUBERuntimeResource;
|
|
import org.gcube.common.core.scope.GCUBEScope;
|
|
import org.gcube.common.core.utils.logging.GCUBEClientLog;
|
|
import org.gcube.portal.databook.shared.ApplicationProfile;
|
|
import org.gcube.portal.databook.shared.Feed;
|
|
import org.gcube.portal.databook.shared.FeedType;
|
|
import org.gcube.portal.databook.shared.ImageType;
|
|
import org.gcube.portal.databook.shared.PrivacyLevel;
|
|
|
|
|
|
/**
|
|
*
|
|
* @author Massimiliano Assante, ISTI-CNR
|
|
* @version 0.1 Dec 2012
|
|
*
|
|
* 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
|
|
*/
|
|
public class ApplicationNewsManager extends SocialPortalBridge implements NewsManager {
|
|
static GCUBEClientLog _log = new GCUBEClientLog(ApplicationNewsManager.class);
|
|
private ApplicationProfile applicationProfile;
|
|
|
|
/**
|
|
* the FTP Server RuntimeResource coordinates
|
|
*/
|
|
private static String RUNTIME_RESOURCE_NAME = "SocialPortalStorage";
|
|
private static String CATEGORY_NAME = "FTPServer";
|
|
|
|
/**
|
|
*
|
|
* @param aslSession the ASLSession instance
|
|
* @param portletClassName your servlet class name will be used ad unique identifier for your applicationProfile
|
|
*/
|
|
public ApplicationNewsManager(ASLSession session, String portletClassName) {
|
|
super(session);
|
|
this.applicationProfile = getProfileFromInfrastrucure(portletClassName);
|
|
}
|
|
|
|
public ApplicationProfile getApplicationProfile() {
|
|
return applicationProfile;
|
|
}
|
|
/**
|
|
* this method looks up the applicationProfile profile among the ones available in the infrastructure
|
|
* @param portletClassName your servlet class name will be used ad unique identifier for your applicationProfile
|
|
* @return the applicationProfile profile
|
|
*/
|
|
private ApplicationProfile getProfileFromInfrastrucure(String portletClassName) {
|
|
try {
|
|
ApplicationProfile toReturn = new ApplicationProfile();
|
|
ISClient client = GHNContext.getImplementation(ISClient.class);
|
|
GCUBEGenericQuery query = client.getQuery(GCUBEGenericQuery.class);
|
|
|
|
|
|
query.setExpression("for $profile in collection('/db/Profiles/GenericResource')//Resource " +
|
|
"where $profile/Profile/SecondaryType/string() eq 'ApplicationProfile' and $profile/Profile/Body/AppId/string() " +
|
|
" eq '" + portletClassName + "'" +
|
|
"return $profile");
|
|
|
|
GCUBEScope scope = aslSession.getScope();
|
|
|
|
List<XMLResult> appProfile = client.execute(query, scope.getInfrastructure());
|
|
if (appProfile == null || appProfile.size() == 0)
|
|
throw new ApplicationProfileNotFoundException("Your applicationProfile is not registered in the infrastructure");
|
|
else {
|
|
XMLResult node = appProfile.get(0);
|
|
List<String> currValue = null;
|
|
currValue = node.evaluate("/Resource/Profile/Name/text()");
|
|
if (currValue != null && currValue.size() > 0) {
|
|
toReturn.setName(currValue.get(0));
|
|
}
|
|
else throw new ApplicationProfileNotFoundException("Your applicationProfile NAME was not found in the profile");
|
|
|
|
currValue = node.evaluate("/Resource/Profile/Description/text()");
|
|
if (currValue != null && currValue.size() > 0) {
|
|
toReturn.setDescription(currValue.get(0));
|
|
}
|
|
else _log.warn("No Description exists for " + toReturn.getName());
|
|
|
|
currValue = node.evaluate("/Resource/Profile/Body/AppId/text()");
|
|
if (currValue != null && currValue.size() > 0) {
|
|
toReturn.setKey(currValue.get(0));
|
|
}
|
|
else throw new ApplicationProfileNotFoundException("Your applicationProfile ID n was not found in the profile, consider adding <AppId> element in <Body>");
|
|
|
|
currValue = node.evaluate("/Resource/Profile/Body/ThumbnailURL/text()");
|
|
if (currValue != null && currValue.size() > 0) {
|
|
toReturn.setImageUrl(currValue.get(0));
|
|
}
|
|
else throw new ApplicationProfileNotFoundException("Your applicationProfile Image Url was not found in the profile, consider adding <ThumbnailURL> element in <Body>");
|
|
currValue = node.evaluate("/Resource/Profile/Body/EndPoint/Scope/text()");
|
|
if (currValue != null && currValue.size() > 0) {
|
|
List<String> scopes = currValue;
|
|
boolean foundUrl = false;
|
|
for (int i = 0; i < scopes.size(); i++) {
|
|
if (currValue.get(i).trim().compareTo(scope.toString()) == 0) {
|
|
toReturn.setUrl(node.evaluate("/Resource/Profile/Body/EndPoint/URL/text()").get(i));
|
|
toReturn.setScope(scope.toString());
|
|
foundUrl = true;
|
|
break;
|
|
}
|
|
}
|
|
if (! foundUrl)
|
|
throw new ApplicationProfileNotFoundException("Your applicationProfile URL was not found in the profile for Scope: " + scope.toString());
|
|
}
|
|
else throw new ApplicationProfileNotFoundException("Your applicationProfile EndPoint was not found in the profile, consider adding <EndPoint><Scope> element in <Body>");
|
|
return toReturn;
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
_log.error("Error while trying to fetch applicationProfile profile from the infrastructure");
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
|
|
}
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
@Override
|
|
public boolean shareApplicationUpdate(String feedText) {
|
|
return getStoreInstance().saveAppFeed(buildFeed(feedText, "", "", "", ""));
|
|
}
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
@Override
|
|
public boolean shareApplicationUpdate(String feedText, String uriParams) {
|
|
return getStoreInstance().saveAppFeed(buildFeed(feedText, uriParams, "", "", ""));
|
|
}
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
@Override
|
|
public boolean shareApplicationUpdate(String feedText, String uriParams, String previewTitle, String previewDescription, String previewThumbnailUrl) {
|
|
return getStoreInstance().saveAppFeed(buildFeed(feedText, uriParams, previewTitle, previewDescription, previewThumbnailUrl));
|
|
}
|
|
/**
|
|
* {@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
|
|
*
|
|
* @param description add a description for the update you are sharing
|
|
* @param uriParams the additional parameteres your applicationProfile needs to open the subject of this update e.g. id=12345&type=foo
|
|
* @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
|
|
*/
|
|
private Feed buildFeed(String description, String uriParams, String previewTitle, String previewDescription, String previewThumbnailUrl) {
|
|
String descToAdd = escapeHtml(description);
|
|
|
|
String uri = applicationProfile.getUrl();
|
|
//add the GET params if necessary
|
|
if (uriParams != null && uriParams.compareTo("") != 0)
|
|
uri += "?"+uriParams;
|
|
//String scope = getScopeByOrganizationId(""+aslSession.getGroupId());
|
|
String scope = aslSession.getScopeName();
|
|
System.out.println("scope: " + aslSession.getScopeName());
|
|
Feed toReturn = new Feed(
|
|
UUID.randomUUID().toString(),
|
|
FeedType.PUBLISH,
|
|
applicationProfile.getKey(),
|
|
new Date(),
|
|
scope,
|
|
uri,
|
|
previewThumbnailUrl,
|
|
descToAdd,
|
|
PrivacyLevel.SINGLE_VRE,
|
|
applicationProfile.getName(),
|
|
"no-email",
|
|
applicationProfile.getImageUrl(),
|
|
previewTitle,
|
|
previewDescription,
|
|
"",
|
|
true);
|
|
return toReturn;
|
|
}
|
|
/**
|
|
*
|
|
* @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 = "";
|
|
String fileName = UUID.randomUUID() + "." + imageExtension.toString().toLowerCase();
|
|
try {
|
|
GCUBERuntimeResource res = getConfigurationFromIS();
|
|
ftpUrl = res.getAccessPoints().get(0).getEndpoint();
|
|
httpBaseURL = res.getHostedOn();
|
|
user = res.getAccessPoints().get(0).getUsername();
|
|
pwd = res.getAccessPoints().get(0).getPassword();
|
|
|
|
// 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" );
|
|
}
|
|
}
|
|
StringBuilder sb = new StringBuilder().append(httpBaseURL).append(fileName);
|
|
_log.info( "Uploaded file FTP server: http url: " + sb );
|
|
return sb.toString();
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return the runtime resource of the FTP Server node
|
|
* @throws Exception
|
|
*/
|
|
private GCUBERuntimeResource getConfigurationFromIS() throws Exception {
|
|
ISClient client = GHNContext.getImplementation(ISClient.class);
|
|
GHNContext ctx = GHNContext.getContext();
|
|
String scope = "/" + (String) ctx.getProperty(GHNContext.INFRASTRUCTURE_NAME, true);
|
|
GCUBERuntimeResourceQuery query = client.getQuery(GCUBERuntimeResourceQuery.class);
|
|
query.addAtomicConditions(new AtomicCondition("/Profile/Name", RUNTIME_RESOURCE_NAME));
|
|
query.addAtomicConditions(new AtomicCondition("/Profile/Category", CATEGORY_NAME));
|
|
return client.execute(query, GCUBEScope.getScope(scope)).get(0);
|
|
}
|
|
}
|