Search in sources :

Example 1 with UnstructuredDataReactiveResult

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

the class GreetingUnstructuredDataCollectionResourceReactive method get.

@Override
public void get(String key, @CallbackParam Callback<UnstructuredDataReactiveResult> callback) {
    if (key.equals("callbackError")) {
        callback.onError(new NoPermissionException("missing access permission"));
        return;
    }
    Writer<ByteString> writer = chooseGreetingWriter(key);
    String contentType;
    if (key.equals("goodNullContentType")) {
        contentType = null;
    } else {
        contentType = MIME_TYPE;
    }
    UnstructuredDataReactiveResult result = new UnstructuredDataReactiveResult(EntityStreams.newEntityStream(writer), contentType);
    callback.onSuccess(result);
}
Also used : ByteString(com.linkedin.data.ByteString) UnstructuredDataReactiveResult(com.linkedin.restli.server.UnstructuredDataReactiveResult) NoPermissionException(javax.naming.NoPermissionException) ByteString(com.linkedin.data.ByteString)

Example 2 with UnstructuredDataReactiveResult

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

the class FeedDownloadResourceReactive method get.

@Override
public void get(Long key, @CallbackParam Callback<UnstructuredDataReactiveResult> callback) {
    ChunkedByteStringWriter writer = new ChunkedByteStringWriter(CONTENT, 2);
    callback.onSuccess(new UnstructuredDataReactiveResult(EntityStreams.newEntityStream(writer), CONTENT_TYPE));
}
Also used : UnstructuredDataReactiveResult(com.linkedin.restli.server.UnstructuredDataReactiveResult) ChunkedByteStringWriter(com.linkedin.data.ChunkedByteStringWriter)

Example 3 with UnstructuredDataReactiveResult

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

the class UnstructuredDataReactiveCollectionResource method get.

@Override
public void get(String key, @CallbackParam Callback<UnstructuredDataReactiveResult> callback) {
    Writer<ByteString> writer = new SingletonWriter<>(ByteString.copy(UNSTRUCTURED_DATA_BYTES));
    UnstructuredDataReactiveResult result = new UnstructuredDataReactiveResult(EntityStreams.newEntityStream(writer), MIME_TYPE);
    callback.onSuccess(result);
}
Also used : ByteString(com.linkedin.data.ByteString) UnstructuredDataReactiveResult(com.linkedin.restli.server.UnstructuredDataReactiveResult) SingletonWriter(com.linkedin.entitystream.SingletonWriter)

Example 4 with UnstructuredDataReactiveResult

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

the class RestLiMethodInvoker method doInvoke.

@SuppressWarnings("deprecation")
private void doInvoke(final ResourceMethodDescriptor descriptor, final ResourceMethodConfig methodConfig, final RestLiCallback callback, final Object resource, final ServerResourceContext resourceContext, final Object... arguments) throws IllegalAccessException {
    final Method method = descriptor.getMethod();
    final RequestContext requestContext = resourceContext.getRawRequestContext();
    TimingContextUtil.endTiming(requestContext, FrameworkTimingKeys.SERVER_REQUEST_RESTLI.key());
    TimingContextUtil.endTiming(requestContext, FrameworkTimingKeys.SERVER_REQUEST.key());
    TimingContextUtil.beginTiming(requestContext, FrameworkTimingKeys.RESOURCE.key());
    try {
        switch(descriptor.getInterfaceType()) {
            case CALLBACK:
                int callbackIndex = descriptor.indexOfParameterType(ParamType.CALLBACK);
                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));
                    }

                    @Override
                    public void onSuccess(Object result) {
                        if (result instanceof UnstructuredDataReactiveResult) {
                            UnstructuredDataReactiveResult reactiveResult = (UnstructuredDataReactiveResult) result;
                            resourceContext.setResponseEntityStream(reactiveResult.getEntityStream());
                            resourceContext.setResponseHeader(RestConstants.HEADER_CONTENT_TYPE, reactiveResult.getContentType());
                            callback.onSuccess(new EmptyRecord());
                        } else {
                            callback.onSuccess(result);
                        }
                    }
                };
                method.invoke(resource, arguments);
                // App code should use the callback
                break;
            case SYNC:
                Object applicationResult = method.invoke(resource, arguments);
                callback.onSuccess(applicationResult);
                break;
            case PROMISE:
                if (!checkEngine(callback, descriptor)) {
                    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 = withTimeout(createRestLiParSeqTask(arguments, contextIndex, method, resource), methodConfig);
                // propagate the result to the callback
                restliTask.addListener(new CallbackPromiseAdapter<>(callback));
                addListenerFromContext(restliTask, resourceContext);
                runTask(restliTask, toPlanClass(descriptor));
                break;
            case TASK:
                if (!checkEngine(callback, descriptor)) {
                    break;
                }
                // addListener requires Task<Object> in this case
                @SuppressWarnings("unchecked") Task<Object> task = withTimeout((Task<Object>) method.invoke(resource, arguments), methodConfig);
                if (task == null) {
                    callback.onError(new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Error in application code: null Task"));
                } else {
                    task.addListener(new CallbackPromiseAdapter<>(callback));
                    addListenerFromContext(task, resourceContext);
                    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);
        } else {
            callback.onError(new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, _internalErrorMessage, e.getCause()));
        }
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) UnstructuredDataReactiveResult(com.linkedin.restli.server.UnstructuredDataReactiveResult) RequestContext(com.linkedin.r2.message.RequestContext)

Example 5 with UnstructuredDataReactiveResult

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

the class GreetingUnstructuredDataAssociationResourceReactive method get.

@Override
public void get(CompoundKey key, @CallbackParam Callback<UnstructuredDataReactiveResult> callback) {
    Writer<ByteString> writer = new SingletonWriter<>(ByteString.copy(UNSTRUCTURED_DATA_BYTES));
    callback.onSuccess(new UnstructuredDataReactiveResult(EntityStreams.newEntityStream(writer), MIME_TYPE));
}
Also used : ByteString(com.linkedin.data.ByteString) UnstructuredDataReactiveResult(com.linkedin.restli.server.UnstructuredDataReactiveResult) SingletonWriter(com.linkedin.entitystream.SingletonWriter)

Aggregations

UnstructuredDataReactiveResult (com.linkedin.restli.server.UnstructuredDataReactiveResult)6 ByteString (com.linkedin.data.ByteString)4 SingletonWriter (com.linkedin.entitystream.SingletonWriter)3 ChunkedByteStringWriter (com.linkedin.data.ChunkedByteStringWriter)1 RequestContext (com.linkedin.r2.message.RequestContext)1 EmptyRecord (com.linkedin.restli.common.EmptyRecord)1 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 NoPermissionException (javax.naming.NoPermissionException)1