Search in sources :

Example 16 with UpdateResponse

use of com.linkedin.restli.server.UpdateResponse in project rest.li by linkedin.

the class ComplexKeysDataProvider method batchUpdate.

public BatchUpdateResult<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchUpdate(BatchPatchRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> patches) {
    final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateResponse> results = new HashMap<>();
    for (Map.Entry<ComplexResourceKey<TwoPartKey, TwoPartKey>, PatchRequest<Message>> patch : patches.getData().entrySet()) {
        try {
            this.partialUpdate(patch.getKey(), patch.getValue());
            results.put(patch.getKey(), new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
        } catch (DataProcessingException e) {
            results.put(patch.getKey(), new UpdateResponse(HttpStatus.S_400_BAD_REQUEST));
        }
    }
    return new BatchUpdateResult<>(results);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchPatchRequest(com.linkedin.restli.server.BatchPatchRequest) HashMap(java.util.HashMap) Map(java.util.Map) DataProcessingException(com.linkedin.data.transform.DataProcessingException)

Example 17 with UpdateResponse

use of com.linkedin.restli.server.UpdateResponse in project rest.li by linkedin.

the class ComplexKeysDataProvider method batchDelete.

public BatchUpdateResult<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchDelete(BatchDeleteRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> ids) {
    final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateResponse> results = new HashMap<>();
    for (ComplexResourceKey<TwoPartKey, TwoPartKey> id : ids.getKeys()) {
        _db.remove(keyToString(id.getKey()));
        results.put(id, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
    }
    return new BatchUpdateResult<>(results);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) UpdateResponse(com.linkedin.restli.server.UpdateResponse) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 18 with UpdateResponse

use of com.linkedin.restli.server.UpdateResponse in project rest.li by linkedin.

the class BatchGreetingResource method update.

@RestMethod.PartialUpdate
public UpdateResponse update(Long id, PatchRequest<Greeting> patch) {
    Greeting greeting = DB.get(id);
    try {
        PatchApplier.applyPatch(greeting, patch);
    } catch (DataProcessingException e) {
        return new UpdateResponse(HttpStatus.S_400_BAD_REQUEST);
    }
    DB.put(id, greeting);
    return new UpdateResponse(HttpStatus.S_204_NO_CONTENT);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) UpdateResponse(com.linkedin.restli.server.UpdateResponse) DataProcessingException(com.linkedin.data.transform.DataProcessingException)

Example 19 with UpdateResponse

use of com.linkedin.restli.server.UpdateResponse in project rest.li by linkedin.

the class CustomTypesResource3 method batchUpdate.

@Override
public BatchUpdateResult<CompoundKey, Greeting> batchUpdate(BatchUpdateRequest<CompoundKey, Greeting> entities) {
    Set<CompoundKey> keys = entities.getData().keySet();
    Map<CompoundKey, UpdateResponse> responseMap = new HashMap<>();
    Map<CompoundKey, RestLiServiceException> errorMap = new HashMap<>();
    for (CompoundKey key : keys) {
        responseMap.put(key, new UpdateResponse(HttpStatus.S_201_CREATED));
    }
    return new BatchUpdateResult<>(responseMap);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap)

Example 20 with UpdateResponse

use of com.linkedin.restli.server.UpdateResponse in project rest.li by linkedin.

the class PartialUpdateResponseBuilder method buildRestLiResponseData.

@Override
@SuppressWarnings({ "unchecked" })
public RestLiResponseData<PartialUpdateResponseEnvelope> buildRestLiResponseData(Request request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    UpdateResponse updateResponse = (UpdateResponse) result;
    // Verify that the status in the UpdateResponse is not null. If so, this is a developer error.
    if (updateResponse.getStatus() == null) {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. HttpStatus is null inside of an UpdateResponse returned by the resource method: " + routingResult.getResourceMethod());
    }
    final ResourceContext resourceContext = routingResult.getContext();
    RecordTemplate entityResponse = null;
    // Add patched entity to the response if result is an UpdateEntityResponse and the client is asking for the entity
    if (result instanceof UpdateEntityResponse && resourceContext.isReturnEntityRequested()) {
        UpdateEntityResponse<?> updateEntityResponse = (UpdateEntityResponse<?>) updateResponse;
        if (updateEntityResponse.hasEntity()) {
            DataMap entityData = updateEntityResponse.getEntity().data();
            TimingContextUtil.beginTiming(resourceContext.getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
            final DataMap data = RestUtils.projectFields(entityData, resourceContext);
            TimingContextUtil.endTiming(resourceContext.getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
            // Returned entity is to be added to the response envelope
            entityResponse = new EntityResponse<>(data, updateEntityResponse.getEntity().getClass());
        } else {
            // If trying to return an error response, a RestLiServiceException should be thrown in the resource method.
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Entity is null inside of an UpdateEntityResponse returned by the resource method: " + routingResult.getResourceMethod());
        }
    }
    return new RestLiResponseDataImpl<>(new PartialUpdateResponseEnvelope(updateResponse.getStatus(), entityResponse), headers, cookies);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) ResourceContext(com.linkedin.restli.server.ResourceContext) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RecordTemplate(com.linkedin.data.template.RecordTemplate) UpdateEntityResponse(com.linkedin.restli.server.UpdateEntityResponse) DataMap(com.linkedin.data.DataMap)

Aggregations

UpdateResponse (com.linkedin.restli.server.UpdateResponse)55 BatchUpdateResult (com.linkedin.restli.server.BatchUpdateResult)21 HashMap (java.util.HashMap)21 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)18 DataProcessingException (com.linkedin.data.transform.DataProcessingException)12 Map (java.util.Map)11 Test (org.testng.annotations.Test)11 CompoundKey (com.linkedin.restli.common.CompoundKey)9 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)6 HttpStatus (com.linkedin.restli.common.HttpStatus)5 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)5 ByteString (com.linkedin.data.ByteString)4 PatchRequest (com.linkedin.restli.common.PatchRequest)4 UpdateStatus (com.linkedin.restli.common.UpdateStatus)4 Photo (com.linkedin.restli.example.Photo)4 ValidationDemo (com.linkedin.restli.examples.greetings.api.ValidationDemo)4 BatchPatchRequest (com.linkedin.restli.server.BatchPatchRequest)4 Callback (com.linkedin.common.callback.Callback)3 DataMap (com.linkedin.data.DataMap)3 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)3