001: package org.apache.turbine.services.xmlrpc.util;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.net.URL;
023: import java.util.Vector;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.turbine.services.xmlrpc.TurbineXmlRpc;
028: import org.apache.turbine.util.TurbineException;
029:
030: /**
031: * Test class for FileHandler.
032: *
033: * @deprecated This is not scope of the Service itself but of an
034: * application which uses the service. This class shouldn't
035: * be part of Turbine but of an addon application.
036: * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
037: * @version $Id: FileTransfer.java 534527 2007-05-02 16:10:59Z tv $
038: */
039: public class FileTransfer {
040: /** Logging */
041: private static Log log = LogFactory.getLog(FileTransfer.class);
042:
043: /**
044: * Method to allow a client to send a file to a server.
045: *
046: * @param serverURL
047: * @param sourceLocationProperty
048: * @param sourceFileName
049: * @param destinationLocationProperty
050: * @param destinationFileName
051: */
052: public static void send(String serverURL,
053: String sourceLocationProperty, String sourceFileName,
054: String destinationLocationProperty,
055: String destinationFileName) throws TurbineException {
056: try {
057: Vector params = new Vector();
058:
059: /*
060: * fileContents
061: */
062: params.add(FileHandler.readFileContents(
063: sourceLocationProperty, sourceFileName));
064:
065: /*
066: * property in TR.props which refers to the directory
067: * where the fileContents should land.
068: */
069: params.add(destinationLocationProperty);
070:
071: /*
072: * name to give the file contents.
073: */
074: params.add(destinationFileName);
075:
076: Boolean b = (Boolean) TurbineXmlRpc.executeRpc(new URL(
077: serverURL), "file.send", params);
078:
079: } catch (Exception e) {
080: log.error("Error sending file to server:", e);
081: throw new TurbineException(e);
082: }
083: }
084:
085: /**
086: * Method to allow a client to send a file to a server
087: * that requires a user name and password.
088: *
089: * @param serverURL
090: * @param username
091: * @param password
092: * @param sourceLocationProperty
093: * @param sourceFileName
094: * @param destinationLocationProperty
095: * @param destinationFileName
096: * @throws TurbineException
097: */
098: public static void send(String serverURL, String username,
099: String password, String sourceLocationProperty,
100: String sourceFileName, String destinationLocationProperty,
101: String destinationFileName) throws TurbineException {
102: try {
103: Vector params = new Vector();
104:
105: /*
106: * fileContents
107: */
108: params.add(FileHandler.readFileContents(
109: sourceLocationProperty, sourceFileName));
110:
111: /*
112: * property in TR.props which refers to the directory
113: * where the fileContents should land.
114: */
115: params.add(destinationLocationProperty);
116:
117: /*
118: * name to give the file contents.
119: */
120: params.add(destinationFileName);
121:
122: Boolean b = (Boolean) TurbineXmlRpc
123: .executeAuthenticatedRpc(new URL(serverURL),
124: username, password, "file.send", params);
125:
126: } catch (Exception e) {
127: log.error("Error sending file to server:", e);
128: throw new TurbineException(e);
129: }
130: }
131:
132: /**
133: * Method to allow a client to get a file to a server.
134: *
135: * @param serverURL
136: * @param sourceLocationProperty
137: * @param sourceFileName
138: * @param destinationLocationProperty
139: * @param destinationFileName
140: * @throws TurbineException
141: */
142: public static void get(String serverURL,
143: String sourceLocationProperty, String sourceFileName,
144: String destinationLocationProperty,
145: String destinationFileName) throws TurbineException {
146:
147: try {
148: Vector params = new Vector();
149:
150: /*
151: * property in TR.props which refers to the directory
152: * where the fileContents should land.
153: */
154: params.add(sourceLocationProperty);
155:
156: /*
157: * name to give the file contents.
158: */
159: params.add(sourceFileName);
160:
161: String fileContents = (String) TurbineXmlRpc.executeRpc(
162: new URL(serverURL), "file.get", params);
163:
164: /*
165: * Now we have the file contents, we can write
166: * them out to disk.
167: */
168: FileHandler.writeFileContents(fileContents,
169: destinationLocationProperty, destinationFileName);
170: } catch (Exception e) {
171: log.error("Error getting file from server:", e);
172: throw new TurbineException(e);
173: }
174: }
175:
176: /**
177: * Method to allow a client to get a file from a server
178: * that requires a user name and password.
179: *
180: * @param serverURL
181: * @param username
182: * @param password
183: * @param sourceLocationProperty
184: * @param sourceFileName
185: * @param destinationLocationProperty
186: * @param destinationFileName
187: */
188: public static void get(String serverURL, String username,
189: String password, String sourceLocationProperty,
190: String sourceFileName, String destinationLocationProperty,
191: String destinationFileName) throws TurbineException {
192:
193: try {
194: Vector params = new Vector();
195:
196: /*
197: * property in TR.props which refers to the directory
198: * where the fileContents should land.
199: */
200: params.add(sourceLocationProperty);
201:
202: /*
203: * name to give the file contents.
204: */
205: params.add(sourceFileName);
206:
207: String fileContents = (String) TurbineXmlRpc
208: .executeAuthenticatedRpc(new URL(serverURL),
209: username, password, "file.get", params);
210:
211: /*
212: * Now we have the file contents, we can write
213: * them out to disk.
214: */
215: FileHandler.writeFileContents(fileContents,
216: destinationLocationProperty, destinationFileName);
217: } catch (Exception e) {
218: log.error("Error getting file from server:", e);
219: throw new TurbineException(e);
220: }
221: }
222:
223: /**
224: * Method to allow a client to remove a file from
225: * the server
226: *
227: * @param serverURL
228: * @param sourceLocationProperty
229: * @param sourceFileName
230: */
231: public static void remove(String serverURL,
232: String sourceLocationProperty, String sourceFileName)
233: throws TurbineException {
234: try {
235: Vector params = new Vector();
236:
237: /*
238: * property in TR.props which refers to the directory
239: * where the fileContents should land.
240: */
241: params.add(sourceLocationProperty);
242:
243: /*
244: * name to give the file contents.
245: */
246: params.add(sourceFileName);
247:
248: TurbineXmlRpc.executeRpc(new URL(serverURL), "file.remove",
249: params);
250: } catch (Exception e) {
251: log.error("Error removing file from server:", e);
252: throw new TurbineException(e);
253: }
254: }
255:
256: /**
257: * Method to allow a client to remove a file from
258: * a server that requires a user name and password.
259: *
260: * @param serverURL
261: * @param username
262: * @param password
263: * @param sourceLocationProperty
264: * @param sourceFileName
265: */
266: public static void remove(String serverURL, String username,
267: String password, String sourceLocationProperty,
268: String sourceFileName) throws TurbineException {
269: try {
270: Vector params = new Vector();
271:
272: /*
273: * property in TR.props which refers to the directory
274: * where the fileContents should land.
275: */
276: params.add(sourceLocationProperty);
277:
278: /*
279: * name to give the file contents.
280: */
281: params.add(sourceFileName);
282:
283: TurbineXmlRpc.executeAuthenticatedRpc(new URL(serverURL),
284: username, password, "file.remove", params);
285: } catch (Exception e) {
286: log.error("Error removing file from server:", e);
287: throw new TurbineException(e);
288: }
289: }
290: }
|