001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.common;
019:
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.util.ArrayList;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Map;
029:
030: import org.ofbiz.base.util.Debug;
031: import org.ofbiz.service.DispatchContext;
032: import org.ofbiz.service.ServiceUtil;
033:
034: import org.apache.commons.net.ftp.FTP;
035: import org.apache.commons.net.ftp.FTPClient;
036: import org.apache.commons.net.ftp.FTPReply;
037:
038: /**
039: * FTP Services
040: *
041: */
042: public class FtpServices {
043:
044: public final static String module = FtpServices.class.getName();
045:
046: public static Map putFile(DispatchContext dctx, Map context) {
047: Debug.logInfo("[putFile] starting...", module);
048:
049: InputStream localFile = null;
050: try {
051: localFile = new FileInputStream((String) context
052: .get("localFilename"));
053: } catch (IOException ioe) {
054: Debug.logError(ioe, "[putFile] Problem opening local file",
055: module);
056: return ServiceUtil
057: .returnError("ERROR: Could not open local file");
058: }
059:
060: List errorList = new ArrayList();
061:
062: FTPClient ftp = new FTPClient();
063: try {
064: Debug.logInfo("[putFile] connecting to: "
065: + (String) context.get("hostname"), module);
066: ftp.connect((String) context.get("hostname"));
067: if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
068: Debug.logInfo("[putFile] Server refused connection",
069: module);
070: errorList.add("connection refused");
071: } else {
072: String username = (String) context.get("username");
073: String password = (String) context.get("password");
074: Debug.logInfo("[putFile] logging in: username="
075: + username + ", password=" + password, module);
076: if (!ftp.login(username, password)) {
077: Debug.logInfo("[putFile] login failed", module);
078: errorList.add("Login failed (" + username + ", "
079: + password + ")");
080: } else {
081: Boolean binaryTransfer = (Boolean) context
082: .get("binaryTransfer");
083: boolean binary = (binaryTransfer == null) ? false
084: : binaryTransfer.booleanValue();
085: if (binary) {
086: ftp.setFileType(FTP.BINARY_FILE_TYPE);
087: }
088:
089: Boolean passiveMode = (Boolean) context
090: .get("passiveMode");
091: boolean passive = (passiveMode == null) ? true
092: : passiveMode.booleanValue();
093: if (passive) {
094: ftp.enterLocalPassiveMode();
095: }
096:
097: Debug.logInfo(
098: "[putFile] storing local file remotely as: "
099: + context.get("remoteFilename"),
100: module);
101: if (!ftp.storeFile((String) context
102: .get("remoteFilename"), localFile)) {
103: Debug.logInfo(
104: "[putFile] store was unsuccessful",
105: module);
106: errorList.add("File not sent succesfully: "
107: + ftp.getReplyString());
108: } else {
109: Debug.logInfo("[putFile] store was successful",
110: module);
111: List siteCommands = (List) context
112: .get("siteCommands");
113: if (siteCommands != null) {
114: Iterator ci = siteCommands.iterator();
115: while (ci.hasNext()) {
116: String command = (String) ci.next();
117: Debug.logInfo(
118: "[putFile] sending SITE command: "
119: + command, module);
120: if (!ftp.sendSiteCommand(command)) {
121: errorList.add("SITE command ("
122: + command + ") failed: "
123: + ftp.getReplyString());
124: }
125: }
126: }
127: }
128: }
129: ftp.logout();
130: }
131: } catch (IOException ioe) {
132: Debug.log(ioe, "[putFile] caught exception: "
133: + ioe.getMessage(), module);
134: errorList.add("Problem with FTP transfer: "
135: + ioe.getMessage());
136: } finally {
137: if (ftp.isConnected()) {
138: try {
139: ftp.disconnect();
140: } catch (IOException dce) {
141: Debug.logWarning(dce,
142: "[putFile] Problem with FTP disconnect",
143: module);
144: }
145: }
146: }
147:
148: try {
149: localFile.close();
150: } catch (IOException ce) {
151: Debug.logWarning(ce,
152: "[putFile] Problem closing local file", module);
153: }
154:
155: if (errorList.size() > 0) {
156: Debug.logError("[putFile] The following error(s) ("
157: + errorList.size() + ") occurred: " + errorList,
158: module);
159: return ServiceUtil.returnError(errorList);
160: }
161:
162: Debug.logInfo("[putFile] finished successfully", module);
163:
164: return ServiceUtil.returnSuccess();
165: }
166:
167: public static Map getFile(DispatchContext dctx, Map context) {
168:
169: String localFilename = (String) context.get("localFilename");
170:
171: OutputStream localFile = null;
172: try {
173: localFile = new FileOutputStream(localFilename);
174: } catch (IOException ioe) {
175: Debug.logError(ioe, "[getFile] Problem opening local file",
176: module);
177: return ServiceUtil
178: .returnError("ERROR: Could not open local file");
179: }
180:
181: List errorList = new ArrayList();
182:
183: FTPClient ftp = new FTPClient();
184: try {
185: ftp.connect((String) context.get("hostname"));
186: if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
187: errorList.add("Server refused connection");
188: } else {
189: String username = (String) context.get("username");
190: String password = (String) context.get("password");
191:
192: if (!ftp.login(username, password)) {
193: errorList.add("Login failed (" + username + ", "
194: + password + ")");
195: } else {
196: Boolean binaryTransfer = (Boolean) context
197: .get("binaryTransfer");
198: boolean binary = (binaryTransfer == null) ? false
199: : binaryTransfer.booleanValue();
200: if (binary) {
201: ftp.setFileType(FTP.BINARY_FILE_TYPE);
202: }
203:
204: Boolean passiveMode = (Boolean) context
205: .get("passiveMode");
206: boolean passive = (passiveMode == null) ? false
207: : passiveMode.booleanValue();
208: if (passive) {
209: ftp.enterLocalPassiveMode();
210: }
211:
212: if (!ftp.retrieveFile((String) context
213: .get("remoteFilename"), localFile)) {
214: errorList.add("File not received succesfully: "
215: + ftp.getReplyString());
216: }
217: }
218: ftp.logout();
219: }
220: } catch (IOException ioe) {
221: errorList.add("Problem with FTP transfer: "
222: + ioe.getMessage());
223: } finally {
224: if (ftp.isConnected()) {
225: try {
226: ftp.disconnect();
227: } catch (IOException dce) {
228: Debug.logWarning(dce,
229: "[getFile] Problem with FTP disconnect",
230: module);
231: }
232: }
233: }
234:
235: try {
236: localFile.close();
237: } catch (IOException ce) {
238: Debug.logWarning(ce,
239: "[getFile] Problem closing local file", module);
240: }
241:
242: if (errorList.size() > 0) {
243: Debug.logError("[getFile] The following error(s) ("
244: + errorList.size() + ") occurred: " + errorList,
245: module);
246: return ServiceUtil.returnError(errorList);
247: }
248:
249: return ServiceUtil.returnSuccess();
250: }
251: }
|