001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.servlet;
022:
023: import com.liferay.portal.kernel.servlet.HttpHeaders;
024: import com.liferay.portal.kernel.util.GetterUtil;
025: import com.liferay.portal.kernel.util.ServerDetector;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.util.FileUtil;
029:
030: import java.io.BufferedOutputStream;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.io.OutputStream;
034:
035: import javax.servlet.http.HttpServletResponse;
036:
037: import org.apache.commons.codec.net.URLCodec;
038: import org.apache.commons.lang.CharUtils;
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041:
042: /**
043: * <a href="ServletResponseUtil.java.html"><b><i>View Source</i></b></a>
044: *
045: * @author Brian Wing Shun Chan
046: *
047: */
048: public class ServletResponseUtil {
049:
050: public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
051:
052: public static void cleanUp(InputStream is) {
053: try {
054: if (is != null) {
055: is.close();
056: }
057: } catch (Exception e) {
058: if (_log.isWarnEnabled()) {
059: _log.warn(e);
060: }
061: }
062: }
063:
064: public static void cleanUp(OutputStream os) {
065: try {
066: if (os != null) {
067: os.flush();
068: }
069: } catch (Exception e) {
070: if (_log.isWarnEnabled()) {
071: _log.warn(e);
072: }
073: }
074:
075: try {
076: if (os != null) {
077: os.close();
078: }
079: } catch (Exception e) {
080: if (_log.isWarnEnabled()) {
081: _log.warn(e);
082: }
083: }
084: }
085:
086: public static void cleanUp(OutputStream os, InputStream is) {
087: cleanUp(os);
088: cleanUp(is);
089: }
090:
091: public static void sendFile(HttpServletResponse res,
092: String fileName, byte[] byteArray) throws IOException {
093:
094: sendFile(res, fileName, byteArray, null);
095: }
096:
097: public static void sendFile(HttpServletResponse res,
098: String fileName, byte[] byteArray, String contentType)
099: throws IOException {
100:
101: setHeaders(res, fileName, contentType);
102:
103: write(res, byteArray);
104: }
105:
106: public static void sendFile(HttpServletResponse res,
107: String fileName, InputStream is) throws IOException {
108:
109: sendFile(res, fileName, is, null);
110: }
111:
112: public static void sendFile(HttpServletResponse res,
113: String fileName, InputStream is, String contentType)
114: throws IOException {
115:
116: setHeaders(res, fileName, contentType);
117:
118: write(res, is);
119: }
120:
121: public static void write(HttpServletResponse res, String s)
122: throws IOException {
123:
124: write(res, s.getBytes("UTF-8"));
125: }
126:
127: public static void write(HttpServletResponse res, byte[] byteArray)
128: throws IOException {
129:
130: write(res, byteArray, 0);
131: }
132:
133: public static void write(HttpServletResponse res, byte[] byteArray,
134: int contentLength) throws IOException {
135:
136: OutputStream os = null;
137:
138: try {
139:
140: // LEP-3122
141:
142: if (!res.isCommitted() || ServerDetector.isPramati()) {
143:
144: // LEP-536
145:
146: if (contentLength == 0) {
147: contentLength = byteArray.length;
148: }
149:
150: res.setContentLength(contentLength);
151:
152: os = new BufferedOutputStream(res.getOutputStream());
153:
154: os.write(byteArray, 0, contentLength);
155: }
156: } finally {
157: cleanUp(os);
158: }
159: }
160:
161: public static void write(HttpServletResponse res, InputStream is)
162: throws IOException {
163:
164: OutputStream os = null;
165:
166: try {
167: if (!res.isCommitted()) {
168: os = new BufferedOutputStream(res.getOutputStream());
169:
170: int c = is.read();
171:
172: while (c != -1) {
173: os.write(c);
174:
175: c = is.read();
176: }
177: }
178: } finally {
179: cleanUp(os, is);
180: }
181: }
182:
183: protected static void setHeaders(HttpServletResponse res,
184: String fileName, String contentType) throws IOException {
185:
186: if (_log.isDebugEnabled()) {
187: _log.debug("Sending file of type " + contentType);
188: }
189:
190: // LEP-2201
191:
192: if (Validator.isNotNull(contentType)) {
193: res.setContentType(contentType);
194: }
195:
196: res.setHeader(HttpHeaders.CACHE_CONTROL, HttpHeaders.PUBLIC);
197: res.setHeader(HttpHeaders.PRAGMA, HttpHeaders.PUBLIC);
198:
199: if (Validator.isNotNull(fileName)) {
200: String contentDisposition = "attachment; filename=\""
201: + fileName + "\"";
202:
203: // If necessary for non-ASCII characters, encode based on RFC 2184.
204: // However, not all browsers support RFC 2184. See LEP-3127.
205:
206: boolean ascii = true;
207:
208: for (int i = 0; i < fileName.length(); i++) {
209: if (!CharUtils.isAscii(fileName.charAt(i))) {
210: ascii = false;
211:
212: break;
213: }
214: }
215:
216: try {
217: if (!ascii) {
218: URLCodec codec = new URLCodec("UTF-8");
219:
220: String encodedFileName = StringUtil.replace(codec
221: .encode(fileName), "+", "%20");
222:
223: contentDisposition = "attachment; filename*=UTF-8''"
224: + encodedFileName;
225: }
226: } catch (Exception e) {
227: if (_log.isWarnEnabled()) {
228: _log.warn(e);
229: }
230: }
231:
232: String extension = GetterUtil.getString(FileUtil
233: .getExtension(fileName));
234:
235: if (extension.equals("pdf")) {
236: contentDisposition = StringUtil.replace(
237: contentDisposition, "attachment; ", "inline; ");
238: }
239:
240: res.setHeader(HttpHeaders.CONTENT_DISPOSITION,
241: contentDisposition);
242: }
243: }
244:
245: private static Log _log = LogFactory
246: .getLog(ServletResponseUtil.class);
247:
248: }
|