Vaadin 7.0.5 HttpServletRequest

Hi,

i cant find a way to get HttpServletRequest con my Vaadin 7.0.5 Application. There is a way to do that?? I need to grab a com.sun.security.auth.UserPrincipal and cant find a way to do that without HttpServletRequest.

I need something like HttpServletRequestListenere here https://vaadin.com/forum/#!/thread/160260 !!

Tnx.

Can you extend the VaadinServlet class, then override the service method?
Something like below:


public class TestServlet extends VaadinServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //code for pulling UserPrincipal goes here...
        
        super.service(request, response);
    }
}

Note that this method will work only if your code doesn’t need any Vaadin-isms and can rely on pure servlet code. It would be really helpful if there were some sort of callback to process a request after Vaadin has set up all of its threadlocals, but before it’s processed the specific request. We’d like to be able to call on VaadinSession.getCurrent().getUIs(), but it won’t work because if you put code before super.service(), it won’t be set up yet, and if put code after, the vaadin session info will have been cleared.

I’m trying to use VaadinServlet too. I’m able to grab UserPrincipal and redirect too my UI after that. Now i need a way to pass data from VaadinServlet to UI.

Maybe the changes in 7.1 will make this easier (see https://vaadin.com/forum#!/thread/3209316), but our hack for now is in our UI.init() we set a session attribute that represents our UI (we only have one per session AFAIK):

VaadinSession.getCurrent().getSession().setAttribute(“EsfVaadinUI”, this);

and then in our extended VaadinServlet’s service() method we do:

EsfVaadinUI ui = (EsfVaadinUI)request.getSession().getAttribute(“EsfVaadinUI”);

Of course you want to check that you have the attribute before you set anything as the service() method is called before your UI.init() will have been called to set it.

Okay, I actually found a perhaps cleaner way since we are already subclassing VaadinServlet. I found this based on setting up CustomizedSystemMessages as described in Section 4.5.2 https://vaadin.com/book/vaadin7/-/page/application.errors.html

Use the service() method to override determining the a login is required and needs a redirect.

But instead of storing the UI instance in the session, just override the servletInitialized() method in you subclassed VaadinServlet, too, since you get then get UI, VaadinService, etc. at this point. It will then allow you to call your UI as needed. In our case, we use it twice, once to tell our application about a new request (we track user, IP, date/time etc), and also to configure system messages to our liking.


    @Override
    protected void servletInitialized() throws ServletException {
        super.servletInitialized();
        
        EsfVaadinUI ui = (EsfVaadinUI)UI.getCurrent();
		if ( ui != null ) {
			ui.setHttpRequest(VaadinService.getCurrentRequest());
		}

		getService().setSystemMessagesProvider(
        	    new SystemMessagesProvider() {
					private static final long serialVersionUID = 5776072298071825406L;

				@Override 
        	    public SystemMessages getSystemMessages(SystemMessagesInfo systemMessagesInfo) {
        	        CustomizedSystemMessages messages = new CustomizedSystemMessages();
        	        EsfVaadinUI ui = (EsfVaadinUI)UI.getCurrent();
        	        if ( ui != null ) {
        	        	ui.setCustomizedSystemMessages(messages);
        	        }
        	        return messages;
        	    }
        	   });
    }

Tnx David,

i will try this way too. But maybe it’s little different to what i need. I dont intialize UI first. So if i try to use VaadinSession in VadinServlet i have NullPointerException.

Unfortunately i cant wait Vaadin 7.1 ç_ç

Done it with some triks, i dont know if this is the best way.

Or authentication system send username to LoginServlet (extend VaadinServlet). LogintServlet add the username in the session and redirect to our UI. In the UI i get username with:


String username = (String) VaadinService.getCurrentRequest().getWrappedSession().getAttribute("username");

Any comment about this system is welcome :slight_smile:

I’m not sure when exactly the API has been added, but you can ask for the principal as follows:

VaadinService.getCurrentRequest().getUserPrincipal()

You can even gain access to a HttpServletRequest the similar way, to e.g. perform a programmatic login:

((VaadinServletRequest) VaadinService.getCurrentRequest()).getHttpServletRequest().login("nbusr", "nbusr123");