Search in sources :

Example 51 with UpdateResponse

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

the class GreetingsResourceImpl method update.

@RestMethod.Update
public UpdateResponse update(Long key, Greeting entity) {
    Greeting g = _db.get(key);
    if (g == null) {
        return new UpdateResponse(HttpStatus.S_404_NOT_FOUND);
    }
    _db.put(key, entity);
    return new UpdateResponse(HttpStatus.S_204_NO_CONTENT);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) UpdateResponse(com.linkedin.restli.server.UpdateResponse)

Example 52 with UpdateResponse

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

the class ValidationDemoResource method batchUpdate.

@RestMethod.BatchPartialUpdate
public BatchUpdateResult<Integer, ValidationDemo> batchUpdate(final BatchPatchRequest<Integer, ValidationDemo> entityUpdates, @ValidatorParam RestLiDataValidator validator) {
    Map<Integer, UpdateResponse> results = new HashMap<>();
    Map<Integer, RestLiServiceException> errors = new HashMap<>();
    for (Map.Entry<Integer, PatchRequest<ValidationDemo>> entry : entityUpdates.getData().entrySet()) {
        Integer key = entry.getKey();
        PatchRequest<ValidationDemo> patch = entry.getValue();
        ValidationResult result = validator.validateInput(patch);
        if (result.isValid()) {
            results.put(key, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
        } else {
            errors.put(key, new RestLiServiceException(HttpStatus.S_422_UNPROCESSABLE_ENTITY, result.getMessages().toString()));
        }
    }
    return new BatchUpdateResult<>(results, errors);
}
Also used : HashMap(java.util.HashMap) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchPatchRequest(com.linkedin.restli.server.BatchPatchRequest) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 53 with UpdateResponse

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

the class AssociationAltKeyResource method update.

@Override
public UpdateResponse update(CompoundKey key, PatchRequest<Greeting> patch) {
    Greeting g = get(key);
    if (g == null) {
        return new UpdateResponse(HttpStatus.S_404_NOT_FOUND);
    }
    try {
        PatchApplier.applyPatch(g, patch);
    } catch (DataProcessingException e) {
        return new UpdateResponse(HttpStatus.S_400_BAD_REQUEST);
    }
    update(key, g);
    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 54 with UpdateResponse

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

the class GroupMembershipsResource2 method batchUpdate.

@Override
public BatchUpdateResult<CompoundKey, GroupMembership> batchUpdate(BatchPatchRequest<CompoundKey, GroupMembership> patches) {
    Map<CompoundKey, UpdateResponse> results = new HashMap<>();
    for (Map.Entry<CompoundKey, PatchRequest<GroupMembership>> entry : patches.getData().entrySet()) {
        CompoundKey key = entry.getKey();
        PatchRequest<GroupMembership> patch = entry.getValue();
        GroupMembership groupMembership = _app.getMembershipMgr().get(key);
        if (groupMembership == null) {
            results.put(key, new UpdateResponse(HttpStatus.S_404_NOT_FOUND));
        } else {
            try {
                PatchApplier.applyPatch(groupMembership, patch);
                _app.getMembershipMgr().save(groupMembership);
                results.put(key, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
            } catch (DataProcessingException e) {
                results.put(key, 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) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap) GroupMembership(com.linkedin.restli.examples.groups.api.GroupMembership) 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 55 with UpdateResponse

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

the class NullGreetingsResourceImpl method batchUpdate.

@RestMethod.BatchUpdate
public BatchUpdateResult<Long, Greeting> batchUpdate(BatchUpdateRequest<Long, Greeting> entities) {
    final Map<Long, UpdateResponse> responseMap = new HashMap<>();
    responseMap.put(3l, new UpdateResponse(HttpStatus.S_201_CREATED));
    final Map<Long, RestLiServiceException> errorsMap = new HashMap<>();
    errorsMap.put(8l, new RestLiServiceException(HttpStatus.S_202_ACCEPTED));
    if (entities.getData().containsKey(1l)) {
        // Return a null BatchUpdateResult
        return null;
    } else if (entities.getData().containsKey(2l)) {
        // Return a BatchUpdateResult with a null results Map
        return new BatchUpdateResult<>(null);
    } else if (entities.getData().containsKey(3l)) {
        // Return a BatchUpdateResult with a null errors Map
        return new BatchUpdateResult<>(responseMap, null);
    } else if (entities.getData().containsKey(4l)) {
        // Return a BatchUpdateResult with a errors Map that has a null key in it
        errorsMap.put(null, new RestLiServiceException(HttpStatus.S_202_ACCEPTED));
        return new BatchUpdateResult<>(responseMap, errorsMap);
    } else if (entities.getData().containsKey(5l)) {
        // Return a BatchUpdateResult with a errors Map that has a null value in it
        errorsMap.put(9l, null);
        return new BatchUpdateResult<>(responseMap, errorsMap);
    } else if (entities.getData().containsKey(6l)) {
        // Return a BatchUpdateResult with a map that has a null key in it
        responseMap.put(null, new UpdateResponse(HttpStatus.S_201_CREATED));
        return new BatchUpdateResult<>(responseMap);
    } else {
        /*
       * Return a BatchUpdateResult with java.util.concurrent.ConcurrentHashMap(s).
       * This test is in place because certain map implementations, such as ConcurrentHashMap, can throw an NPE when
       * calling contains(null). We want to verify that checking for the existence of nulls in maps returned by
       * Rest.li resource methods do not cause such NPEs.
       * This is one of the few cases in this file where an error will not be generated by Rest.li.
       */
        final Map<Long, UpdateResponse> concurrentResponseMap = new ConcurrentHashMap<>(responseMap);
        return new BatchUpdateResult<>(concurrentResponseMap, new ConcurrentHashMap<>());
    }
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

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