示例#1
0
  public static <T> T getEntity(
      ClientMessage msg, Class<T> type, Type genericType, ResteasyProviderFactory factory) {
    int size = msg.getBodySize();
    if (size <= 0) return null;

    byte[] body = new byte[size];
    msg.getBodyBuffer().readBytes(body);

    String contentType = msg.getStringProperty(HttpHeaderProperty.CONTENT_TYPE);
    if (contentType == null) {
      throw new UnknownMediaType(
          "Message did not have a Content-Type header cannot extract entity");
    }
    MediaType ct = MediaType.valueOf(contentType);
    MessageBodyReader<T> reader = factory.getMessageBodyReader(type, genericType, null, ct);
    if (reader == null) {
      throw new UnmarshalException(
          "Unable to find a JAX-RS reader for type "
              + type.getName()
              + " and media type "
              + contentType);
    }
    try {
      return reader.readFrom(type, genericType, null, ct, null, new ByteArrayInputStream(body));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }