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());
}
}
}
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);
}
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;
}
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);
}
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);
}
Aggregations