Exemple #1
0
  // This throws Exception to remind the caller to deal with arbitrary exceptions including
  // RuntimeException
  // in a way appropriate for the public method that was originally invoked.
  private RestRequest buildRequest(
      URI uri,
      ResourceMethod method,
      DataMap dataMap,
      Map<String, String> headers,
      List<String> cookies,
      ProtocolVersion protocolVersion,
      ContentType contentType,
      List<AcceptType> acceptTypes)
      throws Exception {
    RestRequestBuilder requestBuilder =
        new RestRequestBuilder(uri).setMethod(method.getHttpMethod().toString());

    requestBuilder.setHeaders(headers);
    requestBuilder.setCookies(cookies);
    addAcceptHeaders(requestBuilder, acceptTypes);
    addEntityAndContentTypeHeaders(requestBuilder, dataMap, contentType);
    addProtocolVersionHeader(requestBuilder, protocolVersion);

    if (method.getHttpMethod() == HttpMethod.POST) {
      requestBuilder.setHeader(RestConstants.HEADER_RESTLI_REQUEST_METHOD, method.toString());
    }

    return requestBuilder.build();
  }
Exemple #2
0
 // We handle accept types based on the following precedence order:
 // 1. Request header
 // 2. RestLiRequestOptions
 // 3. RestClient configuration
 private void addAcceptHeaders(RestRequestBuilder builder, List<AcceptType> acceptTypes) {
   if (builder.getHeader(RestConstants.HEADER_ACCEPT) == null) {
     List<AcceptType> types = _acceptTypes;
     if (acceptTypes != null && !acceptTypes.isEmpty()) {
       types = acceptTypes;
     }
     if (types != null && !types.isEmpty()) {
       builder.setHeader(RestConstants.HEADER_ACCEPT, createAcceptHeader(types));
     }
   }
 }
Exemple #3
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);
      }
    }
  }
Exemple #4
0
 /**
  * Adds the protocol version of Rest.li used to build the request to the headers for this request
  *
  * @param builder
  * @param protocolVersion
  */
 private void addProtocolVersionHeader(
     RestRequestBuilder builder, ProtocolVersion protocolVersion) {
   builder.setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
 }