Exemplo n.º 1
0
  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());

    String contentType = 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();

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