From 7332351c320388e92a44e047a5ec6809325fdd2c Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Thu, 6 Oct 2016 12:49:55 +0000 Subject: [PATCH] updated opengraph checkURL for getting images in case of HTTP redirect permanently is encountered git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portlets/user/share-updates@132816 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 16 ++++++++--- .project | 1 + .settings/org.eclipse.wst.common.component | 5 +++- .tern-project | 18 ++++++++++++- distro/changelog.xml | 5 ++++ pom.xml | 2 +- .../server/ShareUpdateServiceImpl.java | 8 +++--- .../server/opengraph/OpenGraph.java | 27 ++++++++++--------- 8 files changed, 59 insertions(+), 23 deletions(-) diff --git a/.classpath b/.classpath index 6e74f86..a463a53 100644 --- a/.classpath +++ b/.classpath @@ -1,19 +1,27 @@ - + - + + + + + - + + + + + @@ -31,5 +39,5 @@ - + diff --git a/.project b/.project index a5483d8..eaa6395 100644 --- a/.project +++ b/.project @@ -55,5 +55,6 @@ org.eclipse.wst.common.project.facet.core.nature org.eclipse.wst.jsdt.core.jsNature com.google.gwt.eclipse.core.gwtNature + com.liferay.ide.core.liferayNature diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 7dc47c7..167b886 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -5,7 +5,10 @@ - + + uses + + uses diff --git a/.tern-project b/.tern-project index a185ba0..61348cc 100644 --- a/.tern-project +++ b/.tern-project @@ -1 +1,17 @@ -{"ide":{"scriptPaths":[]},"plugins":{"aui":{},"liferay":{},"yui":{}},"libs":["ecma5","browser"]} \ No newline at end of file +{ + "plugins": { + "aui": { + + }, + "liferay": { + + }, + "yui": { + + } + }, + "libs": [ + "ecma5", + "browser" + ] +} \ No newline at end of file diff --git a/distro/changelog.xml b/distro/changelog.xml index fcc456b..75d89ff 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -1,4 +1,9 @@ + + updated opengraph checkURL for getting images in case of HTTP redirect permanently is encountered + + Updated to Liferay 6.2.5 diff --git a/pom.xml b/pom.xml index f106e89..0afa64f 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ org.gcube.portlets.user share-updates war - 2.0.0-SNAPSHOT + 2.1.0-SNAPSHOT gCube Share Updates Portlet gCube Share Updates for exchanging updates with other users of VREs. 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 1fb1b9e..10605cd 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 @@ -107,7 +107,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar /** - * the current ASLSession + * the current ASsLSession * @return the session */ private ASLSession getASLSession() { @@ -122,7 +122,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar } public String getDevelopmentUser() { String user = TEST_USER; - // user = "costantino.perciante"; + user = "costantino.perciante"; return user; } /** @@ -770,10 +770,10 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar ArrayList imageUrls = new ArrayList(); //get the host from the url String host = pageURL.getHost().replaceAll("www.", ""); - //try openGraph First OpenGraph ogLink = null; try { + System.out.println("linkToCheck=" + linkToCheck); ogLink = new OpenGraph(linkToCheck, true, siteConnection); if (ogLink == null || ogLink.getContent("title") == null) { //there is no OpenGraph for this link @@ -787,7 +787,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar description = ((description.length() > 256) ? description.substring(0, 256)+"..." : description); //look for the image ask the guesser if not present if (ogLink.getContent("image") != null) { - String imageUrl = TextTransfromUtils.getImageUrlFromSrcAttribute(pageURL, ogLink.getContent("image")); + String imageUrl = TextTransfromUtils.getImageUrlFromSrcAttribute(ogLink.getRealURL(), ogLink.getContent("image")); imageUrls.add(imageUrl); _log.trace("OpenGraph getImage = " +imageUrl) ; } diff --git a/src/main/java/org/gcube/portlets/user/shareupdates/server/opengraph/OpenGraph.java b/src/main/java/org/gcube/portlets/user/shareupdates/server/opengraph/OpenGraph.java index 46a6ffa..6625683 100644 --- a/src/main/java/org/gcube/portlets/user/shareupdates/server/opengraph/OpenGraph.java +++ b/src/main/java/org/gcube/portlets/user/shareupdates/server/opengraph/OpenGraph.java @@ -1,10 +1,6 @@ package org.gcube.portlets.user.shareupdates.server.opengraph; -import org.htmlcleaner.HtmlCleaner; -import org.htmlcleaner.TagNode; - import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; @@ -14,15 +10,20 @@ import java.util.Hashtable; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.htmlcleaner.HtmlCleaner; +import org.htmlcleaner.TagNode; + /** * A Java object representation of an Open Graph enabled webpage. * A simplified layer over a Hastable. * * @author Callum Jones + * @author Massimiliano Assante */ -public class OpenGraph -{ +public class OpenGraph { private String pageUrl; + + private URL realURL; private ArrayList pageNamespaces; private Hashtable> metaAttributes; private String baseType; @@ -47,8 +48,7 @@ public class OpenGraph /** * Create an open graph representation for generating your own Open Graph object */ - public OpenGraph() - { + public OpenGraph() { pageNamespaces = new ArrayList(); metaAttributes = new Hashtable>(); hasChanged = false; @@ -70,14 +70,13 @@ public class OpenGraph // download the (X)HTML content, but only up to the closing head tag. We do not want to waste resources parsing irrelevant content Charset charset = getConnectionCharset(siteConnection); BufferedReader dis = new BufferedReader(new InputStreamReader(siteConnection.getInputStream(), charset)); + this.realURL = siteConnection.getURL(); String inputLine; StringBuffer headContents = new StringBuffer(); // Loop through each line, looking for the closing head element - while ((inputLine = dis.readLine()) != null) - { - if (inputLine.contains("")) - { + while ((inputLine = dis.readLine()) != null) { + if (inputLine.contains("")) { inputLine = inputLine.substring(0, inputLine.indexOf("") + 7); inputLine = inputLine.concat(""); headContents.append(inputLine + "\r\n"); @@ -156,6 +155,10 @@ public class OpenGraph pageUrl = realURL.toExternalForm(); } + public URL getRealURL() { + return realURL; + } + /** * Gets the charset for specified connection. * Content Type header is parsed to get the charset name.