Restlet directory - index.html file is not serviced by default

I have:

Directory webdir = new Directory(getContext(), "clap://class/webapp");
webdir.setDeeplyAccessible(true);
router.attach("",webdir);

This works when serving all the files in a directory by name.

However, it should serve index.htmlwhen you visit the "/", but it is not. I tried all combinations of paths, additional routers, etc. Etc., And it still does not work.

When you visit "/", you get a 200 response and the type of application content / octet stream. The answer is otherwise empty. getIndexNameon Directory makes me thisindex

I also tried getMetadataService().addExtension("html", MediaType.TEXT_HTML, true);to help him pick up the index.html file, but to no avail, and also set the accept header in the request for text / html.

ETA: this is the same (unresolved) problem described here: http://restlet-discuss.1400322.n2.nabble.com/Serving-static-files-using-Directory-and-CLAP-from-a-jar -td7578543.html

Can anyone help with this? It drives me crazy.

After messing around a bit, I now have a workaround, but I would prefer not to redirect if possible:

        Redirector redirector = new Redirector(getContext(), "/index.html", Redirector.MODE_CLIENT_PERMANENT);
        TemplateRoute route = router.attach("/",redirector);
        route.setMatchingMode(Template.MODE_EQUALS);
+4
source share
2 answers

The behavior is caused by how the class ClapClientHelperidentifies the target as a file or directory. A workaround is to replace the ClapClientHelper class with another almost identical, called say JarClapClientHelper. Copy the source code from ClapClientHelperand change the following snippet in the method handleClassLoader.

  // The ClassLoader returns a directory listing in some cases.
  // As this listing is partial, it is of little value in the context
  // of the CLAP client, so we have to ignore them.
  if (url != null) {
    if (url.getProtocol().equals("file")) {
      File file = new File(url.getFile());
      modificationDate = new Date(file.lastModified());

      if (file.isDirectory()) {
        url = null;
      }
    //NEW CODE HERE
    } else if (url.getProtocol().equals("jar")) {
      try {
        JarURLConnection conn = (JarURLConnection) url.openConnection();
        modificationDate = new Date(conn.getJarEntry().getLastModifiedTime().toMillis());
        if (conn.getJarEntry().isDirectory()) {
          url = null;
        }

      } catch (IOException ioe) {
        getLogger().log(Level.WARNING,
                "Unable to open the representation input stream",
                ioe);
        response.setStatus(Status.SERVER_ERROR_INTERNAL);
      }
    }
  }

. ( @Thierry Boileau.)

: , - , . - ServiceLoader :

  • META-INF/services/org.restlet.engine.ClientHelper .
  • : ( )

, :

Engine.getInstance().getRegisteredClients().add(0, new JarClapClientHelper(null));
+2

WAR:

Directory directory = new Directory(getContext(), "war:///");
directory.setIndexName("index.html");
router.attach("/", directory);

, , NullPointerException.

( Maven, ).

0

All Articles