FTP-Listener
dinozzo85 Jul 15, 2009 5:53 AMHi guys...
Hope you can help me.
I got a ftp-listener with the following config :
<ftp-message-filter username="username" password="pw" directory="dir" input-suffix="*/*.zip" work-suffix=".esbInProcess" post-delete="true" error-delete="false" error-directory="@bhi2unio.ftp.errordirectory@" error-suffix=".esbERROR" passive="true" read-only="false"/> </ftp-bus>
The special thing here is that the files on the ftp are in subdirectories of the directory (see input-suffix).
The listenere config is here:
<ftp-listener name="ImportFTP" is-gateway="true" busidref="bhi2unio_zip_channel" schedule-frequency="@bhi2unio.ftp.pollfrequency@" maxThreads="1" > <property name="composer-class" value="com.unio.rmc.transfer.listener.RemoteToLocalMessageComposer"/> <property name="local-mirror-dir" value="@local-mirror-dir@"/> </ftp-listener>
Then i wrote a MessageComposer which writes some properties in the message and downloads the file from the ftp server to my local hd..
Thats the code:
public class RemoteToLocalMessageComposer<T extends File> implements MessageComposer<T> { static Logger log = Logger.getLogger(RemoteToLocalMessageComposer.class); public static final String LOCAL_MIRROR_DIR_TAG = "local-mirror-dir"; public static final String PN_MIRROR_FILE_NAME = "com.unio.rmc.mirror.file.name"; public static final String PN_FILE_PATH_RELATIVE = "com.unio.rmc.file.path.relative"; public static final String WORKING_FILE_SUFFIX = ".esbInProcess"; private FTPEpr ftpEpr; private File downloadDir; private MessagePayloadProxy payloadProxy; private File localFileDir; public void setConfiguration(ConfigTree config) throws ConfigurationException { EPR epr = ListenerUtil.assembleEpr(config); if (!(epr instanceof FTPEpr)) { throw new ConfigurationException( "This Gateway only accepts FTP and SFTP."); } ftpEpr = (FTPEpr) epr; // This may look a bit odd... The "file" input dir is our FTP download // dir...> downloadDir = UnioAbstractFileGateway.getFileInputDirectory(config); payloadProxy = new MessagePayloadProxy(config); localFileDir = getLocalFileDir(config); } private File getLocalFileDir(ConfigTree config) throws ConfigurationException { String uriStr = config.getAttribute(LOCAL_MIRROR_DIR_TAG); URI uri = null; if (uriStr != null) { try { uri = new URI(uriStr); } catch (URISyntaxException e) { throw new ConfigurationException("Invalid '" + LOCAL_MIRROR_DIR_TAG + "' value '" + uriStr + "'. Must be a valid URI."); } } else { config.getRequiredAttribute(LOCAL_MIRROR_DIR_TAG); } try { return new File(uri); } catch (Exception e) { return new File(uri.getPath()); } } public Message compose(T inputFile) throws MessageDeliverException { AssertArgument.isNotNull(inputFile, "inputFile"); File localFile = null; try { localFile = writeRemoteToLocalFile(inputFile); } catch (IOException e) { throw new MessageDeliverException( "Error reading remote input file '" + inputFile.getPath() + "'.", e); } catch (RemoteFileSystemException e) { throw new MessageDeliverException( "Error reading remote input file '" + inputFile.getPath() + "'.", e); } Message message = MessageFactory.getInstance().getMessage(); payloadProxy.setPayload(message, getPayload(inputFile)); message.getProperties().setProperty( PN_MIRROR_FILE_NAME, localFile.getAbsolutePath()); log.info("message " + message); message.getProperties().setProperty(PN_FILE_PATH_RELATIVE, getRelativeFilePath(inputFile)); return message; } private String getRelativeFilePath(File remoteFile) { String relativeFilePath = remoteFile.getPath(); // remove file name if (relativeFilePath.contains(File.separator)) { relativeFilePath = relativeFilePath.substring(0, relativeFilePath.lastIndexOf(File.separator)); } else { relativeFilePath = ""; } return relativeFilePath; } protected Object getPayload(T inputFile) { // we leave the body empty return new byte[] {}; } public Object decompose(Message message, T inputMessage) throws MessageDeliverException { log.info("blabalblablabla"); return payloadProxy.getPayload(message); } private File writeRemoteToLocalFile(File remoteFile) throws IOException, RemoteFileSystemException { File localFile = null; RemoteFileSystem remoteFileSystem = null; String localFileName = remoteFile.toString(); if (localFileName.endsWith(WORKING_FILE_SUFFIX)) { localFileName = localFileName.substring(0, localFileName .indexOf(WORKING_FILE_SUFFIX)); } try { localFile = new File(localFileDir, localFileName); localFile.mkdirs(); // file could be stored in a sub-directory localFile.createNewFile(); remoteFileSystem = RemoteFileSystemFactory.getRemoteFileSystem(ftpEpr, true); remoteFileSystem.setRemoteDir(FtpClientUtil.fileToFtpString(downloadDir)); log.info(FtpClientUtil.fileToFtpString(downloadDir)); String old = remoteFileSystem.getRemoteDir(); if (remoteFile.toString().contains(File.separator)){ int index = remoteFile.toString().lastIndexOf(File.separator); remoteFileSystem.setRemoteDir(remoteFileSystem.getRemoteDir() +"/" + remoteFile.toString().substring(0, index)); remoteFile = new File(remoteFile.toString().substring(index + 1)); log.info("remoteDir : " + remoteFileSystem.getRemoteDir()); } log.info("remoteFile : " + remoteFile.toString()); remoteFileSystem.downloadFile(remoteFile.toString(), localFile.getAbsolutePath()); log.info("download done to " + localFile.getAbsolutePath()); } finally { if (null != remoteFileSystem){ remoteFileSystem.quit(); } } return localFile; }
The file will be downloaded but if all goes well the esb cant delete the file on the ftp.....
11:42:51,895 ERROR [AbstractFileGateway] File test\stx3.xml 2.zip has been processed and renamed to test\stx3.xml 2.zip.esbInProcess, but th ere were problems deleting it from the input directory org.jboss.soa.esb.listeners.gateway.GatewayException: org.jboss.soa.esb.util.RemoteFileSystemException: Failed to delete remote file: 550 te st\stx3.xml 2.zip.esbInProcess: No such file or directory at org.jboss.soa.esb.listeners.gateway.RemoteGatewayListener.deleteFile(RemoteGatewayListener.java:128) at org.jboss.soa.esb.listeners.gateway.AbstractFileGateway.processingComplete(AbstractFileGateway.java:206) at org.jboss.soa.esb.listeners.gateway.AbstractFileGateway.onSchedule(AbstractFileGateway.java:198) at org.jboss.soa.esb.schedule.ScheduleProvider$ESBScheduledJob.execute(ScheduleProvider.java:227) at org.quartz.core.JobRunShell.run(JobRunShell.java:203) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520) Caused by: org.jboss.soa.esb.util.RemoteFileSystemException: Failed to delete remote file: 550 test\stx3.xml 2.zip.esbInProcess: No such fil e or directory at org.jboss.internal.soa.esb.util.FtpImpl.deleteRemoteFile(FtpImpl.java:284) at org.jboss.soa.esb.listeners.gateway.RemoteGatewayListener.deleteFile(RemoteGatewayListener.java:120) ... 5 more
I know the problem is that the filename on the ftp is the wrong... But how can i change it?
I hope u understand me...
Greets dinozzo