Shoaib,
You can use the session in the following way. For your client context, use the commons remote executor to maintain the same session:
Code:
<bean id="httpInvokerProxy" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value>http://${serverName}:${httpPort}${contextPath}/remoting/RemoteService-httpinvoker</value>
</property>
<property name="serviceInterface">
<value>org.timnolan.springsample.domain.logic.RemoteService</value>
</property>
<property name="httpInvokerRequestExecutor">
<ref bean="httpInvokerExecutor"/>
</property>
</bean>
<bean id="httpInvokerExecutor" class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor"/>
To use the session, you can attach it to a ThreadLocal in some way (I've done this by subclassing the Dispatcher servlet, and using ThreadLocals inside a custom class, CurrentThread).
Code:
CurrentThread.setSessionHolder(new SessionHolderImpl(request));
super.doService(request, response);
CurrentThread.cleanUp();
SessionHolderImpl implements SessionHolder interface to hide the Web dependencies.
Code:
public class SessionHolderImpl implements SessionHolder {
private HttpSession session;
public SessionHolderImpl(HttpServletRequest request) {
session = request.getSession();
}
public Object getAttribute(String name) {
return session.getAttribute(name);
}
public void setAttribute(String name, Object value) {
session.setAttribute(name, value);
}
}
Your code can then access the Session.
Code:
public Employee login(String username, String password) {
if (password==null) {
return null;
}
Employee employeeFound = findEmployeeByUsername(username);
if (employeeFound!=null && employeeFound.isActive() && password.equals(employeeFound.getPassword())) {
CurrentThread.getSessionHolder().setAttribute(Constants.EMPLOYEE, employeeFound);
return employeeFound;
}
return null;
}
Having done I wouldn't recommend spreading this code everywhere. I've used it in one case to verify login which works very well. My Web and Remoting clients now use the same custom authentication.