http://ooweb.sourceforge.net/tutorial.html
还有什么方法可以更改底层Pygmy服务器的日志文件吗?
不是一个问题,但我似乎无法停止写这样的东西。也许有人会发现它很有用。
我知道重写HTTP服务器是而不是关闭讽刺的方式;)
/* 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;
}
}
答案 0 :(得分:0)
只需注释掉OowebServer类中的System.out.println(getQuip())行,然后重新编译即可删除quips。
我不确定logg pygmy使用了什么,但我猜它的java.util.logger所以配置正常方式。
答案 1 :(得分:0)
好的,这是我的新解决方案:
/* Copyright (C) 2010 Misha Koshelev. All Rights Reserved. */
package com.mksoft.fbbday.main;
import java.io.InputStream;
import java.io.IOException;
import java.util.Properties;
import net.sf.ooweb.http.AbstractServer;
/**
* OowebServer
* No quips, own properties, logging options
*
* @author Misha Koshelev
*/
class OowebServer extends net.sf.ooweb.http.AbstractServer {
pygmy.core.Server pygmyServer;
Properties cfg=new Properties();
/**
* @throws IOException
*
*/
public OowebServer() throws IOException {
// Load Ooweb defaults
InputStream is=net.sf.ooweb.http.pygmy.OowebServer.class.getResourceAsStream("pygmy-default.properties");
cfg.load(is);
// Load additional properties
is=getClass().getResourceAsStream("/ooweb.properties");
Properties additional=new Properties();
additional.load(is);
cfg.putAll(additional);
}
/**
* @see net.sf.ooweb.http.Server#start()
*/
public void start() throws Exception {
pygmyServer=new pygmy.core.Server(cfg);
pygmyServer.start();
synchronized(pygmyServer) {
pygmyServer.wait();
}
}
public void stop() {
pygmyServer.shutdown();
}
}
对于日志记录,我执行groovy代码:
// Remove default loggers
def logger=Logger.getLogger('')
def handlers=logger.handlers
handlers.each() { handler->logger.removeHandler(handler) }
// Production version?
def productionVersion=true
if (productionVersion) {
// Log INFO and above to file
logger.setLevel Level.INFO
def fileHandler=new FileHandler("${dataDirectory}${File.separator}console.log");
fileHandler.setLevel Level.INFO
logger.addHandler(fileHandler)
} else {
// Log ALL to Console
logger.setLevel Level.ALL
def consoleHandler=new ConsoleHandler()
consoleHandler.setLevel Level.ALL
logger.addHandler(consoleHandler)
}
QED ......或其他东西;)
米莎