@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); }
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 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 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); }
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); }
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);  }