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