Spec mandates to store null when no @Consumes or @Produces annotation is present
Project: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/repo Commit: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/commit/4d04b13b Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/4d04b13b Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/4d04b13b Branch: refs/heads/master Commit: 4d04b13b37f8c866c16f0a171f0f16a5158e768d Parents: 2ac4c23 Author: Carlos Sierra <[email protected]> Authored: Thu Feb 15 15:28:10 2018 +0100 Committer: Carlos Sierra <[email protected]> Committed: Thu Feb 15 15:32:15 2018 +0100 ---------------------------------------------------------------------- jax-rs.itests/src/main/java/test/JaxrsTest.java | 12 ++-- .../introspection/ClassIntrospector.java | 67 ++++++++++++++++---- .../introspection/ClassIntrospectorTest.java | 41 ++++++------ .../types/ResourceMethodInfoDTOWrapper.java | 14 ++-- 4 files changed, 85 insertions(+), 49 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/4d04b13b/jax-rs.itests/src/main/java/test/JaxrsTest.java ---------------------------------------------------------------------- diff --git a/jax-rs.itests/src/main/java/test/JaxrsTest.java b/jax-rs.itests/src/main/java/test/JaxrsTest.java index da66980..3d70015 100644 --- a/jax-rs.itests/src/main/java/test/JaxrsTest.java +++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java @@ -140,8 +140,7 @@ public class JaxrsTest extends TestHelper { ResourceMethodInfoDTO resourceMethod = resourceMethods[0]; assertEquals(HttpMethod.GET, resourceMethod.method); assertEquals("/", resourceMethod.path); - assertArrayEquals( - new String[]{MediaType.WILDCARD}, resourceMethod.consumingMimeType); + assertNull(resourceMethod.consumingMimeType); assertArrayEquals( new String[]{MediaType.TEXT_PLAIN}, resourceMethod.producingMimeType); @@ -236,8 +235,7 @@ public class JaxrsTest extends TestHelper { ResourceMethodInfoDTO resourceMethod = resourceMethods[0]; assertEquals(HttpMethod.GET, resourceMethod.method); assertEquals("/", resourceMethod.path); - assertArrayEquals( - new String[]{MediaType.WILDCARD}, resourceMethod.consumingMimeType); + assertNull(resourceMethod.consumingMimeType); assertArrayEquals( new String[]{MediaType.TEXT_PLAIN}, resourceMethod.producingMimeType); @@ -266,10 +264,8 @@ public class JaxrsTest extends TestHelper { ResourceMethodInfoDTO resourceMethod = resourceDTO.resourceMethods[0]; assertEquals(HttpMethod.GET, resourceMethod.method); assertEquals("/{name}", resourceMethod.path); - assertArrayEquals( - new String[]{MediaType.WILDCARD}, resourceMethod.consumingMimeType); - assertArrayEquals( - new String[]{MediaType.WILDCARD}, resourceMethod.producingMimeType); + assertNull(resourceMethod.consumingMimeType); + assertNull(resourceMethod.producingMimeType); assertArrayEquals(new String[]{}, resourceMethod.nameBindings); } http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/4d04b13b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospector.java ---------------------------------------------------------------------- diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospector.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospector.java index e596b30..968eaef 100644 --- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospector.java +++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospector.java @@ -21,14 +21,19 @@ import org.apache.cxf.Bus; import org.apache.cxf.jaxrs.model.ClassResourceInfo; import org.apache.cxf.jaxrs.model.MethodDispatcher; import org.apache.cxf.jaxrs.model.OperationResourceInfo; +import org.apache.cxf.jaxrs.utils.AnnotationUtils; import org.apache.cxf.jaxrs.utils.JAXRSUtils; import org.apache.cxf.jaxrs.utils.ResourceUtils; import org.osgi.service.jaxrs.runtime.dto.ResourceMethodInfoDTO; +import javax.ws.rs.Consumes; import javax.ws.rs.Path; +import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; +import java.lang.reflect.Method; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -50,8 +55,8 @@ public class ClassIntrospector { clazz, clazz, true, true, bus); Stream<ResourceMethodInfoDTO> convert = convert( - new HashSet<>(), "/", null, _ALL_TYPES_LIST, _ALL_TYPES_LIST, - Collections.emptySet(), true, classResourceInfo); + new HashSet<>(), "/", null, null, null, Collections.emptySet(), + true, classResourceInfo); return convert.collect(Collectors.toList()); } @@ -68,13 +73,17 @@ public class ClassIntrospector { String path = parentPath + getPathSafe(classResourceInfo); - List<MediaType> consumeMime = classResourceInfo.getConsumeMime(); - if (consumeMime.equals(_ALL_TYPES_LIST)) { + List<MediaType> consumeMime = getConsumesInfo( + classResourceInfo.getResourceClass()); + + if (consumeMime == null) { consumeMime = defaultConsumeTypes; } - List<MediaType> produceMime = classResourceInfo.getProduceMime(); - if (consumeMime.equals(_ALL_TYPES_LIST)) { + List<MediaType> produceMime = getProducesInfo( + classResourceInfo.getResourceClass()); + + if (produceMime == null) { produceMime = defaultProduceTypes; } @@ -89,8 +98,8 @@ public class ClassIntrospector { Set<OperationResourceInfo> operationResourceInfos = methodDispatcher.getOperationResourceInfos(); - ArrayList<MediaType> consumeParam = new ArrayList<>(consumeMime); - ArrayList<MediaType> produceParam = new ArrayList<>(produceMime); + List<MediaType> consumeParam = consumeMime == null ? null : new ArrayList<>(consumeMime); + List<MediaType> produceParam = produceMime == null ? null : new ArrayList<>(produceMime); HashSet<String> nameBindingsParam = new HashSet<>(nameBindings); Stream<ResourceMethodInfoDTO> stream = @@ -116,12 +125,16 @@ public class ClassIntrospector { Set<String> defaultNameBindings, boolean recurse, OperationResourceInfo operationResourceInfo) { - List<MediaType> consumeTypes = operationResourceInfo.getConsumeTypes(); + List<MediaType> consumeTypes = getConsumesInfo( + operationResourceInfo.getAnnotatedMethod()); + if (consumeTypes == null) { consumeTypes = defaultConsumeTypes; } - List<MediaType> produceTypes = operationResourceInfo.getProduceTypes(); + List<MediaType> produceTypes = getProducesInfo( + operationResourceInfo.getAnnotatedMethod()); + if (produceTypes == null) { produceTypes = defaultProduceTypes; } @@ -166,14 +179,16 @@ public class ClassIntrospector { ResourceMethodInfoDTO resourceMethodInfoDTO = new ResourceMethodInfoDTO(); - resourceMethodInfoDTO.consumingMimeType = consumeTypes.stream(). + resourceMethodInfoDTO.consumingMimeType = consumeTypes == null ? null : + consumeTypes.stream(). map( MediaType::toString ).toArray( String[]::new ); - resourceMethodInfoDTO.producingMimeType = produceTypes.stream(). + resourceMethodInfoDTO.producingMimeType = produceTypes == null ? null : + produceTypes.stream(). map( MediaType::toString ).toArray( @@ -194,6 +209,20 @@ public class ClassIntrospector { return Stream.of(resourceMethodInfoDTO); } + private static List<MediaType> getConsumesInfo(Method method) { + Consumes consumes = AnnotationUtils.getMethodAnnotation( + method, Consumes.class); + + return consumes == null ? null : JAXRSUtils.getMediaTypes(consumes.value()); + } + + private static List<MediaType> getConsumesInfo(Class<?> clazz) { + Consumes consumes = AnnotationUtils.getClassAnnotation( + clazz, Consumes.class); + + return consumes == null ? null : JAXRSUtils.getMediaTypes(consumes.value()); + } + private static String getPathSafe(ClassResourceInfo classResourceInfo) { Path path = classResourceInfo.getPath(); if (path == null) { @@ -203,4 +232,18 @@ public class ClassIntrospector { return path.value(); } + private static List<MediaType> getProducesInfo(Method method) { + Produces produces = AnnotationUtils.getMethodAnnotation( + method, Produces.class); + + return produces == null ? null : JAXRSUtils.getMediaTypes(produces.value()); + } + + private static List<MediaType> getProducesInfo(Class<?> clazz) { + Produces produces = AnnotationUtils.getClassAnnotation( + clazz, Produces.class); + + return produces == null ? null : JAXRSUtils.getMediaTypes(produces.value()); + } + } http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/4d04b13b/jax-rs.whiteboard/src/test/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospectorTest.java ---------------------------------------------------------------------- diff --git a/jax-rs.whiteboard/src/test/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospectorTest.java b/jax-rs.whiteboard/src/test/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospectorTest.java index 168640c..867d5a2 100644 --- a/jax-rs.whiteboard/src/test/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospectorTest.java +++ b/jax-rs.whiteboard/src/test/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospectorTest.java @@ -38,6 +38,7 @@ import java.util.stream.Collectors; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; public class ClassIntrospectorTest { @@ -61,10 +62,8 @@ public class ClassIntrospectorTest { resourceMethodInfoDTOS[0]; assertEquals(HttpMethod.GET, resourceMethodInfoDTO.method); - assertArrayEquals( - ALL_TYPES, resourceMethodInfoDTO.consumingMimeType); - assertArrayEquals( - ALL_TYPES, resourceMethodInfoDTO.producingMimeType); + assertNull(resourceMethodInfoDTO.consumingMimeType); + assertNull(resourceMethodInfoDTO.producingMimeType); assertEquals("/", resourceMethodInfoDTO.path); assertArrayEquals(new String[]{}, resourceMethodInfoDTO.nameBindings); } @@ -94,8 +93,8 @@ public class ClassIntrospectorTest { new ResourceMethodInfoDTO(); resourceMethodInfoDTO.method = HttpMethod.GET; - resourceMethodInfoDTO.consumingMimeType = ALL_TYPES; - resourceMethodInfoDTO.producingMimeType = ALL_TYPES; + resourceMethodInfoDTO.consumingMimeType = null; + resourceMethodInfoDTO.producingMimeType = null; resourceMethodInfoDTO.path = "/"; resourceMethodInfoDTO.nameBindings = new String[]{"test.types.Bound"}; @@ -105,8 +104,8 @@ public class ClassIntrospectorTest { resourceMethodInfoDTO = new ResourceMethodInfoDTO(); resourceMethodInfoDTO.method = HttpMethod.POST; - resourceMethodInfoDTO.consumingMimeType = ALL_TYPES; - resourceMethodInfoDTO.producingMimeType = ALL_TYPES; + resourceMethodInfoDTO.consumingMimeType = null; + resourceMethodInfoDTO.producingMimeType = null; resourceMethodInfoDTO.path = "/"; resourceMethodInfoDTO.nameBindings = new String[]{"test.types.Bound"}; @@ -141,8 +140,8 @@ public class ClassIntrospectorTest { new ResourceMethodInfoDTO(); resourceMethodInfoDTO.method = HttpMethod.GET; - resourceMethodInfoDTO.consumingMimeType = ALL_TYPES; - resourceMethodInfoDTO.producingMimeType = ALL_TYPES; + resourceMethodInfoDTO.consumingMimeType = null; + resourceMethodInfoDTO.producingMimeType = null; resourceMethodInfoDTO.path = "/"; resourceMethodInfoDTO.nameBindings = new String[]{}; @@ -153,8 +152,8 @@ public class ClassIntrospectorTest { resourceMethodInfoDTO = new ResourceMethodInfoDTO(); resourceMethodInfoDTO.method = HttpMethod.POST; - resourceMethodInfoDTO.consumingMimeType = ALL_TYPES; - resourceMethodInfoDTO.producingMimeType = ALL_TYPES; + resourceMethodInfoDTO.consumingMimeType = null; + resourceMethodInfoDTO.producingMimeType = null; resourceMethodInfoDTO.path = "/"; resourceMethodInfoDTO.nameBindings = new String[]{}; @@ -188,8 +187,8 @@ public class ClassIntrospectorTest { new ResourceMethodInfoDTO(); resourceMethodInfoDTO.method = HttpMethod.GET; - resourceMethodInfoDTO.consumingMimeType = ALL_TYPES; - resourceMethodInfoDTO.producingMimeType = ALL_TYPES; + resourceMethodInfoDTO.consumingMimeType = null; + resourceMethodInfoDTO.producingMimeType = null; resourceMethodInfoDTO.path = "/common"; resourceMethodInfoDTO.nameBindings = new String[]{}; @@ -201,8 +200,8 @@ public class ClassIntrospectorTest { new ResourceMethodInfoDTO(); resourceMethodInfoDTO.method = HttpMethod.POST; - resourceMethodInfoDTO.consumingMimeType = ALL_TYPES; - resourceMethodInfoDTO.producingMimeType = ALL_TYPES; + resourceMethodInfoDTO.consumingMimeType = null; + resourceMethodInfoDTO.producingMimeType = null; resourceMethodInfoDTO.path = "/common"; resourceMethodInfoDTO.nameBindings = new String[]{}; @@ -236,8 +235,8 @@ public class ClassIntrospectorTest { new ResourceMethodInfoDTO(); resourceMethodInfoDTO.method = HttpMethod.GET; - resourceMethodInfoDTO.consumingMimeType = ALL_TYPES; - resourceMethodInfoDTO.producingMimeType = ALL_TYPES; + resourceMethodInfoDTO.consumingMimeType = null; + resourceMethodInfoDTO.producingMimeType = null; resourceMethodInfoDTO.path = "/common"; resourceMethodInfoDTO.nameBindings = new String[]{}; @@ -248,8 +247,8 @@ public class ClassIntrospectorTest { resourceMethodInfoDTO = new ResourceMethodInfoDTO(); resourceMethodInfoDTO.method = HttpMethod.POST; - resourceMethodInfoDTO.consumingMimeType = ALL_TYPES; - resourceMethodInfoDTO.producingMimeType = ALL_TYPES; + resourceMethodInfoDTO.consumingMimeType = null; + resourceMethodInfoDTO.producingMimeType = null; resourceMethodInfoDTO.path = "/common/different"; resourceMethodInfoDTO.nameBindings = new String[]{}; @@ -283,7 +282,7 @@ public class ClassIntrospectorTest { new ResourceMethodInfoDTO(); resourceMethodInfoDTO.method = HttpMethod.GET; - resourceMethodInfoDTO.consumingMimeType = ALL_TYPES; + resourceMethodInfoDTO.consumingMimeType = null; resourceMethodInfoDTO.producingMimeType = new String[]{MediaType.APPLICATION_XML}; resourceMethodInfoDTO.path = "/resource"; http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/4d04b13b/jax-rs.whiteboard/src/test/java/test/types/ResourceMethodInfoDTOWrapper.java ---------------------------------------------------------------------- diff --git a/jax-rs.whiteboard/src/test/java/test/types/ResourceMethodInfoDTOWrapper.java b/jax-rs.whiteboard/src/test/java/test/types/ResourceMethodInfoDTOWrapper.java index ee10b6b..53eeed4 100644 --- a/jax-rs.whiteboard/src/test/java/test/types/ResourceMethodInfoDTOWrapper.java +++ b/jax-rs.whiteboard/src/test/java/test/types/ResourceMethodInfoDTOWrapper.java @@ -21,6 +21,7 @@ package test.types; import org.osgi.service.jaxrs.runtime.dto.ResourceMethodInfoDTO; import java.util.Arrays; +import java.util.Objects; public class ResourceMethodInfoDTOWrapper extends ResourceMethodInfoDTO { @@ -44,14 +45,11 @@ public class ResourceMethodInfoDTOWrapper ResourceMethodInfoDTO that = (ResourceMethodInfoDTO) o; return - path.equals(that.path) && - method.equals(that.method) && - Arrays.asList(consumingMimeType).equals( - Arrays.asList(that.consumingMimeType)) && - Arrays.asList(producingMimeType).equals( - Arrays.asList(that.producingMimeType)) && - Arrays.asList(nameBindings).equals( - Arrays.asList(that.nameBindings)); + Objects.equals(path, that.path) && + Objects.equals(method, that.method) && + Arrays.equals(consumingMimeType, that.consumingMimeType) && + Arrays.equals(producingMimeType, that.producingMimeType) && + Arrays.equals(nameBindings, that.nameBindings); } }
