View Javadoc

1   package org.kuali.student.common.ui.server.gwt;
2   
3   import java.io.IOException;
4   
5   import javax.servlet.ServletOutputStream;
6   import javax.servlet.http.HttpServlet;
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpServletResponse;
9   
10  import org.apache.log4j.Logger;
11  import org.kuali.student.common.ui.client.util.ExportUtils;
12  
13  public class ExportDocumentDownload extends HttpServlet {
14  
15  	final Logger LOG = Logger.getLogger(ExportDocumentDownload.class);
16  	private static final long serialVersionUID = 1L;
17  
18  	public void doGet(HttpServletRequest request, HttpServletResponse response) {
19  		try {
20  			// If PDF is generated to to temp file, read it
21  			String exportId = request.getParameter("exportId");
22  			String format = request.getParameter("format");
23  			// Get from session the byte array that was cached by the GWT Servlet
24  			byte[] bytes = (byte[]) request.getSession().getAttribute(exportId);
25  
26  			sendPDF(response, bytes, format);
27  		} catch (Exception ex) {
28  			// TODO Nina how must we handle exceptions here??
29  			// do something here
30  		}
31  	}
32  
33  	void sendPDF(HttpServletResponse response, byte[] bytes, String format) throws IOException {
34  		ServletOutputStream stream = response.getOutputStream();
35  		if (format.equals(ExportUtils.PDF)) {
36  			response.setContentType("application/pdf");
37  			response.addHeader("Content-Type", "application/pdf");
38  			response.addHeader("Content-Disposition", "inline; filename=export.pdf");
39  			response.setContentLength((int) bytes.length);
40  		} else if (format.equals(ExportUtils.XLS)) {
41  			response.setContentType("application/xls");
42  			response.addHeader("Content-Type", "application/xls");
43  			response.addHeader("Content-Disposition", "inline; filename=export.xls");
44  			response.setContentLength((int) bytes.length);   
45  
46  		} else {
47  			response.setContentType("application/ms-word");
48  			response.addHeader("Content-Type", "application/ms-word");
49  			response.addHeader("Content-Disposition", "inline; filename=export.doc");
50  			response.setContentLength((int) bytes.length);
51  		}
52  		stream.write(bytes);
53  		stream.close();
54  	}
55  }