View Javadoc

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