1 23 package com.scalagent.joram.mom.dest.ftp; 24 25 import java.net.InetAddress ; 26 import java.net.URI ; 27 import java.net.URL ; 28 import java.util.Enumeration ; 29 30 import org.objectweb.joram.mom.notifications.ClientMessages; 31 import org.objectweb.util.monolog.api.BasicLevel; 32 import org.objectweb.util.monolog.api.Logger; 33 34 import fr.dyade.aaa.agent.AgentId; 35 import fr.dyade.aaa.agent.Channel; 36 import fr.dyade.aaa.agent.Debug; 37 38 public class FtpThread extends Thread { 39 40 public static Logger logger = 41 Debug.getLogger("com.scalagent.joram.mom.dest.ftp.FtpThread"); 42 43 private TransferItf transfer; 44 private AgentId destId; 45 private FtpMessage ftpMsg; 46 private AgentId dmqId; 47 private int clientContext; 48 private int requestId; 49 private String user; 50 private String pass; 51 private String path; 52 53 public String ftpImplName; 54 55 public FtpThread(TransferItf transfer, 56 FtpMessage ftpMsg, 57 AgentId destId, 58 AgentId dmqId, 59 int clientContext, 60 int requestId, 61 String user, 62 String pass, 63 String path) { 64 this.transfer = transfer; 65 this.ftpMsg = ftpMsg; 66 this.destId = destId; 67 this.dmqId = dmqId; 68 this.clientContext = clientContext; 69 this.requestId = requestId; 70 this.user = user; 71 this.pass = pass; 72 this.path = path; 73 if (logger.isLoggable(BasicLevel.DEBUG)) 74 logger.log(BasicLevel.DEBUG, "--- " + this); 75 } 76 77 public void run() { 78 if (logger.isLoggable(BasicLevel.DEBUG)) 79 logger.log(BasicLevel.DEBUG, "--- run()"); 80 doFtp(ftpMsg); 81 } 82 83 84 protected void doFtp(FtpMessage msg) { 85 String urlName = null; 86 long crc = -1; 87 boolean ack = false; 88 89 try { 90 urlName = msg.getStringProperty(SharedObj.url); 91 crc = msg.getLongProperty(SharedObj.crc); 92 ack = msg.getBooleanProperty(SharedObj.ack); 93 94 URL url = new URL (urlName); 95 String urlFileName = url.getFile(); 96 String fileName = null; 97 String type = null; 98 String remotePath = null; 99 100 if (logger.isLoggable(BasicLevel.DEBUG)) 101 logger.log(BasicLevel.DEBUG, "--- doFtp : url host = " + url.getHost() + 102 ", urlFileName = " + urlFileName); 103 104 if (urlFileName.indexOf(";") > 1) { 105 fileName = urlFileName.substring( 106 urlFileName.lastIndexOf('/')+1,urlFileName.indexOf(";")); 107 type = urlFileName.substring( 108 urlFileName.lastIndexOf("=")+1,urlFileName.length()); 109 } else { 110 fileName = urlFileName.substring( 111 urlFileName.lastIndexOf('/')+1,urlFileName.length()); 112 } 113 114 if (urlFileName.indexOf('/') < urlFileName.lastIndexOf('/')) 115 remotePath = urlFileName.substring( 116 urlFileName.indexOf('/')+1,urlFileName.lastIndexOf('/')); 117 118 119 String userInfo = url.getUserInfo(); 120 String remoteUser = "anonymous"; 121 String remotePass = "no@no.no"; 122 if (userInfo != null) { 123 remoteUser = userInfo.substring(0,userInfo.indexOf(':')); 124 remotePass = userInfo.substring(userInfo.indexOf(':')+1,userInfo.length()); 125 } 126 String protocol = url.getProtocol(); 127 int port = url.getPort(); 128 129 if (logger.isLoggable(BasicLevel.DEBUG)) 130 logger.log(BasicLevel.DEBUG, 131 "doFtp : remoteUser = " + remoteUser + 132 ", protocol = " + protocol + 133 ", port = " + port); 134 135 String file = transfer.getFile(protocol, 136 InetAddress.getByName(url.getHost()).getHostName(), 137 port, 138 remoteUser, 139 remotePass, 140 remotePath, 141 path, 142 fileName, 143 fileName, 144 type, 145 crc); 146 147 file = file.replace('\\','/'); 148 if (!file.startsWith("/")) 149 file = "/" + file; 150 151 URI uri = new URI ("file",null,file,null,null); 152 uri = uri.normalize(); 153 154 if (logger.isLoggable(BasicLevel.DEBUG)) 155 logger.log(BasicLevel.DEBUG, "--- doFtp : uri = " + uri); 156 157 175 176 FtpMessage clone = (FtpMessage) msg.clone(); 177 clone.clearProperties(); 178 for (Enumeration e = msg.getPropertyNames(); e.hasMoreElements(); ) { 179 String key = (String ) e.nextElement(); 180 clone.setObjectProperty(key, msg.getObjectProperty(key)); 181 } 182 clone.setStringProperty("url", uri.toString()); 183 184 Channel.sendTo(destId, new FtpNot(clientContext, 185 requestId, 186 clone.getSharedMessage())); 187 188 } catch (Exception exc) { 189 ClientMessages deadM = 190 new ClientMessages(clientContext, 191 requestId); 192 msg.setNotWriteable(true); 193 deadM.addMessage(msg.getSharedMessage()); 194 if (dmqId != null) 195 Channel.sendTo(dmqId, deadM); 196 } 197 } 198 199 public String toString() { 200 StringBuffer buf = new StringBuffer (); 201 buf.append("FtpThread ("); 202 buf.append("transfer="); 203 buf.append(transfer); 204 buf.append(", ftpMsg="); 205 buf.append(ftpMsg); 206 buf.append(", destId="); 207 buf.append(destId); 208 buf.append(", dmqId="); 209 buf.append(dmqId); 210 buf.append(", clientContext="); 211 buf.append(clientContext); 212 buf.append(", requestId="); 213 buf.append(requestId); 214 buf.append(", user="); 215 buf.append(user); 216 buf.append(", pass="); 217 buf.append(pass); 218 buf.append(", path="); 219 buf.append(path); 220 buf.append(")"); 221 return buf.toString(); 222 } 223 } 224 | Popular Tags |