001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.export.web;
018:
019: import java.io.IOException;
020: import java.text.DateFormat;
021: import java.text.SimpleDateFormat;
022: import java.util.Date;
023:
024: import javax.servlet.ServletException;
025: import javax.servlet.http.HttpServlet;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import edu.iu.uis.eden.KEWServiceLocator;
030: import edu.iu.uis.eden.export.ExportDataSet;
031: import edu.iu.uis.eden.export.ExportFormat;
032: import edu.iu.uis.eden.export.Exporter;
033:
034: /**
035: * A servet which generates and returns a file conforming to the specified {@link ExportFormat}
036: * with the exported data in it.
037: *
038: * @author ewestfal
039: */
040: public class ExportServlet extends HttpServlet {
041:
042: private static final long serialVersionUID = -7766819916650887737L;
043:
044: public static final String EXPORT_DATA_SET_KEY = "ExportDataSet";
045:
046: protected void doPost(HttpServletRequest request,
047: HttpServletResponse response) throws ServletException,
048: IOException {
049: ExportDataSet dataSet = (ExportDataSet) request.getSession()
050: .getAttribute(EXPORT_DATA_SET_KEY);
051: request.getSession().removeAttribute(EXPORT_DATA_SET_KEY);
052: if (dataSet == null) {
053: throw new ServletException("No data set was specified.");
054: }
055: String contentType = dataSet.getFormat().getMimeType();
056: Exporter exporter = null;
057: if (ExportFormat.XML.equals(dataSet.getFormat())) {
058: exporter = KEWServiceLocator.getXmlExporterService();
059: } else {
060: throw new ServletException(
061: "Cannot export for the given format "
062: + dataSet.getFormat());
063: }
064: byte[] data = exporter.export(dataSet.getFormat(), dataSet);
065: response.setContentType(contentType);
066: response.setContentLength(data.length);
067: response.setHeader("Content-disposition",
068: "attachment; filename=" + extractFileName(request));
069: response.getOutputStream().write(data);
070: response.getOutputStream().close();
071: }
072:
073: protected void doGet(HttpServletRequest request,
074: HttpServletResponse response) throws ServletException,
075: IOException {
076: doPost(request, response);
077: }
078:
079: private String extractFileName(HttpServletRequest request) {
080: String path = request.getPathInfo();
081: int index = path.lastIndexOf('/');
082: if (index >= 0) {
083: path = path.substring(index + 1);
084: }
085: return path;
086: }
087:
088: public static final String generateExportPath(
089: HttpServletRequest request, ExportDataSet dataSet) {
090: DateFormat format = new SimpleDateFormat(
091: "yyyy-MM-dd'T'hh_mm_ss");
092: String basePath = request.getScheme() + "://"
093: + request.getServerName() + ":"
094: + request.getServerPort() + request.getContextPath();
095: return basePath + "/export/wf-export-"
096: + format.format(new Date())
097: + dataSet.getFormat().getExtension();
098: }
099:
100: }
|