具体代码如下:
public class DirGuardStartRun { /** * web browser input:http://localhost:8182/docs * and then it need a HTTP_BASIC verifier * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // URI of the root directory. 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() { Router router = new Router(getContext()); // Create a simple password verifier MapVerifier verifier = new MapVerifier(); verifier.getLocalSecrets().put("scott", "tiger".toCharArray()); verifier.getLocalSecrets().put("test", "test".toCharArray()); // Create a Guard ChallengeAuthenticator guard = new ChallengeAuthenticator(getContext(), ChallengeScheme.HTTP_BASIC, "Tutorial"); guard.setVerifier(verifier); Directory directory = new Directory(getContext(), ROOT_URI); directory.setListingAllowed(true); guard.setNext(directory); router.attach("/docs", guard); return router; } }; // Attach the application to the component and start it component.getDefaultHost().attach(application); component.start(); } }
把我们需要访问的那些文件放到c盘的restlet/docs/api下就可以了。它会去自动识别index页面进行访问。