1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.portals.gems.file;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24
25 import javax.portlet.PortletConfig;
26 import javax.portlet.PortletException;
27 import javax.portlet.PortletPreferences;
28 import javax.portlet.PortletRequest;
29 import javax.portlet.RenderRequest;
30 import javax.portlet.RenderResponse;
31 import javax.servlet.http.HttpServletRequest;
32
33 import org.apache.jetspeed.PortalReservedParameters;
34 import org.apache.jetspeed.request.RequestContext;
35 import org.apache.portals.bridges.common.GenericServletPortlet;
36
37 /***
38 * FilePortlet
39 *
40 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
41 * @version $Id: FilePortlet.java 550717 2007-06-26 07:06:44Z taylor $
42 */
43 public class FilePortlet extends GenericServletPortlet
44 {
45
46 /***
47 * Name of portlet preference for source file url
48 */
49 public static final String PARAM_SOURCE_FILE = "file";
50
51 /***
52 * Name of portlet preference for source file url
53 */
54 public static final String PARAM_SOURCE_BASE_PATH = "basepath";
55
56 /***
57 * Name of portlet preference for source file url
58 */
59 public static final String PARAM_SOURCE_FILE_PATH = "filepath";
60
61 /***
62 * Is the file stored in the webapp or outside of the webapp? valid values
63 * "webapp" and "filesystem", defaults to webapp
64 */
65 public static final String PARAM_LOCATION = "location";
66
67 private boolean webappLocation = true;
68
69 /***
70 * Default URL for the source file
71 */
72 private String defaultSourceFile = null;
73
74 /***
75 * Default base URL for the source file
76 */
77 private String defaultSourceBasePath = null;
78
79 public void init(PortletConfig config) throws PortletException
80 {
81 super.init(config);
82 this.defaultSourceFile = config.getInitParameter(PARAM_SOURCE_FILE);
83 this.defaultSourceBasePath = config
84 .getInitParameter(PARAM_SOURCE_BASE_PATH);
85 String location = config.getInitParameter(PARAM_LOCATION);
86 if (location != null && location.equals("filesystem"))
87 webappLocation = false;
88 else
89 webappLocation = true;
90 }
91
92 private RequestContext getRequestContext(PortletRequest request)
93 {
94 return (RequestContext) request
95 .getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
96
97 }
98
99 private HttpServletRequest getHttpServletRequest(PortletRequest pRequest)
100 {
101 return getRequestContext(pRequest).getRequest();
102
103 }
104
105 public void doView(RenderRequest request, RenderResponse response)
106 throws PortletException, IOException
107 {
108
109 HttpServletRequest req = getHttpServletRequest(request);
110 String fileName = (String) req.getSession().getAttribute("file");
111 if (fileName != null && !fileName.equals(""))
112 {
113 InputStream is = null;
114 try
115 {
116 fileName = getFilePath(fileName);
117 is = new FileInputStream(fileName);
118 if (is == null)
119 {
120 byte[] bytes = ("File " + fileName + " not found.")
121 .getBytes();
122 response.getPortletOutputStream().write(bytes);
123 return;
124 }
125 setContentType(fileName, response);
126 drain(is, response.getPortletOutputStream());
127 response.getPortletOutputStream().flush();
128 is.close();
129 req.getSession().removeAttribute("file");
130 } catch (Exception e)
131 {
132 if (is != null) is.close();
133 byte[] bytes = ("File " + fileName + " not found.").getBytes();
134 req.getSession().removeAttribute("file");
135 response.setContentType("text/html");
136 response.getPortletOutputStream().write(bytes);
137 return;
138 }
139 } else
140 {
141 String path = (String) request
142 .getAttribute(PortalReservedParameters.PATH_ATTRIBUTE);
143 if (null == path)
144 {
145 PortletPreferences prefs = request.getPreferences();
146 path = prefs
147 .getValue(PARAM_SOURCE_FILE, this.defaultSourceFile);
148 }
149
150 if (null == path && this.defaultSourceBasePath != null)
151 {
152 String filepath = request.getParameter(PARAM_SOURCE_FILE_PATH);
153 if (filepath == null)
154 {
155 filepath = (String) request
156 .getAttribute(PARAM_SOURCE_FILE_PATH);
157 }
158
159 if (filepath != null)
160 {
161 path = ((this.defaultSourceBasePath.length() > 0) ? (this.defaultSourceBasePath + "/")
162 : "")
163 + filepath;
164 }
165 }
166
167 if (null == path)
168 {
169 response.setContentType("text/html");
170 response.getWriter().println("Could not find source document.");
171 } else
172 {
173
174 File temp = new File(path);
175 if (webappLocation)
176 {
177 path = "/WEB-INF/" + temp.getPath();
178 }
179 setContentType(path, response);
180 renderFile(response, path);
181 }
182 }
183 }
184
185 protected void setContentType(String path, RenderResponse response)
186 {
187
188 if (path.endsWith(".html"))
189 {
190 response.setContentType("text/html");
191 } else if (path.endsWith(".pdf"))
192 {
193 response.setContentType("application/pdf");
194 } else if (path.endsWith(".zip"))
195 {
196 response.setContentType("application/zip");
197 } else if (path.endsWith(".csv"))
198 {
199 response.setContentType("text/csv");
200 } else if (path.endsWith(".xml") || path.endsWith(".xsl"))
201 {
202 response.setContentType("text/xml");
203 } else if (path.endsWith(".psml") || path.endsWith(".link"))
204 {
205 response.setContentType("text/xml");
206 } else
207 {
208 response.setContentType("text/html");
209 }
210 }
211
212 protected void renderFile(RenderResponse response, String fileName)
213 throws PortletException, IOException
214 {
215 InputStream is = null;
216
217 if (this.webappLocation)
218 {
219 is = this.getPortletContext().getResourceAsStream(fileName);
220 } else
221 {
222 is = new FileInputStream(fileName);
223 }
224 if (is == null)
225 {
226 byte[] bytes = ("File " + fileName + " not found.").getBytes();
227 response.getPortletOutputStream().write(bytes);
228 return;
229 }
230 drain(is, response.getPortletOutputStream());
231 response.getPortletOutputStream().flush();
232 is.close();
233 }
234
235 static final int BLOCK_SIZE = 4096;
236
237 public static void drain(InputStream r, OutputStream w) throws IOException
238 {
239 byte[] bytes = new byte[BLOCK_SIZE];
240 try
241 {
242 int length = r.read(bytes);
243 while (length != -1)
244 {
245 if (length != 0)
246 {
247 w.write(bytes, 0, length);
248 }
249 length = r.read(bytes);
250 }
251 } finally
252 {
253 bytes = null;
254 }
255 }
256
257 private String getFilePath(String path)
258 {
259 String pageRoot = System.getProperty("java.io.tmpdir");
260 String sep = System.getProperty("file.separator");
261 if (sep == null || sep.equals("")) sep = "/";
262
263 String ar[] = path.split("_");
264 if (ar.length == 1) return pageRoot + sep + path;
265 return pageRoot + sep + ar[0] + sep + ar[1];
266 }
267 }