001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.servlet;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
021    import com.liferay.portal.kernel.servlet.HttpHeaders;
022    import com.liferay.portal.kernel.servlet.StringServletResponse;
023    import com.liferay.portal.kernel.util.ArrayUtil;
024    import com.liferay.portal.kernel.util.FileUtil;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.HttpUtil;
027    import com.liferay.portal.kernel.util.PropsUtil;
028    import com.liferay.portal.kernel.util.StreamUtil;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.util.StringUtil;
031    import com.liferay.portal.kernel.util.Validator;
032    
033    import java.io.IOException;
034    import java.io.InputStream;
035    
036    import java.net.SocketException;
037    
038    import javax.servlet.ServletOutputStream;
039    import javax.servlet.http.HttpServletRequest;
040    import javax.servlet.http.HttpServletResponse;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Shuyang Zhou
045     */
046    public class ServletResponseUtil {
047    
048            public static void sendFile(
049                            HttpServletRequest request, HttpServletResponse response,
050                            String fileName, byte[] bytes)
051                    throws IOException {
052    
053                    sendFile(request, response, fileName, bytes, null);
054            }
055    
056            public static void sendFile(
057                            HttpServletRequest request, HttpServletResponse response,
058                            String fileName, byte[] bytes, String contentType)
059                    throws IOException {
060    
061                    setHeaders(request, response, fileName, contentType);
062    
063                    write(response, bytes);
064            }
065    
066            public static void sendFile(
067                            HttpServletRequest request, HttpServletResponse response,
068                            String fileName, InputStream is)
069                    throws IOException {
070    
071                    sendFile(request, response, fileName, is, null);
072            }
073    
074            public static void sendFile(
075                            HttpServletRequest request, HttpServletResponse response,
076                            String fileName, InputStream is, int contentLength,
077                            String contentType)
078                    throws IOException {
079    
080                    setHeaders(request, response, fileName, contentType);
081    
082                    write(response, is, contentLength);
083            }
084    
085            public static void sendFile(
086                            HttpServletRequest request, HttpServletResponse response,
087                            String fileName, InputStream is, String contentType)
088                    throws IOException {
089    
090                    sendFile(request, response, fileName, is, 0, contentType);
091            }
092    
093            /**
094             * @deprecated
095             */
096            public static void sendFile(
097                            HttpServletResponse response, String fileName, byte[] bytes)
098                    throws IOException {
099    
100                    sendFile(null, response, fileName, bytes);
101            }
102    
103            /**
104             * @deprecated
105             */
106            public static void sendFile(
107                            HttpServletResponse response, String fileName, byte[] bytes,
108                            String contentType)
109                    throws IOException {
110    
111                    sendFile(null, response, fileName, bytes, contentType);
112            }
113    
114            /**
115             * @deprecated
116             */
117            public static void sendFile(
118                            HttpServletResponse response, String fileName, InputStream is)
119                    throws IOException {
120    
121                    sendFile(null, response, fileName, is);
122            }
123    
124            /**
125             * @deprecated
126             */
127            public static void sendFile(
128                            HttpServletResponse response, String fileName, InputStream is,
129                            int contentLength, String contentType)
130                    throws IOException {
131    
132                    sendFile(null, response, fileName, is, contentLength, contentType);
133            }
134    
135            /**
136             * @deprecated
137             */
138            public static void sendFile(
139                            HttpServletResponse response, String fileName, InputStream is,
140                            String contentType)
141                    throws IOException {
142    
143                    sendFile(null, response, fileName, is, contentType);
144            }
145    
146            public static void write(HttpServletResponse response, byte[] bytes)
147                    throws IOException {
148    
149                    write(response, bytes, 0);
150            }
151    
152            public static void write(
153                            HttpServletResponse response, byte[] bytes, int contentLength)
154                    throws IOException {
155    
156                    try {
157    
158                            // LEP-3122
159    
160                            if (!response.isCommitted()) {
161    
162                                    // LEP-536
163    
164                                    if (contentLength == 0) {
165                                            contentLength = bytes.length;
166                                    }
167    
168                                    response.setContentLength(contentLength);
169    
170                                    ServletOutputStream servletOutputStream =
171                                            response.getOutputStream();
172    
173                                    servletOutputStream.write(bytes, 0, contentLength);
174                            }
175                    }
176                    catch (IOException ioe) {
177                            if (ioe instanceof SocketException ||
178                                    ioe.getClass().getName().equals(_CLIENT_ABORT_EXCEPTION)) {
179    
180                                    if (_log.isWarnEnabled()) {
181                                            _log.warn(ioe);
182                                    }
183                            }
184                            else {
185                                    throw ioe;
186                            }
187                    }
188            }
189    
190            public static void write(HttpServletResponse response, byte[][] bytesArray)
191                    throws IOException {
192    
193                    try {
194    
195                            // LEP-3122
196    
197                            if (!response.isCommitted()) {
198                                    int contentLength = 0;
199    
200                                    for (byte[] bytes : bytesArray) {
201                                            contentLength += bytes.length;
202                                    }
203    
204                                    response.setContentLength(contentLength);
205    
206                                    ServletOutputStream servletOutputStream =
207                                            response.getOutputStream();
208    
209                                    for (byte[] bytes : bytesArray) {
210                                            servletOutputStream.write(bytes);
211                                    }
212                            }
213                    }
214                    catch (IOException ioe) {
215                            if (ioe instanceof SocketException ||
216                                    ioe.getClass().getName().equals(_CLIENT_ABORT_EXCEPTION)) {
217    
218                                    if (_log.isWarnEnabled()) {
219                                            _log.warn(ioe);
220                                    }
221                            }
222                            else {
223                                    throw ioe;
224                            }
225                    }
226            }
227    
228            public static void write(HttpServletResponse response, InputStream is)
229                    throws IOException {
230    
231                    write(response, is, 0);
232            }
233    
234            public static void write(
235                            HttpServletResponse response, InputStream is, int contentLength)
236                    throws IOException {
237    
238                    if (response.isCommitted()) {
239                            return;
240                    }
241    
242                    if (contentLength > 0) {
243                            response.setContentLength(contentLength);
244                    }
245    
246                    StreamUtil.transfer(is, response.getOutputStream());
247            }
248    
249            public static void write(HttpServletResponse response, String s)
250                    throws IOException {
251    
252                    write(response, s.getBytes(StringPool.UTF8));
253            }
254    
255            public static void write(
256                            HttpServletResponse response, StringServletResponse stringResponse)
257                    throws IOException {
258    
259                    if (stringResponse.isCalledGetOutputStream()) {
260                            UnsyncByteArrayOutputStream unsyncByteArrayInputStream =
261                                    stringResponse.getUnsyncByteArrayOutputStream();
262    
263                            write(
264                                    response, unsyncByteArrayInputStream.unsafeGetByteArray(),
265                                    unsyncByteArrayInputStream.size());
266                    }
267                    else {
268                            write(response, stringResponse.getString());
269                    }
270            }
271    
272            protected static void setHeaders(
273                    HttpServletRequest request, HttpServletResponse response,
274                    String fileName, String contentType) {
275    
276                    if (_log.isDebugEnabled()) {
277                            _log.debug("Sending file of type " + contentType);
278                    }
279    
280                    // LEP-2201
281    
282                    if (Validator.isNotNull(contentType)) {
283                            response.setContentType(contentType);
284                    }
285    
286                    response.setHeader(
287                            HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_PUBLIC_VALUE);
288                    response.setHeader(HttpHeaders.PRAGMA, HttpHeaders.PRAGMA_PUBLIC_VALUE);
289    
290                    if (Validator.isNotNull(fileName)) {
291                            String contentDisposition =
292                                    "attachment; filename=\"" + fileName + "\"";
293    
294                            // If necessary for non-ASCII characters, encode based on RFC 2184.
295                            // However, not all browsers support RFC 2184. See LEP-3127.
296    
297                            boolean ascii = true;
298    
299                            for (int i = 0; i < fileName.length(); i++) {
300                                    if (!Validator.isAscii(fileName.charAt(i))) {
301                                            ascii = false;
302    
303                                            break;
304                                    }
305                            }
306    
307                            try {
308                                    if (!ascii) {
309                                            String encodedFileName = HttpUtil.encodeURL(fileName, true);
310    
311                                            if (BrowserSnifferUtil.isIe(request)) {
312                                                    contentDisposition =
313                                                            "attachment; filename=\"" + encodedFileName + "\"";
314                                            }
315                                            else {
316                                                    contentDisposition =
317                                                            "attachment; filename*=UTF-8''" + encodedFileName;
318                                            }
319                                    }
320                            }
321                            catch (Exception e) {
322                                    if (_log.isWarnEnabled()) {
323                                            _log.warn(e);
324                                    }
325                            }
326    
327                            String extension = GetterUtil.getString(
328                                    FileUtil.getExtension(fileName)).toLowerCase();
329    
330                            String[] mimeTypesContentDispositionInline = null;
331    
332                            try {
333                                    mimeTypesContentDispositionInline = PropsUtil.getArray(
334                                            "mime.types.content.disposition.inline");
335                            }
336                            catch (Exception e) {
337                                    mimeTypesContentDispositionInline = new String[0];
338                            }
339    
340                            if (ArrayUtil.contains(
341                                            mimeTypesContentDispositionInline, extension)) {
342    
343                                    contentDisposition = StringUtil.replace(
344                                            contentDisposition, "attachment; ", "inline; ");
345                            }
346    
347                            response.setHeader(
348                                    HttpHeaders.CONTENT_DISPOSITION, contentDisposition);
349                    }
350            }
351    
352            private static final String _CLIENT_ABORT_EXCEPTION =
353                    "org.apache.catalina.connector.ClientAbortException";
354    
355            private static Log _log = LogFactoryUtil.getLog(ServletResponseUtil.class);
356    
357    }