Search in sources :

Example 81 with RestLiServiceException

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

the class RestLiMethodInvoker method doInvoke.

@SuppressWarnings("deprecation")
private void doInvoke(final ResourceMethodDescriptor descriptor, final RequestExecutionCallback<Object> callback, final RequestExecutionReportBuilder requestExecutionReportBuilder, final Object resource, final ServerResourceContext resourceContext, final Object... arguments) throws IllegalAccessException {
    final Method method = descriptor.getMethod();
    try {
        switch(descriptor.getInterfaceType()) {
            case CALLBACK:
                int callbackIndex = descriptor.indexOfParameterType(ParamType.CALLBACK);
                final RequestExecutionReport executionReport = getRequestExecutionReport(requestExecutionReportBuilder);
                //Delegate the callback call to the request execution callback along with the
                //request execution report.
                arguments[callbackIndex] = new Callback<Object>() {

                    @Override
                    public void onError(Throwable e) {
                        callback.onError(e instanceof RestLiServiceException ? e : new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, e), executionReport, resourceContext.getRequestAttachmentReader(), resourceContext.getResponseAttachments());
                    }

                    @Override
                    public void onSuccess(Object result) {
                        callback.onSuccess(result, executionReport, resourceContext.getResponseAttachments());
                    }
                };
                method.invoke(resource, arguments);
                // App code should use the callback
                break;
            case SYNC:
                Object applicationResult = method.invoke(resource, arguments);
                callback.onSuccess(applicationResult, getRequestExecutionReport(requestExecutionReportBuilder), resourceContext.getResponseAttachments());
                break;
            case PROMISE:
                if (!checkEngine(resourceContext, callback, descriptor, requestExecutionReportBuilder)) {
                    break;
                }
                int contextIndex = descriptor.indexOfParameterType(ParamType.PARSEQ_CONTEXT_PARAM);
                if (contextIndex == -1) {
                    contextIndex = descriptor.indexOfParameterType(ParamType.PARSEQ_CONTEXT);
                }
                // run through the engine to get the context
                Task<Object> restliTask = new RestLiParSeqTask(arguments, contextIndex, method, resource);
                // propagate the result to the callback
                restliTask.addListener(new CallbackPromiseAdapter<>(callback, restliTask, requestExecutionReportBuilder, resourceContext.getRequestAttachmentReader(), resourceContext.getResponseAttachments()));
                runTask(restliTask, toPlanClass(descriptor));
                break;
            case TASK:
                if (!checkEngine(resourceContext, callback, descriptor, requestExecutionReportBuilder)) {
                    break;
                }
                //addListener requires Task<Object> in this case
                @SuppressWarnings("unchecked") Task<Object> task = (Task<Object>) method.invoke(resource, arguments);
                if (task == null) {
                    callback.onError(new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Error in application code: null Task"), getRequestExecutionReport(requestExecutionReportBuilder), resourceContext.getRequestAttachmentReader(), resourceContext.getResponseAttachments());
                } else {
                    task.addListener(new CallbackPromiseAdapter<>(callback, task, requestExecutionReportBuilder, resourceContext.getRequestAttachmentReader(), resourceContext.getResponseAttachments()));
                    runTask(task, toPlanClass(descriptor));
                }
                break;
            default:
                throw new AssertionError("Unexpected interface type " + descriptor.getInterfaceType());
        }
    } catch (InvocationTargetException e) {
        // InvocationTargetException wrapped around the root cause.
        if (RestLiServiceException.class.isAssignableFrom(e.getCause().getClass())) {
            RestLiServiceException restLiServiceException = (RestLiServiceException) e.getCause();
            callback.onError(restLiServiceException, getRequestExecutionReport(requestExecutionReportBuilder), resourceContext.getRequestAttachmentReader(), resourceContext.getResponseAttachments());
        } else {
            callback.onError(new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, _errorResponseBuilder.getInternalErrorMessage(), e.getCause()), getRequestExecutionReport(requestExecutionReportBuilder), resourceContext.getRequestAttachmentReader(), resourceContext.getResponseAttachments());
        }
    }
}
Also used : BaseTask(com.linkedin.parseq.BaseTask) Task(com.linkedin.parseq.Task) Method(java.lang.reflect.Method) RequestExecutionReport(com.linkedin.restli.server.RequestExecutionReport) InvocationTargetException(java.lang.reflect.InvocationTargetException) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException)

Example 82 with RestLiServiceException

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

the class AutomaticValidationDemoResource method batchUpdate.

@RestMethod.BatchUpdate
public BatchUpdateResult<Integer, ValidationDemo> batchUpdate(final BatchUpdateRequest<Integer, ValidationDemo> entities) {
    Map<Integer, UpdateResponse> results = new HashMap<Integer, UpdateResponse>();
    Map<Integer, RestLiServiceException> errors = new HashMap<Integer, RestLiServiceException>();
    for (Map.Entry<Integer, ValidationDemo> entry : entities.getData().entrySet()) {
        Integer key = entry.getKey();
        ValidationDemo entity = entry.getValue();
        results.put(key, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
    }
    return new BatchUpdateResult<Integer, ValidationDemo>(results, errors);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 83 with RestLiServiceException

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

the class AutomaticValidationDemoResource method get.

@RestMethod.Get
public ValidationDemo get(final Integer key) {
    if (key == 0) {
        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST);
    }
    // Generate an entity that does not conform to the data schema
    ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
    union.setMyEnum(myEnum.BARBAR);
    ValidationDemo validationDemo = new ValidationDemo().setStringA("stringA is readOnly").setUnionFieldWithInlineRecord(union);
    return validationDemo;
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 84 with RestLiServiceException

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

the class ChainedTyperefResource method batchUpdate.

@Override
public BatchUpdateResult<CompoundKey, Greeting> batchUpdate(BatchUpdateRequest<CompoundKey, Greeting> entities) {
    Set<CompoundKey> keys = entities.getData().keySet();
    Map<CompoundKey, UpdateResponse> responseMap = new HashMap<CompoundKey, UpdateResponse>();
    Map<CompoundKey, RestLiServiceException> errorMap = new HashMap<CompoundKey, RestLiServiceException>();
    for (CompoundKey key : keys) {
        responseMap.put(key, new UpdateResponse(HttpStatus.S_201_CREATED));
    }
    return new BatchUpdateResult<CompoundKey, Greeting>(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 85 with RestLiServiceException

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

the class ComplexKeysDataProvider method batchUpdate.

public BatchUpdateResult<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchUpdate(BatchUpdateRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> entities) {
    final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateResponse> results = new HashMap<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateResponse>();
    final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, RestLiServiceException> errors = new HashMap<ComplexResourceKey<TwoPartKey, TwoPartKey>, RestLiServiceException>();
    for (Map.Entry<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> entry : entities.getData().entrySet()) {
        if (_db.containsKey(keyToString(entry.getKey().getKey()))) {
            _db.put(keyToString(entry.getKey().getKey()), entry.getValue());
            results.put(entry.getKey(), new UpdateResponse(HttpStatus.S_200_OK));
        } else {
            errors.put(entry.getKey(), new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
        }
    }
    return new BatchUpdateResult<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>(results, errors);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) Message(com.linkedin.restli.examples.greetings.api.Message) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) HashMap(java.util.HashMap) Map(java.util.Map)

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