arinte
arinte

Reputation: 3728

Determine if a resource from the ServletContext is a file or directory

Given that we use the code below Set paths = servletCtxt.getResourcePaths("/app/themes");

How can we tell if the path in paths is directory or a actual file?

This would usually be in an zipped war.

What I did find is this, but I am betting it isn't reliable:

When it is a file

servletCtxt.getResource(dir).getContent()
    returned (java.io.ByteArrayInputStream) java.io.ByteArrayInputStream@5a645a64

When it is a directory it returned:

(org.apache.naming.resources.FileDirContext) org.apache.naming.resources.FileDirContext@57155715

Is it safe to say that I could use a instanceof with InputStream on what is returned by getContent?

Thanks

Upvotes: 1

Views: 662

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340733

Without testing I see the following in the JavaDoc of getResourcePaths():

Paths indicating subdirectory paths end with a /.

[...]

For example, for a web application containing:

  • /welcome.html

  • /catalog/index.html

  • /catalog/products.html

  • /catalog/offers/books.html

  • /catalog/offers/music.html

  • /customer/login.jsp

  • /WEB-INF/web.xml

  • /WEB-INF/classes/com.example.OrderServlet.class

  • /WEB-INF/lib/catalog.jar!/META-INF/resources/catalog/moreOffers/books.html

getResourcePaths("/") would return {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"}, and getResourcePaths("/catalog/") would return {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/", "/catalog/moreOffers/"}.

Seems like you can simply rely on the last character of the path.

Upvotes: 3

Related Questions