Hi ,
I am going to improve the existing File connector to support ftp over http,
existing file connector supports Common-vfs and it it produces high rate
of failure and then advised to use Java Client (commons-net 3.4).
For that I am going to implement a method to support ftp over http and I
follow the doc[1].I want to clarify few details that in current version of
connector, I used a source as a parameter and possible values are
(file:///home/vives/Desktop/file/Append/me.txt or
ftp://username:password@localhost/www/me.txt). My question is that, first
do I need to check the path is whether ftp or local? and then if it is ftp,
do I want to call the newly created method which supports ftp over proxy?.
And for a new methods, these are the parameters, proxyHost ,proxyPort ,
proxyUser, proxyPassword ,ftpUsername ,ftpPassword , ftpServer ftpPort ,
and getting values for these param from user,is it correct?
I decided to use "Squid" for the Http proxy.I am following this[2] doc.
I have attached the sample code (*CustomVFSMediator.java*) and I go through
that code.
please give me suggestion that I have to consider when I implement this
methods.
[1] https://commons.apache.org/proper/commons-net/
[2] http://www.tecmint.com/configure-squid-server-in-linux/
package org.wso2.carbon.sample;
import org.apache.axiom.om.OMOutputFormat;
import org.apache.axis2.transport.MessageFormatter;
import org.apache.axis2.transport.base.BaseUtils;
import org.apache.axis2.util.MessageProcessorSelector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.*;
import org.apache.synapse.MessageContext;
import org.apache.synapse.commons.json.JsonUtil;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
public class CustomVFSMediator extends AbstractMediator {
private static final Log log = LogFactory.getLog(CustomVFSMediator.class);
private String proxyHost = "";
private int proxyPort = 80;
private String proxyUser = "";
private String proxyPassword = "";
private String ftpUsername = "";
private String ftpPassword = "";
private String ftpServer = "";
private String targetPath = "";
private int ftpPort = 21;
private long keepAliveTimeout = -1;
private int controlKeepAliveReplyTimeout = -1;
static boolean binaryTransfer = false,
error = false,
hidden = false,
localActive = false;
public CustomVFSMediator() {
}
public boolean mediate(MessageContext messageContext) {
String target_file = messageContext.getProperty("file_name").toString();
final FTPClient ftp;
if (proxyHost != null) {
ftp = new FTPHTTPClient(proxyHost, proxyPort, proxyUser, proxyPassword);
} else {
ftp = new FTPClient();
}
if (keepAliveTimeout >= 0) {
ftp.setControlKeepAliveTimeout(keepAliveTimeout);
}
if (controlKeepAliveReplyTimeout >= 0) {
ftp.setControlKeepAliveReplyTimeout(controlKeepAliveReplyTimeout);
}
ftp.setListHiddenFiles(hidden);
// suppress login details
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
try {
int reply;
if (ftpPort > 0) {
ftp.connect(ftpServer, ftpPort);
} else {
ftp.connect(ftpServer);
}
if(log.isDebugEnabled()){
log.debug(" Connected to " + ftpServer + " on " + (ftpPort > 0 ? ftpPort : ftp.getDefaultPort()));
}
// After connection attempt, should check the reply code to verify
// success.
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
log.error("FTP ftpServer refused connection.");
}
} catch (IOException e) {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException f) {
// do nothing
}
}
log.error("Could not connect to FTP ftpServer.", e);
}
InputStream inputStream = null;
__main:
try {
if (!ftp.login(ftpUsername, ftpPassword)) {
ftp.logout();
error = true;
break __main;
}
// log.info("Remote system is " + ftp.getSystemType());
if (binaryTransfer) {
ftp.setFileType(FTP.BINARY_FILE_TYPE);
} else {
// in theory this should not be necessary as servers should
// default to ASCII
// but they don't all do so - see NET-500
ftp.setFileType(FTP.ASCII_FILE_TYPE);
}
// Use passive mode as default because most of us are
// behind firewalls these days.
if (localActive) {
ftp.enterLocalActiveMode();
} else {
ftp.enterLocalPassiveMode();
}
org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
MessageFormatter messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext);
String contentType = messageFormatter.getContentType(msgContext, format, messageContext.getSoapAction());
if(contentType.contains("application/json")){
inputStream = JsonUtil.toJsonStream(messageContext.getEnvelope().getBody().getFirstElement());
}else{
inputStream = new ByteArrayInputStream(messageContext.getEnvelope().getBody().getFirstElement().toString().getBytes());
}
ftp.changeWorkingDirectory(targetPath);
ftp.storeFile(target_file, inputStream);
if(log.isDebugEnabled()){
log.debug("Successfully FTPed the File");
}
ftp.noop(); // check that control connection is working OK
ftp.logout();
inputStream.close();
} catch (FTPConnectionClosedException e) {
error = true;
log.error("Server closed connection.", e);
} catch (IOException e) {
error = true;
log.error("Error occurred while uploading", e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException f) {
// do nothing
}
}
}
return true;
}
public String getProxyHost() {
return proxyHost;
}
public void setProxyHost(String proxyHost) {
this.proxyHost = proxyHost;
}
public int getProxyPort() {
return proxyPort;
}
public void setProxyPort(int proxyPort) {
this.proxyPort = proxyPort;
}
public String getProxyUser() {
return proxyUser;
}
public void setProxyUser(String proxyUser) {
this.proxyUser = proxyUser;
}
public String getProxyPassword() {
return proxyPassword;
}
public void setProxyPassword(String proxyPassword) {
this.proxyPassword = proxyPassword;
}
public String getFtpUsername() {
return ftpUsername;
}
public void setFtpUsername(String ftpUsername) {
this.ftpUsername = ftpUsername;
}
public String getFtpPassword() {
return ftpPassword;
}
public void setFtpPassword(String ftpPassword) {
this.ftpPassword = ftpPassword;
}
public String getFtpServer() {
return ftpServer;
}
public void setFtpServer(String ftpServer) {
this.ftpServer = ftpServer;
}
public String getTargetPath() {
return targetPath;
}
public void setTargetPath(String targetPath) {
this.targetPath = targetPath;
}
public int getFtpPort() {
return ftpPort;
}
public void setFtpPort(int ftpPort) {
this.ftpPort = ftpPort;
}
}
_______________________________________________
Architecture mailing list
[email protected]
https://mail.wso2.org/cgi-bin/mailman/listinfo/architecture