01: /**********************************************************************************
02: *
03: * $Id: SpreadsheetUtil.java 22226 2007-03-06 17:42:53Z ray@media.berkeley.edu $
04: *
05: ***********************************************************************************
06: *
07: * Copyright (c) 2007 The Regents of the University of California
08: *
09: * Licensed under the Educational Community License, Version 1.0 (the "License");
10: * you may not use this file except in compliance with the License.
11: * You may obtain a copy of the License at
12: *
13: * http://www.opensource.org/licenses/ecl1.php
14: *
15: * Unless required by applicable law or agreed to in writing, software
16: * distributed under the License is distributed on an "AS IS" BASIS,
17: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18: * See the License for the specific language governing permissions and
19: * limitations under the License.
20: *
21: **********************************************************************************/package org.sakaiproject.jsf.spreadsheet;
22:
23: import java.util.List;
24:
25: import javax.faces.context.FacesContext;
26: import javax.servlet.http.HttpServletResponse;
27:
28: /**
29: *
30: */
31: public class SpreadsheetUtil {
32: /**
33: * Download a spreadsheet file containing the input list of data.
34: *
35: * @param spreadsheetData a list of rows, beginning with a header row, each being a list
36: * @param fileName not including the file extension, since that's format-dependent
37: */
38: public static void downloadSpreadsheetData(
39: List<List<Object>> spreadsheetData, String fileName,
40: SpreadsheetDataFileWriter fileWriter) {
41: FacesContext faces = FacesContext.getCurrentInstance();
42: HttpServletResponse response = (HttpServletResponse) faces
43: .getExternalContext().getResponse();
44: protectAgainstInstantDeletion(response);
45: fileWriter.writeDataToResponse(spreadsheetData, fileName,
46: response);
47: faces.responseComplete();
48: }
49:
50: /**
51: * Try to head off a problem with downloading files from a secure HTTPS
52: * connection to Internet Explorer.
53: *
54: * When IE sees it's talking to a secure server, it decides to treat all hints
55: * or instructions about caching as strictly as possible. Immediately upon
56: * finishing the download, it throws the data away.
57: *
58: * Unfortunately, the way IE sends a downloaded file on to a helper
59: * application is to use the cached copy. Having just deleted the file,
60: * it naturally isn't able to find it in the cache. Whereupon it delivers
61: * a very misleading error message like:
62: * "Internet Explorer cannot download roster from sakai.yoursite.edu.
63: * Internet Explorer was not able to open this Internet site. The requested
64: * site is either unavailable or cannot be found. Please try again later."
65: *
66: * There are several ways to turn caching off, and so to be safe we use
67: * several ways to turn it back on again.
68: *
69: * This current workaround should let IE users save the files to disk.
70: * Unfortunately, errors may still occur if a user attempts to open the
71: * file directly in a helper application from a secure web server.
72: *
73: * TODO Keep checking on the status of this.
74: */
75: private static void protectAgainstInstantDeletion(
76: HttpServletResponse response) {
77: response.reset(); // Eliminate the added-on stuff
78: response.setHeader("Pragma", "public"); // Override old-style cache control
79: response
80: .setHeader("Cache-Control",
81: "public, must-revalidate, post-check=0, pre-check=0, max-age=0"); // New-style
82: }
83:
84: }
|