@Override
  public PartialRestResponse buildResponse(
      final RestRequest request,
      final RoutingResult routingResult,
      final Object object,
      final Map<String, String> headers)
      throws IOException {
    @SuppressWarnings({"unchecked"})
    /** constrained by the signature of {@link CollectionResource#batchCreate} */
    BatchCreateResult<?, ?> list = (BatchCreateResult<?, ?>) object;

    CollectionResponse<CreateStatus> batchResponse =
        new CollectionResponse<CreateStatus>(CreateStatus.class);

    for (CreateResponse e : list.getResults()) {
      CreateStatus s = new CreateStatus();
      if (e.hasId()) {
        s.setId(e.getId().toString());
      }
      s.setStatus(e.getStatus().getCode());

      batchResponse.getElements().add(s);
    }
    headers.put(RestConstants.HEADER_LINKEDIN_TYPE, CollectionResponse.class.getName());
    headers.put(RestConstants.HEADER_LINKEDIN_SUB_TYPE, CreateStatus.class.getName());

    return new PartialRestResponse(batchResponse);
  }
Exemplo n.º 2
0
  @Override
  public RestLiResponseEnvelope buildRestLiResponseData(
      RestRequest request,
      RoutingResult routingResult,
      Object result,
      Map<String, String> headers,
      List<HttpCookie> cookies) {
    if (result instanceof BatchCreateKVResult) {
      BatchCreateKVResult<?, ?> list = (BatchCreateKVResult<?, ?>) result;
      if (list.getResults() == null) {
        throw new RestLiServiceException(
            HttpStatus.S_500_INTERNAL_SERVER_ERROR,
            "Unexpected null encountered. Null List inside of a BatchCreateKVResult returned by the resource method: "
                + routingResult.getResourceMethod());
      }
      List<CreateCollectionResponseEnvelope.CollectionCreateResponseItem> collectionCreateList =
          new ArrayList<CreateCollectionResponseEnvelope.CollectionCreateResponseItem>(
              list.getResults().size());

      for (CreateKVResponse e : list.getResults()) {
        if (e == null) {
          throw new RestLiServiceException(
              HttpStatus.S_500_INTERNAL_SERVER_ERROR,
              "Unexpected null encountered. Null element inside of List inside of a BatchCreateResult returned by the resource method: "
                  + routingResult.getResourceMethod());
        } else {
          Object id =
              ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(e.getId(), routingResult);
          if (e.getError() == null) {
            CreateIdEntityStatus<Object, RecordTemplate> entry =
                new CreateIdEntityStatus<Object, RecordTemplate>(
                    e.getStatus().getCode(),
                    id,
                    e.getEntity(),
                    null,
                    ProtocolVersionUtil.extractProtocolVersion(headers));
            collectionCreateList.add(
                new CreateCollectionResponseEnvelope.CollectionCreateResponseItem(entry));
          } else {
            collectionCreateList.add(
                new CreateCollectionResponseEnvelope.CollectionCreateResponseItem(
                    e.getError(), id));
          }
        }
      }
      return new CreateCollectionResponseEnvelope(collectionCreateList, headers, cookies);
    } else {
      BatchCreateResult<?, ?> list = (BatchCreateResult<?, ?>) result;

      // Verify that a null list was not passed into the BatchCreateResult. If so, this is a
      // developer error.
      if (list.getResults() == null) {
        throw new RestLiServiceException(
            HttpStatus.S_500_INTERNAL_SERVER_ERROR,
            "Unexpected null encountered. Null List inside of a BatchCreateResult returned by the resource method: "
                + routingResult.getResourceMethod());
      }

      List<CreateCollectionResponseEnvelope.CollectionCreateResponseItem> collectionCreateList =
          new ArrayList<CreateCollectionResponseEnvelope.CollectionCreateResponseItem>(
              list.getResults().size());
      for (CreateResponse e : list.getResults()) {
        // Verify that a null element was not passed into the BatchCreateResult list. If so, this is
        // a developer error.
        if (e == null) {
          throw new RestLiServiceException(
              HttpStatus.S_500_INTERNAL_SERVER_ERROR,
              "Unexpected null encountered. Null element inside of List inside of a BatchCreateResult returned by the resource method: "
                  + routingResult.getResourceMethod());
        } else {
          Object id =
              ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(e.getId(), routingResult);
          if (e.getError() == null) {
            CreateIdStatus<Object> entry =
                new CreateIdStatus<Object>(
                    e.getStatus().getCode(),
                    id,
                    null,
                    ProtocolVersionUtil.extractProtocolVersion(headers));
            collectionCreateList.add(
                new CreateCollectionResponseEnvelope.CollectionCreateResponseItem(entry));
          } else {
            collectionCreateList.add(
                new CreateCollectionResponseEnvelope.CollectionCreateResponseItem(
                    e.getError(), id));
          }
        }
      }

      return new CreateCollectionResponseEnvelope(collectionCreateList, headers, cookies);
    }
  }