marmotta-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wik...@apache.org
Subject [1/2] marmotta git commit: MARMOTTA-591: accepted patch by @schlegel to allow multiple accept headers in sparql endpoints. This closes #10.
Date Tue, 10 Feb 2015 08:31:18 GMT
Repository: marmotta
Updated Branches:
  refs/heads/develop 0b8f74833 -> 7a655117a


MARMOTTA-591: accepted patch by @schlegel to allow multiple accept headers in sparql endpoints.
This closes #10.


Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/b3e88dc5
Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/b3e88dc5
Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/b3e88dc5

Branch: refs/heads/develop
Commit: b3e88dc5548dcd908e8a2d0e95ffc9603647775d
Parents: 0b8f748
Author: Sergio Fernández <wikier@apache.org>
Authored: Tue Feb 10 09:21:24 2015 +0100
Committer: Sergio Fernández <wikier@apache.org>
Committed: Tue Feb 10 09:21:24 2015 +0100

----------------------------------------------------------------------
 .../commons/http/MarmottaHttpUtils.java         | 22 ++++++++++++++++++++
 .../sparql/webservices/SparqlWebService.java    | 19 ++++++++++-------
 .../webservices/SparqlWebServiceTest.java       | 14 +++++++++++++
 3 files changed, 47 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/marmotta/blob/b3e88dc5/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/MarmottaHttpUtils.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/MarmottaHttpUtils.java
b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/MarmottaHttpUtils.java
index 34f0213..46f38f5 100644
--- a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/MarmottaHttpUtils.java
+++ b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/MarmottaHttpUtils.java
@@ -64,6 +64,28 @@ public class MarmottaHttpUtils {
         return contentTypes;
     }
 
+    /**
+     * A utility method for parsing HTTP Content-Type and Accept header, taking into account
different parameters that
+     * are typically passed. Recognized parameters:
+     * - charset: gives the charset of the content
+     * - q: gives the precedence of the content
+     * The result is an ordered list of content types in order of the computed preference
in the header value passed as
+     * string argument.
+     * <p/>
+     * Author: Kai Schlegel
+     */
+    public static List<ContentType> parseAcceptHeaders(List<String> headers)
{
+        List<ContentType> result = new ArrayList<>();
+
+        for(String header : headers) {
+            result.addAll(parseAcceptHeader(header));
+        }
+
+        Collections.sort(result);
+
+        return result;
+    }
+
 
     public static List<ContentType> parseStringList(Collection<String> types)
{
         List<ContentType> contentTypes = new ArrayList<ContentType>(types.size());

http://git-wip-us.apache.org/repos/asf/marmotta/blob/b3e88dc5/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java
b/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java
index 5ee8aa3..7d5f76b 100644
--- a/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java
+++ b/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java
@@ -19,6 +19,7 @@ package org.apache.marmotta.platform.sparql.webservices;
 
 import com.google.common.collect.Lists;
 import com.google.common.io.CharStreams;
+import org.apache.commons.collections.EnumerationUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.marmotta.commons.http.ContentType;
 import org.apache.marmotta.commons.http.MarmottaHttpUtils;
@@ -234,13 +235,15 @@ public class SparqlWebService {
      */
     private Response select(String query, String resultType, HttpServletRequest request)
{
         try {
-            String acceptHeader = StringUtils.defaultString(request.getHeader(ACCEPT), "");
+            List<String> acceptHeaders = EnumerationUtils.toList(request.getHeaders(ACCEPT));
             if (StringUtils.isBlank(query)) { //empty query
-                if (acceptHeader.contains("html")) {
-                    return Response.seeOther(new URI(configurationService.getServerUri()
+ "sparql/admin/squebi.html")).build();
-                } else {
-                    return Response.status(Status.ACCEPTED).entity("no SPARQL query specified").build();
+                for(String acceptHeader : acceptHeaders) {
+                    if (acceptHeader.contains("html")) {
+                        return Response.seeOther(new URI(configurationService.getServerUri()
+ "sparql/admin/squebi.html")).build();
+                    }
                 }
+
+                return Response.status(Status.ACCEPTED).entity("no SPARQL query specified").build();
             } else {
                 //query duck typing
                 QueryType queryType = sparqlService.getQueryType(QueryLanguage.SPARQL, query);
@@ -249,7 +252,7 @@ public class SparqlWebService {
                 if (resultType != null) {
                     acceptedTypes = MarmottaHttpUtils.parseAcceptHeader(resultType);
                 } else {
-                    acceptedTypes = MarmottaHttpUtils.parseAcceptHeader(acceptHeader);
+                    acceptedTypes = MarmottaHttpUtils.parseAcceptHeaders(acceptHeaders);
                 }
                 if (QueryType.TUPLE.equals(queryType)) {
                     offeredTypes = MarmottaHttpUtils.parseQueryResultFormatList(TupleQueryResultWriterRegistry.getInstance().getKeys());
@@ -391,7 +394,7 @@ public class SparqlWebService {
                 return Response.ok().build();
             } else {
                 if (resultType == null) {
-                    List<ContentType> acceptedTypes = MarmottaHttpUtils.parseAcceptHeader(request.getHeader(ACCEPT));
+                    List<ContentType> acceptedTypes = MarmottaHttpUtils.parseAcceptHeaders(EnumerationUtils.toList(request.getHeaders(ACCEPT)));
                     List<ContentType> offeredTypes = MarmottaHttpUtils.parseStringList(Lists.newArrayList("*/*",
"text/html"));
                     ContentType bestType = MarmottaHttpUtils.bestContentType(offeredTypes,
acceptedTypes);
                     if (bestType != null) {
@@ -466,7 +469,7 @@ public class SparqlWebService {
         if (StringUtils.isBlank(request.getHeader(ACCEPT))) {
             acceptedTypes = Collections.singletonList(MarmottaHttpUtils.parseContentType(RDFXML.getDefaultMIMEType()));
         } else {
-            acceptedTypes = MarmottaHttpUtils.parseAcceptHeader(request.getHeader(ACCEPT));
+            acceptedTypes = MarmottaHttpUtils.parseAcceptHeaders(EnumerationUtils.toList(request.getHeaders(ACCEPT)));
         }
         
         ContentType _bestType = null;

http://git-wip-us.apache.org/repos/asf/marmotta/blob/b3e88dc5/platform/marmotta-sparql/src/test/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebServiceTest.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-sparql/src/test/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebServiceTest.java
b/platform/marmotta-sparql/src/test/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebServiceTest.java
index ce12c00..f74b831 100644
--- a/platform/marmotta-sparql/src/test/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebServiceTest.java
+++ b/platform/marmotta-sparql/src/test/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebServiceTest.java
@@ -190,4 +190,18 @@ public class SparqlWebServiceTest {
                 get("/sparql/select");
     }
 
+    @Test
+    public void testMultipleAcceptHeaders() throws IOException, InterruptedException {
+        expect().
+                log().ifError().
+                statusCode(200).
+                contentType("application/rdf+json").
+                given().
+                header("Accept", "application/na").
+                header("Accept", "application/rdf+json; q=1.0").
+                param("query", "DESCRIBE <http://www.wikier.org/foaf#wikier>").
+                when().
+                get("/sparql/select");
+    }
+
 }


Mime
View raw message