TempDirResource
The TempDirResource class shows examples of the following:
-
Extending the {@link oaj.microservice.resources.DirectoryResource} class.
-
Using the Apache ServletFileUpload class to handle multi-part form posts.
-
Using a system property string variable.
-
Using {@link oajr.RestMatcher RestMatchers}.
Pointing a browser to the resource shows the following:
http://localhost:10000/tempDir
Pointing a browser to the upload link shows a form entry page:
http://localhost:10000/tempDir/upload
Submitting a file redirects to the top-level page:
http://localhost:10000/tempDir
/**
* Sample resource that extends DirectoryResource to open up the temp directory as a REST resource.
*/
@RestResource(
path="/tempDir",
title="Temp Directory View Service",
description="View and download files in the '$R{rootDir}' directory.",
htmldoc=@HtmlDoc(
widgets={
ContentTypeMenuItem.class,
StyleMenuItem.class
},
navlinks={
"up: request:/..",
"options: servlet:/?method=OPTIONS",
"upload: servlet:/upload",
"$W{ContentTypeMenuItem}",
"$W{StyleMenuItem}",
"source: $C{Source/gitHub}/org/apache/juneau/examples/rest/$R{servletClassSimple}.java"
},
aside={
"<div style='max-width:400px' class='text'>",
" <p>Shows how to use the predefined DirectoryResource class.</p>",
" <p>Also shows how to use HTML5 beans to create a form entry page.</p>",
"</div>"
}
),
properties={
@Property(name="rootDir", value="$C{TempDirResource/dir,$S{java.io.tmpdir}}"),
@Property(name="allowViews", value="true"),
@Property(name="allowDeletes", value="true"),
@Property(name="allowPuts", value="false")
}
)
public class TempDirResource extends DirectoryResource {
@Override /* DirectoryResource */
@RestHook(INIT)
public void init(RestContextBuilder b) throws Exception {
super.init(b);
File rootDir = getRootDir();
if (! rootDir.exists()) {
rootDir.mkdirs();
// Make some dummy files.
FileUtils.touch(new File(rootDir, "A.txt"));
FileUtils.touch(new File(rootDir, "B.txt"));
FileUtils.touch(new File(rootDir, "C.txt"));
}
}
/**
* [GET /upload] - Display the form entry page for uploading a file to the temp directory.
*/
@RestMethod(name=GET, path="/upload")
public Form getUploadForm() {
return
form().id("form").action("servlet:/upload").method(POST).enctype("multipart/form-data")
.children(
input().name("contents").type("file"),
button("submit", "Submit")
)
;
}
/**
* [POST /upload] - Upload a file as a multipart form post.
* Shows how to use the Apache Commons ServletFileUpload class for handling multi-part form posts.
*/
@RestMethod(
name=POST,
path="/upload",
matchers=TempDirResource.MultipartFormDataMatcher.class
)
public Redirect uploadFile(RestRequest req) throws Exception {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(req);
while (iter.hasNext()) {
FileItemStream item = iter.next();
if (item.getFieldName().equals("contents")) {
File f = new File(getRootDir(), item.getName());
IOPipe.create(item.openStream(), new FileOutputStream(f)).closeOut().run();
}
}
return new Redirect(); // Redirect to the servlet root.
}
/** Causes a 404 if POST isn't multipart/form-data */
public static class MultipartFormDataMatcher extends RestMatcher {
@Override /* RestMatcher */
public boolean matches(RestRequest req) {
String contentType = req.getContentType();
return contentType != null && contentType.startsWith("multipart/form-data");
}
}
}