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