jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1353323 - in /jackrabbit/oak/trunk/oak-http: ./ src/main/java/org/apache/jackrabbit/oak/http/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/jackrabbit/ src/test/java/org/apache/jackrabbit/oa...
Date Sun, 24 Jun 2012 20:15:20 GMT
Author: jukka
Date: Sun Jun 24 20:15:19 2012
New Revision: 1353323

URL: http://svn.apache.org/viewvc?rev=1353323&view=rev
Log:
OAK-104: HTTP bindings for Oak

Add basic Accept header evaluation logic based on the Tika type registry

Added:
    jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/AcceptHeader.java
    jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/MediaRange.java
    jackrabbit/oak/trunk/oak-http/src/test/
    jackrabbit/oak/trunk/oak-http/src/test/java/
    jackrabbit/oak/trunk/oak-http/src/test/java/org/
    jackrabbit/oak/trunk/oak-http/src/test/java/org/apache/
    jackrabbit/oak/trunk/oak-http/src/test/java/org/apache/jackrabbit/
    jackrabbit/oak/trunk/oak-http/src/test/java/org/apache/jackrabbit/oak/
    jackrabbit/oak/trunk/oak-http/src/test/java/org/apache/jackrabbit/oak/http/
    jackrabbit/oak/trunk/oak-http/src/test/java/org/apache/jackrabbit/oak/http/AcceptHeaderTest.java
Modified:
    jackrabbit/oak/trunk/oak-http/pom.xml

Modified: jackrabbit/oak/trunk/oak-http/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-http/pom.xml?rev=1353323&r1=1353322&r2=1353323&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-http/pom.xml (original)
+++ jackrabbit/oak/trunk/oak-http/pom.xml Sun Jun 24 20:15:19 2012
@@ -82,6 +82,16 @@
       <version>0.3-SNAPSHOT</version>
     </dependency>
     <dependency>
+      <groupId>org.apache.tika</groupId>
+      <artifactId>tika-core</artifactId>
+      <version>1.1</version>
+    </dependency>
+    <dependency>
+      <groupId>com.fasterxml.jackson.core</groupId>
+      <artifactId>jackson-core</artifactId>
+      <version>2.0.0</version>
+    </dependency>
+    <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
       <version>2.5</version>

Added: jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/AcceptHeader.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/AcceptHeader.java?rev=1353323&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/AcceptHeader.java
(added)
+++ jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/AcceptHeader.java
Sun Jun 24 20:15:19 2012
@@ -0,0 +1,65 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.List;
+
+import org.apache.tika.mime.MediaType;
+import org.apache.tika.mime.MediaTypeRegistry;
+
+public class AcceptHeader {
+
+    private static final MediaTypeRegistry registry =
+            MediaTypeRegistry.getDefaultRegistry();
+
+    private final List<MediaRange> ranges = new ArrayList<MediaRange>();
+
+    public AcceptHeader(String accept) {
+        if (accept == null) {
+            ranges.add(new MediaRange(MediaType.parse("*/*"), 1.0));
+        } else {
+            for (String part : accept.split("(\\s*,)+\\s*")) {
+                MediaRange range = MediaRange.parse(part, registry); 
+                if (range != null) {
+                    ranges.add(range);
+                }
+            }
+        }
+
+    }
+
+    public String resolve(String... types) {
+        int maxIndex = 0;
+        double[] qs = new double[types.length];
+        for (int i = 0; i < types.length; i++) {
+            MediaType type = registry.normalize(MediaType.parse(types[i]));
+            for (MediaRange range : ranges) {
+                qs[i] = Math.max(qs[i], range.match(type, registry));
+            }
+            if (qs[i] > qs[maxIndex]) {
+                maxIndex = i;
+            }
+        }
+        if (qs[maxIndex] > 0.0) {
+            return types[maxIndex];
+        } else {
+            return MediaType.OCTET_STREAM.toString();
+        }
+    }
+
+}

Added: 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=1353323&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/MediaRange.java
(added)
+++ jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/MediaRange.java
Sun Jun 24 20:15:19 2012
@@ -0,0 +1,88 @@
+/*
+ * 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 java.util.HashMap;
+import java.util.Map;
+
+import org.apache.tika.mime.MediaType;
+import org.apache.tika.mime.MediaTypeRegistry;
+
+public class MediaRange {
+
+    private final MediaType type;
+
+    private final double q;
+
+    public static MediaRange parse(String range, MediaTypeRegistry registry) {
+        MediaType type = MediaType.parse(range);
+        if (type == null) {
+            return null;
+        }
+        type = registry.normalize(type);
+
+        Map<String, String> parameters =
+                new HashMap<String, String>(type.getParameters());
+        String q = parameters.remove("q");
+        if (q != null) {
+            try {
+                return new MediaRange(
+                        new MediaType(type.getBaseType(), parameters),
+                        Double.parseDouble(q));
+            } catch (NumberFormatException e) {
+                return null;
+            }
+        }
+
+        return new MediaRange(type, 1.0);
+    }
+
+    public MediaRange(MediaType type, double q) {
+        this.type = type;
+        this.q = q;
+    }
+
+    public double match(MediaType type, MediaTypeRegistry registry) {
+        if (type.equals(this.type)) { // shortcut
+            return q;
+        }
+
+        for (Map.Entry<String, String> entry
+                : this.type.getParameters().entrySet()) {
+            String key = entry.getKey();
+            String value = entry.getValue();
+            if (!value.equals(type.getParameters().get(key))) {
+                return 0.0;
+            }
+        }
+
+        if ("*/*".equals(this.type.toString())) {
+            return q;
+        } else if ("*".equals(this.type.getSubtype())
+                && type.getType().equals(this.type.getType())) {
+            return q;
+        } else {
+            MediaType superType = registry.getSupertype(type);
+            if (superType != null) {
+                return match(superType, registry);
+            } else {
+                return 0.0;
+            }
+        }
+    }
+
+}

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=1353323&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
Sun Jun 24 20:15:19 2012
@@ -0,0 +1,50 @@
+/*
+ * 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 junit.framework.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class AcceptHeaderTest {
+
+    @Test
+    public void testRfcExample1() {
+        AcceptHeader accept = new AcceptHeader(
+                "text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c");
+
+        assertEquals("text/plain", accept.resolve("text/plain"));
+        assertEquals("text/html", accept.resolve("text/html"));
+        assertEquals("text/x-dvi", accept.resolve("text/x-dvi"));
+        assertEquals("text/x-c", accept.resolve("text/x-c"));
+
+        assertEquals(
+                "application/octet-stream",
+                accept.resolve("application/octet-stream"));
+        assertEquals(
+                "application/octet-stream",
+                accept.resolve("application/pdf"));
+
+        assertEquals("text/html", accept.resolve("text/plain", "text/html"));
+        assertEquals("text/x-c", accept.resolve("text/x-dvi", "text/x-c"));
+        assertEquals("text/x-dvi", accept.resolve("text/x-dvi", "text/plain"));
+
+        assertEquals("text/html", accept.resolve("text/html", "text/x-c"));
+        assertEquals("text/x-c", accept.resolve("text/x-c", "text/html"));
+    }
+
+}



Mime
View raw message