Exemple #1
0
 /**
  * Read {@link DataMap} from InputStream.
  *
  * @param stream input stream
  * @return {@link DataMap}
  */
 public static DataMap readMap(final InputStream stream) {
   try {
     return CODEC.readMap(stream);
   } catch (IOException e) {
     throw new RestLiInternalException(e);
   }
 }
Exemple #2
0
 public static byte[] listToBytes(final DataList dataList) {
   try {
     return CODEC.listToBytes(dataList);
   } catch (IOException e) {
     throw new RestLiInternalException(e);
   }
 }
Exemple #3
0
 /**
  * Encode {@link DataMap} as a byte array using {@link JacksonDataCodec}.
  *
  * @param dataMap input {@link DataMap}
  * @return byte array
  */
 public static byte[] mapToBytes(final DataMap dataMap) {
   try {
     return CODEC.mapToBytes(dataMap);
   } catch (IOException e) {
     throw new RestLiInternalException(e);
   }
 }
Exemple #4
0
 /**
  * A combination of {@link #readMap(java.io.InputStream)} and {@link
  * #convert(com.linkedin.data.DataMap, Class)} for collection responses.
  *
  * @param stream input stream
  * @param recordClass class of the requested type
  * @param <T> requested object type
  * @return a new object of the requested type constructed with DataMap read from input stream
  */
 public static <T extends RecordTemplate> CollectionResponse<T> readCollectionResponse(
     final InputStream stream, final Class<T> recordClass) {
   try {
     DataMap dataMap = CODEC.readMap(stream);
     return new CollectionResponse<T>(dataMap, recordClass);
   } catch (IOException e) {
     throw new RestLiInternalException(e);
   }
 }
Exemple #5
0
 /**
  * Effectively a combination of {@link #readMap(InputStream)} and {@link #convert(DataMap,
  * Class)}.
  *
  * @param stream input stream
  * @param recordClass class of the requested type
  * @param <T> requested object type
  * @return a new object of the requested type constructed with DataMap read from input stream
  * @throws IOException on error reading input stream
  */
 public static <T extends RecordTemplate> T read(
     final InputStream stream, final Class<T> recordClass) throws IOException {
   try {
     DataMap dataMap = CODEC.readMap(stream);
     return DataTemplateUtil.wrap(dataMap, recordClass);
   } catch (IllegalArgumentException e) {
     throw new RestLiInternalException(e);
   } catch (SecurityException e) {
     throw new RestLiInternalException(e);
   }
 }
Exemple #6
0
  // Request content type resolution follows similar precedence order to accept type:
  // 1. Request header
  // 2. RestLiRequestOption
  // 3. RestClient configuration
  private void addEntityAndContentTypeHeaders(
      RestRequestBuilder builder, DataMap dataMap, ContentType contentType) throws IOException {
    if (dataMap != null) {
      String header = builder.getHeader(RestConstants.HEADER_CONTENT_TYPE);

      ContentType type;
      if (header == null) {
        if (contentType != null) {
          type = contentType;
        } else if (_contentType != null) {
          type = _contentType;
        } else {
          type = DEFAULT_CONTENT_TYPE;
        }
        builder.setHeader(RestConstants.HEADER_CONTENT_TYPE, type.getHeaderKey());
      } else {
        javax.mail.internet.ContentType headerContentType;
        try {
          headerContentType = new javax.mail.internet.ContentType(header);
        } catch (ParseException e) {
          throw new IllegalStateException("Unable to parse Content-Type: " + header);
        }

        if (headerContentType
            .getBaseType()
            .equalsIgnoreCase(RestConstants.HEADER_VALUE_APPLICATION_JSON)) {
          type = ContentType.JSON;
        } else if (headerContentType
            .getBaseType()
            .equalsIgnoreCase(RestConstants.HEADER_VALUE_APPLICATION_PSON)) {
          type = ContentType.PSON;
        } else {
          throw new IllegalStateException("Unknown Content-Type: " + headerContentType.toString());
        }
      }

      switch (type) {
        case PSON:
          builder.setEntity(PSON_DATA_CODEC.mapToBytes(dataMap));
          break;
        case JSON:
          builder.setEntity(JACKSON_DATA_CODEC.mapToBytes(dataMap));
          break;
        default:
          throw new IllegalStateException("Unknown ContentType:" + type);
      }
    }
  }