5 Replies Latest reply on Feb 14, 2007 2:36 PM by gavin.king

    ServletRequest.getRemoteAddr()

    monkeyden

      I'm looking for the Seam equivalent of ServletRequest.getRemoteAddr(). The api docs don't tell me much. Thanks.

      http://docs.jboss.com/seam/1.1.6.GA/api/org/jboss/seam/contexts/WebRequestContext.html

        • 1. Re: ServletRequest.getRemoteAddr()

          This might be instructive: http://jboss.org/index.html?module=bb&op=viewtopic&p=3998480

          Obviously, if you want it in Java, just translate the EL to the equivalent calls.

          • 2. Re: ServletRequest.getRemoteAddr()
            monkeyden

            Thanks Norm. That's essentially the same reason I'd like to log the IP. The only difference is, I'd like to do it within an interceptor (like the @LoggedIn example given in the docs). I don't know if it's not working because you cant do this in an Interceptor or because components.xml is wrong.

            @Name("loggedInInterceptor")
            public class LoggedInInterceptor {
            
             @Logger
             private Log log;
            
             @In("#{remoteAddr}") private String ipAddress;
            
             @AroundInvoke
             public Object checkLoggedIn(InvocationContext invocation) throws Exception {
             User user = (User)Contexts.getSessionContext().get("user");
            
             boolean isLoggedIn = user != null;
             try {
             if (isLoggedIn) {
             return invocation.proceed();
             } else {
             throw new SecurityException();
             }
             } catch (SecurityException se) {
             log.info("IP #0 is trying to access a secure location without having logged in.", ipAddress);
             return "/login.seam";
             }
             }
            }


            Given my ignorance of advanced XML, I assume I can use the default namespace, which gives me "http://jboss.com/products/seam/components"

            <?xml version="1.0" encoding="UTF-8"?>
            <components xmlns="http://jboss.com/products/seam/components"
             xmlns:core="http://jboss.com/products/seam/core"
             xmlns:security="http://jboss.com/products/seam/security"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation=
             "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-1.1.xsd
             http://jboss.com/products/seam/components http://jboss.com/products/seam/components-1.1.xsd
             http://jboss.com/products/seam/security http://jboss.com/products/seam/security-1.1.xsd">
             <core:init debug="true" jndi-pattern="nemoves-pws/#{ejbName}/local"/>
             <core:ejb installed="false"/>
             <factory name="remoteAddr" value="#{facesContext.externalContext.request.remoteAddr}"/>
            </components>


            • 3. Re: ServletRequest.getRemoteAddr()
              monkeyden

              Also, this code produces a RequestFacade but it's remoteAddr field is null:

              String ip = FacesContext.getCurrentInstance().getExternalContext().getRequest().getRemoteAddr();

              • 4. Re: ServletRequest.getRemoteAddr()
                gavin.king

                An interceptor is not a Seam component. You can't use @In and friends. Use Component.getInstance(), etc.

                • 5. Re: ServletRequest.getRemoteAddr()
                  gavin.king

                  Or BuiltInComponent.instance(), or whatever.

                  And interceptors should not have @Names.