--- /dev/null
+/**\r
+ * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy\r
+ * of this software and associated documentation files (the "Software"), to deal\r
+ * in the Software without restriction, including without limitation the rights\r
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+ * copies of the Software, and to permit persons to whom the Software is\r
+ * furnished to do so, subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in\r
+ * all copies or substantial portions of the Software.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
+ * SOFTWARE.\r
+ */\r
+\r
+package com.pentila.entSavoie.servlet.impl;\r
+\r
+import java.io.BufferedOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.OutputStream;\r
+import java.net.SocketException;\r
+\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import org.apache.commons.codec.net.URLCodec;\r
+import org.apache.commons.lang.CharUtils;\r
+\r
+import com.liferay.portal.kernel.log.Log;\r
+import com.liferay.portal.kernel.log.LogFactoryUtil;\r
+import com.liferay.portal.kernel.servlet.HttpHeaders;\r
+import com.liferay.portal.kernel.util.ArrayUtil;\r
+import com.liferay.portal.kernel.util.FileUtil;\r
+import com.liferay.portal.kernel.util.GetterUtil;\r
+import com.liferay.portal.kernel.util.PropsUtil;\r
+import com.liferay.portal.kernel.util.StringPool;\r
+import com.liferay.portal.kernel.util.StringUtil;\r
+import com.liferay.portal.kernel.util.Validator;\r
+import com.pentila.entSavoie.servlet.ServletResponseService;\r
+\r
+/**\r
+ * <a href="ServletResponseUtil.java.html"><b><i>View Source</i></b></a>\r
+ *\r
+ * @author Brian Wing Shun Chan\r
+ *\r
+ */\r
+public class ServletResponseServiceImpl implements ServletResponseService {\r
+\r
+ public void cleanUp(InputStream is) {\r
+ try {\r
+ if (is != null) {\r
+ is.close();\r
+ }\r
+ }\r
+ catch (Exception e) {\r
+ if (e instanceof SocketException ||\r
+ e.getClass().getName().equals(_CLIENT_ABORT_EXCEPTION)) {\r
+\r
+ if (_log.isWarnEnabled()) {\r
+ _log.warn(e.getClass().getSimpleName());\r
+ }\r
+ }\r
+ else {\r
+ _log.error(e);\r
+ }\r
+ }\r
+ }\r
+\r
+ public void cleanUp(OutputStream os) {\r
+ try {\r
+ if (os != null) {\r
+ os.flush();\r
+ }\r
+ }\r
+ catch (Exception e) {\r
+ if (e instanceof SocketException ||\r
+ e.getClass().getName().equals(_CLIENT_ABORT_EXCEPTION)) {\r
+\r
+ if (_log.isWarnEnabled()) {\r
+ _log.warn(e.getClass().getSimpleName());\r
+ }\r
+ }\r
+ else {\r
+ _log.error(e);\r
+ }\r
+ }\r
+\r
+ try {\r
+ if (os != null) {\r
+ os.close();\r
+ }\r
+ }\r
+ catch (Exception e) {\r
+ if (e instanceof SocketException ||\r
+ e.getClass().getName().equals(_CLIENT_ABORT_EXCEPTION)) {\r
+\r
+ if (_log.isWarnEnabled()) {\r
+ _log.warn(e.getClass().getSimpleName());\r
+ }\r
+ }\r
+ else {\r
+ _log.error(e);\r
+ }\r
+ }\r
+ }\r
+\r
+ public void cleanUp(OutputStream os, InputStream is) {\r
+ cleanUp(os);\r
+ cleanUp(is);\r
+ }\r
+\r
+ public void sendFile(\r
+ HttpServletResponse response, String fileName, byte[] bytes)\r
+ throws IOException {\r
+\r
+ sendFile(response, fileName, bytes, null);\r
+ }\r
+\r
+ public void sendFile(\r
+ HttpServletResponse response, String fileName, byte[] bytes,\r
+ String contentType)\r
+ throws IOException {\r
+\r
+ setHeaders(response, fileName, contentType, "");\r
+\r
+ write(response, bytes);\r
+ }\r
+ \r
+ public void sendFile(\r
+ HttpServletResponse response, String fileName, byte[] bytes,\r
+ String contentType, String forceContentDisposition)\r
+ throws IOException {\r
+\r
+ setHeaders(response, fileName, contentType, forceContentDisposition);\r
+\r
+ write(response, bytes);\r
+ }\r
+\r
+ public void sendFile(\r
+ HttpServletResponse response, String fileName, InputStream is)\r
+ throws IOException {\r
+\r
+ sendFile(response, fileName, is, null);\r
+ }\r
+\r
+ public void sendFile(\r
+ HttpServletResponse response, String fileName, InputStream is,\r
+ String contentType)\r
+ throws IOException {\r
+\r
+ sendFile(response, fileName, is, 0, contentType);\r
+ }\r
+\r
+ public void sendFile(\r
+ HttpServletResponse response, String fileName, InputStream is,\r
+ int contentLength, String contentType)\r
+ throws IOException {\r
+\r
+ setHeaders(response, fileName, contentType, "");\r
+\r
+ write(response, is, contentLength);\r
+ }\r
+ \r
+ public void sendFile(\r
+ HttpServletResponse response, String fileName, InputStream is,\r
+ int contentLength, String contentType, String forceContentDisposition)\r
+ throws IOException {\r
+\r
+ setHeaders(response, fileName, contentType, forceContentDisposition);\r
+\r
+ write(response, is, contentLength);\r
+ }\r
+\r
+ public void write(HttpServletResponse response, String s)\r
+ throws IOException {\r
+\r
+ write(response, s.getBytes(StringPool.UTF8));\r
+ }\r
+\r
+ public void write(HttpServletResponse response, byte[] bytes)\r
+ throws IOException {\r
+\r
+ write(response, bytes, 0);\r
+ }\r
+\r
+ public void write(\r
+ HttpServletResponse response, byte[] bytes, int contentLength)\r
+ throws IOException {\r
+\r
+ OutputStream os = null;\r
+\r
+ try {\r
+\r
+ // LEP-3122\r
+\r
+ if (!response.isCommitted()) {\r
+\r
+ // LEP-536\r
+\r
+ if (contentLength == 0) {\r
+ contentLength = bytes.length;\r
+ }\r
+\r
+ response.setContentLength(contentLength);\r
+\r
+ os = new BufferedOutputStream(response.getOutputStream());\r
+\r
+ os.write(bytes, 0, contentLength);\r
+ }\r
+ }\r
+ catch (IOException ioe) {\r
+ if (ioe instanceof SocketException ||\r
+ ioe.getClass().getName().equals(_CLIENT_ABORT_EXCEPTION)) {\r
+\r
+ if (_log.isWarnEnabled()) {\r
+ _log.warn(ioe.getClass().getSimpleName());\r
+ }\r
+ }\r
+ else {\r
+ _log.error(ioe);\r
+ }\r
+ }\r
+ finally {\r
+ cleanUp(os);\r
+ }\r
+ }\r
+\r
+ public void write(HttpServletResponse response, InputStream is)\r
+ throws IOException {\r
+\r
+ write(response, is, 0);\r
+ }\r
+\r
+ public void write(\r
+ HttpServletResponse response, InputStream is, int contentLength)\r
+ throws IOException {\r
+\r
+ OutputStream os = null;\r
+\r
+ try {\r
+ if (!response.isCommitted()) {\r
+ if (contentLength > 0) {\r
+ response.setContentLength(contentLength);\r
+ }\r
+\r
+ os = new BufferedOutputStream(response.getOutputStream());\r
+\r
+ int c = is.read();\r
+\r
+ while (c != -1) {\r
+ os.write(c);\r
+\r
+ c = is.read();\r
+ }\r
+ }\r
+ }\r
+ catch (Exception e) {\r
+ if (e instanceof SocketException ||\r
+ e.getClass().getName().equals(_CLIENT_ABORT_EXCEPTION)) {\r
+\r
+ if (_log.isWarnEnabled()) {\r
+ _log.warn(e.getClass().getSimpleName());\r
+ }\r
+ }\r
+ else {\r
+ _log.error(e);\r
+ }\r
+ }\r
+ finally {\r
+ cleanUp(os, is);\r
+ }\r
+ }\r
+\r
+ public void setHeaders(\r
+ HttpServletResponse response,\r
+ String fileName,\r
+ String contentType,\r
+ String forceContentDisposition) {\r
+\r
+ if (_log.isDebugEnabled()) {\r
+ _log.debug("Sending file of type " + contentType);\r
+ }\r
+\r
+ // LEP-2201\r
+\r
+ if (Validator.isNotNull(contentType)) {\r
+ response.setContentType(contentType);\r
+ }\r
+\r
+ response.setHeader(\r
+ HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_PUBLIC_VALUE);\r
+ response.setHeader(HttpHeaders.PRAGMA, HttpHeaders.PRAGMA_PUBLIC_VALUE);\r
+\r
+ if (Validator.isNotNull(fileName)) {\r
+ String contentDisposition =\r
+ "attachment; filename=\"" + fileName + "\"";\r
+\r
+ // If necessary for non-ASCII characters, encode based on RFC 2184.\r
+ // However, not all browsers support RFC 2184. See LEP-3127.\r
+\r
+ boolean ascii = true;\r
+\r
+ for (int i = 0; i < fileName.length(); i++) {\r
+ if (!CharUtils.isAscii(fileName.charAt(i))) {\r
+ ascii = false;\r
+\r
+ break;\r
+ }\r
+ }\r
+\r
+ try {\r
+ if (!ascii) {\r
+ URLCodec codec = new URLCodec(StringPool.UTF8);\r
+\r
+ String encodedFileName =\r
+ StringUtil.replace(codec.encode(fileName), "+", "%20");\r
+\r
+ contentDisposition =\r
+ "attachment; filename*=UTF-8''" + encodedFileName;\r
+ }\r
+ }\r
+ catch (Exception e) {\r
+ if (_log.isWarnEnabled()) {\r
+ _log.warn(e);\r
+ }\r
+ }\r
+\r
+ String extension = GetterUtil.getString(\r
+ FileUtil.getExtension(fileName)).toLowerCase();\r
+\r
+ if (forceContentDisposition.equals("")){\r
+ String[] mimeTypesContentDispositionInline = null;\r
+\r
+ try {\r
+ mimeTypesContentDispositionInline = PropsUtil.getArray(\r
+ "mime.types.content.disposition.inline");\r
+ }\r
+ catch (Exception e) {\r
+ mimeTypesContentDispositionInline = new String[0];\r
+ }\r
+\r
+ if (ArrayUtil.contains(\r
+ mimeTypesContentDispositionInline, extension)) {\r
+\r
+ contentDisposition = StringUtil.replace(\r
+ contentDisposition, "attachment; ", "inline; ");\r
+ }\r
+\r
+ }\r
+ else {\r
+ contentDisposition = StringUtil.replace(\r
+ contentDisposition, "attachment; ", forceContentDisposition+"; ");\r
+ }\r
+ response.setHeader(\r
+ HttpHeaders.CONTENT_DISPOSITION, contentDisposition);\r
+ \r
+ }\r
+ }\r
+\r
+ private static final String _CLIENT_ABORT_EXCEPTION =\r
+ "org.apache.catalina.connector.ClientAbortException";\r
+\r
+ private static Log _log = LogFactoryUtil.getLog(ServletResponseServiceImpl.class);\r
+\r
+}
\ No newline at end of file