@Override
 public JSONEntity read(Class<? extends JSONEntity> clazz, HttpInputMessage inputMessage)
     throws IOException, HttpMessageNotReadableException {
   // First read the string
   String jsonString =
       JSONEntityHttpMessageConverter.readToString(
           inputMessage.getBody(), inputMessage.getHeaders().getContentType().getCharSet());
   try {
     return EntityFactory.createEntityFromJSONString(jsonString, clazz);
   } catch (JSONObjectAdapterException e) {
     // Try to convert entity type to a concrete type and try again. See PLFM-2079.
     try {
       JSONObject jsonObject = new JSONObject(jsonString);
       if (jsonObject.has(ENTITY_TYPE)) {
         // get the entity type so we can replace it with concrete type
         String type = jsonObject.getString(ENTITY_TYPE);
         jsonObject.remove(ENTITY_TYPE);
         jsonObject.put(CONCRETE_TYPE, type);
         jsonString = jsonObject.toString();
         // try again
         return EntityFactory.createEntityFromJSONString(jsonString, clazz);
       } else {
         // Something else went wrong
         throw new HttpMessageNotReadableException(e.getMessage(), e);
       }
     } catch (JSONException e1) {
       throw new HttpMessageNotReadableException(e1.getMessage(), e);
     } catch (JSONObjectAdapterException e2) {
       throw new HttpMessageNotReadableException(e2.getMessage(), e);
     }
   }
 }
 @Override
 public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
     throws IOException, HttpMessageNotReadableException {
   try {
     TumblrResponse tumblrResponse =
         objectMapper.readValue(inputMessage.getBody(), TumblrResponse.class);
     checkResponse(tumblrResponse);
     Object result;
     if (TumblrResponse.class.equals(type)) {
       // don't parse the response json, callee is going to process is manually
       result = tumblrResponse;
     } else {
       // parse the response json into an instance of the given class
       JavaType javaType = getJavaType(type, contextClass);
       String response = tumblrResponse.getResponseJson();
       result = objectMapper.readValue(response, javaType);
     }
     return result;
   } catch (JsonParseException ex) {
     throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
   } catch (EOFException ex) {
     throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
   } catch (Exception e) {
     e.printStackTrace();
     throw new IOException(e);
   }
 }
 @Override
 protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage)
     throws IOException, HttpMessageNotReadableException {
   MediaType contentType = inputMessage.getHeaders().getContentType();
   Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
   return FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset));
 }
  @Override
  public BufferedImage read(Class<? extends BufferedImage> clazz, HttpInputMessage inputMessage)
      throws IOException, HttpMessageNotReadableException {

    ImageInputStream imageInputStream = null;
    ImageReader imageReader = null;
    try {
      imageInputStream = createImageInputStream(inputMessage.getBody());
      MediaType contentType = inputMessage.getHeaders().getContentType();
      Iterator<ImageReader> imageReaders =
          ImageIO.getImageReadersByMIMEType(contentType.toString());
      if (imageReaders.hasNext()) {
        imageReader = imageReaders.next();
        ImageReadParam irp = imageReader.getDefaultReadParam();
        process(irp);
        imageReader.setInput(imageInputStream, true);
        return imageReader.read(0, irp);
      } else {
        throw new HttpMessageNotReadableException(
            "Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
      }
    } finally {
      if (imageReader != null) {
        imageReader.dispose();
      }
      if (imageInputStream != null) {
        try {
          imageInputStream.close();
        } catch (IOException ex) {
          // ignore
        }
      }
    }
  }
 private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
   try {
     if (inputMessage instanceof MappingJacksonInputMessage) {
       Class<?> deserializationView =
           ((MappingJacksonInputMessage) inputMessage).getDeserializationView();
       if (deserializationView != null) {
         return this.objectMapper
             .readerWithView(deserializationView)
             .forType(javaType)
             .readValue(inputMessage.getBody());
       }
     }
     return this.objectMapper.readValue(inputMessage.getBody(), javaType);
   } catch (IOException ex) {
     throw new HttpMessageNotReadableException("Could not read document: " + ex.getMessage(), ex);
   }
 }
 @Override
 protected OAuth2AccessToken readInternal(
     Class<? extends OAuth2AccessToken> clazz, HttpInputMessage response)
     throws IOException, HttpMessageNotReadableException {
   MediaType contentType = response.getHeaders().getContentType();
   if (contentType != null && JSON_MEDIA_TYPE.includes(contentType)) {
     try {
       return getSerializationService().deserializeJsonAccessToken(response.getBody());
     } catch (SerializationException e) {
       throw new OAuth2Exception(
           "Error getting the access token, and unable to read the details of the error in the JSON response.",
           e);
     }
   } else {
     // the spec currently says json is required, but facebook, for example, still returns
     // form-encoded.
     MultiValueMap<String, String> map = FORM_MESSAGE_CONVERTER.read(null, response);
     return getSerializationService().deserializeAccessToken(map.toSingleValueMap());
   }
 }
  private Object success(Class<?> clazz, HttpInputMessage inputMessage)
      throws JsonParseException, IOException {

    // Note, parsing code used from ektorp project
    JsonParser jp = objectMapper.getJsonFactory().createJsonParser(inputMessage.getBody());
    if (jp.nextToken() != JsonToken.START_OBJECT) {
      throw new RuntimeException("Expected data to start with an Object");
    }
    Map<String, Integer> fields = readHeaderFields(jp);

    List result;
    if (fields.containsKey(TOTAL_ROWS_FIELD_NAME)) {
      int totalRows = fields.get(TOTAL_ROWS_FIELD_NAME);
      if (totalRows == 0) {
        return Collections.emptyList();
      }
      result = new ArrayList(totalRows);
    } else {
      result = new ArrayList();
    }

    ParseState state = new ParseState();

    Object first = parseFirstRow(jp, state, clazz);
    if (first == null) {
      return Collections.emptyList();
    } else {
      result.add(first);
    }

    while (jp.getCurrentToken() != null) {
      skipToField(jp, state.docFieldName, state);
      if (atEndOfRows(jp)) {
        return result;
      }
      result.add(jp.readValueAs(clazz));
      endRow(jp, state);
    }
    return result;
  }