package net.forcedata.common;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import net.forcedata.asset.ibatis.domain.LoginVO;
import java.util.Enumeration;
import net.forcedata.asset.ibatis.service.LoginService;
import net.forcedata.asset.struts.LoginForm;
import org.ajaxanywhere.AAUtils;
import org.apache.struts.actions.DispatchAction;
/*
*<rickcr-r> and then maybe when they login you set the datasource name
<rickcr-r> hmm
<Spinlock> ya, that might work
<Jerome> boo
<rickcr-r> yea that's what id do
<rickcr-r> hi jerome
<Jerome> select tableName.* from databaseName.tableName where tableName.bla = 4
<Jerome> problem sovled.
<Jerome> Here's the ibatis version
<Jerome> select tableName.* from $clientName$.tableName where tableName.bla = 4
<Jerome> iBatis version
<Spinlock> hmm, I like it
<Spinlock> just trying to think how it would work
<Jerome> or rather
<Jerome> select tableName.* from $clientName$.tableName where tableName.bla = #valueObject.bla#
<Jerome> queryForList("select bla", Map<String,String>())
<Jerome> where the map =
<Jerome> map (clientName => 'Rickisgayinc', valueObject => new BallsInMyMouth(40))
<Spinlock> haha
<Spinlock> but at the log in page I need to have the company name in the url as the company log show up on the login page
<Spinlock> but that's minor potato's
<Jerome> ez
<Spinlock> next q, is sata-2 raid hot swappable?
<Jerome> Which raid mode
<Spinlock> 5
<Jerome> Go raid 5
<Jerome> Yesh
<Jerome> (IF you have the hotswappable drive bays)
<Spinlock> ah yes, I've seen those
<Spinlock> so if there's 3 drives in total, if one fails, just a simple swap and the array builds itself while tomcat is still dish'n up content?
<Jerome> Yes
<Jerome> You could lose one drive
<Spinlock> oh good :)
*/
/**
* This MemberFilter will check to see if the user is currently logged in. If
* they are not logged in the user will automatically be set up with an the
* anonymous log in. This log in is defined as the first record in the
* database.
*/
public class MemberFilter extends DispatchAction implements Filter {
FilterConfig config;
// Create Log4j category instance for logging
// static private org.apache.log4j.Category log = org.apache.log4j.Category.getInstance(MemberFilter.class.getName());
/**
* Simple Filter init that can be used to initialize the filter the first time
* it is loaded.
*
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig config) throws ServletException{
this.config = config;
}
/**
* Checks to see if the user is logged in (they have a member object in the session.)
*
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.Fi
* lterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest servletRequest = (HttpServletRequest) request;
HttpSession session = servletRequest.getSession();
//SECTION FOR J2ME AUTHENTICATION
String cookieSession = servletRequest.getHeader("Set-Cookie");
String sessionID = servletRequest.getHeader("session-id");
//System.out.println("*** COOKIE SESSION ***: " + sessionID);
//System.out.println("*** session id ***: " + servletRequest.getHeader("session-id"));
/************************** BEGIN TESTING ***************************************/
String ss = servletRequest.getQueryString();
String sr = servletRequest.getRequestURI();
System.out.println("sr value: " + sr);
String gm = servletRequest.getMethod();
String queryString = servletRequest.getQueryString();
LoginVO lVO = (LoginVO) session.getAttribute("User-Information");
System.out.println("lVO value: " + lVO);
String[] clients = new String[]{"192", "localhost"/*, "rdsb"*/};
//int clientId = -1;
if(lVO == null) {
System.out.println("LVO IS NULL");
int clientId = -1;
System.out.println("URI: " + servletRequest.getServerName());
String who = servletRequest.getServerName();
if(!who.equals("localhost")) {
who = who.substring(0, who.indexOf('.'));
}
if(who.equals("192")) {
clientId = 0;
} else if(who.equals("localhost")) {
clientId = 1;
} else if(who.equals("rdsb")) {
clientId = 2;
}
System.out.println("clientId: " + clientId);
session.setAttribute("clientId", clientId + "");
BaseDAO dao = new BaseDAO(clients[clientId]);
session.setAttribute("dao", dao);
request.setAttribute("cap", clients[clientId]);
//ServletContext sc = session.getServletContext();
//ArrayList lll = (ArrayList) sc.getAttribute("capsule");
//request.setAttribute("cap", clients[clientId]);
}
String cid = (String) session.getAttribute("clientId");
System.out.println("cid: " + cid);
int clientId = Integer.parseInt(cid);
ServletContext sc = session.getServletContext();
ArrayList capsule = (ArrayList) sc.getAttribute("capsule");
ClientCapsule cc = (ClientCapsule) capsule.get(clientId);
cc.dao = (BaseDAO) session.getAttribute("dao");
request.setAttribute("locations", cc.locations);
request.setAttribute("userNames", cc.userNames);
request.setAttribute("supplierNames", cc.supplierNames);
request.setAttribute("accounts", cc.accounts);
request.setAttribute("departments", cc.departments);
request.setAttribute("tagCategories", cc.tagCategories);
request.setAttribute("workorderNotificationsEmail", cc.workorderNotificationsEmail);
if(ss == null)
ss = "";
if(sessionID != null) { //used for updating data on the server - j2me
if(!sessionID.equals("")) {
chain.doFilter(request, response);
}
} else if(cookieSession != null) { //used for getting data from the server - j2me
if(!cookieSession.equals("")) {
chain.doFilter(request, response);
}
} else { // WEB APP AUTHENTICATION
if(ss.equals("dispatch=perfAbsorb")) { //GET THE INCOMING DATA FROM THE SENSORS
chain.doFilter(request, response);
} else if(ss.indexOf("alAuth") != -1) { //if wer're capturing the human answsering Lucky Charm's call, store in the db.
chain.doFilter(request,response); //pass control to proper method
}
/* NEEDED TO PIDGEON HOLE BUT IS GETTING CAUGHT ON .CSS FILE, used to stop the pages from loading twice, need to rework this part
else if(sr.indexOf('.') != -1) {
System.out.println("caught an error, yay");
}
*/
else if(lVO == null) { //login vo is null
if(request.getParameter("goLogin") != null) {
System.out.println("USER IS ON THE LOG IN PAGETRYING TO GET IN");
chain.doFilter(request, response); //userLogin?dispatch=userLogin //time tested and true
} else {
//setLoginPic(lVO, request);
RequestDispatcher rd = servletRequest.getRequestDispatcher("/");
rd.forward(request, response);
}
} else { //user is already logged in, business as usual
System.out.println("business as usual");
request.setAttribute("lVO", lVO); //put the userinfo in the reqeust for application permissions
chain.doFilter(request, response);
}
}
}
private void setLoginPic(LoginVO lVO, ServletRequest request ) {
/*
if (AAUtils.isAjaxRequest(request)){
System.out.println("AJAX REQUEST MEMBER FILTERzzzzzz");
AAUtils.addZonesToRefresh(request,"userPicZone");
LoginService service = LoginService.getInstance();
lVO = new LoginVO();
String userName = request.getParameter("username");
lVO.setUsername(userName);
try {
lVO = (LoginVO) service.getUserPic(lVO);
} catch (DaoException ex) {
ex.printStackTrace();
}
if(lVO != null) {
request.setAttribute("userName",lVO.getUserPic());
}
}
*/
}
/**
* Not implemented.
* @see javax.servlet.Filter#destroy()
*/
public void destroy(){}
}