001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/contrib/ufp/usermembership/trunk/tool/src/java/org/sakaiproject/umem/tool/ui/Export.java $
003: * $Id: Export.java 4298 2007-03-16 12:47:20Z nuno@ufp.pt $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006, 2007 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.umem.tool.ui;
021:
022: import java.io.IOException;
023: import java.io.OutputStream;
024: import java.text.DateFormat;
025: import java.text.SimpleDateFormat;
026: import java.util.Date;
027:
028: import javax.faces.application.StateManager;
029: import javax.faces.context.FacesContext;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.sakaiproject.util.ResourceLoader;
035:
036: public class Export {
037: /** Our log (commons). */
038: private static Log LOG = LogFactory.getLog(SiteListBean.class);
039: /** Resource bundle */
040: private static transient ResourceLoader msgs = new ResourceLoader(
041: "org.sakaiproject.umem.tool.bundle.Messages");
042:
043: public static void writeAsCsv(String csvString,
044: String prefixFileName) {
045: String fileName = getFileName(prefixFileName);
046: FacesContext faces = FacesContext.getCurrentInstance();
047: HttpServletResponse response = (HttpServletResponse) faces
048: .getExternalContext().getResponse();
049: protectAgainstInstantDeletion(response);
050: response.setContentType("text/comma-separated-values");
051: response.setHeader("Content-disposition",
052: "attachment; filename=" + fileName + ".csv");
053: response.setContentLength(csvString.length());
054: OutputStream out = null;
055: try {
056: out = response.getOutputStream();
057: out.write(csvString.getBytes());
058: out.flush();
059: } catch (IOException e) {
060: LOG.error(e);
061: e.printStackTrace();
062: } finally {
063: try {
064: if (out != null)
065: out.close();
066: } catch (IOException e) {
067: LOG.error(e);
068: e.printStackTrace();
069: }
070: }
071: faces.responseComplete();
072: StateManager stateManager = (StateManager) faces
073: .getApplication().getStateManager();
074: stateManager.saveSerializedView(faces);
075: }
076:
077: public static StringBuffer appendQuoted(StringBuffer sb,
078: String toQuote) {
079: if (toQuote == null) {
080: ;
081: } else if ((toQuote.indexOf(',') >= 0)
082: || (toQuote.indexOf('"') >= 0)) {
083: String out = toQuote.replaceAll("\"", "\"\"");
084: if (LOG.isDebugEnabled())
085: LOG.debug("Turning '" + toQuote + "' to '" + out + "'");
086: sb.append("\"").append(out).append("\"");
087: } else {
088: sb.append(toQuote);
089: }
090: return sb;
091: }
092:
093: /**
094: * Gets the filename for the export
095: * @param prefix Filenameprefix
096: * @return The appropriate filename for the export
097: */
098: public static String getFileName(String prefix) {
099: Date now = new Date();
100: DateFormat df = new SimpleDateFormat(msgs
101: .getString("export_filename_date_format"));
102: StringBuffer fileName = new StringBuffer(prefix);
103: fileName.append("-");
104: fileName.append(df.format(now));
105: return fileName.toString();
106: }
107:
108: /**
109: * Try to head off a problem with downloading files from a secure HTTPS
110: * connection to Internet Explorer. When IE sees it's talking to a secure
111: * server, it decides to treat all hints or instructions about caching as
112: * strictly as possible. Immediately upon finishing the download, it throws
113: * the data away. Unfortunately, the way IE sends a downloaded file on to a
114: * helper application is to use the cached copy. Having just deleted the
115: * file, it naturally isn't able to find it in the cache. Whereupon it
116: * delivers a very misleading error message like: "Internet Explorer cannot
117: * download roster from sakai.yoursite.edu. Internet Explorer was not able
118: * to open this Internet site. The requested site is either unavailable or
119: * cannot be found. Please try again later." There are several ways to turn
120: * caching off, and so to be safe we use several ways to turn it back on
121: * again. This current workaround should let IE users save the files to
122: * disk. Unfortunately, errors may still occur if a user attempts to open
123: * the file directly in a helper application from a secure web server. TODO
124: * Keep checking on the status of this.
125: */
126: public static void protectAgainstInstantDeletion(
127: HttpServletResponse response) {
128: response.reset(); // Eliminate the added-on stuff
129: response.setHeader("Pragma", "public"); // Override old-style cache
130: // control
131: response
132: .setHeader("Cache-Control",
133: "public, must-revalidate, post-check=0, pre-check=0, max-age=0"); // New-style
134: }
135: }
|