/** * Called to construct the actual application instance. Extenders will * generally override this method. * * @param context * the Restlet application context * @return the newly constructed application instance */ protected Application doCreateApplication(Context context) { // FIXME Workaround for a bug in Restlet 2.1M7 - the context should be // passed to the Application // constructor. Application app = new Application(); app.setContext(context); return app; }
/** * Returns the parent application. If it wasn't set, it attempts to retrieve * the current one via {@link org.restlet.Application#getCurrent()} if it * exists, or instantiates a new one as a last resort. * * @return The parent application if it exists, or a new one. */ public org.restlet.Application getApplication() { org.restlet.Application result = this.application; if (result == null) { result = org.restlet.Application.getCurrent(); if (result == null) { result = new org.restlet.Application(getContext()); } this.application = result; } return result; }
public static void main(String[] args) throws Exception { // Instantiating the Application providing the Range Service Application app = new Application(); // Plug the server resource. app.setInboundRoot(HelloServerResource.class); // Instantiating the HTTP server and listening on port 8111 new Server(Protocol.HTTP, 8111, app).start(); }
final Router router = new Router(); router.attachDefault(HttpListener.class); MySharedObj myobj = MySharedObj.newInstance(); org.restlet.Application myApp = new org.restlet.Application() { @Override public org.restlet.Restlet createInboundRoot() { return router; }; }; Component component = new Component(); component.getDefaultHost().attach("/", myApp); new Server(Protocol.HTTP, port, component).start(); // in a different thread MySharedObj myobj = MySharedObj.get(); myobj.doStuff()
/** * Creates the restlet application that will be used to handle all rest calls * Takes a map of paths to resource classes * <p/> * Use the following three lines to access the routing multibinder: * TypeLiteral<String> pathType = new TypeLiteral<String>() {}; * TypeLiteral<Class<? extends ServerResource>> clazzType = new TypeLiteral<Class<? extends ServerResource>>() {}; * MapBinder<String, Class<? extends ServerResource>> resourceBinder = MapBinder.newMapBinder(binder(), pathType, clazzType); * resourceBinder.bind("/my/path").toInstance(MyResource.class); */ @Provides Application createApplication( FinderFactory factory, Map<String, Class<? extends ServerResource>> routes ) { Context context = new Context(); Application application = new Application(); application.setContext( context ); Router router = new Router( context ); // Set binding rules here for ( Entry<String, Class<? extends ServerResource>> entry : routes.entrySet() ) { final Class<? extends ServerResource> resource = entry.getValue(); logger.info( "Binding '" + entry.getKey() + "' to " + resource ); router.attach( entry.getKey(), factory.finder( resource ) ); } application.setInboundRoot( router ); return application; }
Router router = new Router(); // Remove final from this. router.attachDefault(HttpListener.class); Component component = new Component(); Context ctx = component.getApplication().getContext().createChildContext(); // Remove final ctx.getAttributes().put("mysharedobj", new MySharedObj()); org.restlet.Application myApp = new org.restlet.Application(ctx) { @Override public org.restlet.Restlet createInboundRoot() { return router; }; };