コード例 #1
0
  private RestResponseBuilder encodeResult(
      RestResponseBuilder builder, DataMap dataMap, String acceptTypes) {
    String bestType = RestUtils.pickBestEncoding(acceptTypes);

    if (RestConstants.HEADER_VALUE_APPLICATION_PSON.equalsIgnoreCase(bestType)) {
      builder.setHeader(
          RestConstants.HEADER_CONTENT_TYPE, RestConstants.HEADER_VALUE_APPLICATION_PSON);
      builder.setEntity(DataMapUtils.mapToPsonBytes(dataMap));
    } else if (RestConstants.HEADER_VALUE_APPLICATION_JSON.equalsIgnoreCase(bestType)) {
      builder.setHeader(
          RestConstants.HEADER_CONTENT_TYPE, RestConstants.HEADER_VALUE_APPLICATION_JSON);
      builder.setEntity(DataMapUtils.mapToBytes(dataMap, _permissiveEncoding));
    } else {
      throw new RoutingException(
          "No acceptable types can be returned", HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
    }
    return builder;
  }
コード例 #2
0
  /**
   * @param request {@link RestRequest}
   * @param routingResult {@link RoutingResult}
   * @param responseObject response value
   * @return {@link RestResponse}
   * @throws IOException if cannot build response
   */
  public RestResponse buildResponse(
      final RestRequest request, final RoutingResult routingResult, final Object responseObject)
      throws IOException {
    Map<String, String> headers = new HashMap<String, String>();
    headers.putAll(((ServerResourceContext) routingResult.getContext()).getResponseHeaders());

    headers.put(
        RestConstants.HEADER_RESTLI_PROTOCOL_VERSION,
        ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion().toString());

    if (responseObject == null) {
      boolean isAction = routingResult.getResourceMethod().getType().equals(ResourceMethod.ACTION);
      RestResponseBuilder builder = new RestResponseBuilder();
      builder.setStatus(
          isAction ? HttpStatus.S_200_OK.getCode() : HttpStatus.S_404_NOT_FOUND.getCode());
      builder.setHeaders(headers);
      if (!isAction) {
        builder.setHeader(
            RestConstants.HEADER_LINKEDIN_ERROR_RESPONSE,
            RestConstants.HEADER_VALUE_ERROR_APPLICATION);
      }
      return builder.build();
    }

    RestLiResponseBuilder responseBuilder = chooseResponseBuilder(responseObject, routingResult);

    if (responseBuilder == null) {
      // this should not happen if valid return types are specified
      ResourceMethodDescriptor resourceMethod = routingResult.getResourceMethod();
      String fqMethodName =
          resourceMethod.getResourceModel().getResourceClass().getName()
              + '#'
              + routingResult.getResourceMethod().getMethod().getName();
      throw new RestLiInternalException(
          "Invalid return type '"
              + responseObject.getClass()
              + " from method '"
              + fqMethodName
              + '\'');
    }

    PartialRestResponse partialResponse =
        responseBuilder.buildResponse(request, routingResult, responseObject, headers);

    RestResponseBuilder builder =
        new RestResponseBuilder()
            .setHeaders(headers)
            .setStatus(partialResponse.getStatus().getCode());

    if (partialResponse.hasData()) {
      DataMap dataMap = partialResponse.getDataMap();
      String acceptTypes = request.getHeader(RestConstants.HEADER_ACCEPT);
      builder = encodeResult(builder, dataMap, acceptTypes);
    }

    return builder.build();
  }