Search in sources :

Example 86 with RestLiServiceException

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

the class ValidationDemoResource method batchCreate.

@RestMethod.BatchCreate
public BatchCreateResult<Integer, ValidationDemo> batchCreate(final BatchCreateRequest<Integer, ValidationDemo> entities, @ValidatorParam RestLiDataValidator validator) {
    List<CreateResponse> results = new ArrayList<CreateResponse>();
    int id = 0;
    for (ValidationDemo entity : entities.getInput()) {
        ValidationResult result = validator.validateInput(entity);
        if (result.isValid()) {
            results.add(new CreateResponse(id));
            id++;
        } else {
            results.add(new CreateResponse(new RestLiServiceException(HttpStatus.S_422_UNPROCESSABLE_ENTITY, result.getMessages().toString())));
        }
    }
    return new BatchCreateResult<Integer, ValidationDemo>(results);
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) CreateResponse(com.linkedin.restli.server.CreateResponse) ArrayList(java.util.ArrayList) BatchCreateResult(com.linkedin.restli.server.BatchCreateResult) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 87 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException 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<Integer, UpdateResponse>();
    Map<Integer, RestLiServiceException> errors = new HashMap<Integer, RestLiServiceException>();
    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<Integer, ValidationDemo>(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 88 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException 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<Long, UpdateResponse>();
    responseMap.put(3l, new UpdateResponse(HttpStatus.S_201_CREATED));
    final Map<Long, RestLiServiceException> errorsMap = new HashMap<Long, RestLiServiceException>();
    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<Long, Greeting>(null);
    } else if (entities.getData().containsKey(3l)) {
        //Return a BatchUpdateResult with a null errors Map
        return new BatchUpdateResult<Long, Greeting>(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<Long, Greeting>(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<Long, Greeting>(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<Long, Greeting>(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<Long, UpdateResponse>(responseMap);
        return new BatchUpdateResult<Long, Greeting>(concurrentResponseMap, new ConcurrentHashMap<Long, RestLiServiceException>());
    }
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) 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)

Example 89 with RestLiServiceException

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

the class StreamingGreetings method create.

public void create(Greeting entity, @CallbackParam Callback<CreateResponse> callback, @RestLiAttachmentsParam RestLiAttachmentReader attachmentReader) {
    if (attachmentReader != null) {
        final String headerValue = getContext().getRequestHeaders().get("createHeader");
        getContext().setResponseHeader("createHeader", headerValue);
        attachmentReader.registerAttachmentReaderCallback(new GreetingBlobReaderCallback(callback));
        return;
    }
    callback.onError(new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "You must supply some attachments!"));
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ByteString(com.linkedin.data.ByteString)

Example 90 with RestLiServiceException

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

the class StringKeysResource method batchGet.

@RestMethod.BatchGet
public Map<String, Message> batchGet(Set<String> ids) {
    Map<String, Message> batch = new HashMap<String, Message>();
    Map<String, RestLiServiceException> errors = new HashMap<String, RestLiServiceException>();
    for (String id : ids) {
        Message g = _db.get(id);
        if (g != null) {
            batch.put(id, g);
        } else {
            errors.put(id, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
        }
    }
    return new BatchResult<String, Message>(batch, errors);
}
Also used : Message(com.linkedin.restli.examples.greetings.api.Message) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) BatchResult(com.linkedin.restli.server.BatchResult)

Aggregations

RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)93 Test (org.testng.annotations.Test)36 HashMap (java.util.HashMap)31 UpdateResponse (com.linkedin.restli.server.UpdateResponse)18 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)17 RoutingException (com.linkedin.restli.server.RoutingException)17 RestException (com.linkedin.r2.message.rest.RestException)16 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)16 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)16 BeforeTest (org.testng.annotations.BeforeTest)16 DataMap (com.linkedin.data.DataMap)14 RequestExecutionReportBuilder (com.linkedin.restli.server.RequestExecutionReportBuilder)13 Map (java.util.Map)13 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)12 EmptyRecord (com.linkedin.restli.common.EmptyRecord)12 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)12 BatchUpdateResult (com.linkedin.restli.server.BatchUpdateResult)11 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)11 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)11 RecordTemplate (com.linkedin.data.template.RecordTemplate)10