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 2006-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  
 
 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  
  * A servet which generates and returns a file conforming to the specified {@link ExportFormat} 
 35  
  * with the exported data in it.
 36  
  *
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
 }