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