hilla
hilla copied to clipboard
VaadinRequest.getCurrent() doesn't seem to be working properly in Endpoints
VaadinConnectController has the following lines to configure current request and response: https://github.com/vaadin/flow/blob/4ba194f354ea4ee039324e7ca9d42eece5a50708/fusion-endpoint/src/main/java/com/vaadin/flow/server/connect/VaadinConnectController.java#L279-L282
However, according to VaadinService, VaadinService.getCurrent() will return a non-null value only in the following cases:
- The block surrounded with
runWithServiceContext(...)is executed duringVaadinService.init() - The request is being processed in
VaadinService.handleRequest() - Current
VaadinServicewas set explicitly by callingVaadinService.setCurrent()
According to stacktrace, none of this is happening when VaadinConnectController processes endpoint request. As a result, VaadinRequest instance is incomplete and it's getService() method returns null, which violates method contract.
We may need to reconsider ticket https://github.com/vaadin/flow/issues/7488, The second option provided by manolo might be better,
add some magic API to the controller so as it can inject the request when needed by the user
@Endpoint
public class MyEndpoint {
@AnonymousAllowed
public String echo(String parameter, HttpRequest request) {
if ( request.getUserPrincipal() != null) {
return request.getUserPrincipal().getName();
}
return anonymous;
}
}
I agree with @Legioth on this one: it won't be portable in this case. Ideally, if we want to really share ecosystem, we should bring VaadinConnectController under VaadinService (as another request handler, probably), then we'll naturally get both VaadinXxx.getCurrent() and lay a foundation for OSGi/CDI/anything-that-is-not-Spring support.