Author: rharo
Date: Mon Jun 24 10:40:26 2013
New Revision: 1495987
URL: http://svn.apache.org/r1495987
Log:
Applying Patch
https://issues.apache.org/jira/secure/attachment/12589385/STANBOL-1118_20130624_rw.patch
to STANBOL-1118
Modified:
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/main/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProvider.java
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/test/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProviderTest.java
Modified:
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/main/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProvider.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/main/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProvider.java?rev=1495987&r1=1495986&r2=1495987&view=diff
==============================================================================
---
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/main/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProvider.java
(original)
+++
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/main/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProvider.java
Mon Jun 24 10:40:26 2013
@@ -17,10 +17,10 @@
package org.apache.stanbol.commons.namespaceprefix.provider.prefixcc;
import java.io.IOException;
+import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
-import java.net.URLConnection;
import java.util.Date;
import java.util.List;
import java.util.ServiceLoader;
@@ -28,6 +28,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
+import org.apache.commons.io.IOUtils;
import org.apache.stanbol.commons.namespaceprefix.NamespacePrefixProvider;
import
org.apache.stanbol.commons.namespaceprefix.impl.NamespacePrefixProviderImpl;
import org.slf4j.Logger;
@@ -37,17 +38,13 @@ public class PrefixccProvider implements
private static final Logger log =
LoggerFactory.getLogger(PrefixccProvider.class);
- public static final URLConnection GET_ALL;
+ public static final URL GET_ALL;
static {
try {
- URL url = new URL("http://prefix.cc/popular/all.file.txt");
- GET_ALL = url.openConnection();
- GET_ALL.connect();
+ GET_ALL = new URL("http://prefix.cc/popular/all.file.txt");
} catch (MalformedURLException e) {
throw new IllegalStateException("Unable to create http://prefix.cc
URL",e);
- } catch (IOException e) {
- throw new IllegalStateException("Unable to open
http://prefix.cc URLConnection",e);
- }
+ }
}
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
@@ -115,11 +112,26 @@ public class PrefixccProvider implements
protected final void loadMappings() {
try {
log.info("Load Namespace Prefix Mappings form {}",GET_ALL);
- if(GET_ALL.getContentType().equals("text/plain") &&
((HttpURLConnection)GET_ALL).getResponseCode() == 200){
- cache = new
NamespacePrefixProviderImpl(GET_ALL.getInputStream());
- cacheStamp = System.currentTimeMillis();
- log.info(" ... completed");
+ HttpURLConnection con =
(HttpURLConnection)GET_ALL.openConnection();
+ con.setReadTimeout(5000); //set the max connect & read timeout to
5sec
+ con.setConnectTimeout(5000);
+ con.connect();
+ String contentType = con.getContentType();
+ if("text/plain".equalsIgnoreCase(contentType)){
+ InputStream in = con.getInputStream();
+ try {
+ cache = new NamespacePrefixProviderImpl(in);
+ cacheStamp = System.currentTimeMillis();
+ log.info(" ... completed");
+ } finally {
+ IOUtils.closeQuietly(in);
+ }
+ } else {
+ log.warn("Response from prefix.cc does have the wrong content
type '"
+ + contentType + "' (expected: text/plain). This indicates
that the "
+ + "service is currently unavailable!");
}
+ con.disconnect(); //we connect once every {long-period}
} catch (IOException e) {
log.warn("Unable to load prefix.cc NamespaceMappings (Message: "
+ e.getMessage() +")",e);
Modified:
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/test/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProviderTest.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/test/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProviderTest.java?rev=1495987&r1=1495986&r2=1495987&view=diff
==============================================================================
---
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/test/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProviderTest.java
(original)
+++
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/test/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProviderTest.java
Mon Jun 24 10:40:26 2013
@@ -19,11 +19,13 @@ package org.apache.stanbol.commons.names
import java.io.File;
import java.io.IOException;
+import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Date;
import java.util.concurrent.TimeUnit;
+import org.apache.commons.io.IOUtils;
import org.apache.stanbol.commons.namespaceprefix.NamespacePrefixService;
import
org.apache.stanbol.commons.namespaceprefix.service.StanbolNamespacePrefixService;
import org.junit.Assert;
@@ -65,27 +67,41 @@ public class PrefixccProviderTest {
Assert.assertFalse(pcp.isAvailable());
Assert.assertNull(pcp.getCacheTimeStamp());
}
-
- @Test
- public void testServiceLoader() throws IOException{
-
- // Check if the service is down
- PrefixccProvider pcp = new PrefixccProvider(10,TimeUnit.SECONDS);
- if(!pcp.isAvailable()){
- log.info("Unable to retrieve prefixes from http://prefix.cc ...
deactivating "
- + PrefixccProvider.class.getSimpleName()+ "ServiceLoader
support test");
- return;
- }
-
- //this test works only if online
+ /**
+ * Checks if the service is reachable (test is performed online) and if
+ * prefix.cc sends information with the correct content type.
+ * @return
+ */
+ private boolean checkServiceAvailable(){
try {
- PrefixccProvider.GET_ALL.getInputStream();
+ HttpURLConnection con =
(HttpURLConnection)PrefixccProvider.GET_ALL.openConnection();
+ con.setReadTimeout(5000); //set the max connect & read timeout to
5sec
+ con.setConnectTimeout(5000);
+ con.connect();
+ String contentType = con.getContentType();
+ IOUtils.closeQuietly(con.getInputStream()); //close the stream
+ if("text/plain".equalsIgnoreCase(contentType)){
+ return true;
+ } else {
+ log.info("Request to http://prefix.cc ... returned an
unexpected "
+ + "ContentType "+contentType+ " (expected: text/plain)
"
+ + " ... deactivate" +
PrefixccProvider.class.getSimpleName()
+ + " test");
+ return false; //service seams to be down ... skip tests
+ }
} catch (IOException e) {
log.info("Unable to connect to http://prefix.cc ... deactivating "
- + PrefixccProvider.class.getSimpleName()+ "ServiceLoader
support test");
- return;
+ + PrefixccProvider.class.getSimpleName()+ " test");
+ return false;
+ }
+ }
+
+ @Test
+ public void testServiceLoader() throws IOException{
+ //this test works only if online
+ if(!checkServiceAvailable()){
+ return; //skip test
}
-
//this test for now does not use predefined mappings
URL mappingURL = PrefixccProviderTest.class.getClassLoader()