Commit 323bbc63 authored by Matija Obreza's avatar Matija Obreza
Browse files

Resolved #34 - Invalid WIEWS Institute URLs result in search engine

indexing errors
parent 16c4ac3b
......@@ -16,8 +16,12 @@
package org.genesys2.server.model.impl;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.Column;
......@@ -30,6 +34,7 @@ import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.AclAwareModel;
import org.genesys2.server.model.BusinessModel;
import org.genesys2.server.model.EntityId;
......@@ -69,8 +74,8 @@ public class FaoInstitute extends BusinessModel implements GeoReferencedEntity,
@Column(length = 10)
private String vCode;
@Column(name = "codeSGSV")
private String codeSGSV;
@Column(name = "codeSGSV")
private String codeSGSV;
private boolean current;
......@@ -93,8 +98,8 @@ public class FaoInstitute extends BusinessModel implements GeoReferencedEntity,
private Double elevation;
private boolean uniqueAcceNumbs = true;
@Column(name = "allowMaterialRequests", columnDefinition = "boolean default true", nullable = false)
private boolean allowMaterialRequests = true;
@Column(name = "allowMaterialRequests", columnDefinition = "boolean default true", nullable = false)
private boolean allowMaterialRequests = true;
public FaoInstitute() {
}
......@@ -247,19 +252,81 @@ public class FaoInstitute extends BusinessModel implements GeoReferencedEntity,
this.current = current;
}
public boolean isAllowMaterialRequests() {
return allowMaterialRequests;
}
public boolean isAllowMaterialRequests() {
return allowMaterialRequests;
}
public void setAllowMaterialRequests(boolean allowMaterialRequests) {
this.allowMaterialRequests = allowMaterialRequests;
}
public void setAllowMaterialRequests(boolean allowMaterialRequests) {
this.allowMaterialRequests = allowMaterialRequests;
}
public String getCodeSGSV() {
return codeSGSV;
}
public void setCodeSGSV(String codeSGSV) {
this.codeSGSV = codeSGSV;
}
public String getCodeSGSV() {
return codeSGSV;
}
/**
* Transitive
*
* @throws MalformedURLException
*/
public List<URL> getUrls() throws MalformedURLException {
if (StringUtils.isBlank(this.url)) {
return null;
}
ArrayList<URL> urls = new ArrayList<URL>();
String[] s = this.url.split("[,;\\s]+");
for (String u : s) {
if (StringUtils.isBlank(u)) {
continue;
}
u = u.trim();
URL url = null;
try {
url = new URL(u);
} catch (MalformedURLException e) {
if (e.getMessage().startsWith("no protocol")) {
try {
url = new URL("http://" + u);
} catch (MalformedURLException e2) {
throw e;
}
} else if (e.getMessage().startsWith("unknown protocol")) {
try {
url = new URL("http://" + u.substring(u.indexOf("://") + 3));
} catch (MalformedURLException e2) {
throw e;
}
} else {
throw e;
}
}
if (url != null) {
urls.add(url);
}
}
return urls;
}
public void setCodeSGSV(String codeSGSV) {
this.codeSGSV = codeSGSV;
}
/**
* Same as {@link #getUrls()} above, but no exceptions are thrown
*
* @return
*/
public List<URL> getSafeUrls() {
try {
return getUrls();
} catch (MalformedURLException e) {
// Ignore
return null;
}
}
}
......@@ -85,7 +85,9 @@
--%>
<div class="col-sm-12">
<spring:message code="faoInstitute.url" />:
<a href="<c:out value="${faoInstitute.url}" />"><span property="schema:Organization#sameAs"><c:out value="${faoInstitute.url}" /></span></a>
<c:forEach items="${faoInstitute.safeUrls}" var="url">
<a target="_blank" rel="nofollow" href="<c:out value="${url}" />"><span property="schema:Organization#sameAs"><c:out value="${url}" /></span></a>
</c:forEach>
</div>
</div>
......
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.model.impl;
import static org.junit.Assert.assertTrue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
public class FaoInstituteTest {
@Test
public void testUrlsDefault() throws MalformedURLException {
FaoInstitute i = new FaoInstitute();
assertTrue(i.getUrl() == null);
assertTrue(i.getUrls() == null);
}
@Test
public void testUrlsBlank() throws MalformedURLException {
FaoInstitute i = new FaoInstitute();
i.setUrl("");
assertTrue(i.getUrl() != null);
assertTrue(i.getUrls() == null);
}
@Test
public void testUrlsBlank2() throws MalformedURLException {
FaoInstitute i = new FaoInstitute();
i.setUrl(";");
assertTrue(i.getUrl() != null);
assertTrue(i.getUrls() != null);
assertTrue(i.getUrls().size() == 0);
}
@Test
public void testUrlsValid1() throws MalformedURLException {
FaoInstitute i = new FaoInstitute();
i.setUrl("https://www.iita.org");
assertTrue(i.getUrl() != null);
assertTrue(i.getUrls() != null);
assertTrue(i.getUrls().size() == 1);
}
@Test
public void testUrlsValid2() throws MalformedURLException {
FaoInstitute i = new FaoInstitute();
i.setUrl("https://www.iita.org; http://genebank.iita.org, ");
assertTrue(i.getUrl() != null);
List<URL> urls = i.getUrls();
assertTrue(urls != null);
assertTrue(urls.size() == 2);
}
@Test
public void testUrlsNoProtocol() throws MalformedURLException {
FaoInstitute i = new FaoInstitute();
i.setUrl("www.iita.org");
assertTrue(i.getUrl() != null);
List<URL> urls = i.getUrls();
assertTrue(urls != null);
assertTrue(urls.size() == 1);
assertTrue(urls.get(0).equals(new URL("http://www.iita.org")));
}
@Test
public void testUrlsNoProtocol2() throws MalformedURLException {
FaoInstitute i = new FaoInstitute();
i.setUrl("www.iita.org,https://foo.bar;www.somehost.org");
assertTrue(i.getUrl() != null);
List<URL> urls = i.getUrls();
assertTrue(urls != null);
assertTrue(urls.size() == 3);
assertTrue(urls.get(0).equals(new URL("http://www.iita.org")));
assertTrue(urls.get(1).equals(new URL("https://foo.bar")));
assertTrue(urls.get(2).equals(new URL("http://www.somehost.org")));
}
@Test
// http://www.sasa.gov.uk www.scottishlandraces.org.uk
// www.varieties.potato.org.uk www.agricrops.org
public void testUrlsSample1() throws MalformedURLException {
FaoInstitute i = new FaoInstitute();
i.setUrl("http://www.sasa.gov.uk www.scottishlandraces.org.uk www.varieties.potato.org.uk www.agricrops.org");
assertTrue(i.getUrl() != null);
List<URL> urls = i.getUrls();
assertTrue(urls != null);
assertTrue(urls.size() == 4);
assertTrue(urls.get(0).equals(new URL("http://www.sasa.gov.uk")));
assertTrue(urls.get(1).equals(new URL("http://www.scottishlandraces.org.uk")));
assertTrue(urls.get(2).equals(new URL("http://www.varieties.potato.org.uk")));
assertTrue(urls.get(3).equals(new URL("http://www.agricrops.org")));
}
@Test
public void testDump1() throws IOException {
InputStream fis = getClass().getResourceAsStream("/org/genesys2/server/model/impl/wiews-urls.txt");
BufferedReader sr = new BufferedReader(new InputStreamReader(fis));
String l = null;
FaoInstitute i = new FaoInstitute();
while ((l = sr.readLine()) != null) {
int expectedUrls = StringUtils.isBlank(l) ? 0 : l.trim().split("[,;\\s]+").length;
i.setUrl(l);
try {
List<URL> urls = i.getUrls();
if (StringUtils.isBlank(l)) {
assertTrue(urls == null);
} else {
assertTrue(urls != null);
assertTrue(urls.size() == expectedUrls);
}
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
System.err.println(l);
System.err.println(Arrays.asList(l.trim().split("[,;\\s]+")));
}
}
IOUtils.closeQuietly(fis);
}
@Test
public void testFunny1() throws MalformedURLException {
FaoInstitute i=new FaoInstitute();
i.setUrl("www.http://ongrc.org");
List<URL> urls = i.getUrls();
assertTrue(urls.get(0).equals(new URL("http://ongrc.org")));
i.setUrl("htt://www.conagebio.go.cr");
urls = i.getUrls();
assertTrue(urls.get(0).equals(new URL("http://www.conagebio.go.cr")));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment