DeserializingInterceptor implements Interceptor { public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance) throws InterceptionException { Consumes consumesAnnotation = method.getMethod().getAnnotation(Consumes.class); List<String> supported = Arrays.asList(consumesAnnotation.value()); if(request.getContentType() == null) { logger.warn("Request does not have Content-Type and parameters will be not deserialized"); stack.next(method, resourceInstance); return; } String contentType = mime(request.getContentType()); if (!supported.isEmpty() && !supported.contains(contentType)) { unsupported(String.format("Request with media type [%s]. Expecting one of %s.", contentType, supported)); return; } try { Deserializer deserializer = deserializers.deserializerFor(contentType, container); if (deserializer == null) { unsupported(String.format("Unable to handle media type [%s]: no deserializer found.", contentType)); return; } Object[] deserialized = deserializer.deserialize(request.getInputStream(), method); Object[] parameters = methodInfo.getParameters(); logger.debug("Deserialized parameters for {} are {} ", method, deserialized); for (int i = 0; i < deserialized.length; i++) { if (deserialized[i] != null) { parameters[i] = deserialized[i]; } } stack.next(method, resourceInstance); } catch (IOException e) { throw new InterceptionException(e); } } DeserializingInterceptor(HttpServletRequest servletRequest, Deserializers deserializers,
			MethodInfo methodInfo, Container container, Status status); boolean accepts(ResourceMethod method); void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance);  }
@Test public void willSetHttpStatusCode415IfTheResourceMethodDoesNotSupportTheGivenMediaTypes() throws Exception { when(request.getContentType()).thenReturn("image/jpeg"); interceptor.intercept(stack, consumeXml, null); verify(status).unsupportedMediaType("Request with media type [image/jpeg]. Expecting one of [application/xml]."); verifyZeroInteractions(stack); }
@Test public void willSetHttpStatusCode415IfThereIsNoDeserializerButIsAccepted() throws Exception { when(request.getContentType()).thenReturn("application/xml"); when(deserializers.deserializerFor("application/xml", container)).thenReturn(null); interceptor.intercept(stack, consumeXml, null); verify(status).unsupportedMediaType("Unable to handle media type [application/xml]: no deserializer found."); verifyZeroInteractions(stack); }
@Test public void willSetMethodParametersWithDeserializationAndContinueStackAfterDeserialization() { when(request.getContentType()).thenReturn("application/xml"); final Deserializer deserializer = mock(Deserializer.class); methodInfo.setParameters(new Object[2]); when(deserializer.deserialize(null, consumeXml)).thenReturn(new Object[] {"abc", "def"}); when(deserializers.deserializerFor("application/xml", container)).thenReturn(deserializer); interceptor.intercept(stack, consumeXml, null); assertEquals(methodInfo.getParameters()[0], "abc"); assertEquals(methodInfo.getParameters()[1], "def"); verify(stack).next(consumeXml, null); }
@Test public void willSetMethodParametersWithDeserializationEvenIfTheContentTypeHasCharsetDeclaration() { when(request.getContentType()).thenReturn("application/xml; charset=UTF-8"); final Deserializer deserializer = mock(Deserializer.class); methodInfo.setParameters(new Object[2]); when(deserializer.deserialize(null, consumeXml)).thenReturn(new Object[] {"abc", "def"}); when(deserializers.deserializerFor("application/xml", container)).thenReturn(deserializer); interceptor.intercept(stack, consumeXml, null); assertEquals(methodInfo.getParameters()[0], "abc"); assertEquals(methodInfo.getParameters()[1], "def"); verify(stack).next(consumeXml, null); }
@Test public void willDeserializeForAnyContentTypeIfPossible() throws Exception { when(request.getContentType()).thenReturn("application/xml"); methodInfo.setParameters(new Object[2]); final DefaultResourceMethod consumesAnything = new DefaultResourceMethod(null, DummyResource.class.getDeclaredMethod("consumesAnything")); final Deserializer deserializer = mock(Deserializer.class); when(deserializer.deserialize(null, consumesAnything)).thenReturn(new Object[] {"abc", "def"}); when(deserializers.deserializerFor("application/xml", container)).thenReturn(deserializer); interceptor.intercept(stack, consumesAnything, null); assertEquals(methodInfo.getParameters()[0], "abc"); assertEquals(methodInfo.getParameters()[1], "def"); verify(stack).next(consumesAnything, null); }
@Test public void shouldNotDeserializeIfHasNoContentType() throws Exception { when(request.getContentType()).thenReturn(null); methodInfo.setParameters(new Object[2]); final DefaultResourceMethod consumesAnything = new DefaultResourceMethod(null, DummyResource.class.getDeclaredMethod("consumesAnything")); interceptor.intercept(stack, consumesAnything, null); assertEquals(methodInfo.getParameters()[0], null); assertEquals(methodInfo.getParameters()[1], null); verify(stack).next(consumesAnything, null); }
@Test public void willSetOnlyNonNullParameters() throws Exception { when(request.getContentType()).thenReturn("application/xml"); final Deserializer deserializer = mock(Deserializer.class); methodInfo.setParameters(new Object[] {"original1", "original2"}); when(deserializer.deserialize(null, consumeXml)).thenReturn(new Object[] {null, "deserialized"}); when(deserializers.deserializerFor("application/xml", container)).thenReturn(deserializer); interceptor.intercept(stack, consumeXml, null); assertEquals(methodInfo.getParameters()[0], "original1"); assertEquals(methodInfo.getParameters()[1], "deserialized"); verify(stack).next(consumeXml, null); }
@Test(expected = InterceptionException.class) public void shouldThrowInterceptionExceptionIfAnIOExceptionOccurs() throws Exception { when(request.getInputStream()).thenThrow(new IOException()); when(request.getContentType()).thenReturn("application/xml"); final Deserializer deserializer = mock(Deserializer.class); when(deserializers.deserializerFor("application/xml", container)).thenReturn(deserializer); interceptor.intercept(stack, consumeXml, null); }