1 package org.apache.turbine.services.xmlrpc.util;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.BufferedReader;
58 import java.io.File;
59 import java.io.FileInputStream;
60 import java.io.FileWriter;
61 import java.io.InputStreamReader;
62 import java.io.IOException;
63 import java.io.StringWriter;
64
65 import javax.mail.internet.MimeUtility;
66
67 import org.apache.turbine.util.Log;
68
69 import org.apache.turbine.services.resources.TurbineResources;
70 import org.apache.turbine.services.servlet.TurbineServlet;
71
72 /***
73 * A Handler for use with the XML-RPC service that will deal
74 * with clients sending file to the server (Turbine application)
75 * and clients getting files from the server (Turbine application).
76 *
77 * 1) In the first case where the client sends a file to the server,
78 * the client has encoded the file contents and passes those
79 * encoded file contents on to the server:
80 *
81 * Client --------> encoded file contents -------------> Server
82 *
83 * The server must then decode the file contents and write the
84 * decoded file contents to disk.
85 *
86 * 2) In the second case where the client gets a file from the
87 * the server, the server has encoded the file contents and
88 * passes those encoded file contents on to the client:
89 *
90 * Client <------- encoded file contents <------------- Server
91 *
92 * The client must then decode the file contents and write the
93 * decoded file contents to disk.
94 *
95 * @version $Id: FileHandler.java,v 1.4 2001/10/18 20:24:10 thorhauer Exp $
96 * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
97 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
98 */
99 public class FileHandler
100 {
101 /***
102 * Default Constructor
103 */
104 public FileHandler()
105 {
106 }
107
108 /***
109 * The client has indicated that it would like
110 * to send a file to the server and have it
111 * stored in a certain location on the server.
112 *
113 * So a client Turbine application might use the
114 * following bit of code to send a file to a server
115 * Turbine application:
116 *
117 * TurbineXmlRpc.executeRpc("file.send", params)
118 *
119 * Where:
120 *
121 * params.get(0) = contents of the file as a string.
122 * params.get(1) = the name the file should have when it lands.
123 * params.get(2) = property describing where the file should land.
124 *
125 * @param fileContents: The contents of the file to store. It
126 * is assumed that any xml content is properly encoded!
127 *
128 * @param fileName: Name to give the file created to store
129 * the contents.
130 *
131 * @param targetLocationProperty: storage location of this file
132 * is controlled by this property that is specified in
133 * the TR.props file or an included properties file.
134 */
135 public boolean send(String fileContents,
136 String targetLocationProperty,
137 String fileName)
138 {
139 /*
140 * Simply take the file contents that have been sent
141 * by the client and write them to disk in the
142 * specified location: targetLocationProperty specifies
143 * the directory in which to place the fileContents
144 * with the name fileName.
145 */
146 return writeFileContents(fileContents, targetLocationProperty,
147 fileName);
148 }
149
150 /***
151 * The client has indicated that it would like
152 * to get a file from the server.
153 *
154 * So a client Turbine application might use the
155 * following bit of code to get a file from a server
156 * Turbine application:
157 *
158 * TurbineXmlRpc.executeRpc("file.get", params)
159 *
160 * Where:
161 *
162 * params.get(0) = the name the file should have when it lands.
163 * params.get(1) = property describing where the file should land.
164 *
165 * @param fileName: Name to give the file created to store
166 * the contents.
167 *
168 * @param targetLocationProperty: storage location of this file
169 * is controlled by this property that is specified in
170 * the TR.props file or an included properties file.
171 *
172 * @return the file contents encoded with base64.
173 */
174 public String get(String targetLocationProperty,
175 String fileName)
176 {
177 /*
178 * Place the contents of the file with the name
179 * fileName in the directory specified by
180 * targetLocationProperty.
181 */
182 return readFileContents(targetLocationProperty, fileName);
183 }
184
185 /***
186 * Return the content of file encoded for transfer
187 *
188 * @param file: path to file to encode.
189 * @return String encoded contents of the requested file.
190 */
191 public static String readFileContents(String targetLocationProperty,
192 String fileName)
193 {
194 File tmpF = new File(".");
195
196 String file = TurbineServlet.getRealPath(
197 TurbineResources.getString(targetLocationProperty) +
198 tmpF.separator + fileName);
199
200 StringWriter sw = null;
201 BufferedReader reader = null;
202 try
203 {
204 /*
205 * This little routine was borrowed from the
206 * velocity ContentResource class.
207 */
208
209 sw = new StringWriter();
210
211 reader = new BufferedReader(
212 new InputStreamReader(
213 new FileInputStream(file)));
214
215 char buf[] = new char[1024];
216 int len = 0;
217
218 while ((len = reader.read(buf, 0, 1024)) != -1)
219 {
220 sw.write( buf, 0, len );
221 }
222
223 return MimeUtility.encodeText(sw.toString(), "UTF-8", "B");
224 }
225 catch (IOException ioe)
226 {
227 Log.error("[FileHandler] Unable to encode the contents " +
228 "of the request file.", ioe);
229
230 return null;
231 }
232 finally
233 {
234 try
235 {
236 if (sw != null)
237 {
238 sw.close();
239 }
240 if (reader != null)
241 {
242 reader.close();
243 }
244 }
245 catch (Exception e)
246 {
247 }
248 }
249 }
250
251 public static boolean writeFileContents(String fileContents,
252 String targetLocationProperty,
253 String fileName)
254 {
255 /*
256 * The target location is always within the webapp to
257 * make the application fully portable. So use the TurbineServlet
258 * service to map the target location in the webapp space.
259 */
260 File targetLocation = new File(
261 TurbineServlet.getRealPath(
262 TurbineResources.getString(
263 targetLocationProperty)));
264
265 if (targetLocation.exists() == false)
266 {
267 /*
268 * If the target location doesn't exist then
269 * attempt to create the target location and any
270 * necessary parent directories as well.
271 */
272 if (targetLocation.mkdirs() == false)
273 {
274 Log.error("[FileHandler] Could not create target location: " +
275 targetLocation + ". Cannot transfer file from client.");
276
277 return false;
278 }
279 else
280 {
281 Log.info("[FileHandler] Creating target location:" +
282 targetLocation +
283 " in order to complete file transfer from client.");
284 }
285 }
286
287 FileWriter fileWriter = null;
288 try
289 {
290 /*
291 * Try to create the target file and write it out
292 * to the target location.
293 */
294 fileWriter = new FileWriter(
295 targetLocation + "/" + fileName);
296
297 /*
298 * It is assumed that the file has been encoded
299 * and therefore must be decoded before the
300 * contents of the file are stored to disk.
301 */
302 fileWriter.write(MimeUtility.decodeText(fileContents));
303
304 return true;
305 }
306 catch (IOException ioe)
307 {
308 Log.error("[FileHandler] Could not write the decoded file " +
309 "contents to disk for the following reason.", ioe);
310
311 return false;
312 }
313 finally
314 {
315 try
316 {
317 if (fileWriter != null)
318 {
319 fileWriter.close();
320 }
321 }
322 catch (Exception e)
323 {
324 }
325 }
326 }
327
328 /***
329 * Method to allow a client to remove a file from
330 * the server
331 *
332 * @param serverURL
333 * @param sourceLocationProperty
334 * @param sourceFileName
335 */
336 public static void remove(String sourceLocationProperty,
337 String sourceFileName)
338 {
339 /*
340 * The target location is always within the webapp to
341 * make the application fully portable. So use the TurbineServlet
342 * service to map the target location in the webapp space.
343 */
344 File sourceFile = new File(
345 TurbineServlet.getRealPath(
346 TurbineResources.getString(sourceLocationProperty) +
347 "/" + sourceFileName));
348
349 if (sourceFile.exists())
350 {
351 sourceFile.delete();
352 }
353 }
354 }
This page was automatically generated by Maven