Coverage Report - org.kuali.rice.kew.export.web.ExportServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
ExportServlet
0%
0/24
0%
0/4
1.75
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 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  
  * A servet which generates and returns a file conforming to the specified {@link ExportFormat} 
 36  
  * with the exported data in it.
 37  
  *
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
 }