Class DirectoryResource
- All Implemented Interfaces:
jakarta.servlet.Servlet,jakarta.servlet.ServletConfig,Serializable,BasicUniversalConfig,DefaultConfig,DefaultHtmlConfig
The root directory is specified in one of two ways:
-
Specifying the location via a
DirectoryResource.rootDir property. -
Overriding the
getRootDir()method.
Files are listed with VIEW and DOWNLOAD
operations through ?method=. Both are declared read=true. Deleting a file needs a real
DELETE, for example:
curl -X DELETE http://localhost:10000/<mount-path>/foo.txt
Read/write access control is handled through the following properties:
-
DirectoryResource.allowViews - Iftrue , allows view and download access to files. -
DirectoryResource.allowUploads - Iftrue , allows files to be created or overwritten. -
DirectoryResource.allowDeletes - Iftrue , allows files to be deleted.
Note: the method query parameter is accepted only for HEAD, OPTIONS and the two read
verbs VIEW and DOWNLOAD, so a link can view or download a file but never change one. A subclass that
widens allowedMethodParams still can't turn a GET into a write: the router refuses any override of a
GET to a method that is not a read. Uploads and deletes still change files, so put the resource behind a
CSRF boundary before mounting it anywhere a browser can reach. A boundary judges a ?method=VIEW or
?method=DOWNLOAD link as a write and refuses it, so behind one the listing's links stop working; send real
VIEW or DOWNLOAD requests instead.
See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classFile action link.classFile or directory details for REST response.Nested classes/interfaces inherited from class org.apache.juneau.rest.server.servlet.RestServlet
RestServlet.Builder<R extends RestServlet,SELF extends RestServlet.Builder<R, SELF>>, RestServlet.DefaultBuilder<R extends RestServlet> -
Field Summary
Fields inherited from class jakarta.servlet.http.HttpServlet
LEGACY_DO_HEAD -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.apache.juneau.microservice.examples.DirectoryResource.RedirectToRootdeleteFile(String path) Deletes a file on the file system.org.apache.juneau.microservice.examples.DirectoryResource.FileContentsdownloadFile(RestResponse res, String path) Downloads the contents of a file.getFile(RestRequest req, String path) Returns information about the specified file or directory.protected FileReturns the root directory.org.apache.juneau.microservice.examples.DirectoryResource.RedirectToRootupdateFile(InputStream is, String path) Adds or overwrites a file on the file system.org.apache.juneau.microservice.examples.DirectoryResource.FileContentsviewFile(RestResponse res, String path) Views the contents of a file.Methods inherited from class org.apache.juneau.rest.server.servlet.RestServlet
builder, createAuthenticator, destroy, doLog, getContext, getPath, getPaths, getRequest, getResponse, getRestBuilder, init, log, log, service, setContextMethods inherited from class jakarta.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPatch, doPost, doPut, doTrace, getLastModified, isSensitiveHeader, serviceMethods inherited from class jakarta.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
-
Constructor Details
-
DirectoryResource
Constructor.- Parameters:
c- The microservice configuration.
-
-
Method Details
-
deleteFile
public org.apache.juneau.microservice.examples.DirectoryResource.RedirectToRoot deleteFile(@Path("/*") String path) throws MethodNotAllowed Deletes a file on the file system.- Parameters:
path- The file path.- Returns:
- Redirect to root on success.
- Throws:
MethodNotAllowed- If deletes are not enabled.
-
downloadFile
public org.apache.juneau.microservice.examples.DirectoryResource.FileContents downloadFile(RestResponse res, @Path("/*") String path) throws NotFound, MethodNotAllowed Downloads the contents of a file.- Parameters:
res- The response.path- The file path.- Returns:
- The file contents stream.
- Throws:
NotFound- If the file is not found.MethodNotAllowed- If views are not enabled.
-
getFile
public DirectoryResource.FileResource getFile(RestRequest req, @Path("/*") String path) throws NotFound, Exception Returns information about the specified file or directory. -
updateFile
public org.apache.juneau.microservice.examples.DirectoryResource.RedirectToRoot updateFile(@Content @Schema(type="string",format="binary") InputStream is, @Path("/*") String path) throws InternalServerError Adds or overwrites a file on the file system.- Parameters:
is- The file contents.path- The file path.- Returns:
- Redirect to root on success.
- Throws:
InternalServerError- If the write fails.
-
viewFile
public org.apache.juneau.microservice.examples.DirectoryResource.FileContents viewFile(RestResponse res, @Path("/*") String path) throws NotFound, MethodNotAllowed Views the contents of a file.- Parameters:
res- The response.path- The file path.- Returns:
- The file contents stream.
- Throws:
NotFound- If the file is not found.MethodNotAllowed- If views are not enabled.
-
getRootDir
Returns the root directory.- Returns:
- The root directory.
-