1
22
23 package com.liferay.util.servlet;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.servlet.HttpHeaders;
28 import com.liferay.portal.kernel.util.ArrayUtil;
29 import com.liferay.portal.kernel.util.FileUtil;
30 import com.liferay.portal.kernel.util.GetterUtil;
31 import com.liferay.portal.kernel.util.PropsUtil;
32 import com.liferay.portal.kernel.util.StringPool;
33 import com.liferay.portal.kernel.util.StringUtil;
34 import com.liferay.portal.kernel.util.Validator;
35
36 import java.io.BufferedOutputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.OutputStream;
40
41 import javax.portlet.ResourceResponse;
42
43 import org.apache.commons.codec.net.URLCodec;
44 import org.apache.commons.lang.CharUtils;
45
46
52 public class PortletResponseUtil {
53
54 public static void sendFile(
55 ResourceResponse resourceResponse, String fileName, byte[] bytes)
56 throws IOException {
57
58 sendFile(resourceResponse, fileName, bytes, null);
59 }
60
61 public static void sendFile(
62 ResourceResponse resourceResponse, String fileName, byte[] bytes,
63 String contentType)
64 throws IOException {
65
66 setHeaders(resourceResponse, fileName, contentType);
67
68 write(resourceResponse, bytes);
69 }
70
71 public static void sendFile(
72 ResourceResponse resourceResponse, String fileName, InputStream is)
73 throws IOException {
74
75 sendFile(resourceResponse, fileName, is, null);
76 }
77
78 public static void sendFile(
79 ResourceResponse resourceResponse, String fileName, InputStream is,
80 String contentType)
81 throws IOException {
82
83 sendFile(resourceResponse, fileName, is, 0, contentType);
84 }
85
86 public static void sendFile(
87 ResourceResponse resourceResponse, String fileName, InputStream is,
88 int contentLength, String contentType)
89 throws IOException {
90
91 setHeaders(resourceResponse, fileName, contentType);
92
93 write(resourceResponse, is, contentLength);
94 }
95
96 public static void write(ResourceResponse resourceResponse, String s)
97 throws IOException {
98
99 write(resourceResponse, s.getBytes(StringPool.UTF8));
100 }
101
102 public static void write(ResourceResponse resourceResponse, byte[] bytes)
103 throws IOException {
104
105 write(resourceResponse, bytes, 0);
106 }
107
108 public static void write(
109 ResourceResponse resourceResponse, byte[] bytes, int contentLength)
110 throws IOException {
111
112 OutputStream os = null;
113
114 try {
115
116
118 if (!resourceResponse.isCommitted()) {
119
120
122 if (contentLength == 0) {
123 contentLength = bytes.length;
124 }
125
126 resourceResponse.setContentLength(contentLength);
127
128 os = new BufferedOutputStream(
129 resourceResponse.getPortletOutputStream());
130
131 os.write(bytes, 0, contentLength);
132 }
133 }
134 finally {
135 ServletResponseUtil.cleanUp(os);
136 }
137 }
138
139 public static void write(ResourceResponse resourceResponse, InputStream is)
140 throws IOException {
141
142 write(resourceResponse, is, 0);
143 }
144
145 public static void write(
146 ResourceResponse resourceResponse, InputStream is,
147 int contentLength)
148 throws IOException {
149
150 OutputStream os = null;
151
152 try {
153 if (!resourceResponse.isCommitted()) {
154 if (contentLength > 0) {
155 resourceResponse.setContentLength(contentLength);
156 }
157
158 os = new BufferedOutputStream(
159 resourceResponse.getPortletOutputStream());
160
161 int c = is.read();
162
163 while (c != -1) {
164 os.write(c);
165
166 c = is.read();
167 }
168 }
169 }
170 finally {
171 ServletResponseUtil.cleanUp(os, is);
172 }
173 }
174
175 protected static void setHeaders(
176 ResourceResponse resourceResponse, String fileName,
177 String contentType) {
178
179 if (_log.isDebugEnabled()) {
180 _log.debug("Sending file of type " + contentType);
181 }
182
183
185 if (Validator.isNotNull(contentType)) {
186 resourceResponse.setContentType(contentType);
187 }
188
189 resourceResponse.setProperty(
190 HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_PUBLIC_VALUE);
191 resourceResponse.setProperty(
192 HttpHeaders.PRAGMA, HttpHeaders.PRAGMA_PUBLIC_VALUE);
193
194 if (Validator.isNotNull(fileName)) {
195 String contentDisposition =
196 "attachment; filename=\"" + fileName + "\"";
197
198
201 boolean ascii = true;
202
203 for (int i = 0; i < fileName.length(); i++) {
204 if (!CharUtils.isAscii(fileName.charAt(i))) {
205 ascii = false;
206
207 break;
208 }
209 }
210
211 try {
212 if (!ascii) {
213 URLCodec codec = new URLCodec(StringPool.UTF8);
214
215 String encodedFileName =
216 StringUtil.replace(codec.encode(fileName), "+", "%20");
217
218 contentDisposition =
219 "attachment; filename*=UTF-8''" + encodedFileName;
220 }
221 }
222 catch (Exception e) {
223 if (_log.isWarnEnabled()) {
224 _log.warn(e);
225 }
226 }
227
228 String extension = GetterUtil.getString(
229 FileUtil.getExtension(fileName)).toLowerCase();
230
231 String[] mimeTypesContentDispositionInline = null;
232
233 try {
234 mimeTypesContentDispositionInline = PropsUtil.getArray(
235 "mime.types.content.disposition.inline");
236 }
237 catch (Exception e) {
238 mimeTypesContentDispositionInline = new String[0];
239 }
240
241 if (ArrayUtil.contains(
242 mimeTypesContentDispositionInline, extension)) {
243
244 contentDisposition = StringUtil.replace(
245 contentDisposition, "attachment; ", "inline; ");
246 }
247
248 resourceResponse.setProperty(
249 HttpHeaders.CONTENT_DISPOSITION, contentDisposition);
250 }
251 }
252
253 private static Log _log = LogFactoryUtil.getLog(PortletResponseUtil.class);
254
255 }