You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by fm...@apache.org on 2014/07/08 12:24:32 UTC

svn commit: r1608708 - in /chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src: main/java/org/apache/chemistry/opencmis/server/impl/ test/java/org/apache/chemistry/opencmis/server/impl/

Author: fmui
Date: Tue Jul  8 10:24:31 2014
New Revision: 1608708

URL: http://svn.apache.org/r1608708
Log:
improved call context implementation

Added:
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/CallContextImplTest.java   (with props)
Modified:
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/CallContextImpl.java

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/CallContextImpl.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/CallContextImpl.java?rev=1608708&r1=1608707&r2=1608708&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/CallContextImpl.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/CallContextImpl.java Tue Jul  8 10:24:31 2014
@@ -77,7 +77,10 @@ public class CallContextImpl implements 
             return;
         }
 
-        rangeHeader = rangeHeader.trim().toLowerCase(Locale.ENGLISH);
+        remove(OFFSET);
+        remove(LENGTH);
+
+        rangeHeader = rangeHeader.replaceAll("\\s", "").toLowerCase(Locale.ENGLISH);
 
         if (rangeHeader.length() > 6 && rangeHeader.startsWith("bytes=") && rangeHeader.indexOf(',') == -1
                 && rangeHeader.charAt(6) != '-') {
@@ -119,15 +122,66 @@ public class CallContextImpl implements 
             return;
         }
 
-        String[] locale = acceptLanguageHeader.split("-");
-        put(LOCALE_ISO639_LANGUAGE, locale[0].trim());
-        if (locale.length > 1) {
-            int x = locale[1].indexOf(',');
-            if (x == -1) {
-                put(LOCALE_ISO3166_COUNTRY, locale[1].trim());
+        remove(LOCALE_ISO639_LANGUAGE);
+        remove(LOCALE_ISO3166_COUNTRY);
+        remove(LOCALE);
+
+        double lastQ = 0;
+        String language = null;
+        String country = null;
+
+        String[] languageHeader = acceptLanguageHeader.split(",");
+        for (String languageRange : languageHeader) {
+            String langRange = languageRange.trim();
+            double currentQ = 0;
+
+            int x = langRange.indexOf(';');
+            if (x > -1) {
+                String qStr = langRange.substring(x + 1).replaceAll("\\s", "").toLowerCase(Locale.ENGLISH);
+                if (!qStr.startsWith("q=") && qStr.length() < 3) {
+                    continue;
+                }
+                currentQ = Double.parseDouble(qStr.substring(2));
+                langRange = langRange.substring(0, x);
+            } else {
+                currentQ = 1;
+            }
+
+            if (currentQ <= lastQ) {
+                continue;
             } else {
-                put(LOCALE_ISO3166_COUNTRY, locale[1].substring(0, x).trim());
+                lastQ = currentQ;
             }
+
+            String[] locale = langRange.split("-");
+            String local0 = locale[0].trim();
+
+            language = null;
+            country = null;
+
+            if (!locale.equals("*")) {
+                language = local0;
+                if (locale.length > 1) {
+                    String local1 = locale[1].trim();
+                    if (!local1.equals("*")) {
+                        country = local1;
+                    }
+                }
+            }
+
+            if (currentQ >= 1) {
+                break;
+            }
+        }
+
+        if (language != null) {
+            put(LOCALE_ISO639_LANGUAGE, language);
+            put(LOCALE, language);
+        }
+
+        if (country != null) {
+            put(LOCALE_ISO3166_COUNTRY, country);
+            put(LOCALE, language + "-" + country);
         }
     }
 

Added: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/CallContextImplTest.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/CallContextImplTest.java?rev=1608708&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/CallContextImplTest.java (added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/CallContextImplTest.java Tue Jul  8 10:24:31 2014
@@ -0,0 +1,78 @@
+/*
+ * 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.chemistry.opencmis.server.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.apache.chemistry.opencmis.commons.server.CallContext;
+import org.junit.Test;
+
+public class CallContextImplTest {
+
+    @Test
+    public void testLocal() {
+        CallContextImpl context = new CallContextImpl(null, null, null, null, null, null, null, null);
+
+        context.setAcceptLanguage(" en - us ");
+        assertEquals("en", context.get(CallContext.LOCALE_ISO639_LANGUAGE));
+        assertEquals("us", context.get(CallContext.LOCALE_ISO3166_COUNTRY));
+        assertEquals("en-us", context.getLocale());
+
+        context.setAcceptLanguage("en-us; q=0.8 , de-ch ; Q = 0.9, abc-123; q=0.");
+        assertEquals("de", context.get(CallContext.LOCALE_ISO639_LANGUAGE));
+        assertEquals("ch", context.get(CallContext.LOCALE_ISO3166_COUNTRY));
+        assertEquals("de-ch", context.getLocale());
+
+        context.setAcceptLanguage("en-us; q=0.8, abc-123; q=1.0, de-ch ;Q= 0.9");
+        assertEquals("abc", context.get(CallContext.LOCALE_ISO639_LANGUAGE));
+        assertEquals("123", context.get(CallContext.LOCALE_ISO3166_COUNTRY));
+        assertEquals("abc-123", context.getLocale());
+    }
+
+    @Test
+    public void testRange() {
+        CallContextImpl context = new CallContextImpl(null, null, null, null, null, null, null, null);
+
+        context.setRange("bytes=100-299");
+        assertEquals(100L, context.getOffset().longValue());
+        assertEquals(200L, context.getLength().longValue());
+
+        context.setRange(" bytes  = 1 - 2");
+        assertEquals(1L, context.getOffset().longValue());
+        assertEquals(2L, context.getLength().longValue());
+
+        context.setRange("bytes=456-");
+        assertEquals(456L, context.getOffset().longValue());
+        assertNull(context.getLength());
+
+        // not supported ranges
+        context.setRange("bytes=10-20,30-40");
+        assertNull(context.getOffset());
+        assertNull(context.getLength());
+
+        context.setRange("bytes=-123");
+        assertNull(context.getOffset());
+        assertNull(context.getLength());
+
+        context.setRange("kb=100-299");
+        assertNull(context.getOffset());
+        assertNull(context.getLength());
+    }
+}

Propchange: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/CallContextImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native