src/main/java/net/systran/platform/translation/client/ApiClient.java
/**
   * Deserialize the given JSON string to Java object.
   *
   * @param httpResponse The ClientResponse
   * @param containerType The container type, one of "list", "array" or ""
   * @param cls The type of the Java object
   * @return The deserialized Java object
   */
Deserialize the given JSON string to Java object.
Deserialize the given JSON string to Java object .
public Object deserialize(ClientResponse httpResponse, String containerType, Class cls) throws ApiException {
    String contentType = "";
    if (httpResponse.getHeaders() != null) {
      contentType = httpResponse.getHeaders().getFirst("Content-Type");
    }
    if (contentType == null || (!contentType.contains("multipart/mixed") && !contentType.contains("application/json"))) {
      return httpResponse.getEntityInputStream();
    }
    if (contentType.contains("multipart/mixed")) {
      contentType = contentType.replace("Content-Type: ", "");
      MimeTokenStream mts = new MimeTokenStream();
      mts.parseHeadless(httpResponse.getEntityInputStream(), contentType);
      String partName = "";
      MultipartResponse multipartResponse = new MultipartResponse();
      try {
        for (EntityState state = mts.getState() ; state != EntityState.T_END_OF_STREAM ; state = mts.next()) {
          if (state == EntityState.T_FIELD) {
            if (mts.getField().getName().equals("part-name"))
              partName = mts.getField().getBody();
          } else if (state == EntityState.T_BODY) {
            if (partName.equals("detectedLanguage")) {
              multipartResponse = JsonUtil.getJsonMapper().readValue(mts.getInputStream(), MultipartResponse.class);
            } else if (partName.equals("source")) {
              multipartResponse.setSource(mts.getInputStream());
            } else {
              multipartResponse.setOutput(mts.getInputStream());
            }
            partName = "";
          }
        }
      } catch (MimeException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return multipartResponse;
    }
    if (null != containerType) {
        containerType = containerType.toLowerCase();
    }
    String json = httpResponse.getEntity(String.class);
    try {
      if("list".equals(containerType) || "array".equals(containerType)) {
        JavaType typeInfo = JsonUtil.getJsonMapper().getTypeFactory().constructCollectionType(List.class, cls);
        List response = (List<?>) JsonUtil.getJsonMapper().readValue(json, typeInfo);
        return response;
      }
      else if(String.class.equals(cls)) {
        if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
          return json.substring(1, json.length() - 2);
        else
          return json;
      }
      else {
        return JsonUtil.getJsonMapper().readValue(json, cls);
      }
    }
    catch (IOException e) {
      throw new ApiException(500, e.getMessage(), null, json);
    }
  }
public Object deserialize ( ClientResponse httpResponse , String containerType , Class cls ) throws ApiException { String contentType = "" ; if ( httpResponse . getHeaders ( ) != null ) { contentType = httpResponse . getHeaders ( ) . getFirst ( "Content-Type" ) ; } if ( contentType == null || ( ! contentType . contains ( "multipart/mixed" ) && ! contentType . contains ( "application/json" ) ) ) { return httpResponse . getEntityInputStream ( ) ; } if ( contentType . contains ( "multipart/mixed" ) ) { contentType = contentType . replace ( "Content-Type: " , "" ) ; MimeTokenStream mts = new MimeTokenStream ( ) ; mts . parseHeadless ( httpResponse . getEntityInputStream ( ) , contentType ) ; String partName = "" ; MultipartResponse multipartResponse = new MultipartResponse ( ) ; try { for ( EntityState state = mts . getState ( ) ; state != EntityState . T_END_OF_STREAM ; state = mts . next ( ) ) { if ( state == EntityState . T_FIELD ) { if ( mts . getField ( ) . getName ( ) . equals ( "part-name" ) ) partName = mts . getField ( ) . getBody ( ) ; } else if ( state == EntityState . T_BODY ) { if ( partName . equals ( "detectedLanguage" ) ) { multipartResponse = JsonUtil . getJsonMapper ( ) . readValue ( mts . getInputStream ( ) , MultipartResponse . class ) ; } else if ( partName . equals ( "source" ) ) { multipartResponse . setSource ( mts . getInputStream ( ) ) ; } else { multipartResponse . setOutput ( mts . getInputStream ( ) ) ; } partName = "" ; } } } catch ( MimeException e ) { e . printStackTrace ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } return multipartResponse ; } if ( null != containerType ) { containerType = containerType . toLowerCase ( ) ; } String json = httpResponse . getEntity ( String . class ) ; try { if ( "list" . equals ( containerType ) || "array" . equals ( containerType ) ) { JavaType typeInfo = JsonUtil . getJsonMapper ( ) . getTypeFactory ( ) . constructCollectionType ( List . class , cls ) ; List response = ( List < ? > ) JsonUtil . getJsonMapper ( ) . readValue ( json , typeInfo ) ; return response ; } else if ( String . class . equals ( cls ) ) { if ( json != null && json . startsWith ( "\"" ) && json . endsWith ( "\"" ) && json . length ( ) > 1 ) return json . substring ( 1 , json . length ( ) - 2 ) ; else return json ; } else { return JsonUtil . getJsonMapper ( ) . readValue ( json , cls ) ; } } catch ( IOException e ) { throw new ApiException ( 500 , e . getMessage ( ) , null , json ) ; } }
Deserialize the given JSON string to Java object.
Deserialize the given JSON string to Java object .
// do multipart parser // System.out.println("Header field detected: " + mts.getField().getName() + " : " + mts.getField().getBody()); // System.out.println("Body detected " + partName); // System.out.println("Contents = " + mts.getInputStream()); // System.out.println("Header data = " + mts.getBodyDescriptor());