| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |   | 
  | 17 |  |  package org.kuali.rice.kew.export.web; | 
  | 18 |  |   | 
  | 19 |  |  import java.io.IOException; | 
  | 20 |  |  import java.text.DateFormat; | 
  | 21 |  |  import java.text.SimpleDateFormat; | 
  | 22 |  |  import java.util.Date; | 
  | 23 |  |   | 
  | 24 |  |  import javax.servlet.ServletException; | 
  | 25 |  |  import javax.servlet.http.HttpServlet; | 
  | 26 |  |  import javax.servlet.http.HttpServletRequest; | 
  | 27 |  |  import javax.servlet.http.HttpServletResponse; | 
  | 28 |  |   | 
  | 29 |  |  import org.kuali.rice.kew.export.ExportDataSet; | 
  | 30 |  |  import org.kuali.rice.kew.service.KEWServiceLocator; | 
  | 31 |  |  import org.kuali.rice.kew.xml.export.XmlExporterService; | 
  | 32 |  |   | 
  | 33 |  |   | 
  | 34 |  |   | 
  | 35 |  |   | 
  | 36 |  |   | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 | 0 |  public class ExportServlet extends HttpServlet { | 
  | 41 |  |   | 
  | 42 |  |      private static final long serialVersionUID = -7766819916650887737L; | 
  | 43 |  |       | 
  | 44 |  |      public static final String EXPORT_DATA_SET_KEY = "ExportDataSet"; | 
  | 45 |  |       | 
  | 46 |  |      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | 
  | 47 | 0 |          ExportDataSet dataSet = (ExportDataSet)request.getSession().getAttribute(EXPORT_DATA_SET_KEY); | 
  | 48 | 0 |          request.getSession().removeAttribute(EXPORT_DATA_SET_KEY); | 
  | 49 | 0 |          if (dataSet == null) { | 
  | 50 | 0 |              throw new ServletException("No data set was specified."); | 
  | 51 |  |          } | 
  | 52 | 0 |          String contentType = "application/xml"; | 
  | 53 | 0 |          XmlExporterService exporter = KEWServiceLocator.getXmlExporterService(); | 
  | 54 | 0 |          byte[] data = exporter.export(dataSet); | 
  | 55 | 0 |          response.setContentType(contentType); | 
  | 56 | 0 |          response.setContentLength(data.length); | 
  | 57 | 0 |          response.setHeader("Content-disposition", "attachment; filename="+extractFileName(request)); | 
  | 58 | 0 |          response.getOutputStream().write(data); | 
  | 59 | 0 |          response.getOutputStream().close(); | 
  | 60 | 0 |      } | 
  | 61 |  |   | 
  | 62 |  |      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | 
  | 63 | 0 |          doPost(request, response); | 
  | 64 | 0 |      } | 
  | 65 |  |   | 
  | 66 |  |      private String extractFileName(HttpServletRequest request) { | 
  | 67 | 0 |          String path = request.getPathInfo(); | 
  | 68 | 0 |          int index = path.lastIndexOf('/'); | 
  | 69 | 0 |          if (index >= 0) { | 
  | 70 | 0 |              path = path.substring(index+1); | 
  | 71 |  |          } | 
  | 72 | 0 |          return path; | 
  | 73 |  |      } | 
  | 74 |  |   | 
  | 75 |  |      public static final String generateExportPath(HttpServletRequest request, ExportDataSet dataSet) { | 
  | 76 | 0 |          DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh_mm_ss"); | 
  | 77 | 0 |          String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); | 
  | 78 | 0 |          return basePath + "/export/wf-export-"+format.format(new Date())+".xml"; | 
  | 79 |  |      } | 
  | 80 |  |   | 
  | 81 |  |  } |