use of io.micronaut.inject.MethodExecutionHandle in project micronaut-core by micronaut-projects.
the class AbstractNettyWebSocketHandler method callOpenMethod.
/**
* Calls the open method of the websocket bean.
*
* @param ctx THe handler context
*/
protected void callOpenMethod(ChannelHandlerContext ctx) {
if (session == null) {
return;
}
Optional<? extends MethodExecutionHandle<?, ?>> executionHandle = webSocketBean.openMethod();
if (executionHandle.isPresent()) {
MethodExecutionHandle<?, ?> openMethod = executionHandle.get();
BoundExecutable boundExecutable = null;
try {
boundExecutable = bindMethod(originatingRequest, webSocketBinder, openMethod, Collections.emptyList());
} catch (Throwable e) {
if (LOG.isErrorEnabled()) {
LOG.error("Error Binding method @OnOpen for WebSocket [" + webSocketBean + "]: " + e.getMessage(), e);
}
if (session.isOpen()) {
session.close(CloseReason.INTERNAL_ERROR);
}
}
if (boundExecutable != null) {
try {
BoundExecutable finalBoundExecutable = boundExecutable;
Object result = invokeExecutable(finalBoundExecutable, openMethod);
if (Publishers.isConvertibleToPublisher(result)) {
Flux<?> flowable = Flux.from(instrumentPublisher(ctx, result));
flowable.subscribe(o -> {
}, error -> {
if (LOG.isErrorEnabled()) {
LOG.error("Error Opening WebSocket [" + webSocketBean + "]: " + error.getMessage(), error);
}
if (session.isOpen()) {
session.close(CloseReason.INTERNAL_ERROR);
}
}, () -> {
});
}
} catch (Throwable e) {
forwardErrorToUser(ctx, t -> {
if (LOG.isErrorEnabled()) {
LOG.error("Error Opening WebSocket [" + webSocketBean + "]: " + t.getMessage(), t);
}
}, e);
// since we failed to call onOpen, we should always close here
if (session.isOpen()) {
session.close(CloseReason.INTERNAL_ERROR);
}
}
}
}
}
use of io.micronaut.inject.MethodExecutionHandle in project micronaut-core by micronaut-projects.
the class DefaultRouteBuilder method error.
@Override
public ErrorRoute error(Class originatingClass, Class<? extends Throwable> error, Class type, String method, Class[] parameterTypes) {
Optional<MethodExecutionHandle<?, Object>> executionHandle = executionHandleLocator.findExecutionHandle(type, method, parameterTypes);
MethodExecutionHandle<?, Object> executableHandle = executionHandle.orElseThrow(() -> new RoutingException("No such route: " + type.getName() + "." + method));
DefaultErrorRoute errorRoute = new DefaultErrorRoute(originatingClass, error, executableHandle, conversionService);
this.errorRoutes.add(errorRoute);
return errorRoute;
}
use of io.micronaut.inject.MethodExecutionHandle in project micronaut-core by micronaut-projects.
the class TraceInterceptor method intercept.
@Override
public Object intercept(InvocationContext context) {
if (LOG.isTraceEnabled() && context instanceof MethodExecutionHandle) {
MethodExecutionHandle handle = (MethodExecutionHandle) context;
Collection<MutableArgumentValue<?>> values = context.getParameters().values();
LOG.trace("Invoking method {}#{}(..) with arguments {}", context.getTarget().getClass().getName(), handle.getMethodName(), values.stream().map(ArgumentValue::getValue).collect(Collectors.toList()));
}
Object result = context.proceed();
if (LOG.isTraceEnabled() && context instanceof MethodExecutionHandle) {
MethodExecutionHandle handle = (MethodExecutionHandle) context;
LOG.trace("Method {}#{}(..) returned result {}", context.getTarget().getClass().getName(), handle.getMethodName(), result);
}
return result;
}
use of io.micronaut.inject.MethodExecutionHandle in project micronaut-core by micronaut-projects.
the class DefaultRouteBuilder method status.
@Override
public StatusRoute status(Class originatingClass, HttpStatus status, Class type, String method, Class[] parameterTypes) {
Optional<MethodExecutionHandle<?, Object>> executionHandle = executionHandleLocator.findExecutionHandle(type, method, parameterTypes);
MethodExecutionHandle<?, Object> executableHandle = executionHandle.orElseThrow(() -> new RoutingException("No such route: " + type.getName() + "." + method));
DefaultStatusRoute statusRoute = new DefaultStatusRoute(originatingClass, status, executableHandle, conversionService);
this.statusRoutes.add(statusRoute);
return statusRoute;
}
use of io.micronaut.inject.MethodExecutionHandle in project micronaut-core by micronaut-projects.
the class HttpSessionFilter method encodeSessionId.
private Publisher<MutableHttpResponse<?>> encodeSessionId(HttpRequest<?> request, Publisher<MutableHttpResponse<?>> responsePublisher) {
Flux<SessionAndResponse> responseFlowable = Flux.from(responsePublisher).switchMap(response -> {
Optional<MethodExecutionHandle> routeMatch = request.getAttribute(HttpAttributes.ROUTE_MATCH, MethodExecutionHandle.class);
Optional<?> body = response.getBody();
String sessionAttr;
if (body.isPresent()) {
sessionAttr = routeMatch.flatMap(m -> {
if (!m.hasAnnotation(SessionValue.class)) {
return Optional.empty();
} else {
String attributeName = m.stringValue(SessionValue.class).orElse(null);
if (!StringUtils.isEmpty(attributeName)) {
return Optional.of(attributeName);
} else {
throw new InternalServerException("@SessionValue on a return type must specify an attribute name");
}
}
}).orElse(null);
} else {
sessionAttr = null;
}
Optional<Session> opt = request.getAttributes().get(SESSION_ATTRIBUTE, Session.class);
if (opt.isPresent()) {
Session session = opt.get();
if (sessionAttr != null) {
session.put(sessionAttr, body.get());
}
if (session.isNew() || session.isModified()) {
return Flux.from(Publishers.fromCompletableFuture(() -> sessionStore.save(session))).map(s -> new SessionAndResponse(Optional.of(s), response));
}
} else if (sessionAttr != null) {
Session newSession = sessionStore.newSession();
newSession.put(sessionAttr, body.get());
return Flux.from(Publishers.fromCompletableFuture(() -> sessionStore.save(newSession))).map(s -> new SessionAndResponse(Optional.of(s), response));
}
return Flux.just(new SessionAndResponse(opt, response));
});
return responseFlowable.map(sessionAndResponse -> {
Optional<Session> session = sessionAndResponse.session;
MutableHttpResponse<?> response = sessionAndResponse.response;
if (session.isPresent()) {
Session s = session.get();
for (HttpSessionIdEncoder encoder : encoders) {
encoder.encodeId(request, response, s);
}
}
return response;
});
}
Aggregations