Updated Branches: refs/heads/develop 8a7af22f8 -> 60f39bc0e
some optimizations in the prefix.cc lookup service. Project: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/commit/60f39bc0 Tree: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/tree/60f39bc0 Diff: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/diff/60f39bc0 Branch: refs/heads/develop Commit: 60f39bc0e3c4c8f544d6c4928f2e5da4063c33a9 Parents: 8a7af22 Author: Jakob Frank <[email protected]> Authored: Tue Apr 30 11:52:53 2013 +0200 Committer: Jakob Frank <[email protected]> Committed: Tue Apr 30 11:55:04 2013 +0200 ---------------------------------------------------------------------- .../platform/core/services/prefix/PrefixCC.java | 73 ++++++++------ 1 files changed, 42 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/60f39bc0/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/prefix/PrefixCC.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/prefix/PrefixCC.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/prefix/PrefixCC.java index d80e055..b231800 100644 --- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/prefix/PrefixCC.java +++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/prefix/PrefixCC.java @@ -18,31 +18,29 @@ package org.apache.marmotta.platform.core.services.prefix; import java.io.IOException; -import java.util.HashMap; +import java.net.URLEncoder; +import java.nio.charset.Charset; import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; -import org.apache.http.Header; +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.LineIterator; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpHead; -import org.apache.http.util.EntityUtils; import org.apache.marmotta.platform.core.api.http.HttpClientService; import org.apache.marmotta.platform.core.api.prefix.PrefixProvider; import org.apache.marmotta.platform.core.util.http.HttpRequestUtil; -import org.codehaus.jackson.JsonFactory; -import org.codehaus.jackson.map.ObjectMapper; -import org.codehaus.jackson.type.TypeReference; import org.slf4j.Logger; /** * Prefix.cc Provider * * @author Sergio Fernández + * @author Jakob Frank <[email protected]> * */ @ApplicationScoped @@ -59,9 +57,9 @@ public class PrefixCC implements PrefixProvider { @Override public String getNamespace(final String prefix) { - HttpGet get = new HttpGet(URI + prefix + ".file.json"); + HttpGet get = new HttpGet(URI + prefix + ".file.txt"); HttpRequestUtil.setUserAgentString(get, USER_AGENT); - get.setHeader("Accept", "application/json"); + get.setHeader("Accept", "text/plain"); try { return httpClientService.execute(get, new ResponseHandler<String>() { @@ -69,20 +67,21 @@ public class PrefixCC implements PrefixProvider { public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { if (200 == response.getStatusLine().getStatusCode()) { HttpEntity entity = response.getEntity(); - JsonFactory factory = new JsonFactory(); - ObjectMapper mapper = new ObjectMapper(factory); - TypeReference<HashMap<String,String>> typeRef = new TypeReference<HashMap<String,String>>() {}; - HashMap<String,String> result = mapper.readValue(EntityUtils.toString(entity), typeRef); - if (result.containsKey(prefix)) { - return result.get(prefix); - } else { - log.error("Error: prefix '" + prefix + "' not found at prefix.cc"); - return null; + + final LineIterator it = IOUtils.lineIterator(entity.getContent(), Charset.defaultCharset()); + try { + while (it.hasNext()) { + final String l = it.next(); + if (l.startsWith(prefix + "\t")) { + return l.substring(prefix.length()+1); + } + } + } finally { + it.close(); } - } else { - log.error("Error: prefix '" + prefix + "' not found at prefix.cc"); - return null; } + log.error("Error: prefix '" + prefix + "' not found at prefix.cc"); + return null; } }); } catch (Exception e) { @@ -93,20 +92,32 @@ public class PrefixCC implements PrefixProvider { @Override public String getPrefix(final String namespace) { - HttpHead head = new HttpHead(URI + "reverse?uri=" + namespace); - HttpRequestUtil.setFollowRedirect(head, false); - HttpRequestUtil.setUserAgentString(head, USER_AGENT); try { - return httpClientService.execute(head, new ResponseHandler<String>() { + HttpGet get = new HttpGet(URI + "reverse?format=txt&uri=" + URLEncoder.encode(namespace, "utf-8")); + HttpRequestUtil.setUserAgentString(get, USER_AGENT); + get.setHeader("Accept", "text/plain"); + + return httpClientService.execute(get, new ResponseHandler<String>() { + @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { - if (response.containsHeader("location")) { - Header location = response.getFirstHeader("location"); - return location.getValue().substring(URI.length()); - } else { - log.error("Error: reverse namespace lookup for '" + namespace + "' not found at prefix.cc"); - return null; + if (200 == response.getStatusLine().getStatusCode()) { + HttpEntity entity = response.getEntity(); + + final LineIterator it = IOUtils.lineIterator(entity.getContent(), Charset.defaultCharset()); + try { + while (it.hasNext()) { + final String l = it.next(); + if (l.endsWith("\t" + namespace)) { + return l.substring(0, l.indexOf("\t")); + } + } + } finally { + it.close(); + } } + log.error("Error: reverse namespace lookup for '" + namespace + "' not found at prefix.cc"); + return null; } }); } catch (Exception e) {
