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 180583c9b..379eb9555 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,9 +45,6 @@ import java.util.TreeMap; */ public class AcceptType { -// 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; private final Map parameters; @@ -98,8 +95,8 @@ public class AcceptType { } } - private static void - parse(final String format, final List typeSubtype, final Map parameters) { + private static void parse(final String format, final List typeSubtype, + final Map parameters) { final String[] typesAndParameters = format.split(TypeUtil.PARAMETER_SEPARATOR, 2); final String types = typesAndParameters[0]; @@ -127,9 +124,9 @@ public class AcceptType { } /** - * Create an {@link AcceptType} based on given input string (format). - * @param format - * @return a new AcceptType object + * Create a list of {@link AcceptType} objects based on given input string (format). + * @param format accept types, comma-separated, as specified for the HTTP header Accept + * @return a list of AcceptType objects * @throws IllegalArgumentException if input string is not parseable */ public static List create(final String format) { @@ -216,7 +213,7 @@ public class AcceptType { * 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 */ - private static void sort(final List toSort) { + private static void sort(List toSort) { Collections.sort(toSort, new Comparator() { @Override 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 5fa44ce67..20620fcc1 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 @@ -302,22 +302,15 @@ public final class ContentType { } /** - * {@link ContentType}s are compatible - *
    - *
  • if type, subtype have the same value.
  • - *
  • if type and/or subtype is set to "*"
  • - *
- * The set parameters are always ignored (for compare with parameters see {@link #equals(Object)} - * ). - * + *

{@link ContentType}s are compatible + * if type and subtype have the same value.

+ *

The set parameters are always ignored + * (for compare with parameters see {@link #equals(Object)}).

* @return true if both instances are equal (see definition above), otherwise false. */ public boolean isCompatible(final ContentType obj) { - Boolean compatible = isEqualWithoutParameters(obj); - if (compatible == null) { - return true; - } - return compatible.booleanValue(); + final Boolean compatible = isEqualWithoutParameters(obj); + return compatible == null || compatible.booleanValue(); } /** diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CustomContentTypeSupportProcessor.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CustomContentTypeSupportProcessor.java index edcad7290..0d94a522b 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CustomContentTypeSupportProcessor.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CustomContentTypeSupportProcessor.java @@ -20,6 +20,8 @@ package org.apache.olingo.server.api.processor; import java.util.List; +import org.apache.olingo.commons.api.format.ContentType; + /** * A processor which supports custom content types can implement this interface. The processor can also remove default * content types if the default serializer of Olingo are not used. By default this interface is not implemented and @@ -35,7 +37,7 @@ public interface CustomContentTypeSupportProcessor { * @return modified list of supported content types * */ - public List modifySupportedContentTypes( - List defaultContentTypes, Class processorClass); + public List modifySupportedContentTypes( + List defaultContentTypes, Class processorClass); } diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/FormatContentTypeMapping.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/FormatContentTypeMapping.java deleted file mode 100644 index 38ce7d631..000000000 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/FormatContentTypeMapping.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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.olingo.server.api.processor; - -/** - * Mapping between an uri $format option value and a content types. For instance the $format option "xml" maps to - * content type "application/xml". - */ -public class FormatContentTypeMapping { - - private String formatOptionValue; - private String contentTypeValue; - - public FormatContentTypeMapping(final String formatOptionValue, final String contentTypeValue) { - super(); - this.formatOptionValue = formatOptionValue; - this.contentTypeValue = contentTypeValue; - } - - public String getFormatAlias() { - return formatOptionValue; - } - - public String getContentType() { - return contentTypeValue; - } - - public void setFormatAlias(final String formatOptionValue) { - this.formatOptionValue = formatOptionValue; - } - - public void setContentType(final String contentTypeValue) { - this.contentTypeValue = contentTypeValue; - } - - @Override - public String toString() { - return "('" + formatOptionValue + "', '" + contentTypeValue + "')"; - } - -} \ No newline at end of file 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 e7624642b..1adabf70d 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 @@ -28,7 +28,6 @@ import org.apache.olingo.commons.api.format.ODataFormat; import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.processor.CustomContentTypeSupportProcessor; -import org.apache.olingo.server.api.processor.FormatContentTypeMapping; import org.apache.olingo.server.api.processor.MetadataProcessor; import org.apache.olingo.server.api.processor.Processor; import org.apache.olingo.server.api.uri.queryoption.FormatOption; @@ -37,29 +36,28 @@ public class ContentNegotiator { private ContentNegotiator() {} - private static List + private static List getDefaultSupportedContentTypes(final Class processorClass) { - List defaults = new ArrayList(); + List defaults = new ArrayList(); if (processorClass == MetadataProcessor.class) { - defaults.add(new FormatContentTypeMapping("xml", ContentType.APPLICATION_XML.toContentTypeString())); + defaults.add(ODataFormat.XML.getContentType(ODataServiceVersion.V40)); } else { - defaults.add(new FormatContentTypeMapping("json", - ODataFormat.JSON.getContentType(ODataServiceVersion.V40).toContentTypeString())); + defaults.add(ODataFormat.JSON.getContentType(ODataServiceVersion.V40)); + defaults.add(ODataFormat.JSON_NO_METADATA.getContentType(ODataServiceVersion.V40)); } return defaults; } - private static List getSupportedContentTypes(final Processor processor, + private static List getSupportedContentTypes(final Processor processor, final Class processorClass) { - List supportedContentTypes = getDefaultSupportedContentTypes(processorClass); + List supportedContentTypes = getDefaultSupportedContentTypes(processorClass); if (processor instanceof CustomContentTypeSupportProcessor) { - supportedContentTypes = - ((CustomContentTypeSupportProcessor) processor).modifySupportedContentTypes(supportedContentTypes, - processorClass); + supportedContentTypes = ((CustomContentTypeSupportProcessor) processor) + .modifySupportedContentTypes(supportedContentTypes, processorClass); } return supportedContentTypes; @@ -67,98 +65,73 @@ public class ContentNegotiator { public static ContentType doContentNegotiation(final FormatOption formatOption, final ODataRequest request, final Processor processor, final Class processorClass) throws ContentNegotiatorException { - ContentType requestedContentType = null; + final List supportedContentTypes = getSupportedContentTypes(processor, processorClass); + final String acceptHeaderValue = request.getHeader(HttpHeader.ACCEPT); + ContentType result = null; - List supportedContentTypes = getSupportedContentTypes(processor, processorClass); - - String acceptHeaderValue = request.getHeader(HttpHeader.ACCEPT); - - boolean supported = false; - - if (formatOption != null) { - if ("json".equalsIgnoreCase(formatOption.getText().trim())) { - requestedContentType = ODataFormat.JSON.getContentType(ODataServiceVersion.V40); - for (FormatContentTypeMapping entry : supportedContentTypes) { - if (requestedContentType.isCompatible(ContentType.create(entry.getContentType().trim()))) { - supported = true; - break; - } - } - } else if ("xml".equalsIgnoreCase(formatOption.getText().trim())) { - requestedContentType = ContentType.APPLICATION_XML; - for (FormatContentTypeMapping entry : supportedContentTypes) { - if (requestedContentType.isCompatible(ContentType.create(entry.getContentType().trim()))) { - supported = true; - break; - } - } - } else { - for (FormatContentTypeMapping entry : supportedContentTypes) { - if (formatOption.getText().equalsIgnoreCase(entry.getFormatAlias().trim())) { - requestedContentType = ContentType.create(entry.getContentType().trim()); - supported = true; - break; - } - } - } - if (!supported) { - throw new ContentNegotiatorException("Unsupported $format = " + formatOption.getText(), - ContentNegotiatorException.MessageKeys.UNSUPPORTED_FORMAT_OPTION, formatOption.getText()); + if (formatOption != null && formatOption.getFormat() != null) { + final String formatString = formatOption.getFormat().trim(); + final ODataFormat format = + ODataFormat.JSON.name().equalsIgnoreCase(formatString) ? ODataFormat.JSON : + ODataFormat.XML.name().equalsIgnoreCase(formatString) ? ODataFormat.XML : + ODataFormat.ATOM.name().equalsIgnoreCase(formatString) ? ODataFormat.ATOM : null; + result = getSupportedContentType(format == null ? + ContentType.create(formatOption.getFormat()) : format.getContentType(ODataServiceVersion.V40), + supportedContentTypes); + if (result == null) { + throw new ContentNegotiatorException("Unsupported $format = " + formatString, + ContentNegotiatorException.MessageKeys.UNSUPPORTED_FORMAT_OPTION, formatString); } } else if (acceptHeaderValue != null) { - List acceptedContentTypes = AcceptType.create(acceptHeaderValue); - + final List acceptedContentTypes = AcceptType.create(acceptHeaderValue); for (AcceptType acceptedType : acceptedContentTypes) { - for (FormatContentTypeMapping supportedType : supportedContentTypes) { - - ContentType ct = ContentType.create(supportedType.getContentType()); + for (final ContentType supportedContentType : supportedContentTypes) { + ContentType contentType = supportedContentType; if (acceptedType.getParameters().containsKey("charset")) { - String value = acceptedType.getParameters().get("charset"); + final String value = acceptedType.getParameters().get("charset"); if ("utf8".equalsIgnoreCase(value) || "utf-8".equalsIgnoreCase(value)) { - ct = ContentType.create(ct, ContentType.PARAMETER_CHARSET_UTF8); + contentType = ContentType.create(contentType, ContentType.PARAMETER_CHARSET_UTF8); } else { throw new ContentNegotiatorException("charset in accept header not supported: " + acceptHeaderValue, ContentNegotiatorException.MessageKeys.WRONG_CHARSET_IN_HEADER, HttpHeader.ACCEPT, acceptHeaderValue); } } - - if (acceptedType.matches(ct)) { - requestedContentType = ct; - supported = true; + if (acceptedType.matches(contentType)) { + result = contentType; break; } } - if (supported) { + if (result != null) { break; } } - - if (!supported) { + if (result == null) { throw new ContentNegotiatorException( "unsupported accept content type: " + acceptedContentTypes + " != " + supportedContentTypes, ContentNegotiatorException.MessageKeys.UNSUPPORTED_CONTENT_TYPES, acceptedContentTypes.toString()); } } else { - if (processorClass == MetadataProcessor.class) { - requestedContentType = ContentType.APPLICATION_XML; - } else { - requestedContentType = ODataFormat.JSON.getContentType(ODataServiceVersion.V40); - } - - for (FormatContentTypeMapping entry : supportedContentTypes) { - if (requestedContentType.isCompatible(ContentType.create(entry.getContentType().trim()))) { - supported = true; - break; - } + final ContentType requestedContentType = processorClass == MetadataProcessor.class ? + ODataFormat.XML.getContentType(ODataServiceVersion.V40) : + ODataFormat.JSON.getContentType(ODataServiceVersion.V40); + result = getSupportedContentType(requestedContentType, supportedContentTypes); + if (result == null) { + throw new ContentNegotiatorException( + "unsupported accept content type: " + requestedContentType + " != " + supportedContentTypes, + ContentNegotiatorException.MessageKeys.UNSUPPORTED_CONTENT_TYPE, + requestedContentType.toContentTypeString()); } } + return result; + } - if (!supported) { - throw new ContentNegotiatorException( - "unsupported accept content type: " + requestedContentType + " != " + supportedContentTypes, - ContentNegotiatorException.MessageKeys.UNSUPPORTED_CONTENT_TYPE, requestedContentType.toContentTypeString()); + private static ContentType getSupportedContentType(final ContentType requestedContentType, + final List supportedContentTypes) { + for (final ContentType supportedContentType : supportedContentTypes) { + if (requestedContentType.isCompatible(supportedContentType)) { + return supportedContentType; + } } - - return requestedContentType; + return null; } } 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 f858aa0e0..61d0bc85e 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 @@ -20,7 +20,6 @@ package org.apache.olingo.server.core; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -36,9 +35,8 @@ import org.apache.olingo.commons.api.http.HttpMethod; import org.apache.olingo.server.api.OData; import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.ODataResponse; -import org.apache.olingo.server.api.processor.EntityCollectionProcessor; import org.apache.olingo.server.api.processor.CustomContentTypeSupportProcessor; -import org.apache.olingo.server.api.processor.FormatContentTypeMapping; +import org.apache.olingo.server.api.processor.EntityCollectionProcessor; import org.apache.olingo.server.api.processor.MetadataProcessor; import org.apache.olingo.server.api.processor.Processor; import org.apache.olingo.server.api.processor.ServiceDocumentProcessor; @@ -60,49 +58,49 @@ public class ContentNegotiatorTest { //CHECKSTYLE:OFF (Maven checkstyle) String[][] casesServiceDocument = { - /* expected $format accept alias ct mapping */ - { ACCEPT_CASE_MIN, null, null, null ,null }, - { ACCEPT_CASE_MIN, "json", null, null ,null }, - { ACCEPT_CASE_MIN, "json", ACCEPT_CASE_JSONQ, null ,null }, - { "a/a", "a", null, "a" ,"a/a" }, - { ACCEPT_CASE_MIN, null, ACCEPT_CASE_JSONQ, null ,null }, - { ACCEPT_CASE_MIN, null, ACCEPT_CASE_WILDCARD1, null ,null }, - { ACCEPT_CASE_MIN, null, ACCEPT_CASE_WILDCARD2, null ,null }, - { "a/a", "a", null, "a, b" ,"a/a,b/b" }, - { "a/a", " a ", null, " a , b" ," a/a , b/b " }, - { "a/a;x=y", "a", ACCEPT_CASE_WILDCARD1, "a" ,"a/a;x=y" }, - { ACCEPT_CASE_MIN, "json", ACCEPT_CASE_MIN, null ,null }, - { ACCEPT_CASE_FULL, null, ACCEPT_CASE_FULL, "dummy" ,ACCEPT_CASE_FULL }, - { ACCEPT_CASE_MIN_UTF8, null, ACCEPT_CASE_MIN_UTF8, null ,null }, + /* expected $format accept additional content types */ + { ACCEPT_CASE_MIN, null, null, null }, + { ACCEPT_CASE_MIN, "json", null, null }, + { ACCEPT_CASE_MIN, "json", ACCEPT_CASE_JSONQ, null }, + { "a/a", "a/a", null, "a/a" }, + { ACCEPT_CASE_MIN, null, ACCEPT_CASE_JSONQ, null }, + { ACCEPT_CASE_MIN, null, ACCEPT_CASE_WILDCARD1, null }, + { ACCEPT_CASE_MIN, null, ACCEPT_CASE_WILDCARD2, null }, + { "a/a", "a/a", null, "a/a,b/b" }, + { "a/a", " a/a ", null, " a/a , b/b " }, + { "a/a;x=y", "a/a", ACCEPT_CASE_WILDCARD1, "a/a;x=y" }, + { ACCEPT_CASE_MIN, "json", ACCEPT_CASE_MIN, null }, + { ACCEPT_CASE_FULL, null, ACCEPT_CASE_FULL, ACCEPT_CASE_FULL }, + { ACCEPT_CASE_MIN_UTF8, null, ACCEPT_CASE_MIN_UTF8, null }, }; String[][] casesMetadata = { - /* expected $format accept alias ct mapping */ - { "application/xml", null, null, null ,null }, - { "application/xml", "xml", null, null ,null }, - { "application/xml", "xml", ACCEPT_CASE_XML, null ,null }, - { "a/a", "a", null, "a" ,"a/a" }, - { "application/xml", null, ACCEPT_CASE_XML, null ,null }, - { "application/xml", null, ACCEPT_CASE_WILDCARD1, null ,null }, - { "application/xml", null, ACCEPT_CASE_WILDCARD2, null ,null }, - { "a/a", "a", null, "a, b" ,"a/a,b/b" }, - { "a/a", " a ", null, " a , b" ," a/a , b/b " }, - { "a/a;x=y", "a", ACCEPT_CASE_WILDCARD1, "a" ,"a/a;x=y" }, + /* expected $format accept additional content types */ + { ACCEPT_CASE_XML, null, null, null }, + { ACCEPT_CASE_XML, "xml", null, null }, + { ACCEPT_CASE_XML, "xml", ACCEPT_CASE_XML, null }, + { "a/a", "a/a", null, "a/a" }, + { ACCEPT_CASE_XML, null, ACCEPT_CASE_XML, null }, + { ACCEPT_CASE_XML, null, ACCEPT_CASE_WILDCARD1, null }, + { ACCEPT_CASE_XML, null, ACCEPT_CASE_WILDCARD2, null }, + { "a/a", "a/a", null, "a/a,b/b" }, + { "a/a", " a/a ", null, " a/a , b/b " }, + { "a/a;x=y", "a/a", ACCEPT_CASE_WILDCARD1, "a/a;x=y" }, }; String[][] casesFail = { - /* expected $format accept alias ct mapping */ - { "application/xml", "xxx", null, null ,null }, - { "a/a", "a", null, "b" ,"b/b" }, - { "application/xml", null, ACCEPT_CASE_JSONQ, null ,null }, - { "application/json", null, ACCEPT_CASE_FULL, null ,null }, // not jet supported + /* expected $format accept additional content types */ + { ACCEPT_CASE_XML, "xxx/yyy", null, null }, + { "a/a", "a/a", null, "b/b" }, + { ACCEPT_CASE_XML, null, ACCEPT_CASE_JSONQ, null }, + { "application/json", null, ACCEPT_CASE_FULL, null }, // not yet supported }; //CHECKSTYLE:ON //@formatter:on @Test public void testServiceDocumentSingleCase() throws Exception { - String[] useCase = { ACCEPT_CASE_MIN_UTF8, null, ACCEPT_CASE_MIN_UTF8, null, null }; + String[] useCase = { ACCEPT_CASE_MIN_UTF8, null, ACCEPT_CASE_MIN_UTF8, null }; testContentNegotiation(useCase, ServiceDocumentProcessor.class); } @@ -116,7 +114,7 @@ public class ContentNegotiatorTest { @Test public void testMetadataSingleCase() throws Exception { - String[] useCase = { "application/xml", null, null, null, null }; + String[] useCase = { ACCEPT_CASE_XML, null, null, null }; testContentNegotiation(useCase, MetadataProcessor.class); } @@ -129,86 +127,77 @@ public class ContentNegotiatorTest { } @Test - public void testMetadataFail() { + public void testMetadataFail() throws Exception { for (String[] useCase : casesFail) { try { testContentNegotiation(useCase, MetadataProcessor.class); - fail("Exeption expected!"); - } catch (ContentNegotiatorException e) { - - }catch (Exception e) { - e.printStackTrace(); - fail("Wrong Exception: " + e.getClass().getName()); - } + fail("Exception expected!"); + } catch (final ContentNegotiatorException e) {} } } - public void testContentNegotiation(final String[] useCase, final Class processorClass) - throws Exception { + private void testContentNegotiation(final String[] useCase, final Class processorClass) + throws ContentNegotiatorException { ODataRequest request = new ODataRequest(); request.setMethod(HttpMethod.GET); request.setRawODataPath("/" + (useCase[1] == null ? "" : "?$format=" + useCase[1])); - ProcessorStub p = new ProcessorStub(createCustomContentTypeMapping(useCase[3], useCase[4])); + ProcessorStub p = new ProcessorStub(createCustomContentTypes(useCase[3])); FormatOption fo = null; if (useCase[1] != null) { fo = mock(FormatOption.class); - when(fo.getText()).thenReturn(useCase[1].trim()); + when(fo.getFormat()).thenReturn(useCase[1].trim()); } if (useCase[2] != null) { request.addHeader(HttpHeader.ACCEPT, Arrays.asList(useCase[2])); } - ContentType requestedContentType = ContentNegotiator.doContentNegotiation(fo, request, p, processorClass); + final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(fo, request, p, processorClass); assertNotNull(requestedContentType); assertEquals(ContentType.create(useCase[0]), requestedContentType); } - private List createCustomContentTypeMapping(final String formatString, - final String contentTypeString) { - List map = null; + private List createCustomContentTypes(final String contentTypeString) { - assertTrue(!(formatString == null ^ contentTypeString == null)); - - if (formatString != null) { - String[] formats = formatString.split(","); - String[] contentTypes = contentTypeString.split(","); - - assertEquals(formats.length, contentTypes.length); - - map = new ArrayList(); - for (int i = 0; i < formats.length; i++) { - map.add(new FormatContentTypeMapping(formats[i], contentTypes[i])); - } + if (contentTypeString == null) { + return null; } - return map; + String[] contentTypes = contentTypeString.split(","); + + List types = new ArrayList(); + for (int i = 0; i < contentTypes.length; i++) { + types.add(ContentType.create(contentTypes[i].trim())); + } + + return types; } private class ProcessorStub implements ServiceDocumentProcessor, MetadataProcessor, - EntityCollectionProcessor, - CustomContentTypeSupportProcessor { + EntityCollectionProcessor, CustomContentTypeSupportProcessor { - List customMapping; + List customTypes; - ProcessorStub(final List mapping) { - customMapping = mapping; + ProcessorStub(final List types) { + customTypes = types; } @Override public void init(final OData odata, final Edm edm) {} @Override - public List modifySupportedContentTypes( - final List supportedContentTypes, + public List modifySupportedContentTypes(final List supportedContentTypes, final Class processorClass) { - if (customMapping != null) { - supportedContentTypes.addAll(customMapping); + if (customTypes == null) { + return supportedContentTypes; + } else { + List modifiedTypes = new ArrayList(supportedContentTypes); + modifiedTypes.addAll(customTypes); + return modifiedTypes; } - return supportedContentTypes; } @Override diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java index 7e55ab88b..3ac720852 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java @@ -78,7 +78,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(500, response.getStatusCode()); + assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode()); } @Test @@ -92,7 +92,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(200, response.getStatusCode()); + assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode()); String ct = response.getHeaders().get(HttpHeader.CONTENT_TYPE); assertTrue(ct.contains("application/json")); @@ -134,7 +134,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(500, response.getStatusCode()); + assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode()); } @Test @@ -147,7 +147,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(200, response.getStatusCode()); + assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode()); assertEquals(HttpContentType.APPLICATION_XML, response.getHeaders().get(HttpHeader.CONTENT_TYPE)); assertNotNull(response.getContent()); @@ -195,7 +195,7 @@ public class ODataHandlerTest { assertNotNull(response); assertEquals(ODataServiceVersion.V40.toString(), response.getHeaders().get(HttpHeader.ODATA_VERSION)); - assertEquals(400, response.getStatusCode()); + assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), response.getStatusCode()); } @Test @@ -208,7 +208,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(200, response.getStatusCode()); + assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode()); } @Test @@ -217,11 +217,11 @@ public class ODataHandlerTest { request.setMethod(HttpMethod.GET); request.setRawODataPath("$metadata"); - request.setRawQueryPath("$format=notSupported"); + request.setRawQueryPath("$format=not/Supported"); ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(406, response.getStatusCode()); + assertEquals(HttpStatusCode.NOT_ACCEPTABLE.getStatusCode(), response.getStatusCode()); } @Test @@ -233,7 +233,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(501, response.getStatusCode()); + assertEquals(HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), response.getStatusCode()); } @Test @@ -265,7 +265,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(404, response.getStatusCode()); + assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode()); } //TODO: Use this test @@ -279,7 +279,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(404, response.getStatusCode()); + assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode()); } @Test @@ -301,7 +301,7 @@ public class ODataHandlerTest { ODataResponse response = localHandler.process(request); assertNotNull(response); - assertEquals(500, response.getStatusCode()); + assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode()); // TODO: Check for message in case of EdmException // System.out.println(IOUtils.toString(response.getContent())); }