Register | Forgot password
The WebManager architecture allows panels with a user interface to be created very easily. However, when trying to generate a stream to the browser, I discovered nobody has done this the way I wanted and therefore I couldn’t rely on some existing documentation. Today I’ll write about streaming content to a browser from within a panel, streaming content from a browser back to the server and something about reading a ZIP file in Java.
Let’s start with the easy part: uploading a file from the browser to WebManager, using a WCB panel. There are only three pretty straight-forward things to do. First I define a MultipartFile property on the form-backing object, including a getter and setter. Second I place a file upload field in the panel, by adding this fragment to my edit JSP:
<wmedit:fileUpload path="command.importFile" />
At this point, after the WCB is built and deployed, an upload field is visible in the panel and when a file is selected, it is streamed to the server. The last thing we need to do is to process the file on the server, which is done inside the onSubmit() method in the tab controller. Via the form-backing object, the MultipartFile property can be accessed, which gives access to the file name, size, content type and the actual bytes. These bytes can be read into a byte array using getBytes() or streamed using the getInputStream() method.
So at this point I have an InputStream object to read the file the browser is sending. The intention of my panel is to upload a ZIP file, which the server should extract and process. One way to do this is to extract the ZIP file into a temporary directory, process the files from there and afterwards remove the temporary directory. However, Java provides a java.util.zip package, which provides ZipInputStream and ZipEntry for my code. More information on reading and writing ZIP files with this package can be found in the JavaDoc.
try {
zip = new ZipInputStream(stream);
ZipEntry zipEntry = null;
while ((zipEntry = zip.getNextEntry()) != null) {
String filename = zipEntry.getName();
log.finer("Processing entry " + filename);
}
} finally {
if (zip != null) {
zip.close();
}
}
Now you have seen the easy way to upload files from a browser into a panel, I’ll show the easy way to stream content to the browser. On my panel is a button that says “Export” and a click on it will start a process that generates a CSV file which is streamed to the browser. What you already know is that submitting the panel will call the onSubmit() method on my tab controller. What the documentation does not say is that the behavior of WebManager after the onSubmit() method returns can be changed.
By default, WebManager will send a redirect to the browser after the onSubmit() method has finished, so the browser will request a new page. This happens because by default a redirect view is selected by WebManager. However, during onSubmit() this can be changed:
modelAndView.setViewName(null); modelAndView.setView(new MyView());
The MyView class implements the org.springframework.web.servlet.View interface. After the onSubmit() method return and the submitted data is processed, the view will be used to render the data to send to the browser. So I’m in charge again and I’m in the position to stream my export:
class MyView implements View {
public void render(Map controlMap, HttpServletRequest request, HttpServletResponse response) throws Exception {
String curDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date());
String filename = "export-" + curDate + ".csv";
response.addHeader("Content-Disposition", "attachment;filename=" + filename);
PrintWriter pw = new PrintWriter(response.getOutputStream());
for (Entity entity: MyTabController.this.MyService.getAllEntities()) {
// Write one line to the file
}
pw.close();
}
public String getContentType() {
return "application/octet-stream";
}
}
Mark is software engineer with a special interest in Security and Digital WebTV. Mark writes about daily engineering with GX WebManager
Read all Marks blog entries
Other blog entries: