Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance
Java decentralizedintranet-searchintranet-search-enginep2pp2p-networkprivacyprivacy-protectionsearch-enginesearchenginewebweb-search-enginewebsearchwebseearch-engine
Extracted intranet and filtype related rules from getFaviconURL func
luc53781299d85e
diff --git a/htroot/yacysearchitem.java b/htroot/yacysearchitem.javaindex 277f68852..105162c67 100644--- a/htroot/yacysearchitem.java+++ b/htroot/yacysearchitem.java@@ -188,7 +188,10 @@ public class yacysearchitem {boolean isAtomFeed = header.get(HeaderFramework.CONNECTION_PROP_EXT, "").equals("atom");String resultFileName = resultURL.getFileName();prop.putHTML("content_target", target);- DigestURL faviconURL = getFaviconURL(sb.isIntranetMode(), fileType, result, new Dimension(16, 16));+ DigestURL faviconURL = null;+ if ((fileType == FileType.HTML || fileType == FileType.JSON) && !sb.isIntranetMode()) {+ faviconURL = getFaviconURL(result, new Dimension(16, 16));+ }prop.putHTML("content_faviconUrl", processFaviconURL(authenticated, faviconURL));prop.put("content_urlhash", urlhash);prop.put("content_ranking", Float.toString(result.score()));@@ -342,59 +345,59 @@ public class yacysearchitem {* default favicon URL (i.e. "http://host/favicon.ico") from resultURL and* port.*- * @param isIntranetMode- * when true returns null- * @param fileType- * file type result as specified in request header* @param result* solr document result. Must not be null.* @param preferredSize preferred icon size. If no one matches, most close icon is returned.* @return favicon URL or null when even default favicon URL can not be generated* @throws NullPointerException when one requested parameter is null*/- protected static DigestURL getFaviconURL(final boolean isIntranetMode, final RequestHeader.FileType fileType,- final URIMetadataNode result, Dimension preferredSize) {- DigestURL faviconURL = null;- if ((fileType == FileType.HTML || fileType == FileType.JSON) && !isIntranetMode) {- try {+ protected static DigestURL getFaviconURL(final URIMetadataNode result, Dimension preferredSize) {+ /*+ * We look preferably for a standard icon with preferred size, but+ * accept as a fallback other icons below 128x128 or with no known size+ */+ IconEntry faviconEntry = null;+ boolean foundStandard = false;+ double closestDistance = Double.MAX_VALUE;+ for (IconEntry icon : result.getIcons()) {+ boolean isStandard = icon.isStandardIcon();+ double distance = IconEntry.getDistance(icon.getClosestSize(preferredSize), preferredSize);+ boolean match = false;+ if (foundStandard) {+ /*+ * Already found a standard icon : now must find a standard icon+ * with closer size+ */+ match = isStandard && distance < closestDistance;+ } else {+ /*+ * No standard icon yet found : prefer a standard icon, or check+ * size+ */+ match = isStandard || distance < closestDistance;+ }+ if (match) {+ faviconEntry = icon;+ closestDistance = distance;+ foundStandard = isStandard;+ if (isStandard && distance == 0.0) {+ break;+ }+ }+ }+ DigestURL faviconURL;+ try {+ if (faviconEntry == null) {String defaultFaviconURL = result.url().getProtocol() + "://" + result.url().getHost()+ ((result.url().getPort() != -1) ? (":" + result.url().getPort()) : "") + "/favicon.ico";- IconEntry faviconEntry = null;-- /* We look preferably for a standard icon with preferred size, but accept as a fallback other icons below 128x128 or with no known size*/- boolean foundStandard = false;- double closestDistance = Double.MAX_VALUE;- for(IconEntry icon : result.getIcons()) {- boolean isStandard = icon.isStandardIcon();- double distance = IconEntry.getDistance(icon.getClosestSize(preferredSize), preferredSize);- boolean match = false;- if(foundStandard) {- /* Already found a standard icon : now must find a standard icon with closer size */- match = isStandard && distance < closestDistance;- } else {- /* No standard icon yet found : prefer a standard icon, or check size */- match = isStandard || distance < closestDistance;- }- if(match) {- faviconEntry = icon;- closestDistance = distance;- foundStandard = isStandard;- if(isStandard && distance == 0.0) {- break;- }- }- }-- if (faviconEntry == null) {- faviconURL = new DigestURL(defaultFaviconURL);- } else {- faviconURL = faviconEntry.getUrl();- }-- } catch (final MalformedURLException e1) {- ConcurrentLog.logException(e1);- faviconURL = null;+ faviconURL = new DigestURL(defaultFaviconURL);+ } else {+ faviconURL = faviconEntry.getUrl();}++ } catch (final MalformedURLException e1) {+ ConcurrentLog.logException(e1);+ faviconURL = null;}return faviconURL;}diff --git a/test/java/yacysearchitemTest.java b/test/java/yacysearchitemTest.javaindex 125b52047..ef28d519e 100644--- a/test/java/yacysearchitemTest.java+++ b/test/java/yacysearchitemTest.java@@ -28,7 +28,6 @@ import org.junit.Assert;import org.junit.Test;import net.yacy.cora.document.id.DigestURL;-import net.yacy.cora.protocol.RequestHeader;import net.yacy.kelondro.data.meta.URIMetadataNode;import net.yacy.search.schema.CollectionConfiguration;import net.yacy.search.schema.CollectionSchema;@@ -64,14 +63,12 @@ public class yacysearchitemTest {new String[] { "16x16", "32x32", "64x64", "128x128" });/* Search for a size present in icons collection */- DigestURL faviconURL = yacysearchitem.getFaviconURL(false, RequestHeader.FileType.HTML, metadataNode,- new Dimension(32, 32));+ DigestURL faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(32, 32));Assert.assertNotNull(faviconURL);Assert.assertEquals("http://somehost.org/static/images/icon32.png", faviconURL.toNormalform(false));/* Search for a size not in icons collection */- faviconURL = yacysearchitem.getFaviconURL(false, RequestHeader.FileType.HTML, metadataNode,- new Dimension(40, 40));+ faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(40, 40));Assert.assertNotNull(faviconURL);Assert.assertEquals("http://somehost.org/static/images/icon32.png", faviconURL.toNormalform(false));@@ -79,8 +76,7 @@ public class yacysearchitemTest {* Search for a size equals to non-standard : standard icon is stil* preffered*/- faviconURL = yacysearchitem.getFaviconURL(false, RequestHeader.FileType.HTML, metadataNode,- new Dimension(128, 128));+ faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(128, 128));Assert.assertNotNull(faviconURL);Assert.assertEquals("http://somehost.org/static/images/icon64.png", faviconURL.toNormalform(false));}@@ -107,12 +103,11 @@ public class yacysearchitemTest {new String[] { "32x32", "64x64", "128x128" });/* Non standard icon is returned as fallback */- DigestURL faviconURL = yacysearchitem.getFaviconURL(false, RequestHeader.FileType.HTML, metadataNode,- new Dimension(32, 32));+ DigestURL faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(32, 32));Assert.assertNotNull(faviconURL);Assert.assertEquals("http://somehost.org/static/images/mask32.png", faviconURL.toNormalform(false));}-+/*** One standard icon with multiple sizes*@@ -121,30 +116,26 @@ public class yacysearchitemTest {@Testpublic final void testGetFaviconURLMultiSizes() throws MalformedURLException {URIMetadataNode metadataNode = new URIMetadataNode(new DigestURL("http://somehost.org"));- metadataNode- .setField(CollectionSchema.icons_urlstub_sxt.getSolrFieldName(),- new String[] { "somehost.org/static/images/favicon.ico"});+ metadataNode.setField(CollectionSchema.icons_urlstub_sxt.getSolrFieldName(),+ new String[] { "somehost.org/static/images/favicon.ico" });List<String> protocols = CollectionConfiguration- .protocolList2indexedList(Arrays.asList(new String[] { "http"}));+ .protocolList2indexedList(Arrays.asList(new String[] { "http" }));metadataNode.setField(CollectionSchema.icons_protocol_sxt.getSolrFieldName(), protocols);- metadataNode.setField(CollectionSchema.icons_rel_sxt.getSolrFieldName(),- new String[] { "icon"});+ metadataNode.setField(CollectionSchema.icons_rel_sxt.getSolrFieldName(), new String[] { "icon" });metadataNode.setField(CollectionSchema.icons_sizes_sxt.getSolrFieldName(),- new String[] { "16x16 32x32 64x64",});+ new String[] { "16x16 32x32 64x64", });/* Search for a size in sizes set */- DigestURL faviconURL = yacysearchitem.getFaviconURL(false, RequestHeader.FileType.HTML, metadataNode,- new Dimension(32, 32));+ DigestURL faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(32, 32));Assert.assertNotNull(faviconURL);Assert.assertEquals("http://somehost.org/static/images/favicon.ico", faviconURL.toNormalform(false));/* Search for a size not in sizes set */- faviconURL = yacysearchitem.getFaviconURL(false, RequestHeader.FileType.HTML, metadataNode,- new Dimension(40, 40));+ faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(40, 40));Assert.assertNotNull(faviconURL);Assert.assertEquals("http://somehost.org/static/images/favicon.ico", faviconURL.toNormalform(false));}-+/*** One standard icon with no size*@@ -153,22 +144,18 @@ public class yacysearchitemTest {@Testpublic final void testGetFaviconURLNoSize() throws MalformedURLException {URIMetadataNode metadataNode = new URIMetadataNode(new DigestURL("http://somehost.org"));- metadataNode- .setField(CollectionSchema.icons_urlstub_sxt.getSolrFieldName(),- new String[] { "somehost.org/static/images/favicon.ico"});+ metadataNode.setField(CollectionSchema.icons_urlstub_sxt.getSolrFieldName(),+ new String[] { "somehost.org/static/images/favicon.ico" });List<String> protocols = CollectionConfiguration- .protocolList2indexedList(Arrays.asList(new String[] { "http"}));+ .protocolList2indexedList(Arrays.asList(new String[] { "http" }));metadataNode.setField(CollectionSchema.icons_protocol_sxt.getSolrFieldName(), protocols);- metadataNode.setField(CollectionSchema.icons_rel_sxt.getSolrFieldName(),- new String[] { "icon"});+ metadataNode.setField(CollectionSchema.icons_rel_sxt.getSolrFieldName(), new String[] { "icon" });- DigestURL faviconURL = yacysearchitem.getFaviconURL(false, RequestHeader.FileType.HTML, metadataNode,- new Dimension(32, 32));+ DigestURL faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(32, 32));Assert.assertNotNull(faviconURL);Assert.assertEquals("http://somehost.org/static/images/favicon.ico", faviconURL.toNormalform(false));}--+/*** No icon in document*@@ -179,8 +166,7 @@ public class yacysearchitemTest {URIMetadataNode metadataNode = new URIMetadataNode(new DigestURL("http://someHost.org"));/* Default fallback favicon URL should be generated */- DigestURL faviconURL = yacysearchitem.getFaviconURL(false, RequestHeader.FileType.HTML, metadataNode,- new Dimension(32, 32));+ DigestURL faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(32, 32));Assert.assertEquals("http://somehost.org/favicon.ico", faviconURL.toNormalform(false));}