THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public void sendOut(MessageExchange exchange, NormalizedMessage outMsg, HttpServletRequest request, HttpServletResponse response) throws Exception { // define a DataHandler (see Java Activation Framework) DataHandler dh = null; // if the out message received has no attachments, then we send an error to the client if (outMsg.getAttachmentNames().isEmpty()) { // use the super class method sendError to send an error to the client sendError(exchange, new Exception("Invalid answer from handler."), request, response); // and return return; } // get all attachment names from the out message Set set = outMsg.getAttachmentNames(); Iterator iterator = set.iterator(); String key = null; if (iterator.hasNext()) { // we are now only interested in the first attachment key = iterator.next().toString(); // this resolves the key which is also the filename dh = outMsg.getAttachment(key); // this will resolve the data handler (file content) } else { sendError(exchange, new Exception("Invalid answer from handler."), request, response); return; } if (dh == null) { sendError(exchange, new Exception("Invalid answer from handler."), request, response); return; } // create a temporary file File f = File.createTempFile("tmp_", key); // open an output stream to this file FileOutputStream fos = new FileOutputStream(f); // now we use the FileUtil's copyInputStream method to copy the content of the data // handlers data source into the output stream FileUtil.copyInputStream(dh.getDataSource().getInputStream(), fos); // and close the output stream afterwards fos.close(); // now create a file data source with the temporary file FileDataSource fds = new FileDataSource(f); // create a stream datasource with the input stream of the file data source, the // content type is provided by a method of the FileDataSource StreamDataSource sds = new StreamDataSource(fds.getInputStream(), fds.getContentType()); // then a new datahandler is created with the stream datasource DataHandler dhsds = new DataHandler(sds); // tells the response object to open a popup in the clients browser with the given // filename (stored in key) response.setHeader("content-disposition", "attachment; filename=\"" + key + "\""); // set the content type to what the FileDataSource determined automatically response.setContentType(fds.getContentType()); // also set the length of the content response.setContentLength((int)f.length()); try { // now get the output stream of the servlet to send data to the clients browser ServletOutputStream sos = response.getOutputStream(); // use the writeTo method of the data handler to easy to output of the data // into the servlets output stream dhsds.writeTo(sos); // finally set the status of the response to OK response.setStatus(HttpServletResponse.SC_OK); } catch (Exception e) { logger.log(Level.WARNING, "Exception occurred" + e.getMessage(), e); } // afterwards clean up the temporary file f.deleteOnExit(); } |
...