package net.matrix_rad.servlet; import java.lang.*; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import java.net.URLDecoder; //import java.net.URLEncoder; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; import javax.servlet.*; import javax.servlet.http.*; import net.matrix_rad.skang.*; import net.matrix_rad.security.*; /** * Master servlet that wraps all servlet stuff. * * @.requires Java 1.1 at least. * @author David Seikel * @.copyright 2002 David Seikel * @version 0.1 prototype 2002-12-15 03:30:00 * * @.fyi Servlets were not really designed for anything other than HTTP. The reference servlet engine (Tomcat) reflects this. On the other hand, you cannot implement HttpServlet, only Servlet. On the gripping hand, due to the above mentioned "design", Servlet and all its bits are really HttpServlet, ServletRequest and ServletResponse. This is quite handy, as otherwise, Sun forgot to include needed API stuff in the base classes. Since we need to implement rather than extend (so that this can be a skang module) we have to do things the strange way. If this ever gets run under any servlet engine other than Tomcat or skangs built in engine, then this may have to be "fixed". */ public final class Master extends Skang implements Servlet, SingleThreadModel, OutListener { // Should groak(POST,COOKIE,HEADER,CGI) at end of servlet call. // Maybe groak(SERVLET) on servlet stop. // Should groak(SERVLET) and regrok on servlet reload. (regrok DONE) // Only SERVLET used presently, POST is commented out, no others ever mentioned. // Each groak or regrok requires a grokInFullness(). // Should sort out static Thingspace concurrency issues. private static final class DecodedRequest extends Object { public DecodedRequest(PrintStream aWriter, ServletRequest aRequest, ServletResponse aResponse) throws IOException { original = aRequest; response = aResponse; writer = aWriter; errorText = ""; if (writer == null) writer = new PrintStream(response.getOutputStream(), true); Enumeration names = null; // Can't use this because we do it directly, which interferes with this. // names = aRequest.getParameterNames(); // if (names.hasMoreElements()) // { // while (names.hasMoreElements()) // { // String name = (String) names.nextElement(); // String params[] = aRequest.getParameterValues(name); // for (int i = 0; i < params.length; i++) // { // try // {params[i] = URLDecoder.decode(params[i], "UTF8");} // catch (Exception e) // {logMe(e.toString() + " " + e.getMessage());} // if (name.equals("do")) // doSkang.addElement(params[i]); // } // if (!name.equals("do")) // query.put(name, params); // } // } // else { try { BufferedReader in = aRequest.getReader(); String text; try { while ((text = in.readLine()) != null) { text += "&"; int pos = 0; int lastPos = 0; while ((pos = text.indexOf('&', pos)) != -1) { String line = text.substring(lastPos, pos); if (line.startsWith("do=")) doSkang.addElement(URLDecoder.decode(line.substring(3), "UTF8")); else if (!line.startsWith(":")) { int subPos = line.indexOf('='); if (subPos != -1) { String name = line.substring(0, subPos); String params[] = new String[1]; params[0] = URLDecoder.decode(line.substring(subPos + 1), "UTF8"); query.put(name, params); } } body.addElement(URLDecoder.decode(line, "UTF8")); lastPos = ++pos; } } } catch (Exception e) {errorText = errorText + "\n" + (e.toString() + " " + e.getMessage()); e.printStackTrace();} } catch (Exception e) {errorText = errorText + "\n" + (e.toString() + " " + e.getMessage()); e.printStackTrace();} } String params[] = (String[]) query.get("sessionID"); if (params != null) sessionID = params[0]; scheme = aRequest.getScheme(); try { // Look at all this lovely stuff Sun forgot to include // in the design of ServletRequest. While the query // can be retreived from ServletRequest, none of this // other rather important stuff can. request = (HttpServletRequest) aRequest; httpResponse = (HttpServletResponse) response; isHTTP = true; method = request.getMethod(); server = request.getContextPath().substring(1); path = ""; servlet = request.getServletPath(); queryString = request.getQueryString(); authType = request.getAuthType(); user = request.getRemoteUser(); int pos = servlet.lastIndexOf('/'); if (pos != -1) { path = servlet.substring(0, pos); servlet = servlet.substring(pos + 1); } for (int i = Who.GROUPS.length - 1; i >= 0; i--) { if (request.isUserInRole(Who.GROUPS[i].toLowerCase())) groups.addElement(Who.GROUPS[i]); } names = request.getHeaderNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); headers.put(name, request.getHeaders(name)); } cookies = request.getCookies(); session = request.getSession(false); try {jSessionID = session.getId();} catch (Exception e) {session = null;} } catch (Exception e) {errorText = errorText + "\n" + (e.toString() + " " + e.getMessage()); e.printStackTrace();} } public HttpServletRequest request = null; public ServletRequest original = null; public HttpServletResponse httpResponse = null; public ServletResponse response = null; public HttpSession session = null; public PrintStream writer = null; public Vector body = new Vector(); public Vector doSkang = new Vector(); // String public Vector groups = new Vector(); // String public Hashtable headers = new Hashtable(); // Enumeration of String public Hashtable query = new Hashtable(); // String[] public boolean isHTTP = false; public Cookie cookies[] = null; // Cookie, getName(), getValue() public String method = null; public String scheme = null; public String server = null; public String path = null; public String servlet = null; public String queryString = null; public String authType = null; public String user = null; public String sessionID = null; public String jSessionID = null; public String errorText = null; } private static final class Session extends Object { // Replicating java.servlet.HttpSession. private Session(DecodedRequest aRequest) { sessionID = aRequest.sessionID; myRoot = What.root.copy("Master." + aRequest.sessionID); user = new Who("DUMMY REALM FOR Master$Session"); } public static Session getSession(DecodedRequest aRequest) { Session result = null; boolean newSession = false; // Should make use of HttpSession and JSESSIONID if possible. // Using new HttpSession probably not possible, as this request might not output text/html. if (aRequest.sessionID == null) { if (aRequest.jSessionID != null) { aRequest.sessionID = aRequest.jSessionID; newSession = true; } else { aRequest.sessionID = "000000000" + (new Integer(nextSessionID++)).toString(); int len = aRequest.sessionID.length(); aRequest.sessionID = "ID" + aRequest.sessionID.substring(len - 9); // result = new Session(aRequest); // sessions.put(aRequest.sessionID, result); newSession = true; } } // else { // Should time out these sessions. if ((sessions.get(aRequest.sessionID) == null) && (aRequest.jSessionID != null)) { aRequest.sessionID = aRequest.jSessionID; newSession = true; } if (sessions.get(aRequest.sessionID) == null) { // Someone might be trying to use an old sessionID, for now, let them. // On the other hand, my fancy applet caching stuff may cause a stale // sessionID at the browser end. // Should check this during the next security audit. result = new Session(aRequest); sessions.put(aRequest.sessionID, result); newSession = true; } else { result = (Session) sessions.get(aRequest.sessionID); // Should check that aRequest.sessionID == result.sessionID. if ((aRequest.jSessionID != null) && (!aRequest.sessionID.equals(aRequest.jSessionID))) { OUTLN("!!!! aRequest.sessionID " + aRequest.sessionID + " != aRequest.jSessionID " + aRequest.jSessionID + ". !!!!"); OUTLN("!!!! aRequest.sessionID " + aRequest.sessionID + " != aRequest.jSessionID " + aRequest.jSessionID + ". !!!!"); if (sessions.get(aRequest.jSessionID) == null) { aRequest.sessionID = aRequest.jSessionID; result = new Session(aRequest); sessions.put(aRequest.sessionID, result); newSession = true; } else { OUTLN("!!!! aRequest.sessionID " + aRequest.sessionID + " != aRequest.jSessionID " + aRequest.jSessionID + ". !!!!"); OUTLN("!!!! aRequest.sessionID " + aRequest.sessionID + " != aRequest.jSessionID " + aRequest.jSessionID + ". !!!!"); // Should check this, it is probably a security hole. // On the other hand, it may be the perfect thing to do. // May need to inform the applet though. aRequest.sessionID = aRequest.jSessionID; result = (Session) sessions.get(aRequest.sessionID); } } } } result.newSession = newSession; // Should check the authentication details match to avoid spoofed sessionID's. if (result.myRoot == null) result.myRoot = What.root.copy(aRequest.sessionID); if (result.user == null) result.user = new Who("DUMMY REALM FOR Master$Session", aRequest.user); // Should also check authType and other JSESSION auth stuff. if ((aRequest.user != null) && !result.user.isAuthentic()) { try { OUTLN("!!!! Authenticating " + aRequest.user + ". !!!!"); result.user.logon(aRequest.user); if (result.user.isAuthentic()) { OUTLN("!!!! Authenticated " + aRequest.user + ". !!!!"); Vector groups = result.user.getGroups(); // Should check that groups matches aRequest.groups. result.authenticationRequest = aRequest; result.myRoot.user = result.user; // result.appletDo = "logonapplet '" + result.user.getRealm() + "', '" + result.user.getLogin() + "', '" + result.user.getId() + "', '" // + result.user.getLevel() + "', '" + result.user.getRoles() + "'"; } else { result.authenticationRequest = null; OUTLN("!!!! NOT Authenticated " + aRequest.user + ". !!!!"); } } catch (Exception e) {ERRLN(e.toString() + " " + e.getMessage()); e.printStackTrace();} } result.myRoot.user = result.user; return result; } public void invalidate() { // Should clear out all secure Things. myRoot.stripSome(null); // Should get applet Fascist to logout user. if (user != null) user.logoff(); myRoot.user = null; myRoot = null; user = null; authenticationRequest = null; if (sessionID != null) sessions.remove(sessionID); sessionID = null; newSession = false; } private static Hashtable sessions = new Hashtable(); private static int nextSessionID = 0; private DecodedRequest authenticationRequest = null; // Set to the request that was used to auth, null for unauthed sessions. private Who user = null; private String sessionID = null; public ThingSpace myRoot = null; public boolean newSession = false; public String appletDo = null; // For dumping a single command into the applets TODO. } public static void main(String someArguments[]) { Skang.main(someArguments); realPath = System.getProperty("user.dir") + "/../.."; System.out.println("This is a servlet that should be run from a servlet engine only."); try {new Master().init(null);} catch (Exception e) {System.err.println(e.toString() + " " + e.getMessage());} // Should grab the CGI details, create a DecodedRequest(System.out, fakerequest, null) and doIt(request). } public Master() { } public void init() { if (!isInitDone) { What.addOutListener((OutListener) this); // this.modules = "net.matrix_rad.squeal.Squeal,SSS"; } super.init(); isInitDone = true; } public void init(ServletConfig aConfig) throws ServletException { try { What.servlet = true; What.setStandAlone(true); this.init(); What.me.pause(10); // Let pending Things happen. // Should probably super.start() at some stage. config = aConfig; if (config != null) { context = config.getServletContext(); Enumeration names = null; if (context != null) { realPath = context.getRealPath(""); engine = "Servlet engine v" + context.getMajorVersion() + "." + context.getMinorVersion() + " : " + context.getServerInfo(); name = context.getServletContextName() + "." + config.getServletName(); // config.context.getInitParameters - from WEB-INF/web.xml names = context.getInitParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); initParams.put(name, context.getInitParameter(name)); } } // config.getInitParameters - from WEB-INF/web.xml - should be none. names = config.getInitParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); initParams.put(name, config.getInitParameter(name)); } if (context != null) { // config.context.getAttributes - ignore names = context.getAttributeNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); initParams.put(name, context.getAttribute(name).toString()); } } } } catch (Exception e) {throw(new ServletException("Exception in Servlet.init(config).", e));} What.root.grokParameters(Parameter.SERVLET, initParams); for (Enumeration keys = initParams.keys(); keys.hasMoreElements();) What.root.ungrokParameter(true, (String) keys.nextElement()); // Should probably remove this when all the auto squeal loading is working everywhere. addModule(What.root, "net.matrix_rad.squeal.Squeal", "SSS"); // Should also load this on demand. addModule(What.root, "net.matrix_rad.security.Who", "SSS"); } public void destroy() { logMe("net.matrix_rad.servlet.Master.destroy()"); What.removeOutListener((OutListener) this); super.destroy(); } public ServletConfig getServletConfig() { return config; } public String getServletInfo() { return getAppletInfo(); } public void service(ServletRequest aRequest, ServletResponse aResponse) throws ServletException, IOException { try {doIt(new DecodedRequest(null, aRequest, aResponse));} catch (IOException e) {throw(e);} catch (ServletException e) {throw(e);} catch (Exception e) {throw(new ServletException("Exception in service(resquest, response).", e));} } public void doIt(DecodedRequest request) throws Exception { int thingsToBeDone = 0; boolean redirect = false; boolean html = false; Enumeration names = null; Hashtable currentParams = new Hashtable(); // String ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PrintStream out = new PrintStream(outputStream); Session session = Session.getSession(request); ThingSpace myRoot = session.myRoot; logRequest(request, session); OUTLN("\n\n **** Request by " + request.user + ", authenticated by " + request.authType + ","); OUTLN(" session " + request.sessionID + ", JSESSIONID " + JSESSIONID + " == " + request.jSessionID + ". ****"); if (!request.errorText.equals("")) { OUTLN(" **** DECODING ERRORS -" + request.errorText + "\n **** END DECODING ERRORS ****"); } names = request.headers.keys(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); Enumeration headers = (Enumeration) request.headers.get(name); while (headers.hasMoreElements()) { String value = (String) headers.nextElement(); currentParams.put(name, value); } } if (request.cookies != null) { for (int i = 0; i < request.cookies.length; i++) { currentParams.put(request.cookies[i].getName(), request.cookies[i].getValue()); } } names = request.query.keys(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String params[] = (String[]) request.query.get(name); for (int i = 0; i < params.length; i++) { currentParams.put(name, params[i]); } } // myRoot.grokParameters(Parameter.POST, currentParams); for (Enumeration keys = currentParams.keys(); keys.hasMoreElements();) { String name = (String) keys.nextElement(); String value = (String) currentParams.get(name); // myRoot.ungrokParameter(true, name); logMe("DOING " + name + "='" + value + "'"); doThing(myRoot, name + "='" + value + "'"); thingsToBeDone++; } if (What.isBoolean(HTML).booleanValue()) { html = true; HTML = "false"; } if (html) { writeBeginning(request.response, out, "Servlet " + request.servlet + " response."); out.println("
");
		}
		else
		    writeBeginning(request.response, out, null);

		if (session.newSession)
		{
		    out.println("sessionID='" + request.sessionID + "'");
		    logMe("DOING sessionID='" + request.sessionID + "'");
		    doThing(myRoot, "sessionID='" + request.sessionID + "'");
		}

		// Clear it of things that may have been echoed from the above set commands.
		myRoot.appletToDo.removeAllElements();
		
		if (session.appletDo != null)
		{
//		    What.me.appletPendingDoThing(myRoot, "module 'net.matrix_rad.security.Who'");
//		    thingsToBeDone += doSomething(session.appletDo, request, out, myRoot);
		    session.appletDo = null;
		}

		names = request.doSkang.elements();
		while (names.hasMoreElements())
		{
			String command = (String) names.nextElement();
			thingsToBeDone += doSomething(command, request, out, myRoot);
		}
//		pause(thingsToBeDone * 3);  // Give any pending doThings time to get done.
		pause(1);  // Give any pending doThings time to get done.
		thingsToBeDone = 0;

		while (!myRoot.ToDo.isEmpty())
		{
			String command = (String) myRoot.ToDo.firstElement();
			myRoot.ToDo.removeElementAt(0);  // index is zero based.
			if (command != null)
			    thingsToBeDone += doSomething(command, request, out, myRoot);
		}
//		pause(thingsToBeDone * 3);  // Give any pending doThings time to get done.
		pause(1);  // Give any pending doThings time to get done.

		if (!myRoot.appletToDo.isEmpty())
		{
			for (Enumeration commands = myRoot.appletToDo.elements(); commands.hasMoreElements();)
			{
				String command = (String) commands.nextElement();
				if ((command != null) && (!command.equals("")))
					out.println(command);
			}
			myRoot.appletToDo.removeAllElements();
		}

		if (request.servlet.equalsIgnoreCase("logoff.svlt"))
		{
OUTLN("\n\n  #### LOG OFF Request by " + request.user + ", authenticated by " + request.authType + ",");
OUTLN(" session " + request.sessionID + ", JSESSIONID " + JSESSIONID + " == " + request.jSessionID + ". ####");
		    if (request.session != null)
		    {
			try
			    {request.session.invalidate();}
			catch (IllegalStateException e)
			    {;}
			request.session = null;
		    }
		    session.invalidate();
		    sessionID = null;
		    out.println("sessionID=''");
		    JSESSIONID = null;
		    out.println("JSESSIONID=''");
		    if (html)
		    {
			request.httpResponse.sendRedirect("http://" + request.original.getServerName() + "/" + servletPath + "/");
			redirect = true;
		    }
		    else
			out.println("postshow 'http://" + request.original.getServerName() + "/" + servletPath + "/', ''");
		}

		if (html)
		{
		    out.println("
"); writeEnding(out); } if (!redirect) { request.writer.println(outputStream.toString()); logMe(">>>" + request.servlet + "\n" + outputStream.toString()); } else { request.writer.println(); logMe(">>>" + request.servlet + " REDIRECTED, output would have been -\n" + outputStream.toString()); } logMe("******************************** END'O'SERVLET. ********************************\n\n"); } protected int doSomething(String command, DecodedRequest request, PrintStream out, ThingSpace myRoot) { int thingsToBeDone = 0; try { OUTLN("<<" + engine + ""); writer.println(realPath + "   " + getServletInfo()); writer.println("

Init parameters

"); writer.println(""); names = initParams.keys(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String param = (String) initParams.get(name); writer.println(""); writer.println(" "); writer.println(" "); } writer.println("
" + name + ":" + param); writer.println("
"); writer.println("

" + request.original.getRemoteHost() + " (" + request.original.getRemoteAddr() + ") " + (request.original.isSecure() ? "SECURELY" : "(maybe) INSECURELY") + " requests

"); writer.println("Session ID " + request.sessionID + "
"); writer.println(request.original.getProtocol() + " " + request.original.getServerName() + ":" + request.original.getServerPort() + "
"); writer.println(request.method + " " + request.scheme + "://" + request.server + request.path + "/" + request.servlet + "?" + request.queryString + "
"); writer.println("

Request body : " + request.original.getContentLength() + " of " + request.original.getContentType() + "

"); names = request.body.elements(); while (names.hasMoreElements()) writer.println(((String) names.nextElement()) + "
"); writer.println("

Locales

"); writer.println(request.original.getCharacterEncoding() + " : " + request.original.getLocale() + ""); writer.println(""); names = request.original.getLocales(); while (names.hasMoreElements()) { String name = names.nextElement().toString(); writer.println(""); writer.println(" "); writer.println(""); } writer.println("
" + name + "
"); writer.println("

Servlet attributes

"); writer.println(""); names = request.original.getAttributeNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String param = request.original.getAttribute(name).toString(); writer.println(""); writer.println(" "); writer.println(" "); } writer.println("
" + name + ":" + param); writer.println("
"); writer.println("

Parameters

"); writer.println(""); names = request.query.keys(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String params[] = (String[]) request.query.get(name); writer.println(""); writer.println(" "); writer.println(" "); } writer.println("
" + name + ":" + params[0]); for (int i = 1; i < params.length; i++) writer.println(", " + params[i]); writer.println("
"); writer.println("

Skang " + request.servlet + "

"); names = request.doSkang.elements(); while (names.hasMoreElements()) writer.println(((String) names.nextElement()) + "
"); writer.println("

Try pretending to be HTTP now.

"); writer.println(request.original.getClass().getName() + " " + request.response.getClass().getName()); try { writer.println("

" + request.authType + " authorisation of " + request.user + "

"); names = request.groups.elements(); while (names.hasMoreElements()) writer.print(((String) names.nextElement()) + " "); writer.println("
"); writer.println("

Headers

"); writer.println(""); names = request.headers.keys(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); Enumeration params = (Enumeration) request.headers.get(name); writer.println(""); writer.println(" "); writer.println(" "); } writer.println("
" + name + ":"); while (params.hasMoreElements()) writer.println((String) params.nextElement() + "
\n"); writer.println("
"); writer.println("

Cookies

"); if (request.cookies != null) { writer.println(""); for (int i = 0; i < request.cookies.length; i++) { writer.println(""); writer.println(" "); writer.println(" "); writer.println(""); } writer.println("
" + request.cookies[i].getDomain() + ":" + request.cookies[i].getPath() + "/" + request.cookies[i].getName() + ":" + request.cookies[i].getValue() + "
"); } writer.println("

URL URI

" + request.request.getRequestURL() + "   " + request.request.getRequestURI()); writer.println("

Paths (info translated context servlet)

"); writer.println(request.request.getPathInfo() + "   " + request.request.getPathTranslated() + "   " + request.request.getContextPath() + "   " + request.request.getServletPath()); } catch (Exception e) {writer.println(e.toString() + "   " + e.getMessage());} writer.println("

END'O'SERVLET.

"); writeEnding(writer); } catch (Exception e) { writer.println(e.toString() + "   " + e.getMessage()); throw(e); } } */ protected void logRequest(DecodedRequest request, Session session) throws Exception { String temp = ""; try { Enumeration names = null; logMe("******************************** " + engine + " ********************************"); logMe(realPath + " " + getServletInfo()); logMe("INIT PARAMETERS & SERVLET ATTRIBUTES :"); names = initParams.keys(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String param = (String) initParams.get(name); logMe(" " + name + " : " + param); } logMe("\n"); logMe("LOCALES :"); logMe(request.original.getCharacterEncoding() + " : " + request.original.getLocale()); names = request.original.getLocales(); while (names.hasMoreElements()) { String name = names.nextElement().toString(); logMe(" " + name); } logMe("\n"); logMe("DECODING ERRORS : " + request.errorText); logMe("\n"); logMe("REQUEST - " + request.original.getRemoteHost() + " (" + request.original.getRemoteAddr() + ") " + (request.original.isSecure() ? "SECURELY" : "(maybe) INSECURELY") + " requests :"); logMe("Session ID " + request.sessionID); logMe(request.original.getProtocol() + " " + request.original.getServerName() + ":" + request.original.getServerPort()); logMe(request.method + " " + request.scheme + "://" + request.server + request.path + "/" + request.servlet + "?" + request.queryString); logMe("REQUEST BODY : " + request.original.getContentLength() + " OF " + request.original.getContentType() + ""); names = request.body.elements(); while (names.hasMoreElements()) logMe(" " + ((String) names.nextElement())); logMe("REQUEST ATTRIBUTES :"); names = request.original.getAttributeNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String param = request.original.getAttribute(name).toString(); logMe(" " + name + " : " + param); } logMe("\n"); if (request.session != null) { logMe("SESSION ATTRIBUTES :"); names = request.session.getAttributeNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String param = request.session.getAttribute(name).toString(); logMe(" " + name + " : " + param); } logMe("\n"); } logMe("PARAMETERS :"); names = request.query.keys(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String params[] = (String[]) request.query.get(name); temp = " " + name + " : " + params[0]; for (int i = 1; i < params.length; i++) temp += ", " + params[i]; logMe(temp); } logMe("\n"); logMe("SKANG " + request.servlet + " :"); names = request.doSkang.elements(); while (names.hasMoreElements()) logMe(((String) names.nextElement())); logMe("\n"); logMe("### Try pretending to be HTTP now. ###"); logMe(request.original.getClass().getName() + " " + request.response.getClass().getName()); try { logMe(request.authType + " AUTHORISATION OF " + request.user + " :"); temp = ""; names = request.groups.elements(); while (names.hasMoreElements()) temp += ((String) names.nextElement()) + " "; logMe(temp); logMe("HEADERS :"); names = request.headers.keys(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); Enumeration params = (Enumeration) request.headers.get(name); temp = " " + name + " : "; while (params.hasMoreElements()) temp += (String) params.nextElement(); logMe(temp); } logMe("\n"); logMe("COOKIES :"); if (request.cookies != null) { for (int i = 0; i < request.cookies.length; i++) logMe(" " + request.cookies[i].getDomain() + ":" + request.cookies[i].getPath() + "/" + request.cookies[i].getName() + " : " + request.cookies[i].getValue()); logMe("\n"); } logMe("URL, URI : " + request.request.getRequestURL() + ", " + request.request.getRequestURI()); logMe("PATHS (INFO, TRANSLATED, CONTEXT, SERVLET) : " + request.request.getPathInfo() + ", " + request.request.getPathTranslated() + ", " + request.request.getContextPath() + ", " + request.request.getServletPath()); } catch (Exception e) {logMe("EXCEPTION : " + e.toString() + " " + e.getMessage());} } catch (Exception e) { logMe("EXCEPTION : " + e.toString() + " " + e.getMessage()); throw(e); } logMe("\n"); // What.root.log("What.root"); // session.myRoot.log("session"); } public static void logMe(String aMessage) { if (context != null) context.log(" " + aMessage); else What.LOGLN(1, aMessage); } public void outputReceived(OutEvent e) { int type = e.getType(); String pre = " "; switch(type) { case OutEvent.OUTPUT : pre = "OUT"; break; case OutEvent.ERROR : pre = "ERR"; break; case OutEvent.LOG : pre = "LOG"; break; } if (context != null) context.log(pre + " " + e.getText()); } protected static void writeBeginning(ServletResponse response, PrintStream writer, String aTitle) { if (aTitle == null) { if (response == null) writer.println("Content-type: text/plain\n"); else response.setContentType("text/plain"); } else { if (response == null) writer.println("Content-type: text/html\n"); else response.setContentType("text/html"); writer.println(""); writer.println(""); writer.println("" + aTitle + ""); writer.println(""); writer.println(""); writer.println("

" + aTitle + "

"); } } protected static void writeEnding(PrintStream writer) { writer.println(""); writer.println(""); } private static String getParam(DecodedRequest request, String aName) { String result = null; String param[] = (String[]) request.query.get(aName); if (param != null) result = param[0]; return result; } // * @.secure S-S /** Load a skang module as a servlet. * * @.skang load file,data */ public void loadServlet(String aModule) throws Exception { loadServlet(aModule, ""); } public void loadServlet(String aModule, String acl) throws Exception { doThing("module '" + aModule + "'" + ((acl.length() > 0) ? (", '" + acl + "'") : "")); // doThing("skang '" + aModule.replace('.', What.separator) + ".skang'"); } private Hashtable initParams = new Hashtable(); // String private String name = null; private static ServletConfig config = null; public static ServletContext context = null; private static String realPath = null; private static String engine = null; private static boolean isInitDone = false; }