http://ooweb.sourceforge.net/tutorial.html
Also any way to change the logging file for the underlying Pygmy server?
Not really a question, but I can't seem to stop writing stuff like this. Maybe someone will find it useful.
I know rewriting an HTTP server is not the way to turn off the quips ;)
/* Copyright 2010 Misha Koshelev. All Rights Reserved. */
package com.mksoft.common;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Simple HTTP Server.
*
* @author Misha Koshelev
*/
public class HttpServer extends Thread {
/*
* Constants
*/
/**
* 404 Not Found Result
*/
protected final static String result404NotFound="<html><head><title>404 Not Found</title></head><body bgcolor='#ffffff'><h1>404 Not Found</h1></body></html>";
/*
* Variables
*/
/**
* Port on which HTTP server handles requests.
*/
protected int port;
public int getPort() { return port; }
public void setPort(int _port) { port=_port; }
/*
* Constructors
*/
public HttpServer(int _port) {
setPort(_port);
}
/*
* Helpers
*/
/**
* Errors
*/
protected void error(String message) {
System.err.println(message);
System.err.flush();
}
/**
* Debugging
*/
protected boolean debugOutput=true;
protected void debug(String message) {
if (debugOutput) {
error(message);
}
}
/**
* Lock object
*/
private Object lock=new Object();
/**
* Should we quit?
*/
protected boolean doQuit=false;
/**
* Are we done?
*/
protected boolean areWeDone=false;
/**
* Process POST request headers
*/
protected String processPostRequest(String url,LinkedHashMap<String,String> headers,String inputLine) {
debug("HttpServer.processPostRequest: url=\""+url);
if (debugOutput) {
for (String key: headers.keySet()) {
debug("HttpServer.processPostRequest: headers."+key+"=\""+headers.get(key)+"\"");
}
}
debug("HttpServer.processPostRequest: inputLine=\""+inputLine+"\"");
try {
inputLine=new URLDecoder().decode(inputLine,"UTF-8");
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
}
String[] keyValues=inputLine.split("&");
LinkedHashMap<String,String> post=new LinkedHashMap<String,String>();
for (int i=0;i<keyValues.length;i++) {
String keyValue=keyValues[i];
int equals=keyValue.indexOf('=');
String key=keyValue.substring(0,equals);
String value=keyValue.substring(equals+1);
post.put(key,value);
}
return post(url,headers,post);
}
/**
* Server loop (here for exception handling purposes)
*/
protected void serverLoop() throws IOException {
/* Start server socket */
ServerSocket serverSocket=null;
try {
serverSocket=new ServerSocket(getPort());
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(1);
}
Socket clientSocket=null;
while (true) {
/* Quit if necessary */
if (doQuit) {
break;
}
/* Accept incoming connections */
try {
clientSocket=serverSocket.accept();
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(1);
}
/* Read request */
BufferedReader in=null;
String inputLine=null;
String firstLine=null;
String blankLine=null;
LinkedHashMap<String,String> headers=new LinkedHashMap<String,String>();
try {
in=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while (true) {
if (blankLine==null) {
inputLine=in.readLine();
} else {
/* POST request, read Content-length bytes */
int contentLength=new Integer(headers.get("Content-Length")).intValue();
StringBuilder sb=new StringBuilder(contentLength);
for (int i=0;i<contentLength;i++) {
sb.append((char)in.read());
}
inputLine=sb.toString();
break;
}
if (firstLine==null) {
firstLine=inputLine;
} else if (blankLine==null) {
if (inputLine.equals("")) {
if (firstLine.startsWith("GET ")) {
break;
}
blankLine=inputLine;
} else {
int colon=inputLine.indexOf(": ");
String key=inputLine.substring(0,colon);
String value=inputLine.substring(colon+2);
headers.put(key,value);
}
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
/* Process request */
String result=null;
firstLine=firstLine.replaceAll(" HTTP/.*","");
if (firstLine.startsWith("GET ")) {
result=get(firstLine.replaceFirst("GET ",""),headers);
} else if (firstLine.startsWith("POST ")) {
result=processPostRequest(firstLine.replaceFirst("POST ",""),headers,inputLine);
} else {
error("HttpServer.ServerLoop: Unhandled request \""+firstLine+"\"");
}
debug("HttpServer.ServerLoop: result=\""+result+"\"");
/* Send response */
PrintWriter out=null;
try {
out=new PrintWriter(clientSocket.getOutputStream(),true);
} catch (IOException ioe) {
ioe.printStackTrace();
}
if (result!=null) {
out.println("HTTP/1.1 200 OK");
} else {
out.println("HTTP/1.0 404 Not Found");
result=result404NotFound;
}
Date now=new Date();
out.println("Date: "+new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z").format(now));
out.println("Content-Type: text/html; charset=UTF-8");
out.println("Content-Length: "+result.length());
out.println("");
out.print(result);
/* Clean up */
out.close();
if (in!=null) {
in.close();
}
clientSocket.close();
}
serverSocket.close();
areWeDone=true;
synchronized(lock) {
lock.notifyAll();
}
}
/*
* Methods
*/
/**
* Run server on port specified in constructor.
*/
public void run() {
try {
serverLoop();
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(1);
}
}
/**
* Process GET request (should be overwritten).
*/
public String get(String url,LinkedHashMap<String,String> headers) {
debug("HttpServer.get: url=\""+url+"\"");
if (debugOutput) {
for (String key: headers.keySet()) {
debug("HttpServer.get: headers."+key+"=\""+headers.get(key)+"\"");
}
}
if (url.equals("/")) {
return "<html><head><title>HttpServer GET Test Page</title></head>\r\n"+
"<body bgcolor='#ffffff'>\r\n"+
"<center><h1>HttpServer GET Test Page</h1></center>\r\n"+
"<hr />\r\n"+
"<center><table>\r\n"+
"<form method='post' action='/'>\r\n"+
"<tr><td align=right>Test 1:</td>\r\n"+
" <td><input type='text' name='text 1' value='test me !!! !@#$'></td></tr>\r\n"+
"<tr><td align=right>Test 2:</td>\r\n"+
" <td><input type='text' name='text 2' value='type smthng'></td></tr>\r\n"+
"<tr><td> </td>\r\n"+
" <td align=right><input type='submit' value='Submit'></td></tr>\r\n"+
"</form>\r\n"+
"</table></center>\r\n"+
"<hr />\r\n"+
"<center><a href='/quit'>Shutdown Server</a></center>\r\n"+
"</html>";
} else if (url.equals("/quit")) {
quit();
return "";
} else {
return null;
}
}
/**
* Process POST request (should be overwritten).
*/
public String post(String url,LinkedHashMap<String,String> headers,LinkedHashMap<String,String> post) {
debug("HttpServer.post: url=\""+url+"\"");
if (debugOutput) {
for (String key: headers.keySet()) {
debug("HttpServer.post: headers."+key+"=\""+headers.get(key)+"\"");
}
}
if (url.equals("/")) {
String result="<html><head><title>HttpServer Post Test Page</title></head>\r\n"+
"<body bgcolor='#ffffff'>\r\n"+
"<center><h1>HttpServer Post Test Page</h1></center>\r\n"+
"<hr />\r\n"+
"<center><table>\r\n"+
"<tr><th>Key</th><th>Value</th></tr>\r\n";
for (String key: post.keySet()) {
result+="<tr><td align=right>"+key+"</td><td align=left>"+post.get(key)+"</td></tr>\r\n";
}
result+="</table></center>\r\n"+
"</html>";
return result;
} else {
return null;
}
}
/**
* Wait for server to quit.
*/
public void waitForCompletion() {
while (areWeDone==false) {
synchronized(lock) {
try {
lock.wait();
} catch (InterruptedException ie) {
}
}
}
}
/**
* Shutdown server.
*/
public void quit() {
doQuit=true;
}
}
Just comment out the System.out.println(getQuip()) line in the OowebServer class and recompile to remove quips.
I'm not sure what logger pygmy uses, but I'd guess its java.util.logger so configure that the normal way.
Ok here is my new solution:
For logging I do groovy code:
QED... or something ;)
Misha