Client IP Address
clerum Feb 2, 2009 10:16 PMI'm trying to add some code to my app which will log a message to the database for certain key action. I want it to be injectable so that anywhere in my app I can go
@In ELog elog;
then...
elog.addLog("This is a test log message);One of the variables that I want to be able to write to the db is the clients IP Address. I show this value on my homepage via the #{request.remoteAddr}
However I don't understand how to get that variable from within my Java Code..
package net.domain.demo;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
@Name("elog")
@Stateful
@Scope(ScopeType.SESSION)
public class ELogBean implements ELog {
@In(required=false)
ActiveUser activeuser;
EntityManager em;
public ELogBean(EntityManager em)
{
this.em = em;
}
public void addLog(String type, String msg)
{
System.out.println("Adding Log for IP: #{request.remoteAddr}");
GregorianCalendar now = (GregorianCalendar) GregorianCalendar.getInstance();
System.out.println("Adding log at:" + now.getTimeInMillis());
EventLog newlog = new EventLog(activeuser.getUser(),type,msg,"#{request.remoteAddr}",now);
em.persist(newlog);
}
public void addLogEarly(User user, String type, String msg)
{
GregorianCalendar now = (GregorianCalendar) GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
EventLog newlog = new EventLog(user,type,msg,"#{request.remoteAddr}",now);
System.out.println("Adding Log for " + newlog.getUser().getUsername());
System.out.println("Adding Log for " + newlog.getType());
System.out.println("Adding Log for " + newlog.getMsg());
System.out.println("Adding Log for " + newlog.getIpaddr());
System.out.println("Adding Log for" + newlog.getTime().toString());
if(em == null)
{
System.out.println("Entity Manager is Null!!!!");
}
else
{
em.persist(newlog);
}
}
@Remove
public void destroy()
{
}
}
#{request.remoteAddr} is literally what is persisted to the database. Also I don't see where request
is defined in the components.xml so I don't know what Class I would event Inject it as.
Anyone run into this before?