Inject HttpServletRequest inside method body (Errai service implementation)

253 Views Asked by At

How to inject HttpServletRequest in a Errai service implementation (server side) for use with code like this:

@Override
public void login(String username, String password, boolean rememberMe) {
    try {
        HttpServletRequest request = null; // <---- Inject here...
        String host = request.getRemoteHost();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe, host);
        try {
            Subject subject = SecurityUtils.getSubject();
            loginWithNewSession(token, subject);
            subject.login(token);
        } catch (AuthenticationException e) {
            throw new IllegalArgumentException("Service could not authenticate caller with the given authorization credentials.");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Something went wrong with the login request");
    }       
}
1

There are 1 best solutions below

0
On BEST ANSWER

You can use the static method RpcContext.getServletRequest() in this case. Internally, it retrieves the HttpServletRequest from a ThreadLocal Message object.