Author: reschke
Date: Thu Mar 21 14:24:59 2019
New Revision: 1855993

URL: http://svn.apache.org/viewvc?rev=1855993&view=rev
Log:
OAK-8135: HTTP service may not select correct media type if multiple are 
specified in Accept header field

Added:
    
jackrabbit/oak/trunk/oak-http/src/test/java/org/apache/jackrabbit/oak/http/AcceptHeaderTest.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/MediaRange.java

Modified: 
jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/MediaRange.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/MediaRange.java?rev=1855993&r1=1855992&r2=1855993&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/MediaRange.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/MediaRange.java
 Thu Mar 21 14:24:59 2019
@@ -28,6 +28,10 @@ public class MediaRange {
 
     private final double q;
 
+    private static final int QRESOLUTION = 1000;
+    private static double CORRECTIONFORSUBTYPEMATCH = 1f / (10 * QRESOLUTION);
+    private static double CORRECTIONFORTYPEANDSUBTYPEMATCH = 2f / (10 * 
QRESOLUTION);
+    
     public static MediaRange parse(String range, MediaTypeRegistry registry) {
         MediaType type = MediaType.parse(range);
         if (type == null) {
@@ -56,6 +60,20 @@ public class MediaRange {
         this.q = q;
     }
 
+    /**
+     * Matches the media range against the specified media type.
+     * <p>
+     * The "derived" quality value if computed from the specified q value (0 to
+     * 1) by subtracting 1/10000 in case the subtype in the range is "*", or
+     * 2/10000 if case both type and subtype are. This takes care of the
+     * precedence specified in RFC 7231, Section 5.3.2.
+     *
+     * @param type
+     *            type to match
+     * @param registry
+     *            media type registry
+     * @return {@code 0.0} for "no match", the derived quality value if match
+     */
     public double match(MediaType type, MediaTypeRegistry registry) {
         if (type.equals(this.type)) { // shortcut
             return q;
@@ -70,14 +88,16 @@ public class MediaRange {
             }
         }
 
-        if ("*/*".equals(this.type.toString())) {
-            return q;
-        } else if ("*".equals(this.type.getSubtype())
-                && type.getType().equals(this.type.getType())) {
-            return q;
+        if (q > 0.0 && "*/*".equals(this.type.toString())) {
+            return q - CORRECTIONFORTYPEANDSUBTYPEMATCH;
+        } else if (q > 0.0 && "*".equals(this.type.getSubtype()) && 
type.getType().equals(this.type.getType())) {
+            return q - CORRECTIONFORSUBTYPEMATCH;
         } else {
             return 0.0;
         }
     }
 
+    public String toString() {
+        return String.format("type=%s, q=%f", type, q);
+    }
 }

Added: 
jackrabbit/oak/trunk/oak-http/src/test/java/org/apache/jackrabbit/oak/http/AcceptHeaderTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-http/src/test/java/org/apache/jackrabbit/oak/http/AcceptHeaderTest.java?rev=1855993&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-http/src/test/java/org/apache/jackrabbit/oak/http/AcceptHeaderTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-http/src/test/java/org/apache/jackrabbit/oak/http/AcceptHeaderTest.java
 Thu Mar 21 14:24:59 2019
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.jackrabbit.oak.http;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.tika.mime.MediaType;
+import org.junit.Test;
+
+import com.fasterxml.jackson.core.JsonFactory;
+
+public class AcceptHeaderTest {
+
+    private Representation json = new 
JsonRepresentation(MediaType.parse("application/json"), new JsonFactory());
+    private Representation html = new HtmlRepresentation();
+
+    @Test
+    public void testSpecificTypePlusGenericRange() {
+        // both have the same q value (default of 1.0), but application/json 
is more specific
+        // see RFC 7231, Section 5.3.2
+        AcceptHeader ah = new AcceptHeader("application/json, */*");
+        Representation selected;
+
+        selected = ah.resolve(json);
+        assertEquals(json, selected);
+
+        selected = ah.resolve(html);
+        assertEquals(html, selected);
+
+        selected = ah.resolve(html);
+        assertEquals(html, selected);
+
+        selected = ah.resolve(json, html);
+        assertEquals(json, selected);
+
+        // OAK-8135
+        selected = ah.resolve(html, json);
+        assertEquals(json, selected);
+ 
+        // retry with q values
+        ah = new AcceptHeader("application/json; q=0.4, */*; q=0.5");
+        selected = ah.resolve(html, json);
+        assertEquals(html, selected);
+
+        ah = new AcceptHeader("application/json, */*; q=0.5");
+        selected = ah.resolve(html, json);
+        assertEquals(json, selected);
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-http/src/test/java/org/apache/jackrabbit/oak/http/AcceptHeaderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to