Coverage Report - org.kuali.rice.core.web.impex.ExportServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
ExportServlet
0%
0/24
0%
0/4
1.75
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.core.web.impex;
 17  
 
 18  
 import org.kuali.rice.core.api.CoreApiServiceLocator;
 19  
 import org.kuali.rice.core.api.impex.ExportDataSet;
 20  
 import org.kuali.rice.core.api.impex.xml.XmlExporterService;
 21  
 
 22  
 import javax.servlet.ServletException;
 23  
 import javax.servlet.http.HttpServlet;
 24  
 import javax.servlet.http.HttpServletRequest;
 25  
 import javax.servlet.http.HttpServletResponse;
 26  
 import java.io.IOException;
 27  
 import java.text.DateFormat;
 28  
 import java.text.SimpleDateFormat;
 29  
 import java.util.Date;
 30  
 
 31  
 
 32  
 /**
 33  
  * A servet which generates and returns a file conforming to the specified {@link ExportFormat} 
 34  
  * with the exported data in it.
 35  
  *
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  */
 38  0
 public class ExportServlet extends HttpServlet {
 39  
 
 40  
     private static final long serialVersionUID = -7766819916650887737L;
 41  
     
 42  
     public static final String EXPORT_DATA_SET_KEY = "ExportDataSet";
 43  
     
 44  
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 45  0
         ExportDataSet dataSet = (ExportDataSet)request.getSession().getAttribute(EXPORT_DATA_SET_KEY);
 46  0
         request.getSession().removeAttribute(EXPORT_DATA_SET_KEY);
 47  0
         if (dataSet == null) {
 48  0
             throw new ServletException("No data set was specified.");
 49  
         }
 50  0
         String contentType = "application/xml";
 51  0
         XmlExporterService exporter = CoreApiServiceLocator.getXmlExporterService();
 52  0
         byte[] data = exporter.export(dataSet);
 53  0
         response.setContentType(contentType);
 54  0
         response.setContentLength(data.length);
 55  0
         response.setHeader("Content-disposition", "attachment; filename="+extractFileName(request));
 56  0
         response.getOutputStream().write(data);
 57  0
         response.getOutputStream().close();
 58  0
     }
 59  
 
 60  
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 61  0
         doPost(request, response);
 62  0
     }
 63  
 
 64  
     private String extractFileName(HttpServletRequest request) {
 65  0
         String path = request.getPathInfo();
 66  0
         int index = path.lastIndexOf('/');
 67  0
         if (index >= 0) {
 68  0
             path = path.substring(index+1);
 69  
         }
 70  0
         return path;
 71  
     }
 72  
 
 73  
     public static final String generateExportPath(HttpServletRequest request, ExportDataSet dataSet) {
 74  0
         DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh_mm_ss");
 75  0
         String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
 76  0
         return basePath + "/export/wf-export-"+format.format(new Date())+".xml";
 77  
     }
 78  
 
 79  
 }