[Simpleweb-Support] SerialLoaderEngine ftw
Brought to you by:
niallg
From: Brian D. <wic...@gm...> - 2005-12-11 17:47:47
|
replaces the need for protocol handlers and a uri-based resolver imo more code provided upon request... public class Server { public Server(Host host) throws Exception { _host =3D host; _loader =3D new SerialLoaderEngine(); Iterator it =3D host.roots(); while (it.hasNext()) { _loader.addContext(new ServerContext((File) it.next(), this)); } _loader.register("com.wickedfastsolutions.server.app.HeaderService"= ); _loader.register("com.wickedfastsolutions.server.app.SandboxService= "); _loader.register("com.wickedfastsolutions.server.app.ProtocolServic= e"); if (getHost().getAddress().equals("127.0.0.1")) { _loader.register("com.wickedfastsolutions.server.app.CoberturaS= ervice"); } _loader.register("com.wickedfastsolutions.server.app.DirectoryListi= ngService"); _loader.register("com.wickedfastsolutions.server.app.FreeMarkerServ= ice"); _loader.register("com.wickedfastsolutions.server.app.FileService"); Connection connection =3D ConnectionFactory.getConnection(_loader); connection.connect(new ServerSocket(_host.getPort(), 50, InetAddress.getByName(_host.getAddress()))); System.err.println("Current Time is " + new Date()); System.err.println("Server is running at " + _host.getAddress() + " port " + _host.getPort()); } public static void main(String[] args) throws Exception { PrintStream out =3D new PrintStream(new FileOutputStream("err.txt")= ); System.setOut(out); System.setErr(out); Host host =3D new Host(); host.addRoot(new File("test")); host.setAddress("127.0.0.1"); host.setPort(2000); _server =3D new Server(host); } public Host getHost() { return _host; } public SerialLoaderEngine getLoaderEngine() { return _loader; } private Host _host; private SerialLoaderEngine _loader; private static Server _server; } public class HeaderService extends SerialService { public HeaderService(Context context) { super(context); } public boolean canHandle(Request req, Response res) { res.set("Server", "www.wickedfastsolutions.com"); res.setDate("Date", System.currentTimeMillis()); res.setMajor(1); res.setMinor(1); res.set("Content-Type", MimeTypes.parse(req.getURI())); return false; } public void process(Request req, Response res) throws Exception { handle(req, res); } } public class SandboxService extends SerialService { public SandboxService(Context context) { super(context); } public boolean canHandle(Request req, Response res) { try { // ensure that this url is properly encoded as iso-8859-1 and // not some overlong UTF-8 sequence.. by doing the transcoding myself int[] decoded =3D CharsetRegistery.getCharset("iso-8859-1").decode(req.getURI().getBytes("UTF= -8")); Unicode uri =3D new Unicode(decoded, 0, decoded.length); // Requests with ../ or ..\\ in the url are not allowed. The security implications for potentially // being allowed outside the web template sandbox far out-weigh the benefits of these types of url's. // I recommend using url's relative to the web template directory only. At first I returned this // warning but then upon further consideration thought that the person or program sending the request // might misinterpret my response for a success and try to continue hacking as opposed to moving along if (uri.indexOf("..") !=3D -1 || uri.indexOf("./") !=3D -1 || uri.indexOf(".\\") !=3D -1 || uri.endsWith(".")) { return true; } } catch (Exception ex) { return true; } return false; } public void process(Request req, Response res) throws Exception { handle(req, res, 404); } } public class ProtocolService extends SerialService { public ProtocolService(Context context) { super(context); } public boolean canHandle(Request req, Response res) { if (!req.getMethod().equals("GET") && !req.getMethod().equals("HEAD= ")) { return true; } if (req.getMajor() !=3D 1) { return true; } if (req.getMinor() !=3D 0 && req.getMinor() !=3D 1) { return true; } // validate uri return false; } public void process(Request req, Response res) throws Exception { handle(req, res, 501); } } public class CoberturaService extends SerialService { public CoberturaService(Context context) { super(context); } public boolean canHandle(Request req, Response res) { return req.getURI().equals("/index.flush"); } public void process(Request req, Response res) throws Exception { // flush cobertura data String className =3D "net.sourceforge.cobertura.coveragedata.Projec= tData"; String methodName =3D "saveGlobalProjectData"; Class saveClass =3D Class.forName(className); java.lang.reflect.Method saveMethod =3D saveClass.getDeclaredMethod(methodName, new Class[0]); saveMethod.invoke(null,new Object[0]); handle(req, res, 200); } } public class FileService extends SerialService { public FileService(Context context) { super(context); _engine =3D new FileEngine(context); } public boolean canHandle(Request req, Response res) { return true; } public void process(Request req, Response res) throws Exception { _engine.resolve(req.getURI()).handle(req, res); } private FileEngine _engine; } |