1 package org.apache.turbine.services.xmlrpc.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.net.URL;
20 import java.util.Vector;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.turbine.services.xmlrpc.TurbineXmlRpc;
25 import org.apache.turbine.util.TurbineException;
26
27 /***
28 * Test class for FileHandler.
29 *
30 * @deprecated This is not scope of the Service itself but of an
31 * application which uses the service. This class shouldn't
32 * be part of Turbine but of an addon application.
33 * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
34 * @version $Id: FileTransfer.java 264148 2005-08-29 14:21:04Z henning $
35 */
36 public class FileTransfer
37 {
38 /*** Logging */
39 private static Log log = LogFactory.getLog(FileTransfer.class);
40
41 /***
42 * Method to allow a client to send a file to a server.
43 *
44 * @param serverURL
45 * @param sourceLocationProperty
46 * @param sourceFileName
47 * @param destinationLocationProperty
48 * @param destinationFileName
49 */
50 public static void send(String serverURL,
51 String sourceLocationProperty,
52 String sourceFileName,
53 String destinationLocationProperty,
54 String destinationFileName)
55 throws TurbineException
56 {
57 try
58 {
59 Vector params = new Vector();
60
61
62
63
64 params.add(FileHandler.readFileContents(
65 sourceLocationProperty, sourceFileName));
66
67
68
69
70
71 params.add(destinationLocationProperty);
72
73
74
75
76 params.add(destinationFileName);
77
78 Boolean b = (Boolean) TurbineXmlRpc.executeRpc(
79 new URL(serverURL), "file.send", params);
80
81 }
82 catch (Exception e)
83 {
84 log.error("Error sending file to server:", e);
85 throw new TurbineException(e);
86 }
87 }
88
89 /***
90 * Method to allow a client to send a file to a server
91 * that requires a user name and password.
92 *
93 * @param serverURL
94 * @param username
95 * @param password
96 * @param sourceLocationProperty
97 * @param sourceFileName
98 * @param destinationLocationProperty
99 * @param destinationFileName
100 * @throws TurbineException
101 */
102 public static void send(String serverURL,
103 String username,
104 String password,
105 String sourceLocationProperty,
106 String sourceFileName,
107 String destinationLocationProperty,
108 String destinationFileName)
109 throws TurbineException
110 {
111 try
112 {
113 Vector params = new Vector();
114
115
116
117
118 params.add(FileHandler.readFileContents(
119 sourceLocationProperty, sourceFileName));
120
121
122
123
124
125 params.add(destinationLocationProperty);
126
127
128
129
130 params.add(destinationFileName);
131
132 Boolean b = (Boolean) TurbineXmlRpc.executeAuthenticatedRpc(
133 new URL(serverURL),
134 username,
135 password,
136 "file.send",
137 params);
138
139 }
140 catch (Exception e)
141 {
142 log.error("Error sending file to server:", e);
143 throw new TurbineException(e);
144 }
145 }
146
147 /***
148 * Method to allow a client to get a file to a server.
149 *
150 * @param serverURL
151 * @param sourceLocationProperty
152 * @param sourceFileName
153 * @param destinationLocationProperty
154 * @param destinationFileName
155 * @throws TurbineException
156 */
157 public static void get(String serverURL,
158 String sourceLocationProperty,
159 String sourceFileName,
160 String destinationLocationProperty,
161 String destinationFileName)
162 throws TurbineException
163 {
164
165 try
166 {
167 Vector params = new Vector();
168
169
170
171
172
173 params.add(sourceLocationProperty);
174
175
176
177
178 params.add(sourceFileName);
179
180 String fileContents = (String) TurbineXmlRpc.executeRpc(
181 new URL(serverURL), "file.get", params);
182
183
184
185
186
187 FileHandler.writeFileContents(fileContents,
188 destinationLocationProperty, destinationFileName);
189 }
190 catch (Exception e)
191 {
192 log.error("Error getting file from server:", e);
193 throw new TurbineException(e);
194 }
195 }
196
197 /***
198 * Method to allow a client to get a file from a server
199 * that requires a user name and password.
200 *
201 * @param serverURL
202 * @param username
203 * @param password
204 * @param sourceLocationProperty
205 * @param sourceFileName
206 * @param destinationLocationProperty
207 * @param destinationFileName
208 */
209 public static void get(String serverURL,
210 String username,
211 String password,
212 String sourceLocationProperty,
213 String sourceFileName,
214 String destinationLocationProperty,
215 String destinationFileName)
216 throws TurbineException
217 {
218
219 try
220 {
221 Vector params = new Vector();
222
223
224
225
226
227 params.add(sourceLocationProperty);
228
229
230
231
232 params.add(sourceFileName);
233
234 String fileContents = (String) TurbineXmlRpc.executeAuthenticatedRpc(
235 new URL(serverURL),
236 username,
237 password,
238 "file.get",
239 params);
240
241
242
243
244
245 FileHandler.writeFileContents(fileContents,
246 destinationLocationProperty, destinationFileName);
247 }
248 catch (Exception e)
249 {
250 log.error("Error getting file from server:", e);
251 throw new TurbineException(e);
252 }
253 }
254
255 /***
256 * Method to allow a client to remove a file from
257 * the server
258 *
259 * @param serverURL
260 * @param sourceLocationProperty
261 * @param sourceFileName
262 */
263 public static void remove(String serverURL,
264 String sourceLocationProperty,
265 String sourceFileName)
266 throws TurbineException
267 {
268 try
269 {
270 Vector params = new Vector();
271
272
273
274
275
276 params.add(sourceLocationProperty);
277
278
279
280
281 params.add(sourceFileName);
282
283 TurbineXmlRpc.executeRpc(new URL(serverURL), "file.remove", params);
284 }
285 catch (Exception e)
286 {
287 log.error("Error removing file from server:", e);
288 throw new TurbineException(e);
289 }
290 }
291
292 /***
293 * Method to allow a client to remove a file from
294 * a server that requires a user name and password.
295 *
296 * @param serverURL
297 * @param username
298 * @param password
299 * @param sourceLocationProperty
300 * @param sourceFileName
301 */
302 public static void remove(String serverURL,
303 String username,
304 String password,
305 String sourceLocationProperty,
306 String sourceFileName)
307 throws TurbineException
308 {
309 try
310 {
311 Vector params = new Vector();
312
313
314
315
316
317 params.add(sourceLocationProperty);
318
319
320
321
322 params.add(sourceFileName);
323
324 TurbineXmlRpc.executeAuthenticatedRpc(new URL(serverURL),
325 username,
326 password,
327 "file.remove",
328 params);
329 }
330 catch (Exception e)
331 {
332 log.error("Error removing file from server:", e);
333 throw new TurbineException(e);
334 }
335 }
336 }