Search in sources :

Example 1 with WebSocketResponseConverter

use of com.kixeye.chassis.transport.websocket.responseconverter.WebSocketResponseConverter in project chassis by Kixeye.

the class WebSocketAction method invoke.

/**
     * Invokes this action.
     *
     * @param handler
     * @param message
     * @param envelope
     * @param session
     * @return
     * @throws MethodArgumentNotValidException
     *
     * @throws InvocationTargetException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
public DeferredResult<?> invoke(Object handler, RawWebSocketMessage<?> message, WebSocketEnvelope envelope, WebSocketSession session) throws Exception {
    Object response = null;
    // invoke the method
    Object[] parameters = new Object[method.getParameterTypes().length];
    Integer payloadParamIndex = parameterTypes.get(ParameterType.ACTION_PAYLOAD);
    if (payloadParamIndex != null) {
        parameters[payloadParamIndex] = message.deserialize(this);
    }
    Integer sessionParamIndex = parameterTypes.get(ParameterType.WEB_SOCKET_SESSION);
    if (sessionParamIndex != null) {
        parameters[sessionParamIndex] = session;
    }
    Integer transactionParamIndex = parameterTypes.get(ParameterType.TRANSACTION_ID);
    if (transactionParamIndex != null) {
        parameters[transactionParamIndex] = envelope.getTransactionId();
    }
    Integer envelopeParamIndex = parameterTypes.get(ParameterType.ENVELOPE);
    if (envelopeParamIndex != null) {
        parameters[envelopeParamIndex] = envelope;
    }
    resolveCustomArguments(parameters, method, session, envelope);
    // now do actual invoke
    response = method.invoke(handler, parameters);
    // otherwise there was no error
    if (response != null) {
        // find a converter
        for (WebSocketResponseConverter converter : responseConverters.get()) {
            if (converter.canConvertResponse(response)) {
                return converter.convertToDeferredResult(response);
            }
        }
        // default to using the object as is
        final DeferredResult<Object> deferredResult = new DeferredResult<>();
        deferredResult.setResult(response);
        return deferredResult;
    } else {
        return null;
    }
}
Also used : WebSocketResponseConverter(com.kixeye.chassis.transport.websocket.responseconverter.WebSocketResponseConverter) DeferredResult(org.springframework.web.context.request.async.DeferredResult)

Aggregations

WebSocketResponseConverter (com.kixeye.chassis.transport.websocket.responseconverter.WebSocketResponseConverter)1 DeferredResult (org.springframework.web.context.request.async.DeferredResult)1