I have a question about Protocol.FILE
usage in this example from the Restlet site
// URI of the root directory.
public static final String ROOT_URI = "file:///c:/restlet/docs/api/";
[...]
// Create a component
Component component = new Component();
component.getServers().add(Protocol.HTTP, 8182);
component.getClients().add(Protocol.FILE);
// Create an application
Application application = new Application() {
@Override
public Restlet createInboundRoot() {
return new Directory(getContext(), ROOT_URI);
}
};
// Attach the application to the component and start it
component.getDefaultHost().attach(application);
component.start();
Why it is needed to add Protocol.FILE
to the list of client connectors in order to serve directory/file content?
Simply because you use this protocol within the ROOT_URI
variable ;-) Regarding protocols, you need to explicitly add them when creating the Restlet component. Client connectors provide a way to use protocols to access resources (local or remote).
Here are some more details about what happens under the hood:
When adding a Restlet extension in the classpath, some elements are registered within the engine. You can have converters, server connectors, client connectors, ... You can see what is registered on the instance of the Engine
itself:
List<ConnectorHelper<Client>> clientConnectors
= Engine.getInstance().getRegisteredClients();
for (ConnectorHelper<Client> clientConnector : clientConnectors) {
System.out.println(clientConnector);
}
Regarding client connectors, they correspond to entities that are able to handle particular protocol(s). For example, the Jetty extension providers a client connector to execute HTTP and HTTPS requests based on the Jetty client API.
HTTP
protocol, Restlet will find the first client connector in the list of registered one that is able to handle this protocol. For execute HTTP request, it will use this connector. If there is no available connector, it will throw an exception...In your case, the client connector for the FILE
protocol is provided by Restlet core itself so it's automatically registered. But you need to explicitly tell Restlet that you want to use this protocol.
Hope it helps you, Thierry