Search in sources :

Example 16 with AnyRecord

use of com.linkedin.restli.internal.server.methods.AnyRecord in project rest.li by linkedin.

the class BatchGetResponseBuilder method buildResponse.

@Override
@SuppressWarnings("unchecked")
public PartialRestResponse buildResponse(RoutingResult routingResult, RestLiResponseData responseData) {
    final Map<Object, BatchResponseEntry> responses = (Map<Object, BatchResponseEntry>) responseData.getBatchResponseEnvelope().getBatchResponseMap();
    // Build the EntityResponse for each key from the merged map with mask from routingResult.
    Map<Object, EntityResponse<RecordTemplate>> entityBatchResponse = buildEntityResponse(routingResult, responses);
    PartialRestResponse.Builder builder = new PartialRestResponse.Builder();
    final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
    @SuppressWarnings("unchecked") final BatchResponse<AnyRecord> response = toBatchResponse(entityBatchResponse, protocolVersion);
    builder.entity(response);
    return builder.headers(responseData.getHeaders()).cookies(responseData.getCookies()).build();
}
Also used : AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) BatchResponseEntry(com.linkedin.restli.internal.server.response.BatchResponseEnvelope.BatchResponseEntry) EntityResponse(com.linkedin.restli.common.EntityResponse) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map)

Example 17 with AnyRecord

use of com.linkedin.restli.internal.server.methods.AnyRecord in project rest.li by linkedin.

the class BatchUpdateResponseBuilder method buildResponse.

@Override
@SuppressWarnings("unchecked")
public PartialRestResponse buildResponse(RoutingResult routingResult, RestLiResponseData responseData) {
    Map<Object, UpdateStatus> mergedResults = new HashMap<Object, UpdateStatus>();
    final Map<Object, BatchResponseEntry> responses = (Map<Object, BatchResponseEntry>) responseData.getBatchResponseEnvelope().getBatchResponseMap();
    generateResultEntityResponse(routingResult, responses, mergedResults);
    PartialRestResponse.Builder builder = new PartialRestResponse.Builder();
    final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
    @SuppressWarnings("unchecked") final BatchResponse<AnyRecord> response = toBatchResponse(mergedResults, protocolVersion);
    return builder.entity(response).headers(responseData.getHeaders()).cookies(responseData.getCookies()).build();
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) HashMap(java.util.HashMap) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) BatchResponseEntry(com.linkedin.restli.internal.server.response.BatchResponseEnvelope.BatchResponseEntry) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map)

Example 18 with AnyRecord

use of com.linkedin.restli.internal.server.methods.AnyRecord in project rest.li by linkedin.

the class CreateResponseBuilder method buildRestLiResponseData.

@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    CreateResponse createResponse = (CreateResponse) result;
    if (createResponse.hasError()) {
        RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(createResponse.getError(), headers, cookies);
        responseData.setResponseEnvelope(new CreateResponseEnvelope(null, responseData));
        return responseData;
    }
    Object id = null;
    if (createResponse.hasId()) {
        id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createResponse.getId(), routingResult);
        final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
        String stringKey = URIParamUtils.encodeKeyForUri(id, UriComponent.Type.PATH_SEGMENT, protocolVersion);
        UriBuilder uribuilder = UriBuilder.fromUri(request.getURI());
        uribuilder.path(stringKey);
        if (routingResult.getContext().hasParameter(RestConstants.ALT_KEY_PARAM)) {
            // add altkey param to location URI
            uribuilder.queryParam(RestConstants.ALT_KEY_PARAM, routingResult.getContext().getParameter(RestConstants.ALT_KEY_PARAM));
        }
        headers.put(RestConstants.HEADER_LOCATION, uribuilder.build((Object) null).toString());
        headers.put(HeaderUtil.getIdHeaderName(protocolVersion), URIParamUtils.encodeKeyForHeader(id, protocolVersion));
    }
    //Verify that a null status was not passed into the CreateResponse. If so, this is a developer error.
    if (createResponse.getStatus() == null) {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. HttpStatus is null inside of a CreateResponse from the resource method: " + routingResult.getResourceMethod());
    }
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(createResponse.getStatus(), headers, cookies);
    CreateResponseEnvelope responseEnvelope;
    if (createResponse instanceof CreateKVResponse) {
        final ResourceContext resourceContext = routingResult.getContext();
        DataMap entityData = ((CreateKVResponse) createResponse).getEntity().data();
        final DataMap data = RestUtils.projectFields(entityData, resourceContext.getProjectionMode(), resourceContext.getProjectionMask());
        responseEnvelope = new CreateResponseEnvelope(new AnyRecord(data), true, responseData);
    } else //Instance of idResponse
    {
        IdResponse<?> idResponse = new IdResponse<Object>(id);
        responseEnvelope = new CreateResponseEnvelope(idResponse, responseData);
    }
    responseData.setResponseEnvelope(responseEnvelope);
    return responseData;
}
Also used : IdResponse(com.linkedin.restli.common.IdResponse) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ResourceContext(com.linkedin.restli.server.ResourceContext) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) CreateResponse(com.linkedin.restli.server.CreateResponse) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) DataMap(com.linkedin.data.DataMap) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) UriBuilder(com.linkedin.jersey.api.uri.UriBuilder) CreateKVResponse(com.linkedin.restli.server.CreateKVResponse)

Example 19 with AnyRecord

use of com.linkedin.restli.internal.server.methods.AnyRecord in project rest.li by linkedin.

the class BatchGetResponseBuilder method buildResponse.

@Override
@SuppressWarnings("unchecked")
public RestLiResponse buildResponse(RoutingResult routingResult, RestLiResponseData<BatchGetResponseEnvelope> responseData) {
    final Map<Object, BatchResponseEntry> responses = (Map<Object, BatchResponseEntry>) responseData.getResponseEnvelope().getBatchResponseMap();
    // Build the EntityResponse for each key from the merged map with mask from routingResult.
    Map<Object, EntityResponse<RecordTemplate>> entityBatchResponse = buildEntityResponse(routingResult, responses);
    RestLiResponse.Builder builder = new RestLiResponse.Builder();
    final ProtocolVersion protocolVersion = routingResult.getContext().getRestliProtocolVersion();
    @SuppressWarnings("unchecked") final BatchResponse<AnyRecord> response = toBatchResponse(entityBatchResponse, protocolVersion);
    builder.entity(response);
    return builder.headers(responseData.getHeaders()).cookies(responseData.getCookies()).build();
}
Also used : AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) BatchResponseEntry(com.linkedin.restli.internal.server.response.BatchResponseEnvelope.BatchResponseEntry) EntityResponse(com.linkedin.restli.common.EntityResponse) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map)

Example 20 with AnyRecord

use of com.linkedin.restli.internal.server.methods.AnyRecord in project rest.li by linkedin.

the class BatchPartialUpdateResponseBuilder method buildUpdateStatus.

/**
 * Defines how to build an update status for batch partial update. If the update response is an {@link UpdateEntityResponse}
 * and the client is requesting the entities to be returned, then build an {@link UpdateEntityStatus} containing the
 * entity to be returned for a given update response.
 * @param resourceContext current resource context
 * @param updateResponse update response returned by the resource method
 * @return update status possibly containing the returned entity
 */
@Override
protected UpdateStatus buildUpdateStatus(ResourceContext resourceContext, UpdateResponse updateResponse) {
    if (updateResponse instanceof UpdateEntityResponse && resourceContext.isReturnEntityRequested()) {
        final RecordTemplate entity = ((UpdateEntityResponse<?>) updateResponse).getEntity();
        final DataMap entityData = entity != null ? entity.data() : null;
        final DataMap projectedData = RestUtils.projectFields(entityData, resourceContext);
        return new UpdateEntityStatus<>(updateResponse.getStatus().getCode(), new AnyRecord(projectedData));
    } else {
        return super.buildUpdateStatus(resourceContext, updateResponse);
    }
}
Also used : UpdateEntityStatus(com.linkedin.restli.common.UpdateEntityStatus) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) UpdateEntityResponse(com.linkedin.restli.server.UpdateEntityResponse) RecordTemplate(com.linkedin.data.template.RecordTemplate) DataMap(com.linkedin.data.DataMap)

Aggregations

AnyRecord (com.linkedin.restli.internal.server.methods.AnyRecord)25 DataMap (com.linkedin.data.DataMap)18 RecordTemplate (com.linkedin.data.template.RecordTemplate)17 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)10 ResourceContext (com.linkedin.restli.server.ResourceContext)8 HashMap (java.util.HashMap)8 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)7 ArrayList (java.util.ArrayList)7 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)6 BatchResponseEntry (com.linkedin.restli.internal.server.response.BatchResponseEnvelope.BatchResponseEntry)6 Map (java.util.Map)6 DataList (com.linkedin.data.DataList)5 CollectionMetadata (com.linkedin.restli.common.CollectionMetadata)4 HttpStatus (com.linkedin.restli.common.HttpStatus)4 CreateResponse (com.linkedin.restli.server.CreateResponse)4 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)3 RequestContext (com.linkedin.r2.message.RequestContext)3 CreateIdStatus (com.linkedin.restli.common.CreateIdStatus)3 UpdateStatus (com.linkedin.restli.common.UpdateStatus)3 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)3