Thread: [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; } |
From: Niall G. <gal...@ya...> - 2005-12-12 14:40:06
|
Hi Brian, Although this is probably a good fit for what you are doing, how well will this scale? If your application grows to incorporate many actions, this scheme will add too much complexity as services are going to be coupled, also loading order seems to determine which service is used. You should at the very least consider scoring the canSearch. I think that the URI based scheme is what it is, its a resource identifier, and should be used as such. If a URI scheme does not meet your needs you can always add to the controller using a RedirectService. public class MyController extends RedirectService { public Resource redirect(Request req, Response resp) { // do somthing ... if(someCondition(request)) { return lookup("someService"); } return lookup("someOtherService"); } } This allows you to augment the URI mapping scheme to include some logic before deciding which service is to handle the request. Niall --- Brian Davis <wic...@gm...> wrote: > 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 = host; > > _loader = new SerialLoaderEngine(); > > Iterator it = 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.ProtocolService"); > > if > (getHost().getAddress().equals("127.0.0.1")) > { > > _loader.register("com.wickedfastsolutions.server.app.CoberturaService"); > } > > > _loader.register("com.wickedfastsolutions.server.app.DirectoryListingService"); > > _loader.register("com.wickedfastsolutions.server.app.FreeMarkerService"); > > _loader.register("com.wickedfastsolutions.server.app.FileService"); > > Connection connection = > 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 = new PrintStream(new > FileOutputStream("err.txt")); > > System.setOut(out); > System.setErr(out); > > Host host = new Host(); > > host.addRoot(new File("test")); > > host.setAddress("127.0.0.1"); > host.setPort(2000); > > _server = 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 = > CharsetRegistery.getCharset("iso-8859-1").decode(req.getURI().getBytes("UTF-8")); > > Unicode uri = 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("..") != -1 || > uri.indexOf("./") != -1 || > uri.indexOf(".\\") != -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")) > { > === message truncated === Niall Gallagher __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com |