import BH.*; |
= |
import BH.*; |
import java.io.IOException; |
|
import java.io.IOException; |
import javax.servlet.*; |
|
import javax.servlet.*; |
import javax.servlet.http.*; |
|
import javax.servlet.http.*; |
import java.io.*; |
|
import java.io.*; |
import java.util.*; |
|
import java.util.*; |
import java.text.SimpleDateFormat; |
|
import java.text.SimpleDateFormat; |
import java.text.DateFormat; |
|
import java.text.DateFormat; |
import java.util.function.*; |
|
import java.util.function.*; |
import static java.util.Arrays.asList; |
|
import static java.util.Arrays.asList; |
import java.util.Random; |
+- |
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
public class sessionServlet extends HttpServlet { |
= |
public class sessionServlet extends HttpServlet { |
private List<String[]> the_sessions; |
|
private List<String[]> the_sessions; |
private DateFormat df; |
|
private DateFormat df; |
public String the_webapp=""; |
+- |
|
private ReentrantLock sessionDBlock; |
|
|
|
= |
|
public void init() throws ServletException { |
|
public void init() throws ServletException { |
the_sessions=new ArrayList<String[]>(); |
|
the_sessions=new ArrayList<String[]>(); |
the_webapp=servletConfig.getServletContext().getInitParameter("thewebapp"); |
+- |
|
df=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); |
= |
df=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); |
sessionDBlock = new ReentrantLock(); |
+- |
|
} |
= |
} |
protected void doGet(HttpServletRequest req, HttpServletResponse res) |
|
protected void doGet(HttpServletRequest req, HttpServletResponse res) |
throws ServletException, IOException |
|
throws ServletException, IOException |
{ |
|
{ |
if ((!(req.getParameter("task")==null))&&(req.getParameter("task").trim().equals("deploy"))) { |
|
if ((!(req.getParameter("task")==null))&&(req.getParameter("task").trim().equals("deploy"))) { |
the_sessions=new ArrayList<String[]>(); |
|
the_sessions=new ArrayList<String[]>(); |
PrintWriter out = res.getWriter(); |
|
PrintWriter out = res.getWriter(); |
out.println("<html>"); |
|
out.println("<html>"); |
out.println("<body>"); |
|
out.println("<body>"); |
String now=new SimpleDateFormat("MM-dd-yyyy HH:mm:ss").format(new Date()); |
|
String now=new SimpleDateFormat("MM-dd-yyyy HH:mm:ss").format(new Date()); |
out.println("<hr /><center><h1>sessionServlet Redeployed at "+now+"</h1></center><hr />"); |
|
out.println("<hr /><center><h1>sessionServlet Redeployed at "+now+"</h1></center><hr />"); |
out.println("</body>"); |
|
out.println("</body>"); |
out.println("</html>"); |
|
out.println("</html>"); |
return; |
|
return; |
} |
|
} |
Consumer <String> forwardTo =(s) ->ForwardTo(s,req,res); |
|
Consumer <String> forwardTo =(s) ->ForwardTo(s,req,res); |
boolean is_first_visit=true; |
|
boolean is_first_visit=true; |
String[] this_session=new String[3]; |
|
String[] this_session=new String[3]; |
String session_string =req.getParameter("session_string");// req.getRemoteAddr(); |
<> |
String ip = req.getRemoteAddr(); |
if (!(session_string==null)) { |
|
|
for (String [] a_session :the_sessions) { |
|
for (String [] a_session :the_sessions) { |
if (a_session[0].equals(session_string)) { //Found an active session |
|
if (a_session[0].equals(ip)) { //Found an active session |
if (req.getParameter("task").trim().equals("99")) { |
|
|
the_sessions.remove(a_session); |
|
|
forwardTo.accept("endSession.jsp"); |
|
|
return; |
|
|
} |
|
|
is_first_visit=false; |
|
is_first_visit=false; |
this_session=a_session; |
|
this_session=a_session; |
break; |
|
break; |
} |
|
|
} |
= |
} |
} |
|
} |
if ((req.getParameter("task")==null)&&(!is_first_visit)) { |
|
if ((req.getParameter("task")==null)&&(!is_first_visit)) { |
the_sessions.remove(this_session); |
|
the_sessions.remove(this_session); |
is_first_visit=true; |
|
is_first_visit=true; |
} |
|
} |
req.setAttribute("thesessioncount",the_sessions.size()); |
|
req.setAttribute("thesessioncount",the_sessions.size()); |
if (is_first_visit) { |
|
if (is_first_visit) { |
if (the_sessions.size()==10) { |
|
if (the_sessions.size()==10) { |
forwardTo.accept("noSessions.jsp"); |
|
forwardTo.accept("noSessions.jsp"); |
return; |
|
return; |
} |
|
} |
session_string=getRandomString(); |
<> |
|
String[] new_session = {session_string,df.format(new Date()),"need a name"}; |
|
String[] new_session = {ip,df.format(new Date()),"need a name"}; |
the_sessions.add(new_session); |
= |
the_sessions.add(new_session); |
this_session=new_session; |
|
this_session=new_session; |
req.setAttribute("thesessionstring",session_string); |
+- |
|
forwardTo.accept("startSession.jsp"); |
= |
forwardTo.accept("startSession.jsp"); |
return; |
|
return; |
} |
|
} |
String the_name=""; |
|
String the_name=""; |
String the_pw=""; |
|
String the_pw=""; |
if (this_session[2].equals("need a name")) { |
|
if (this_session[2].equals("need a name")) { |
the_name=req.getParameter("whoisit"); |
|
the_name=req.getParameter("whoisit"); |
the_pw=req.getParameter("passwd"); |
|
the_pw=req.getParameter("passwd"); |
if ((the_name==null)||(the_name.trim().length()==0)||checkPW(the_name,the_pw)) { |
|
if ((the_name==null)||(the_name.trim().length()==0)||checkPW(the_name,the_pw)) { |
the_sessions.remove(this_session); |
|
the_sessions.remove(this_session); |
req.setAttribute("thesessioncount",the_sessions.size()); |
|
req.setAttribute("thesessioncount",the_sessions.size()); |
forwardTo.accept("startSession.jsp"); |
|
forwardTo.accept("startSession.jsp"); |
return; |
|
return; |
} |
|
} |
} |
|
} |
this_session[2]=the_name.trim(); |
|
this_session[2]=the_name.trim(); |
req.setAttribute("thename", this_session[2]); |
|
req.setAttribute("thename", this_session[2]); |
if (tooLong(this_session[1],df.format(new Date()))) { |
|
if (tooLong(this_session[1],df.format(new Date()))) { |
the_sessions.remove(this_session); |
|
the_sessions.remove(this_session); |
forwardTo.accept("Expired.jsp"); |
|
forwardTo.accept("Expired.jsp"); |
return; |
|
return; |
} else { |
|
} else { |
this_session[1]=df.format(new Date()); |
|
this_session[1]=df.format(new Date()); |
NotesBean thesenotes=new NotesBean(); |
|
NotesBean thesenotes=new NotesBean(); |
if (!req.getParameter("task").trim().equals("0")) { |
|
if (!req.getParameter("task").trim().equals("0")) { |
sessionDBlock.lock(); |
<> |
|
try { |
|
|
thesenotes.setAll(req.getParameter("java_source"),Integer.parseInt(req.getParameter("version"))); |
|
thesenotes.setAll(req.getParameter("java_source"),Integer.parseInt(req.getParameter("version"))); |
if (req.getParameter("task").trim().equals("2")) { |
|
if (req.getParameter("task").trim().equals("2")) { |
thesenotes.setNotes(req.getParameter("notes"),req.getParameter("java_source"),Integer.parseInt(req.getParameter("version"))); |
|
thesenotes.setNotes(req.getParameter("notes"),req.getParameter("java_source"),Integer.parseInt(req.getParameter("version"))); |
} |
|
|
} finally { |
|
|
sessionDBlock.unlock(); |
|
|
} |
= |
} |
|
+- |
|
} |
= |
} |
req.setAttribute("thesessioncount",the_sessions.size()); |
|
req.setAttribute("thesessioncount",the_sessions.size()); |
req.setAttribute("thesessionstring",session_string); |
+- |
|
req.setAttribute("theBean",thesenotes); |
= |
req.setAttribute("theBean",thesenotes); |
req.setAttribute("theURL", "http://www.umsl.edu/~siegelj/turing.jpg"); |
|
req.setAttribute("theURL", "http://www.umsl.edu/~siegelj/turing.jpg"); |
forwardTo.accept("getNotes.jsp"); |
|
forwardTo.accept("getNotes.jsp"); |
return; |
|
return; |
} |
|
} |
} |
|
} |
|
|
|
boolean tooLong(String now,String then){ |
|
boolean tooLong(String now,String then){ |
|
|
|
return false; |
|
return false; |
} |
|
} |
boolean checkPW(String name,String password){ |
|
boolean checkPW(String name,String password){ |
|
|
|
return false; |
|
return false; |
} |
|
} |
|
|
|
public void log(String s){ |
|
public void log(String s){ |
FileWriter fileWriter = null; |
|
FileWriter fileWriter = null; |
try { |
|
try { |
String content =s+" at :"+new Date(System.currentTimeMillis()).toString()+"\n"; |
|
String content =s+" at :"+new Date(System.currentTimeMillis()).toString()+"\n"; |
File theLogFile = new File(the_webapp+"/session.log"); |
<> |
File theLogFile = new File("/var/lib/tomcat/webapps/j-siegel/WEB-INF/classes/session.log"); |
fileWriter = new FileWriter(theLogFile,true); |
= |
fileWriter = new FileWriter(theLogFile,true); |
fileWriter.write(content); |
|
fileWriter.write(content); |
} catch (IOException ex) { |
|
} catch (IOException ex) { |
} finally { |
|
} finally { |
try { |
|
try { |
fileWriter.close(); |
|
fileWriter.close(); |
} catch (IOException ex) { |
|
} catch (IOException ex) { |
|
|
|
} |
|
} |
} |
|
} |
|
|
|
} |
|
} |
|
|
|
void ForwardTo(String s,HttpServletRequest req, HttpServletResponse res) |
|
void ForwardTo(String s,HttpServletRequest req, HttpServletResponse res) |
{ |
|
{ |
RequestDispatcher rd= req.getRequestDispatcher(s); |
|
RequestDispatcher rd= req.getRequestDispatcher(s); |
try { |
|
try { |
rd.forward(req, res); |
|
rd.forward(req, res); |
} catch (IOException|ServletException is) { |
|
} catch (IOException|ServletException is) { |
log(" req from "+req.getRemoteAddr()+" not forwarded at "); |
|
log(" req from "+req.getRemoteAddr()+" not forwarded at "); |
try { |
|
try { |
throw is; |
|
throw is; |
} catch (Exception e) { |
|
} catch (Exception e) { |
} |
|
} |
} |
|
} |
} |
|
} |
|
|
|
public void destroy() |
|
public void destroy() |
{ |
|
{ |
log("The instance was destroyed"); |
|
log("The instance was destroyed"); |
} |
|
} |
public static String getRandomString(){ |
+- |
|
byte[] randbyte=new byte[10]; |
|
|
Random rand = new Random(System.currentTimeMillis()); |
|
|
for (int idx = 0; idx <10; ++idx) { |
|
|
int randomInt = rand.nextInt(26); //0<=randomInt<26 |
|
|
System.out.print(randomInt+" "); |
|
|
randbyte[idx]=(byte)(randomInt+65); //"A" |
|
|
} |
|
|
try { |
|
|
return new String(randbyte, "UTF-8"); |
|
|
} catch (Exception e) { |
|
|
return "bad string" ; |
|
|
} |
|
|
} |
|
|
} |
= |
} |
|
|
|
|
+- |
|