Search in sources :

Example 46 with UpdateResponse

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

the class PhotoResource method update.

// update an existing photo with given entity
@Override
public UpdateResponse update(Long key, Photo entity) {
    final Photo currPhoto = _db.getData().get(key);
    if (currPhoto == null) {
        return new UpdateResponse(HttpStatus.S_404_NOT_FOUND);
    }
    // ID and URN are required fields, so use a dummy value to denote "empty" fields
    if ((entity.hasId() && entity.getId() != -1) || (entity.hasUrn() && !entity.getUrn().equals(""))) {
        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Photo ID is not acceptable in request");
    }
    // make sure the ID in the entity is consistent with the key in the database
    entity.setId(key);
    entity.setUrn(String.valueOf(key));
    _db.getData().put(key, entity);
    return new UpdateResponse(HttpStatus.S_204_NO_CONTENT);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) Photo(com.linkedin.restli.example.Photo)

Example 47 with UpdateResponse

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

the class PhotoResource method update.

// allow partial update to an existing photo
@Override
public UpdateResponse update(Long key, PatchRequest<Photo> patchRequest) {
    final Photo p = _db.getData().get(key);
    if (p == null) {
        return new UpdateResponse(HttpStatus.S_404_NOT_FOUND);
    }
    try {
        PatchApplier.applyPatch(p, patchRequest);
    } catch (DataProcessingException e) {
        return new UpdateResponse(HttpStatus.S_400_BAD_REQUEST);
    }
    // photo's id and URN should not be changed
    p.setId(key);
    p.setUrn(String.valueOf(key));
    _db.getData().put(key, p);
    return new UpdateResponse(HttpStatus.S_202_ACCEPTED);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) Photo(com.linkedin.restli.example.Photo) DataProcessingException(com.linkedin.data.transform.DataProcessingException)

Example 48 with UpdateResponse

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

the class TestPhotoResource method testResourceDelete.

@Test
public void testResourceDelete() {
    final Long id = createPhoto();
    // validate response status code
    final UpdateResponse uResp = _res.delete(id);
    Assert.assertEquals(uResp.getStatus(), HttpStatus.S_204_NO_CONTENT);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) LatLong(com.linkedin.restli.example.LatLong) Test(org.testng.annotations.Test)

Example 49 with UpdateResponse

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

the class ExampleRequestResponseGenerator method delete.

public ExampleRequestResponse delete() {
    checkSupports(ResourceMethod.DELETE);
    DeleteRequestBuilder<Object, RecordTemplatePlaceholder> delete = new DeleteRequestBuilder<>(_uriTemplate, RecordTemplatePlaceholder.class, _resourceSpec, _requestOptions);
    if (_resourceSpec.getKeyType() != null) {
        delete.id(generateKey());
    }
    addParams(delete, ResourceMethod.DELETE);
    addPathKeys(delete);
    DeleteRequest<RecordTemplatePlaceholder> request = delete.build();
    return buildRequestResponse(request, new UpdateResponse(HttpStatus.S_200_OK), buildResourceMethodDescriptorForRestMethod(request));
}
Also used : BatchDeleteRequestBuilder(com.linkedin.restli.client.BatchDeleteRequestBuilder) DeleteRequestBuilder(com.linkedin.restli.client.DeleteRequestBuilder) UpdateResponse(com.linkedin.restli.server.UpdateResponse)

Example 50 with UpdateResponse

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

the class ExampleRequestResponseGenerator method batchDelete.

public ExampleRequestResponse batchDelete() {
    checkSupports(ResourceMethod.BATCH_DELETE);
    BatchDeleteRequestBuilder<Object, RecordTemplatePlaceholder> delete = new BatchDeleteRequestBuilder<>(_uriTemplate, RecordTemplatePlaceholder.class, _resourceSpec, _requestOptions);
    Object id1 = generateKey(0);
    Object id2 = generateKey(1);
    delete.ids(id1, id2);
    addParams(delete, ResourceMethod.BATCH_DELETE);
    addPathKeys(delete);
    BatchDeleteRequest<Object, RecordTemplatePlaceholder> request = delete.build();
    final Map<Object, UpdateResponse> bdResponseData = new HashMap<>();
    bdResponseData.put(id1, new UpdateResponse(HttpStatus.S_200_OK));
    bdResponseData.put(id2, new UpdateResponse(HttpStatus.S_200_OK));
    BatchUpdateResult<Object, RecordTemplatePlaceholder> result = new BatchUpdateResult<>(bdResponseData);
    return buildRequestResponse(request, result, buildResourceMethodDescriptorForRestMethod(request));
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) BatchDeleteRequestBuilder(com.linkedin.restli.client.BatchDeleteRequestBuilder)

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