2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
\r
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
5 * of this software and associated documentation files (the "Software"), to deal
\r
6 * in the Software without restriction, including without limitation the rights
\r
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
8 * copies of the Software, and to permit persons to whom the Software is
\r
9 * furnished to do so, subject to the following conditions:
\r
11 * The above copyright notice and this permission notice shall be included in
\r
12 * all copies or substantial portions of the Software.
\r
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
23 package com.pentila.entSavoie.servlet.impl;
\r
25 import java.io.BufferedOutputStream;
\r
26 import java.io.IOException;
\r
27 import java.io.InputStream;
\r
28 import java.io.OutputStream;
\r
29 import java.net.SocketException;
\r
31 import javax.servlet.http.HttpServletResponse;
\r
33 import org.apache.commons.codec.net.URLCodec;
\r
34 import org.apache.commons.lang.CharUtils;
\r
36 import com.liferay.portal.kernel.log.Log;
\r
37 import com.liferay.portal.kernel.log.LogFactoryUtil;
\r
38 import com.liferay.portal.kernel.servlet.HttpHeaders;
\r
39 import com.liferay.portal.kernel.util.ArrayUtil;
\r
40 import com.liferay.portal.kernel.util.FileUtil;
\r
41 import com.liferay.portal.kernel.util.GetterUtil;
\r
42 import com.liferay.portal.kernel.util.PropsUtil;
\r
43 import com.liferay.portal.kernel.util.StringPool;
\r
44 import com.liferay.portal.kernel.util.StringUtil;
\r
45 import com.liferay.portal.kernel.util.Validator;
\r
46 import com.pentila.entSavoie.servlet.ServletResponseService;
\r
49 * <a href="ServletResponseUtil.java.html"><b><i>View Source</i></b></a>
\r
51 * @author Brian Wing Shun Chan
\r
54 public class ServletResponseServiceImpl implements ServletResponseService {
\r
56 public void cleanUp(InputStream is) {
\r
62 catch (Exception e) {
\r
63 if (e instanceof SocketException ||
\r
64 e.getClass().getName().equals(_CLIENT_ABORT_EXCEPTION)) {
\r
66 if (_log.isWarnEnabled()) {
\r
67 _log.warn(e.getClass().getSimpleName());
\r
76 public void cleanUp(OutputStream os) {
\r
82 catch (Exception e) {
\r
83 if (e instanceof SocketException ||
\r
84 e.getClass().getName().equals(_CLIENT_ABORT_EXCEPTION)) {
\r
86 if (_log.isWarnEnabled()) {
\r
87 _log.warn(e.getClass().getSimpleName());
\r
100 catch (Exception e) {
\r
101 if (e instanceof SocketException ||
\r
102 e.getClass().getName().equals(_CLIENT_ABORT_EXCEPTION)) {
\r
104 if (_log.isWarnEnabled()) {
\r
105 _log.warn(e.getClass().getSimpleName());
\r
114 public void cleanUp(OutputStream os, InputStream is) {
\r
119 public void sendFile(
\r
120 HttpServletResponse response, String fileName, byte[] bytes)
\r
121 throws IOException {
\r
123 sendFile(response, fileName, bytes, null);
\r
126 public void sendFile(
\r
127 HttpServletResponse response, String fileName, byte[] bytes,
\r
128 String contentType)
\r
129 throws IOException {
\r
131 setHeaders(response, fileName, contentType, "");
\r
133 write(response, bytes);
\r
136 public void sendFile(
\r
137 HttpServletResponse response, String fileName, byte[] bytes,
\r
138 String contentType, String forceContentDisposition)
\r
139 throws IOException {
\r
141 setHeaders(response, fileName, contentType, forceContentDisposition);
\r
143 write(response, bytes);
\r
146 public void sendFile(
\r
147 HttpServletResponse response, String fileName, InputStream is)
\r
148 throws IOException {
\r
150 sendFile(response, fileName, is, null);
\r
153 public void sendFile(
\r
154 HttpServletResponse response, String fileName, InputStream is,
\r
155 String contentType)
\r
156 throws IOException {
\r
158 sendFile(response, fileName, is, 0, contentType);
\r
161 public void sendFile(
\r
162 HttpServletResponse response, String fileName, InputStream is,
\r
163 int contentLength, String contentType)
\r
164 throws IOException {
\r
166 setHeaders(response, fileName, contentType, "");
\r
168 write(response, is, contentLength);
\r
171 public void sendFile(
\r
172 HttpServletResponse response, String fileName, InputStream is,
\r
173 int contentLength, String contentType, String forceContentDisposition)
\r
174 throws IOException {
\r
176 setHeaders(response, fileName, contentType, forceContentDisposition);
\r
178 write(response, is, contentLength);
\r
181 public void write(HttpServletResponse response, String s)
\r
182 throws IOException {
\r
184 write(response, s.getBytes(StringPool.UTF8));
\r
187 public void write(HttpServletResponse response, byte[] bytes)
\r
188 throws IOException {
\r
190 write(response, bytes, 0);
\r
194 HttpServletResponse response, byte[] bytes, int contentLength)
\r
195 throws IOException {
\r
197 OutputStream os = null;
\r
203 if (!response.isCommitted()) {
\r
207 if (contentLength == 0) {
\r
208 contentLength = bytes.length;
\r
211 response.setContentLength(contentLength);
\r
213 os = new BufferedOutputStream(response.getOutputStream());
\r
215 os.write(bytes, 0, contentLength);
\r
218 catch (IOException ioe) {
\r
219 if (ioe instanceof SocketException ||
\r
220 ioe.getClass().getName().equals(_CLIENT_ABORT_EXCEPTION)) {
\r
222 if (_log.isWarnEnabled()) {
\r
223 _log.warn(ioe.getClass().getSimpleName());
\r
235 public void write(HttpServletResponse response, InputStream is)
\r
236 throws IOException {
\r
238 write(response, is, 0);
\r
242 HttpServletResponse response, InputStream is, int contentLength)
\r
243 throws IOException {
\r
245 OutputStream os = null;
\r
248 if (!response.isCommitted()) {
\r
249 if (contentLength > 0) {
\r
250 response.setContentLength(contentLength);
\r
253 os = new BufferedOutputStream(response.getOutputStream());
\r
264 catch (Exception e) {
\r
265 if (e instanceof SocketException ||
\r
266 e.getClass().getName().equals(_CLIENT_ABORT_EXCEPTION)) {
\r
268 if (_log.isWarnEnabled()) {
\r
269 _log.warn(e.getClass().getSimpleName());
\r
281 public void setHeaders(
\r
282 HttpServletResponse response,
\r
284 String contentType,
\r
285 String forceContentDisposition) {
\r
287 if (_log.isDebugEnabled()) {
\r
288 _log.debug("Sending file of type " + contentType);
\r
293 if (Validator.isNotNull(contentType)) {
\r
294 response.setContentType(contentType);
\r
297 response.setHeader(
\r
298 HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_PUBLIC_VALUE);
\r
299 response.setHeader(HttpHeaders.PRAGMA, HttpHeaders.PRAGMA_PUBLIC_VALUE);
\r
301 if (Validator.isNotNull(fileName)) {
\r
302 String contentDisposition =
\r
303 "attachment; filename=\"" + fileName + "\"";
\r
305 // If necessary for non-ASCII characters, encode based on RFC 2184.
\r
306 // However, not all browsers support RFC 2184. See LEP-3127.
\r
308 boolean ascii = true;
\r
310 for (int i = 0; i < fileName.length(); i++) {
\r
311 if (!CharUtils.isAscii(fileName.charAt(i))) {
\r
320 URLCodec codec = new URLCodec(StringPool.UTF8);
\r
322 String encodedFileName =
\r
323 StringUtil.replace(codec.encode(fileName), "+", "%20");
\r
325 contentDisposition =
\r
326 "attachment; filename*=UTF-8''" + encodedFileName;
\r
329 catch (Exception e) {
\r
330 if (_log.isWarnEnabled()) {
\r
335 String extension = GetterUtil.getString(
\r
336 FileUtil.getExtension(fileName)).toLowerCase();
\r
338 if (forceContentDisposition.equals("")){
\r
339 String[] mimeTypesContentDispositionInline = null;
\r
342 mimeTypesContentDispositionInline = PropsUtil.getArray(
\r
343 "mime.types.content.disposition.inline");
\r
345 catch (Exception e) {
\r
346 mimeTypesContentDispositionInline = new String[0];
\r
349 if (ArrayUtil.contains(
\r
350 mimeTypesContentDispositionInline, extension)) {
\r
352 contentDisposition = StringUtil.replace(
\r
353 contentDisposition, "attachment; ", "inline; ");
\r
358 contentDisposition = StringUtil.replace(
\r
359 contentDisposition, "attachment; ", forceContentDisposition+"; ");
\r
361 response.setHeader(
\r
362 HttpHeaders.CONTENT_DISPOSITION, contentDisposition);
\r
367 private static final String _CLIENT_ABORT_EXCEPTION =
\r
368 "org.apache.catalina.connector.ClientAbortException";
\r
370 private static Log _log = LogFactoryUtil.getLog(ServletResponseServiceImpl.class);
\r