hilla icon indicating copy to clipboard operation
hilla copied to clipboard

VaadinRequest.getCurrent() doesn't seem to be working properly in Endpoints

Open fluorumlabs opened this issue 4 years ago • 2 comments

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 during VaadinService.init()
  • The request is being processed in VaadinService.handleRequest()
  • Current VaadinService was set explicitly by calling VaadinService.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.

fluorumlabs avatar Dec 15 '20 10:12 fluorumlabs

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;
   }
}

haijian-vaadin avatar Dec 15 '20 21:12 haijian-vaadin

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.

fluorumlabs avatar Dec 15 '20 22:12 fluorumlabs