com/scalagent/joram/mom/dest/ftp/FtpThread.java

00001 /*
00002  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
00003  * Copyright (C) 2001 - 2008 ScalAgent Distributed Technologies
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or any later version.
00009  * 
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00018  * USA.
00019  *
00020  * Initial developer(s): ScalAgent Distributed Technologies
00021  * Contributor(s): 
00022  */
00023 package com.scalagent.joram.mom.dest.ftp;
00024 
00025 import java.net.InetAddress;
00026 import java.net.URI;
00027 import java.net.URL;
00028 import java.util.Enumeration;
00029 
00030 import org.objectweb.joram.mom.util.DMQManager;
00031 import org.objectweb.joram.shared.MessageErrorConstants;
00032 import org.objectweb.util.monolog.api.BasicLevel;
00033 import org.objectweb.util.monolog.api.Logger;
00034 
00035 import fr.dyade.aaa.agent.AgentId;
00036 import fr.dyade.aaa.agent.Channel;
00037 import fr.dyade.aaa.agent.Debug;
00038 
00039 public class FtpThread extends Thread {
00040   public static Logger logger = Debug.getLogger(FtpThread.class.getName());
00041   
00042   private TransferItf transfer;
00043   private AgentId destId;
00044   private FtpMessage ftpMsg;
00045   private AgentId dmqId;
00046   private int clientContext;
00047   private int requestId;
00048   private String user;
00049   private String pass;
00050   private String path;
00051 
00052   public String ftpImplName;
00053 
00054   public FtpThread(TransferItf transfer,
00055                    FtpMessage ftpMsg,
00056                    AgentId destId,
00057                    AgentId dmqId,
00058                    int clientContext,
00059                    int requestId,
00060                    String user,
00061                    String pass,
00062                    String path) {
00063     this.transfer = transfer;
00064     this.ftpMsg = ftpMsg;
00065     this.destId = destId;
00066     this.dmqId = dmqId;
00067     this.clientContext = clientContext;
00068     this.requestId = requestId;
00069     this.user = user;
00070     this.pass = pass;
00071     this.path = path;
00072     if (logger.isLoggable(BasicLevel.DEBUG))
00073       logger.log(BasicLevel.DEBUG, "--- " + this);
00074   }
00075 
00076   public void run() {
00077     if (logger.isLoggable(BasicLevel.DEBUG))
00078       logger.log(BasicLevel.DEBUG, "--- run()");
00079     doFtp(ftpMsg);
00080   }
00081 
00082 
00083   protected void doFtp(FtpMessage msg) {
00084     String urlName = null;
00085     long crc = -1;
00086     boolean ack = false;
00087     
00088     try {
00089       urlName = msg.getStringProperty(SharedObj.url);
00090       crc = msg.getLongProperty(SharedObj.crc);
00091       ack = msg.getBooleanProperty(SharedObj.ack);
00092 
00093       URL url = new URL(urlName);
00094       String urlFileName = url.getFile();
00095       String fileName = null;
00096       String type = null;
00097       String remotePath = null;
00098       
00099       if (logger.isLoggable(BasicLevel.DEBUG))
00100         logger.log(BasicLevel.DEBUG,
00101                    "--- doFtp : url host = " + url.getHost() + ", urlFileName = " + urlFileName);
00102 
00103       if (urlFileName.indexOf(";") > 1) {
00104         fileName = urlFileName.substring(
00105           urlFileName.lastIndexOf('/')+1,urlFileName.indexOf(";"));
00106         type = urlFileName.substring(
00107           urlFileName.lastIndexOf("=")+1,urlFileName.length());
00108       } else {
00109         fileName = urlFileName.substring(
00110           urlFileName.lastIndexOf('/')+1,urlFileName.length());
00111       }
00112       
00113       if (urlFileName.indexOf('/') < urlFileName.lastIndexOf('/'))
00114         remotePath = urlFileName.substring(
00115           urlFileName.indexOf('/')+1,urlFileName.lastIndexOf('/'));
00116       
00117       
00118       String userInfo = url.getUserInfo();
00119       String remoteUser = "anonymous";
00120       String remotePass = "no@no.no";
00121       if (userInfo != null) {
00122         remoteUser = userInfo.substring(0,userInfo.indexOf(':'));
00123         remotePass = userInfo.substring(userInfo.indexOf(':')+1,userInfo.length());
00124       }
00125       String protocol = url.getProtocol();
00126       int port = url.getPort();
00127 
00128       if (logger.isLoggable(BasicLevel.DEBUG))
00129         logger.log(BasicLevel.DEBUG, 
00130                    "doFtp : remoteUser = " + remoteUser + 
00131                    ", protocol = " + protocol + 
00132                    ", port = " + port);
00133 
00134       String file = transfer.getFile(protocol,
00135                                      InetAddress.getByName(url.getHost()).getHostName(),
00136                                      port,
00137                                      remoteUser,
00138                                      remotePass,
00139                                      remotePath, 
00140                                      path, 
00141                                      fileName,
00142                                      fileName,
00143                                      type,
00144                                      crc);
00145       
00146       file = file.replace('\\','/');
00147       if (!file.startsWith("/"))
00148         file = "/" + file;
00149       
00150       URI uri = new URI("file",null,file,null,null);
00151       uri = uri.normalize();
00152 
00153       if (logger.isLoggable(BasicLevel.DEBUG))
00154         logger.log(BasicLevel.DEBUG, "--- doFtp : uri = " + uri);
00155       
00156 //          StringBuffer sb = new StringBuffer();
00157 //          sb.append(protocol + "://");
00158 //          sb.append(user);
00159 //          sb.append(":");
00160 //          sb.append("****");//pass);
00161 //          sb.append("@");
00162 //          sb.append(InetAddress.getLocalHost().getHostName());
00163 //          if (port > -1) {
00164 //            sb.append(":");
00165 //            sb.append(port);
00166 //          }
00167 //          sb.append("/");
00168 //          if (path != null)
00169 //            sb.append(path + "/");
00170 //          sb.append(fileName);
00171 //          sb.append(";type=");
00172 //          sb.append(type);
00173  
00174 
00175       FtpMessage clone = (FtpMessage) msg.clone();
00176       clone.clearProperties();
00177       for (Enumeration e = msg.getPropertyNames(); e.hasMoreElements(); ) {
00178         String key = (String) e.nextElement();
00179         clone.setObjectProperty(key, msg.getObjectProperty(key));
00180       }
00181       clone.setStringProperty("url", uri.toString());
00182       
00183       Channel.sendTo(destId, new FtpNot(clientContext, 
00184                                         requestId,
00185                                         clone.getSharedMessage()));
00186       
00187     } catch (Exception exc) {
00188       DMQManager dmqManager = new DMQManager(dmqId, destId);
00189       dmqManager.addDeadMessage(msg.getSharedMessage(), MessageErrorConstants.UNEXPECTED_ERROR);
00190       dmqManager.sendToDMQ();
00191     }
00192   }
00193 
00194   public String toString() {
00195     StringBuffer buf = new StringBuffer();
00196     buf.append("FtpThread (");
00197     buf.append("transfer=");
00198     buf.append(transfer);
00199     buf.append(", ftpMsg=");
00200     buf.append(ftpMsg);
00201     buf.append(", destId=");
00202     buf.append(destId);
00203     buf.append(", dmqId=");
00204     buf.append(dmqId);
00205     buf.append(", clientContext=");
00206     buf.append(clientContext);
00207     buf.append(", requestId=");
00208     buf.append(requestId);
00209     buf.append(", user=");
00210     buf.append(user);
00211     buf.append(", pass=");
00212     buf.append(pass);
00213     buf.append(", path=");
00214     buf.append(path);
00215     buf.append(")");
00216     return buf.toString();
00217   }
00218 }

Generated on Tue Sep 16 16:14:22 2008 for joram by  doxygen 1.5.0