diff --git a/.classpath b/.classpath
index 3762556..13ea37a 100644
--- a/.classpath
+++ b/.classpath
@@ -1,6 +1,6 @@
-
+
@@ -31,5 +31,5 @@
-
+
diff --git a/.settings/com.google.gdt.eclipse.core.prefs b/.settings/com.google.gdt.eclipse.core.prefs
index f4fc4f8..f7ef8a9 100644
--- a/.settings/com.google.gdt.eclipse.core.prefs
+++ b/.settings/com.google.gdt.eclipse.core.prefs
@@ -1,5 +1,5 @@
eclipse.preferences.version=1
jarsExcludedFromWebInfLib=
-lastWarOutDir=/Users/massi/Documents/workspace/share-updates/target/share-updates-1.2.0-SNAPSHOT
+lastWarOutDir=/Users/massi/Documents/workspace/share-updates/target/share-updates-1.2.1-SNAPSHOT
warSrcDir=src/main/webapp
warSrcDirIsOutput=false
diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component
index dde9c8f..fb4e233 100644
--- a/.settings/org.eclipse.wst.common.component
+++ b/.settings/org.eclipse.wst.common.component
@@ -4,9 +4,6 @@
-
- uses
-
diff --git a/pom.xml b/pom.xml
index 7e5a4d2..8212263 100644
--- a/pom.xml
+++ b/pom.xml
@@ -13,7 +13,7 @@
org.gcube.portlets.user
share-updates
war
- 1.2.0-SNAPSHOT
+ 1.2.1-SNAPSHOT
gCube Share Updates Portlet
@@ -106,13 +106,13 @@
org.gcube.contentmanagement
storage-manager-core
[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)
- compile
+ provided
org.gcube.contentmanagement
storage-manager-wrapper
[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)
- compile
+ provided
org.gcube.applicationsupportlayer
diff --git a/src/main/java/org/gcube/portlets/user/shareupdates/client/ShareUpdates.java b/src/main/java/org/gcube/portlets/user/shareupdates/client/ShareUpdates.java
index 6bdd872..08a9860 100644
--- a/src/main/java/org/gcube/portlets/user/shareupdates/client/ShareUpdates.java
+++ b/src/main/java/org/gcube/portlets/user/shareupdates/client/ShareUpdates.java
@@ -10,9 +10,6 @@ import com.google.gwt.user.client.ui.RootPanel;
*/
public class ShareUpdates implements EntryPoint {
- /**
- * This is the entry point method.
- */
public void onModuleLoad() {
RootPanel.get("shareUpdateDiv").add(new ShareUpdateForm());
}
diff --git a/src/main/java/org/gcube/portlets/user/shareupdates/server/ShareUpdateServiceImpl.java b/src/main/java/org/gcube/portlets/user/shareupdates/server/ShareUpdateServiceImpl.java
index 432d536..217fa2b 100644
--- a/src/main/java/org/gcube/portlets/user/shareupdates/server/ShareUpdateServiceImpl.java
+++ b/src/main/java/org/gcube/portlets/user/shareupdates/server/ShareUpdateServiceImpl.java
@@ -1,12 +1,17 @@
package org.gcube.portlets.user.shareupdates.server;
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
+import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
@@ -57,6 +62,7 @@ import org.gcube.portlets.user.shareupdates.client.ShareUpdateService;
import org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm;
import org.gcube.portlets.user.shareupdates.server.metaseeker.MetaSeeker;
import org.gcube.portlets.user.shareupdates.server.opengraph.OpenGraph;
+import org.gcube.portlets.user.shareupdates.server.opengraph.OpenGraphNamespace;
import org.gcube.portlets.user.shareupdates.shared.LinkPreview;
import org.gcube.portlets.user.shareupdates.shared.UserSettings;
import org.gcube.portlets.widgets.pickuser.shared.PickingUser;
@@ -66,6 +72,8 @@ import org.gcube.vomanagement.usermanagement.impl.liferay.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.impl.liferay.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.GroupModel;
import org.gcube.vomanagement.usermanagement.model.UserModel;
+import org.htmlcleaner.HtmlCleaner;
+import org.htmlcleaner.TagNode;
import org.htmlparser.beans.StringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -134,9 +142,9 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
if (user == null) {
_log.warn("USER IS NULL setting test.user and Running OUTSIDE PORTAL");
user = "test.user";
-// user = "massimiliano.assante";
-// SessionManager.getInstance().getASLSession(sessionID, user).setScope("/gcube/devsec/devVRE");
- withinPortal = false;
+ user = "massimiliano.assante";
+ SessionManager.getInstance().getASLSession(sessionID, user).setScope("/gcube/devsec/devVRE");
+ withinPortal = false;
}
else {
withinPortal = true;
@@ -176,12 +184,12 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
e.printStackTrace();
}
}
-
+
String linkTitle = preview.getTitle();
String linkDesc = preview.getDescription();
String host = preview.getHost();
String url = preview.getUrl();
-
+
Date feedDate = new Date();
//this means the user has shared a file without text in it.
String textToPost = "";
@@ -190,10 +198,10 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
} else {
textToPost = transformUrls(escapedFeedText);
}
-
+
ScopeBean scope = new ScopeBean(session.getScope());
String vreId2Set = scope.is(Type.VRE) ? scope.toString() : "";
-
+
Feed toShare = new Feed(UUID.randomUUID().toString(), feedType, username, feedDate,
vreId2Set, url, urlThumbnail, textToPost, pLevel, fullName, email, thumbnailURL, linkTitle, linkDesc, host);
@@ -233,14 +241,14 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
ClientFeed cf = new ClientFeed(toShare.getKey(), toShare.getType().toString(), username, feedDate, toShare.getUri(),
replaceAmpersand(toShare.getDescription()), fullName, email, thumbnailURL, toShare.getLinkTitle(), toShare.getLinkDescription(),
toShare.getUriThumbnail(), toShare.getLinkHost());
-
-
+
+
//send the notification about this posts to everyone in the group if notifyGroup is true
if (pLevel == PrivacyLevel.SINGLE_VRE && vreId != null && vreId.compareTo("") != 0 && notifyGroup) {
NotificationsManager nm = new ApplicationNotificationsManager(session, NEWS_FEED_PORTLET_CLASSNAME);
Thread thread = new Thread(new PostNotificationsThread(toShare.getKey(), escapedFeedText, ""+session.getGroupId(), nm));
thread.start();
-
+
}
//send the notification to the mentioned users
if (mentionedUsers != null && mentionedUsers.size() > 0) {
@@ -248,7 +256,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
Thread thread = new Thread(new MentionNotificationsThread(toShare.getKey(), escapedFeedText, nm, mentionedUsers));
thread.start();
}
-
+
//it means I also should upload a copy on the user's Workspace root folder
if (fileName != null && filePathOnServer != null) {
//The workspace uploader Thread starts here asyncronously
@@ -278,10 +286,6 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
return escapedFeedText;
}
- private UserSettings getUserSettingsFromSession() {
- return (UserSettings) getASLSession().getAttribute(UserInfo.USER_INFO_ATTR);
- }
-
private void setUserSettingsInSession(UserSettings user) {
getASLSession().setAttribute(UserInfo.USER_INFO_ATTR, user);
}
@@ -330,7 +334,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
sb.append("shared ").append("a file.").append(" ").toString();
return sb.toString();
}
-
+
@Override
public UserSettings getUserSettings() {
try {
@@ -357,13 +361,14 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
return toReturn;
}
else {
- _log.info("Returning test USER");
+
+ _log.info("Returning test USER = " + session.getUsername());
HashMap fakeVreNames = new HashMap();
- fakeVreNames.put("/gcube/devsec/devVRE","devVRE");
+ //fakeVreNames.put("/gcube/devsec/devVRE","devVRE");
//fakeVreNames.put("/gcube/devNext/NexNext","NexNext");
- UserInfo user = new UserInfo(getASLSession().getUsername(), fullName, thumbnailURL, email, "fakeAccountUrl", true, false, fakeVreNames);
- return new UserSettings(user, 0, session.getScopeName(), isInfrastructureScope());
+ UserInfo user = new UserInfo(session.getUsername(), fullName, thumbnailURL, email, "fakeAccountUrl", true, false, fakeVreNames);
+ return new UserSettings(user, 0, session.getScopeName(), false);
}
} catch (Exception e) {
@@ -385,11 +390,11 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
ScopeProvider.instance.set("/"+PortalContext.getConfiguration().getInfrastructureName());
IClient storageClient = new StorageClient(STORAGE_OWNER, AccessType.SHARED, MemoryType.PERSISTENT).getClient();
ScopeProvider.instance.set(currScope);
-
+
String httpURL = "";
//get the url to show, before actually uploading it
String smpURI = storageClient.getUrl().RFile(remoteFilePath);
-
+
//The storage uploader Thread starts here asyncronously
Thread thread = new Thread(new UploadToStorageThread(storageClient, fileName, fileabsolutePathOnServer, remoteFilePath));
thread.start();
@@ -426,7 +431,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
return FilePreviewer.getUnhandledTypePreview(fileName, fileabsolutePathOnServer, httpURL, mimeType);
}
-
+
} catch (Exception e) {
_log.error("Error while resolving or previewing file");
e.printStackTrace();
@@ -449,14 +454,14 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
*/
protected static String getMimeType(File file, String filenameWithExtension) throws IOException {
TikaConfig config = TikaConfig.getDefaultConfig();
- Detector detector = config.getDetector();
- TikaInputStream stream = TikaInputStream.get(file);
- Metadata metadata = new Metadata();
- metadata.add(Metadata.RESOURCE_NAME_KEY, filenameWithExtension);
- MediaType mediaType = detector.detect(stream, metadata);
- return mediaType.getBaseType().toString();
+ Detector detector = config.getDetector();
+ TikaInputStream stream = TikaInputStream.get(file);
+ Metadata metadata = new Metadata();
+ metadata.add(Metadata.RESOURCE_NAME_KEY, filenameWithExtension);
+ MediaType mediaType = detector.detect(stream, metadata);
+ return mediaType.getBaseType().toString();
}
-
+
/**
* return the id as key and the names as value of the vre a user is subscribed to
* @param username
@@ -564,11 +569,11 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
}
String toReturn = html.replaceAll("&", "&").replaceAll("<", "<")
.replaceAll(">", ">");
-
+
// then replace all the line breaks by
, and all the double spaces by the html version
toReturn = toReturn.replaceAll("(\r\n|\n)","
");
toReturn = toReturn.replaceAll("\\s\\s"," ");
-
+
return toReturn;
}
@@ -638,6 +643,9 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
_log.error("url is not reachable");
return null;
}
+ //pretend you're a browser (make my request from Java more “browsery-like”.)
+ siteConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
+
String title;
String description;
ArrayList imageUrls = new ArrayList();
@@ -651,7 +659,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
if (ogLink == null || ogLink.getContent("title") == null) {
//there is no OpenGraph for this link
_log.info("No OpenGraph Found, going Best guess from page content") ;
- toReturn = getInfoFromHTML(pageURL, linkToCheck, host);
+ toReturn = getInfoFromHTML(siteConnection, pageURL, linkToCheck, host);
} else {
//there is OpenGraph
title = ogLink.getContent("title");
@@ -661,7 +669,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
if (ogLink.getContent("image") != null)
imageUrls.add(ogLink.getContent("image"));
else {
- ArrayList images = getImagesFromHTML(pageURL);
+ ArrayList images = getImagesFromHTML(siteConnection, pageURL);
if (! images.isEmpty())
imageUrls = images;
}
@@ -681,11 +689,10 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
* @return a list of image url
* @throws IOException
*/
- private ArrayList getImagesFromHTML(URL pageURL) throws IOException {
+ private ArrayList getImagesFromHTML(URLConnection connection, URL pageURL) throws IOException {
ArrayList toReturn = new ArrayList();
- InputStream input = pageURL.openStream();
try {
- Document document = new Tidy().parseDOM(input, null);
+ Document document = new Tidy().parseDOM(pageURL.openStream(), null);
NodeList imgs = document.getElementsByTagName("img");
int upTo = (imgs.getLength() > 15) ? 15 : imgs.getLength();
for (int i = 0; i < upTo; i++) {
@@ -707,49 +714,126 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar
* @return a LinPreview object instance filled with the extracted information
* @throws IOException
*/
- private LinkPreview getInfoFromHTML(URL pageUrl, String link, String host) throws Exception {
+ private LinkPreview getInfoFromHTML(URLConnection connection, URL pageUrl, String link, String host) throws Exception {
LinkPreview toReturn = null;
String title = "";
String description = "";
- InputStream input = pageUrl.openStream();
- Document document = new Tidy().parseDOM(input, null);
- NodeList titles = document.getElementsByTagName("title");
- if (titles != null && titles.getLength()>0) {
- if (titles.item(0).getFirstChild() == null || titles.item(0).getFirstChild().getNodeValue() == null) {
- _log.error("[MANUAL-PARSE] Something wrong with the title element, returning ... ");
- return toReturn;
- }
- title = titles.item(0).getFirstChild().getNodeValue();
- MetaSeeker ms = null;
- try {
- ms = new MetaSeeker(link);
- } catch(Exception e) {
- _log.error("[MANUAL-PARSE] Something wrong with the meta seeker returning ... ");
- return toReturn;
- }
+ URLConnection conn = pageUrl.openConnection();
+ //pretend you're a browser (make my request from Java more “browsery-like”.)
+ conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
+
+ MetaSeeker ms = null;
+ try {
+ title = getTitleFromHeader(pageUrl);
+ ms = new MetaSeeker(connection, pageUrl);
+
//try the metadata, otherwise ask the guesser
description = (ms.getContent("description") != null && ! ms.getContent("description").isEmpty()) ? ms.getContent("description") : createDescriptionFromContent(link);
ArrayList images = new ArrayList();
- NodeList imgs = document.getElementsByTagName("img");
- int upTo = (imgs.getLength() > 15) ? 15 : imgs.getLength();
- for (int i = 0; i < upTo; i++) {
- String imageUrl = imgs.item(i).getAttributes().getNamedItem("src").getNodeValue();
- if (imageUrl.startsWith("/"))
- imageUrl = pageUrl.getProtocol()+"://"+pageUrl.getHost()+imageUrl;
- else if (!imageUrl.contains("/")) { //then the image is probably in the same folder
- // e.g. http://www.fao.org/docrep/018/i3328e/i3328e00.htm?utm_source
- String imageFolder = pageUrl.toString().substring(0, pageUrl.toString().lastIndexOf("/"));
- imageUrl= imageFolder + "/" + imageUrl;
- }
- images.add(imageUrl);
- _log.trace("[FOUND image] " + imageUrl);
- }
+ images = getImagesWithCleaner(pageUrl);
toReturn = new LinkPreview(title, description, link, host, images);
+
+ } catch(Exception e) {
+ _log.error("[MANUAL-PARSE] Something wrong with the meta seeker returning ... ");
+ return toReturn;
}
return toReturn;
}
+ /**
+ * @param pageURL
+ * @return the title of the page or null if can't read it
+ * @throws IOException
+ */
+ private String getTitleFromHeader(URL pageURL) throws IOException {
+ URLConnection conn = pageURL.openConnection();
+ //pretend you're a browser (make my request from Java more “browsery-like”.)
+ conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
+
+ Charset charset = OpenGraph.getConnectionCharset(conn);
+ BufferedReader dis = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
+ String inputLine;
+ StringBuffer headContents = new StringBuffer();
+
+ // Loop through each line, looking for the closing head element
+ while ((inputLine = dis.readLine()) != null)
+ {
+ if (inputLine.contains("")) {
+ inputLine = inputLine.substring(0, inputLine.indexOf("") + 7);
+ inputLine = inputLine.concat("