use of org.springframework.web.context.request.async.DeferredResult.DeferredResultHandler in project spring-framework by spring-projects.
the class DeferredResultTests method onTimeout.
@Test
public void onTimeout() throws Exception {
final StringBuilder sb = new StringBuilder();
DeferredResultHandler handler = mock(DeferredResultHandler.class);
DeferredResult<String> result = new DeferredResult<>(null, "timeout result");
result.setResultHandler(handler);
result.onTimeout(new Runnable() {
@Override
public void run() {
sb.append("timeout event");
}
});
result.getInterceptor().handleTimeout(null, null);
assertEquals("timeout event", sb.toString());
assertFalse("Should not be able to set result a second time", result.setResult("hello"));
verify(handler).handleResult("timeout result");
}
use of org.springframework.web.context.request.async.DeferredResult.DeferredResultHandler in project spring-framework by spring-projects.
the class DeferredResultTests method setResultTwice.
@Test
public void setResultTwice() {
DeferredResultHandler handler = mock(DeferredResultHandler.class);
DeferredResult<String> result = new DeferredResult<>();
result.setResultHandler(handler);
assertTrue(result.setResult("hello"));
assertFalse(result.setResult("hi"));
verify(handler).handleResult("hello");
}
use of org.springframework.web.context.request.async.DeferredResult.DeferredResultHandler in project spring-framework by spring-projects.
the class DeferredResultTests method setResult.
@Test
public void setResult() {
DeferredResultHandler handler = mock(DeferredResultHandler.class);
DeferredResult<String> result = new DeferredResult<>();
result.setResultHandler(handler);
assertTrue(result.setResult("hello"));
verify(handler).handleResult("hello");
}
use of org.springframework.web.context.request.async.DeferredResult.DeferredResultHandler in project chassis by Kixeye.
the class ActionInvokingWebSocket method onWebSocketBinary.
public void onWebSocketBinary(byte[] payload, int offset, int length) {
try {
// don't accept empty frames
if (payload == null || length < 1) {
throw new WebSocketServiceException(new ServiceError("EMPTY_ENVELOPE", "Empty envelope!"), "UNKNOWN", null);
}
// check if we need to do psk encryption
byte[] processedPayload = pskFrameProcessor.processIncoming(payload, offset, length);
if (processedPayload != payload) {
payload = processedPayload;
offset = 0;
length = payload.length;
}
// get the envelope
final WebSocketEnvelope envelope = new WebSocketEnvelope(serDe.deserialize(payload, offset, length, Envelope.class));
// gets all the actions
Collection<WebSocketAction> actions = mappingRegistry.getActionMethods(envelope.getAction());
final AtomicInteger invokedActions = new AtomicInteger(0);
// invokes them
for (final WebSocketAction action : actions) {
// get and validate type ID
Class<?> messageClass = null;
if (StringUtils.isNotBlank(envelope.getTypeId())) {
messageClass = messageRegistry.getClassByTypeId(envelope.getTypeId());
}
// validate if action has a payload class that it needs
if (action.getPayloadClass() != null && messageClass == null) {
throw new WebSocketServiceException(new ServiceError("INVALID_TYPE_ID", "Unknown type ID!"), envelope.getAction(), envelope.getTransactionId());
}
// invoke this action if allowed
if (action.canInvoke(webSocketSession, messageClass)) {
invokedActions.incrementAndGet();
final Object handler = handlerCache.get(action.getHandlerClass().getName());
final Class<?> finalMessageClass = messageClass;
ListenableFuture<DeferredResult<?>> invocation = serviceExecutor.submit(new Callable<DeferredResult<?>>() {
@Override
public DeferredResult<?> call() throws Exception {
// then invoke
return action.invoke(handler, new RawWebSocketMessage<>(envelope.getPayload(), finalMessageClass, messageValidator, serDe), envelope, webSocketSession);
}
});
Futures.addCallback(invocation, new FutureCallback<DeferredResult<?>>() {
public void onSuccess(DeferredResult<?> result) {
if (result != null) {
result.setResultHandler(new DeferredResultHandler() {
@Override
public void handleResult(Object result) {
if (result instanceof Exception) {
onFailure((Exception) result);
return;
}
sendResponse(result);
}
});
}
}
public void onFailure(Throwable t) {
if (t instanceof InvocationTargetException) {
t = ((InvocationTargetException) t).getTargetException();
}
ServiceError error = ExceptionServiceErrorMapper.mapException(t);
if (error != null && !ExceptionServiceErrorMapper.VALIDATION_ERROR_CODE.equals(error.code)) {
logger.error("Unexpected exception throw while executing action [{}]", envelope.getAction(), t);
}
sendResponse(error);
}
public Future<Void> sendResponse(Object response) {
try {
return sendMessage(envelope.getAction(), envelope.getTransactionId(), response);
} catch (IOException | GeneralSecurityException e) {
logger.error("Unable to send message to channel", e);
return Futures.immediateFuture(null);
}
}
}, responseExecutor);
}
}
// make sure we actually invoked something
if (invokedActions.get() < 1) {
throw new WebSocketServiceException(new ServiceError("INVALID_ACTION_MAPPING", "No actions invoked."), envelope.getAction(), envelope.getTransactionId());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.springframework.web.context.request.async.DeferredResult.DeferredResultHandler in project spring-framework by spring-projects.
the class DeferredResultTests method hasResult.
@Test
public void hasResult() {
DeferredResultHandler handler = mock(DeferredResultHandler.class);
DeferredResult<String> result = new DeferredResult<>();
result.setResultHandler(handler);
assertFalse(result.hasResult());
assertNull(result.getResult());
result.setResult("hello");
assertEquals("hello", result.getResult());
}
Aggregations