/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.net.axis.transport.mailto.client;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.axis.AxisEngine;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.client.Call;
import org.apache.axis.client.Transport;
//import org.jboss.net.axis.transport.mail.MailConstants;

/**
 * <dl>
 * <dt><b>Title: </b><dd>Mail Transport</dd>
 * <p>
 * <dt><b>Description: </b><dd>Client side email transport object. This transport needs at least a recipient and an
 * action to work properly.</dd>
 * </dl>
 * @author <a href="mailto:jasone@greenrivercomputing.com">Jason Essington</a>
 * @version $Revision: 1.1.4.2 $
 */
public class MailTransport extends Transport
{
   public static final String DEFAULT_TRANSPORT_NAME = "mailto";
//   private String toAddress = null;
//   private String fromAddress = null;
//   private String action = null;

//   public MailTransport(String recipient, String action, String sender)
//   {
//      transportName = DEFAULT_TRANSPORT_NAME;
//      this.toAddress = recipient;
//      this.fromAddress = sender;
//      this.action = action;
//   }

//   public MailTransport(String recipient, String action)
//   {
//      transportName = DEFAULT_TRANSPORT_NAME;
//      this.toAddress = recipient;
//      this.action = action;
//   }

   public MailTransport()
   {
      transportName = DEFAULT_TRANSPORT_NAME;
   }

   /* (non-Javadoc)
    * @see org.apache.axis.client.Transport#processReturnedMessageContext(org.apache.axis.MessageContext)
    */
   public void processReturnedMessageContext(MessageContext context)
   {
      // maybe we should do something interesting here.
   }

   /**
    * 
    */
   public void setUrl(String url)
   {
      try
      {
         URL mailto = new URL(url);
         this.transportName = mailto.getProtocol();
//         this.toAddress = mailto.getPath();
         this.url = mailto.toExternalForm();
      }
      catch(MalformedURLException e)
      {
         // maybe we should complain or something
         this.url = url;
      }
   }
   
   public void setupMessageContextImpl(MessageContext msgCtx, Call call, AxisEngine engine) throws AxisFault
   {
      super.setupMessageContextImpl(msgCtx, call, engine);

      // bah! this stuff is now handled by ws-fx/addressing
//      if (toAddress != null)
//         msgCtx.setProperty(MailConstants.MC_IMMEDIATE_DESTINATION, toAddress);
//
//      if (fromAddress != null)
//         msgCtx.setProperty(MailConstants.MC_IMMEDIATE_SENDER, fromAddress);

//      if (action != null)
//      {
//         msgCtx.setUseSOAPAction(true);
//         msgCtx.setSOAPActionURI(this.action);
//      }
      
      // if our service has not been set, we will need to set it based upon
      // the defined SOAPAction otherwise our service handlers will not be run
      if (msgCtx.getService() == null)
      {
         msgCtx.setTargetService(msgCtx.getSOAPActionURI());
      }

   }

//   public void setRecipient(String recipient)
//   {
//      this.toAddress = recipient;
//   }
//
//   public String getRecipient()
//   {
//      return this.toAddress;
//   }
//
//   public void setSender(String sender)
//   {
//      this.fromAddress = sender;
//   }
//
//   public String getSender()
//   {
//      return this.fromAddress;
//   }
//
//   public void setAction(String action)
//   {
//      this.action = action;
//   }
//
//   public String getAction()
//   {
//      return this.action;
//   }

}