I am using the restlet routing APIs like http://localhost:8080/www.example.com/hello/ping But I don't know how to use this type of method:
/{projectName}/{wallName}
that I have seen in Restlet routing nightmare?
Could anyone tell me
1.What is the best practice of using Restlet Routing?
2.How to implement /{projectName}/{wallName}
in java?
3.How to get the value of projectName
from this API?
In fact, there are several part within a Restlet application. Classically, this application is accessible through a Restlet component that can be created as described below:
Standalone mode (outside an application server)
Component component = new Component();
component.setName("My component");
component.getServers().add(Protocol.HTTP, 8182);
MyApplication application = new MyApplication();
// To attach application on /www.example.com
component.getDefaultHost().attach("www.example.com", application);
// To attach application on /
component.getDefaultHost().attachDefault(application);
component.start();
Container mode (servlet support). You can use the extension ext.servlet
for this use case. A front servlet is provided that automatically wraps a component. You only have to specify the class of your application implementation, as described below:
<!-- Application class name -->
<context-param>
<param-name>org.restlet.application</param-name>
<param-value>
packageName.MyApplication
</param-value>
</context-param>
<!– Restlet adapter –>
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>
org.restlet.ext.servlet.ServerServlet
</servlet-class>
</servlet>
<!– Catch all requests –>
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/*</url-pattern>
<!-- or -->
<!-- url-pattern>/www.example.com/*</url-pattern -->
</servlet-mapping>
You can now implement the Restlet application. For this implement, a class that extends Application
. The routing must be defined within its method createInboudRoot
, as described below:
public MyApplication extends Application {
@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/{projectName}/{wallName}", MyServerResource.class);
return router;
}
}
As you can see, a server resource is attached for the path /{projectName}/{wallName}
. This server resource is responsible to handle the request. A sample of implementation is described below:
public class MyServerResource extends ServerResource {
@Get
public Representation handleGet() {
String projectName = getAttribute("projectName");
String wallName = getAttribute("wallName");
(...)
}
}
Hope it helps you, Thierry