olingo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sklev...@apache.org
Subject [1/2] git commit: [OLINGO-338] - refactor ContentType and AccessType class
Date Wed, 09 Jul 2014 12:51:16 GMT
Repository: olingo-odata4
Updated Branches:
  refs/heads/master cbd2b3c99 -> ba5143339


[OLINGO-338] - refactor ContentType and AccessType class


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

Branch: refs/heads/master
Commit: ba5143339567c59c52380b02117a531c697b6a72
Parents: 8e0abd7
Author: Stephan Klevenz <stephan.klevenz@sap.com>
Authored: Wed Jul 9 13:51:13 2014 +0200
Committer: Stephan Klevenz <stephan.klevenz@sap.com>
Committed: Wed Jul 9 14:51:03 2014 +0200

----------------------------------------------------------------------
 .../olingo/fit/tecsvc/http/BasicHttpITCase.java |  9 +-
 .../olingo/commons/api/format/AcceptType.java   | 66 +++----------
 .../olingo/commons/api/format/ContentType.java  | 97 +++++++++-----------
 .../olingo/commons/api/format/ODataFormat.java  | 12 +--
 .../olingo/commons/api/format/TypeUtil.java     |  4 -
 .../commons/api/format/AcceptTypeTest.java      | 93 +++++++++++++------
 .../commons/api/format/ContentTypeTest.java     | 69 ++++++++++++++
 .../apache/olingo/server/api/ODataRequest.java  | 11 ++-
 .../olingo/server/api/ODataRequestTest.java     | 26 +++---
 .../olingo/server/core/ContentNegotiator.java   | 20 ++--
 .../apache/olingo/server/core/ODataHandler.java |  8 +-
 .../server/core/ContentNegotiatorTest.java      |  2 +-
 12 files changed, 238 insertions(+), 179 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ba514333/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicHttpITCase.java
----------------------------------------------------------------------
diff --git a/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicHttpITCase.java b/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicHttpITCase.java
index cc49c28..34ff4d4 100644
--- a/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicHttpITCase.java
+++ b/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicHttpITCase.java
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertEquals;
 import java.net.HttpURLConnection;
 import java.net.URL;
 
+import org.apache.olingo.commons.api.format.ContentType;
 import org.apache.olingo.commons.api.http.HttpHeader;
 import org.apache.olingo.fit.tecsvc.TecSvcConst;
 import org.junit.Test;
@@ -42,7 +43,7 @@ public class BasicHttpITCase {
     int code = connection.getResponseCode();
     assertEquals(200, code);
     String ct = connection.getHeaderField(HttpHeader.CONTENT_TYPE);
-    assertEquals("application/json;odata.metadata=minimal", ct);
+    assertEquals(ContentType.create("application/json;odata.metadata=minimal"), ContentType.create(ct));
   }
 
   @Test
@@ -59,7 +60,7 @@ public class BasicHttpITCase {
     int code = connection.getResponseCode();
     assertEquals(200, code);
     String ct = connection.getHeaderField(HttpHeader.CONTENT_TYPE);
-    assertEquals("application/json;odata.metadata=minimal", ct);
+    assertEquals(ContentType.create("application/json;odata.metadata=minimal"), ContentType.create(ct));
   }
 
   @Test
@@ -76,7 +77,7 @@ public class BasicHttpITCase {
     int code = connection.getResponseCode();
     assertEquals(200, code);
     String ct = connection.getHeaderField(HttpHeader.CONTENT_TYPE);
-    assertEquals("application/json;odata.metadata=minimal", ct);
+    assertEquals(ContentType.create("application/json;odata.metadata=minimal"), ContentType.create(ct));
   }
 
   @Test
@@ -93,7 +94,7 @@ public class BasicHttpITCase {
     int code = connection.getResponseCode();
     assertEquals(200, code);
     String ct = connection.getHeaderField(HttpHeader.CONTENT_TYPE);
-    assertEquals("application/json;odata.metadata=minimal;charset=UTF-8", ct);
+    assertEquals(ContentType.create("application/json;odata.metadata=minimal;charset=utf-8"),
ContentType.create(ct));
   }
 
   @Test

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ba514333/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptType.java
----------------------------------------------------------------------
diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptType.java
b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptType.java
index 2408fcb..180583c 100644
--- a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptType.java
+++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptType.java
@@ -45,8 +45,8 @@ import java.util.TreeMap;
  */
 public class AcceptType {
 
-  public static final AcceptType WILDCARD = create(TypeUtil.MEDIA_TYPE_WILDCARD, TypeUtil.MEDIA_TYPE_WILDCARD,
-      createParameterMap(), 1F);
+//  public static final AcceptType WILDCARD = new AcceptType(TypeUtil.MEDIA_TYPE_WILDCARD,
TypeUtil.MEDIA_TYPE_WILDCARD,
+//      createParameterMap(), 1.0F);
 
   private final String type;
   private final String subtype;
@@ -77,12 +77,15 @@ public class AcceptType {
     }
     List<String> typeSubtype = new ArrayList<String>();
     parameters = createParameterMap();
+    
     parse(type, typeSubtype, parameters);
     this.type = typeSubtype.get(0);
     subtype = typeSubtype.get(1);
+    
     if (TypeUtil.MEDIA_TYPE_WILDCARD.equals(this.type) && !TypeUtil.MEDIA_TYPE_WILDCARD.equals(subtype))
{
       throw new IllegalArgumentException("Illegal combination of WILDCARD type with NONE
WILDCARD subtype.");
     }
+    
     final String q = parameters.get(TypeUtil.PARAMETER_Q);
     if (q == null) {
       quality = 1F;
@@ -97,6 +100,7 @@ public class AcceptType {
 
   private static void
       parse(final String format, final List<String> typeSubtype, final Map<String,
String> parameters) {
+    
     final String[] typesAndParameters = format.split(TypeUtil.PARAMETER_SEPARATOR, 2);
     final String types = typesAndParameters[0];
     final String params = (typesAndParameters.length > 1 ? typesAndParameters[1] : null);
@@ -123,23 +127,6 @@ public class AcceptType {
   }
 
   /**
-   * Creates an accept type.
-   * @param type
-   * @param subtype
-   * @param parameters
-   * @param quality
-   * @return a new <code>AcceptType</code> object
-   */
-  public static AcceptType create(final String type, final String subtype, final Map<String,
String> parameters,
-      final Float quality) {
-    return new AcceptType(type, subtype, parameters, quality);
-  }
-
-  public static AcceptType create(final ContentType contentType) {
-    return create(contentType.getType(), contentType.getSubtype(), contentType.getParameters(),
1F);
-  }
-
-  /**
    * Create an {@link AcceptType} based on given input string (<code>format</code>).
    * @param format
    * @return a new <code>AcceptType</code> object
@@ -153,23 +140,11 @@ public class AcceptType {
       result.add(new AcceptType(value.trim()));
     }
 
+    sort(result);
+    
     return result;
   }
 
-  /**
-   * Parses the given input string (<code>format</code>) and returns created
{@link AcceptType} if input was valid or
-   * return <code>NULL</code> if input was not parseable.
-   * @param format
-   * @return a new <code>ContentType</code> object
-   */
-  public static List<AcceptType> parse(final String format) {
-    try {
-      return AcceptType.create(format);
-    } catch (IllegalArgumentException e) {
-      return null;
-    }
-  }
-
   public String getType() {
     return type;
   }
@@ -193,9 +168,7 @@ public class AcceptType {
     for (final String key : parameters.keySet()) {
       result.append(';').append(key).append('=').append(parameters.get(key));
     }
-//    if (quality < 1F) {
-//      result.append(';').append(TypeUtil.PARAMETER_Q).append('=').append(quality);
-//    }
+
     return result.toString();
   }
 
@@ -238,34 +211,17 @@ public class AcceptType {
   }
 
   /**
-   * Create a list of {@link AcceptType} based on given input strings (<code>contentTypes</code>).
-   * 
-   * If one of the given strings can not be parsed an exception is thrown (hence no list
is returned with the parseable
-   * strings).
-   * @param acceptTypeStrings a list of strings
-   * @return a list of new <code>AcceptType</code> objects
-   * @throws IllegalArgumentException if one of the given input string is not parseable this
exceptions is thrown
-   */
-  public static List<AcceptType> create(final List<String> acceptTypeStrings)
{
-    List<AcceptType> acceptTypes = new ArrayList<AcceptType>(acceptTypeStrings.size());
-    for (String contentTypeString : acceptTypeStrings) {
-      acceptTypes.addAll(create(contentTypeString));
-    }
-    return acceptTypes;
-  }
-
-  /**
    * Sort given list of Accept types
    * according to their quality-parameter values and their specificity
    * as defined in RFC 7231, chapters 3.1.1.1, 5.3.1, and 5.3.2.
    * @param toSort list which is sorted and hence re-arranged
    */
-  public static void sort(final List<AcceptType> toSort) {
+  private static void sort(final List<AcceptType> toSort) {
     Collections.sort(toSort,
         new Comparator<AcceptType>() {
           @Override
           public int compare(final AcceptType a1, final AcceptType a2) {
-            int compare = a1.getQuality().compareTo(a2.getQuality());
+            int compare = a2.getQuality().compareTo(a1.getQuality());
             if (compare != 0) {
               return compare;
             }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ba514333/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java
----------------------------------------------------------------------
diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java
b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java
index b01d047..648a006 100644
--- a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java
+++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java
@@ -45,44 +45,23 @@ import java.util.TreeMap;
  */
 public class ContentType {
 
-  public static final ContentType APPLICATION_XML = create("application", "xml");
-  public static final ContentType APPLICATION_XML_CS_UTF_8 = create(APPLICATION_XML, TypeUtil.PARAMETER_CHARSET,
-      TypeUtil.CHARSET_UTF_8);
-  public static final ContentType APPLICATION_ATOM_XML = create("application", "atom+xml");
-  public static final ContentType APPLICATION_ATOM_XML_CS_UTF_8 = create(APPLICATION_ATOM_XML,
-      TypeUtil.PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8);
-  public static final ContentType APPLICATION_ATOM_XML_ENTRY =
-      create(APPLICATION_ATOM_XML, TypeUtil.PARAMETER_TYPE, "entry");
-  public static final ContentType APPLICATION_ATOM_XML_ENTRY_CS_UTF_8 = create(APPLICATION_ATOM_XML_ENTRY,
-      TypeUtil.PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8);
-  public static final ContentType APPLICATION_ATOM_XML_FEED =
-      create(APPLICATION_ATOM_XML, TypeUtil.PARAMETER_TYPE, "feed");
-  public static final ContentType APPLICATION_ATOM_XML_FEED_CS_UTF_8 = create(APPLICATION_ATOM_XML_FEED,
-      TypeUtil.PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8);
-  public static final ContentType APPLICATION_ATOM_SVC = create("application", "atomsvc+xml");
-  public static final ContentType APPLICATION_ATOM_SVC_CS_UTF_8 = create(APPLICATION_ATOM_SVC,
-
-      TypeUtil.PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8);
-  public static final ContentType APPLICATION_JSON = create("application", "json");
-  public static final ContentType APPLICATION_JSON_CS_UTF_8 = create(APPLICATION_JSON,
-      TypeUtil.PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8);
-
-  public static final ContentType APPLICATION_JSON_MIN = create("application/json;odata.metadata=minimal");
-  public static final ContentType APPLICATION_JSON_MIN_CS_UTF_8 =
-      create("application/json;odata.metadata=minimal;charset=UTF-8");
-
-  public static final ContentType APPLICATION_OCTET_STREAM = create("application", "octet-stream");
-  public static final ContentType TEXT_PLAIN = create("text", "plain");
-  public static final ContentType TEXT_PLAIN_CS_UTF_8 =
-      create(TEXT_PLAIN, TypeUtil.PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8);
-  public static final ContentType MULTIPART_MIXED = create("multipart", "mixed");
-
-  public static final ContentType APPLICATION_XHTML_XML = create("application", "xhtml+xml");
-  public static final ContentType APPLICATION_SVG_XML = create("application", "svg+xml");
-  public static final ContentType APPLICATION_FORM_URLENCODED = create("application", "x-www-form-urlencoded");
-  public static final ContentType MULTIPART_FORM_DATA = create("multipart", "form-data");
-  public static final ContentType TEXT_XML = create("text", "xml");
-  public static final ContentType TEXT_HTML = create("text", "html");
+  public static final ContentType APPLICATION_XML = create("application/xml");
+  public static final ContentType APPLICATION_ATOM_XML = create("application/atom+xml");
+  public static final ContentType APPLICATION_ATOM_XML_ENTRY = create(APPLICATION_ATOM_XML,
"type=entry");
+  public static final ContentType APPLICATION_ATOM_XML_FEED = create(APPLICATION_ATOM_XML,
"type=feed");
+  public static final ContentType APPLICATION_ATOM_SVC = create("application/atomsvc+xml");
+  public static final ContentType APPLICATION_JSON = create("application/json");
+  public static final ContentType APPLICATION_OCTET_STREAM = create("application/octet-stream");
+  public static final ContentType TEXT_PLAIN = create("text/plain");
+  public static final ContentType MULTIPART_MIXED = create("multipart/mixed");
+  public static final ContentType APPLICATION_XHTML_XML = create("application/xhtml+xml");
+  public static final ContentType APPLICATION_SVG_XML = create("application/svg+xml");
+  public static final ContentType APPLICATION_FORM_URLENCODED = create("application/x-www-form-urlencoded");
+  public static final ContentType MULTIPART_FORM_DATA = create("multipart/form-data");
+  public static final ContentType TEXT_XML = create("text/xml");
+  public static final ContentType TEXT_HTML = create("text/html");
+
+  public static final String PARAMETER_CHARSET_UTF8 = "charset=utf-8";
 
   private final String type;
   private final String subtype;
@@ -138,27 +117,37 @@ public class ContentType {
   }
 
   /**
-   * Creates a content type from type and subtype.
-   * @param type
-   * @param subtype
+   * Creates a content type from format and key-value pairs for parameters
+   * @param format for example "application/json"
+   * @param parameters for example "a=b", "c=d"
    * @return a new <code>ContentType</code> object
    */
-  public static ContentType create(final String type, final String subtype) {
-    return new ContentType(type, subtype, null);
+  public static ContentType create(final String format, final String... parameters) {
+    ContentType ct = parse(format);
+
+    for (String p : parameters) {
+      String[] keyvalue = p.split("=");
+      ct.parameters.put(keyvalue[0], keyvalue[1]);
+    }
+
+    return ct;
   }
 
   /**
-   * 
-   * @param contentType
-   * @param parameterKey
-   * @param parameterValue
+   * Creates a content type from format and key-value pairs for parameters
+   * @param format for example "application/json"
+   * @param parameters for example "a=b", "c=d"
    * @return a new <code>ContentType</code> object
    */
-  public static ContentType create(final ContentType contentType,
-      final String parameterKey, final String parameterValue) {
-    ContentType newContentType = new ContentType(contentType.type, contentType.subtype, contentType.parameters);
-    newContentType.parameters.put(parameterKey, parameterValue);
-    return newContentType;
+  public static ContentType create(final ContentType contentType, final String... parameters)
{
+    ContentType ct = new ContentType(contentType.type, contentType.subtype, contentType.parameters);
+
+    for (String p : parameters) {
+      String[] keyvalue = p.split("=");
+      ct.parameters.put(keyvalue[0], keyvalue[1]);
+    }
+
+    return ct;
   }
 
   /**
@@ -339,7 +328,7 @@ public class ContentType {
       if (other.subtype != null) {
         return false;
       }
-    } else if (!subtype.equals(other.subtype)) {
+    } else if (!subtype.equalsIgnoreCase(other.subtype)) {
       return false;
     }
 
@@ -348,7 +337,7 @@ public class ContentType {
       if (other.type != null) {
         return false;
       }
-    } else if (!type.equals(other.type)) {
+    } else if (!type.equalsIgnoreCase(other.type)) {
       return false;
     }
 

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ba514333/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ODataFormat.java
----------------------------------------------------------------------
diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ODataFormat.java
b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ODataFormat.java
index 250b949..7c67d20 100644
--- a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ODataFormat.java
+++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ODataFormat.java
@@ -62,20 +62,20 @@ public enum ODataFormat {
   static {
     final Map<ODataFormat, ContentType> v3 = new HashMap<ODataFormat, ContentType>();
     v3.put(ODataFormat.JSON_NO_METADATA, ContentType.create(
-        ContentType.APPLICATION_JSON, JSON_METADATA_PARAMETER_V3, "nometadata"));
+        ContentType.APPLICATION_JSON, JSON_METADATA_PARAMETER_V3 + "=nometadata"));
     v3.put(ODataFormat.JSON, ContentType.create(
-        ContentType.APPLICATION_JSON, JSON_METADATA_PARAMETER_V3, "minimalmetadata"));
+        ContentType.APPLICATION_JSON, JSON_METADATA_PARAMETER_V3 + "=minimalmetadata"));
     v3.put(ODataFormat.JSON_FULL_METADATA, ContentType.create(
-        ContentType.APPLICATION_JSON, JSON_METADATA_PARAMETER_V3, "fullmetadata"));
+        ContentType.APPLICATION_JSON, JSON_METADATA_PARAMETER_V3 + "=fullmetadata"));
     FORMAT_PER_VERSION.put(ODataServiceVersion.V30, v3);
 
     final Map<ODataFormat, ContentType> v4 = new HashMap<ODataFormat, ContentType>();
     v4.put(ODataFormat.JSON_NO_METADATA, ContentType.create(
-        ContentType.APPLICATION_JSON, JSON_METADATA_PARAMETER_V4, "none"));
+        ContentType.APPLICATION_JSON, JSON_METADATA_PARAMETER_V4 + "=none"));
     v4.put(ODataFormat.JSON, ContentType.create(
-        ContentType.APPLICATION_JSON, JSON_METADATA_PARAMETER_V4, "minimal"));
+        ContentType.APPLICATION_JSON, JSON_METADATA_PARAMETER_V4 + "=minimal"));
     v4.put(ODataFormat.JSON_FULL_METADATA, ContentType.create(
-        ContentType.APPLICATION_JSON, JSON_METADATA_PARAMETER_V4, "full"));
+        ContentType.APPLICATION_JSON, JSON_METADATA_PARAMETER_V4 + "=full"));
     FORMAT_PER_VERSION.put(ODataServiceVersion.V40, v4);
   }
 

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ba514333/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/TypeUtil.java
----------------------------------------------------------------------
diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/TypeUtil.java
b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/TypeUtil.java
index 373829b..8dcf0ce 100644
--- a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/TypeUtil.java
+++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/TypeUtil.java
@@ -32,10 +32,6 @@ class TypeUtil {
   static final String TYPE_SUBTYPE_SEPARATOR = "/";
   static final String TYPE_SUBTYPE_WILDCARD = "*";
 
-  static final String PARAMETER_TYPE = "type";
-  static final String PARAMETER_CHARSET = "charset";
-  static final String CHARSET_UTF_8 = "UTF-8";
- 
   /**
    * Valid input are <code>;</code> separated <code>key=value</code>
pairs
    * without spaces between key and value.

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ba514333/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/AcceptTypeTest.java
----------------------------------------------------------------------
diff --git a/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/AcceptTypeTest.java
b/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/AcceptTypeTest.java
index 935b001..20edd16 100644
--- a/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/AcceptTypeTest.java
+++ b/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/AcceptTypeTest.java
@@ -19,6 +19,7 @@
 package org.apache.olingo.commons.api.format;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
@@ -29,25 +30,79 @@ import org.junit.Test;
 public class AcceptTypeTest {
 
   @Test
-  public void testMultiValueCreate() {
-    List<AcceptType> atl = AcceptType.create("1/1,2/2 , 3/3 ");
+  public void testWildcard() {
+    List<AcceptType> atl = AcceptType.create("*/*");
+
+    assertEquals(1, atl.size());
+    assertEquals("*/*", atl.get(0).toString());
 
-    assertEquals(3, atl.size());
-    assertEquals("1/1", atl.get(0).toString());
-    assertEquals("2/2", atl.get(1).toString());
-    assertEquals("3/3", atl.get(2).toString());
+    assertTrue(atl.get(0).matches(ContentType.create("a/a")));
+    assertTrue(atl.get(0).matches(ContentType.create("b/b")));
+
+    assertEquals("*/*", AcceptType.create("*").get(0).toString());
   }
 
   @Test
-  public void testSingleValueCreate() {
-    List<AcceptType> atl = AcceptType.create(" a/a ");
+  public void testWildcardSubtype() {
+    List<AcceptType> atl = AcceptType.create("a/*");
 
     assertEquals(1, atl.size());
-    assertEquals("a/a", atl.get(0).toString());
+    assertEquals("a/*", atl.get(0).toString());
+
+    assertTrue(atl.get(0).matches(ContentType.create("a/a")));
+    assertFalse(atl.get(0).matches(ContentType.create("b/b")));
+  }
+
+  @Test
+  public void testSingleAcceptType() {
+    assertTrue(AcceptType.create("a/a").get(0).matches(ContentType.create("a/a")));
+    assertTrue(AcceptType.create("a/a;q=0.2").get(0).matches(ContentType.create("a/a")));
+    assertFalse(AcceptType.create("a/a;x=y;q=0.2").get(0).matches(ContentType.create("a/a")));
+    assertTrue(AcceptType.create("a/a;x=y;q=0.2").get(0).matches(ContentType.create("a/a;x=y")));
+    assertTrue(AcceptType.create("a/a; q=0.2").get(0).matches(ContentType.create("a/a")));
+
+    assertEquals("a/a;q=0.2;x=y", AcceptType.create("a/a;x=y;q=0.2").get(0).toString());
   }
 
   @Test
-  public void testWithQParameter() {
+  public void testAcceptTypes() {
+    List<AcceptType> atl;
+
+    atl = AcceptType.create("b/b,*/*,a/a,c/*");
+    assertNotNull(atl);
+    assertTrue(atl.get(0).matches(ContentType.create("b/b")));
+    assertTrue(atl.get(1).matches(ContentType.create("a/a")));
+    assertEquals("c", atl.get(2).getType());
+    assertEquals(TypeUtil.MEDIA_TYPE_WILDCARD, atl.get(2).getSubtype());
+    assertEquals(TypeUtil.MEDIA_TYPE_WILDCARD, atl.get(3).getType());
+    assertEquals(TypeUtil.MEDIA_TYPE_WILDCARD, atl.get(3).getSubtype());
+
+    atl = AcceptType.create("a/a;q=0.3,*/*;q=0.1,b/b;q=0.2");
+    assertNotNull(atl);
+    assertTrue(atl.get(0).matches(ContentType.create("a/a")));
+    assertTrue(atl.get(1).matches(ContentType.create("b/b")));
+    assertEquals(TypeUtil.MEDIA_TYPE_WILDCARD, atl.get(2).getType());
+    assertEquals(TypeUtil.MEDIA_TYPE_WILDCARD, atl.get(2).getSubtype());
+
+    atl = AcceptType.create("a/a;q=0.3,*/*;q=0.3");
+    assertNotNull(atl);
+    assertTrue(atl.get(0).matches(ContentType.create("a/a")));
+    assertEquals(TypeUtil.MEDIA_TYPE_WILDCARD, atl.get(1).getType());
+    assertEquals(TypeUtil.MEDIA_TYPE_WILDCARD, atl.get(1).getSubtype());
+
+    atl = AcceptType.create("a/a;x=y;q=0.1,b/b;x=y;q=0.3");
+    assertNotNull(atl);
+    assertTrue(atl.get(0).matches(ContentType.create("b/b;x=y")));
+    assertFalse(atl.get(0).matches(ContentType.create("b/b;x=z")));
+    assertTrue(atl.get(1).matches(ContentType.create("a/a;x=y")));
+    assertFalse(atl.get(1).matches(ContentType.create("a/a;x=z")));
+
+    atl = AcceptType.create("a/a; q=0.3, */*; q=0.1, b/b; q=0.2");
+    assertNotNull(atl);
+  }
+
+  @Test
+  public void testWithQParameter () {
     List<AcceptType> atl = AcceptType.create("application/json;q=0.2");
 
     assertEquals(1, atl.size());
@@ -57,27 +112,9 @@ public class AcceptTypeTest {
     assertEquals("application/json;q=0.2", atl.get(0).toString());
   }
 
-  @Test
-  public void testMatchWithQParameter() {
-    List<AcceptType> atl = AcceptType.create("application/json;q=0.2");
-    assertEquals(1, atl.size());
-    assertTrue(atl.get(0).matches(ContentType.APPLICATION_JSON));
-  }
-
   @Test(expected = IllegalArgumentException.class)
   public void testWrongQParameter() {
     AcceptType.create(" a/a;q=z ");
   }
 
-  @Test
-  public void testWildcard() {
-    List<AcceptType> atl = AcceptType.create("*; q=.2");
-
-    assertNotNull(atl);
-    assertEquals(1, atl.size());
-    assertEquals("*", atl.get(0).getType());
-    assertEquals("*", atl.get(0).getSubtype());
-    assertEquals(".2", atl.get(0).getParameters().get("q"));
-    assertEquals(new Float(0.2), atl.get(0).getQuality());
-  }
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ba514333/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/ContentTypeTest.java
----------------------------------------------------------------------
diff --git a/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/ContentTypeTest.java
b/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/ContentTypeTest.java
index e2098f6..f160b06 100644
--- a/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/ContentTypeTest.java
+++ b/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/ContentTypeTest.java
@@ -18,6 +18,75 @@
  */
 package org.apache.olingo.commons.api.format;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
 public class ContentTypeTest {
 
+  @Test
+  public void testCreate() {
+    assertEquals("a/b", ContentType.create("a/b").toContentTypeString());
+    assertEquals(ContentType.create("a/b;c=d;x=y"), ContentType.create("a/b;x=y;c=d"));
+    assertEquals(ContentType.create("a/b;c=d;x=y"), ContentType.create("a/b; x=y; c=d"));
+    assertEquals(ContentType.create("A/B"), ContentType.create("a/b"));
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testCreateFail1() {
+    ContentType.create("a");
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testCreateFail2() {
+    ContentType.create(" a / b ");
+  }
+
+  @Test
+  public void testCreateWithParameter() {
+    assertEquals(ContentType.create("a/b;c=d"), ContentType.create("a/b", "c=d"));
+    assertEquals(ContentType.create("a/b;e=f;c=d"), ContentType.create("a/b", "c=d", "e=f"));
+    assertEquals(ContentType.create("a/b;e=f;c=d"), ContentType.create("A/B", "C=D", "E=F"));
+  }
+
+  @Test
+  public void testCreateAndModify() {
+    ContentType ct1 = ContentType.create("a/b");
+    assertEquals(ContentType.create("a/b;c=d"), ContentType.create(ct1, "c=d"));
+
+    ContentType ct2 = ContentType.create("a/b;c=d");
+    assertEquals(ContentType.create("a/b;c=d;e=f"), ContentType.create(ct2, "e=f"));
+    assertEquals(ContentType.create("a/b;c=g"), ContentType.create(ct2, "c=g"));
+
+    assertFalse(ContentType.create(ct2, "c=g").equals(ct2));
+  }
+
+  @Test
+  public void testParse() {
+    assertNull(ContentType.parse("a"));
+    assertFalse(ContentType.isParseable("a"));
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testWildcardFail() {
+    ContentType.create("*/*");
+  }
+
+  @Test
+  public void testCharsetUtf8() {
+    ContentType ct1 = ContentType.create("a/b;charset=utf8");
+    ContentType ct2 = ContentType.create("a/b;charset=utf-8");
+
+    assertNotEquals(ct1, ct2);
+    assertEquals(ct1.getType(), ct2.getType());
+    assertEquals(ct1.getSubtype(), ct2.getSubtype());
+    assertEquals("utf8", ct1.getParameters().get("charset"));
+    assertEquals("utf-8", ct2.getParameters().get("charset"));
+
+    assertTrue(ct1.isCompatible(ct2));
+  }
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ba514333/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataRequest.java
----------------------------------------------------------------------
diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataRequest.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataRequest.java
index e91c657..ab6332b 100644
--- a/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataRequest.java
+++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataRequest.java
@@ -75,11 +75,20 @@ public class ODataRequest {
    * Returns header value for name where name is a case insensitive key.
    * @return the header value or null if not found
    */
-  public List<String> getHeader(final String name) {
+  public List<String> getHeaders(final String name) {
     return headers.get(name.toUpperCase());
   }
 
   /**
+   * Returns first header value for name where name is a case insensitive key.
+   * @return the header value or null if not found
+   */
+  public String getHeader(final String name) {
+    List<String> values = getHeaders(name);
+    return values != null ? values.get(0) : null;
+  }
+
+  /**
    * @return the request payload or null
    */
   public InputStream getBody() {

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ba514333/lib/server-api/src/test/java/org/apache/olingo/server/api/ODataRequestTest.java
----------------------------------------------------------------------
diff --git a/lib/server-api/src/test/java/org/apache/olingo/server/api/ODataRequestTest.java
b/lib/server-api/src/test/java/org/apache/olingo/server/api/ODataRequestTest.java
index d740731..34b497f 100644
--- a/lib/server-api/src/test/java/org/apache/olingo/server/api/ODataRequestTest.java
+++ b/lib/server-api/src/test/java/org/apache/olingo/server/api/ODataRequestTest.java
@@ -32,9 +32,13 @@ public class ODataRequestTest {
 
     r.addHeader("aa", Arrays.asList("cc"));
 
-    assertEquals("cc", r.getHeader("aa").get(0));
-    assertEquals("cc", r.getHeader("aA").get(0));
-    assertEquals("cc", r.getHeader("AA").get(0));
+    assertEquals("cc", r.getHeaders("aa").get(0));
+    assertEquals("cc", r.getHeaders("aA").get(0));
+    assertEquals("cc", r.getHeaders("AA").get(0));
+
+    assertEquals("cc", r.getHeader("aa"));
+    assertEquals("cc", r.getHeader("aA"));
+    assertEquals("cc", r.getHeader("AA"));
 
   }
 
@@ -43,9 +47,9 @@ public class ODataRequestTest {
     ODataRequest r = new ODataRequest();
     r.addHeader("AA", Arrays.asList("dd"));
 
-    assertEquals("dd", r.getHeader("aa").get(0));
-    assertEquals("dd", r.getHeader("aA").get(0));
-    assertEquals("dd", r.getHeader("AA").get(0));
+    assertEquals("dd", r.getHeaders("aa").get(0));
+    assertEquals("dd", r.getHeaders("aA").get(0));
+    assertEquals("dd", r.getHeaders("AA").get(0));
   }
 
   @Test
@@ -54,13 +58,13 @@ public class ODataRequestTest {
 
     r.addHeader("aa", Arrays.asList("a", "b"));
 
-    assertEquals("a", r.getHeader("aa").get(0));
-    assertEquals("b", r.getHeader("aA").get(1));
+    assertEquals("a", r.getHeaders("aa").get(0));
+    assertEquals("b", r.getHeaders("aA").get(1));
 
     r.addHeader("Aa", Arrays.asList("c"));
 
-    assertEquals("a", r.getHeader("aa").get(0));
-    assertEquals("b", r.getHeader("aA").get(1));
-    assertEquals("c", r.getHeader("aA").get(2));
+    assertEquals("a", r.getHeaders("aa").get(0));
+    assertEquals("b", r.getHeaders("aA").get(1));
+    assertEquals("c", r.getHeaders("aA").get(2));
   }
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ba514333/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java
b/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java
index 20348ab..ae932c3 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java
@@ -21,6 +21,7 @@ package org.apache.olingo.server.core;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.olingo.commons.api.ODataRuntimeException;
 import org.apache.olingo.commons.api.format.AcceptType;
 import org.apache.olingo.commons.api.format.ContentType;
 import org.apache.olingo.commons.api.http.HttpHeader;
@@ -76,14 +77,14 @@ public class ContentNegotiator {
 
     List<FormatContentTypeMapping> supportedContentTypes = getSupportedContentTypes(processor,
processorClass);
 
-    List<String> acceptHeaderValues = request.getHeader(HttpHeader.ACCEPT);
+    String acceptHeaderValue = request.getHeader(HttpHeader.ACCEPT);
 
     boolean supported = false;
 
     if (formatOption != null) {
 
       if ("json".equalsIgnoreCase(formatOption.getText().trim())) {
-        requestedContentType = ContentType.APPLICATION_JSON_MIN;
+        requestedContentType = ContentType.create(ContentType.APPLICATION_JSON, "odata.metadata=minimal");
         for (FormatContentTypeMapping entry : supportedContentTypes) {
           if (requestedContentType.isCompatible(ContentType.create(entry.getContentType().trim())))
{
             supported = true;
@@ -107,13 +108,8 @@ public class ContentNegotiator {
           }
         }
       }
-    } else if (acceptHeaderValues != null) {
-      List<AcceptType> acceptedContentTypes = new ArrayList<AcceptType>();
-
-      for (String acceptHeaderValue : acceptHeaderValues) {
-        acceptedContentTypes.addAll((AcceptType.create(acceptHeaderValue)));
-      }
-      AcceptType.sort(acceptedContentTypes);
+    } else if (acceptHeaderValue != null) {
+      List<AcceptType> acceptedContentTypes = AcceptType.create(acceptHeaderValue);
 
       for (AcceptType acceptedType : acceptedContentTypes) {
         for (FormatContentTypeMapping supportedType : supportedContentTypes) {
@@ -122,7 +118,9 @@ public class ContentNegotiator {
           if (acceptedType.getParameters().containsKey("charset")) {
             String value = acceptedType.getParameters().get("charset");
             if ("utf8".equalsIgnoreCase(value) || "utf-8".equalsIgnoreCase(value)) {
-              ct = ContentType.create(ct, "charset", "UTF-8");
+              ct = ContentType.create(ct, ContentType.PARAMETER_CHARSET_UTF8);
+            } else {
+              throw new ODataRuntimeException("charset in accept header not supported: "
+ acceptHeaderValue);
             }
           }
 
@@ -146,7 +144,7 @@ public class ContentNegotiator {
       if (processorClass == MetadataProcessor.class) {
         requestedContentType = ContentType.APPLICATION_XML;
       } else {
-        requestedContentType = ContentType.APPLICATION_JSON_MIN;
+        requestedContentType = ContentType.create(ContentType.APPLICATION_JSON, "odata.metadata=minimal");
       }
 
       for (FormatContentTypeMapping entry : supportedContentTypes) {

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ba514333/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java
b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java
index b8f7bbd..2775e14 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java
@@ -177,11 +177,11 @@ public class ODataHandler {
   }
 
   private void validateODataVersion(final ODataRequest request, final ODataResponse response)
{
-    List<String> maxVersionHeader = request.getHeader(HttpHeader.ODATA_MAX_VERSION);
+    String maxVersion = request.getHeader(HttpHeader.ODATA_MAX_VERSION);
 
-    if (maxVersionHeader != null && maxVersionHeader.size() > 0) {
-      if (ODataServiceVersion.isBiggerThan(ODataServiceVersion.V40.toString(), maxVersionHeader.get(0)))
{
-        throw new ODataRuntimeException("400 Bad Request - ODataVersion not supported: "
+ maxVersionHeader.get(0));
+    if (maxVersion != null) {
+      if (ODataServiceVersion.isBiggerThan(ODataServiceVersion.V40.toString(), maxVersion))
{
+        throw new ODataRuntimeException("400 Bad Request - ODataVersion not supported: "
+ maxVersion);
       }
     }
 

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ba514333/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java
b/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java
index e189f79..74ea57c 100644
--- a/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java
@@ -167,7 +167,7 @@ public class ContentNegotiatorTest {
     ContentType requestedContentType = ContentNegotiator.doContentNegotiation(fo, request,
p, processorClass);
 
     assertNotNull(requestedContentType);
-    assertEquals(useCase[0], requestedContentType.toContentTypeString());
+    assertEquals(ContentType.create(useCase[0]), requestedContentType);
   }
 
   private List<FormatContentTypeMapping> createCustomContentTypeMapping(final String
formatString,


Mime
View raw message