/*
 * 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;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataSource;

import org.apache.axis.AxisFault;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.soap.SOAPConstants;
import org.apache.log4j.Logger;


/**
 * <dl>
 * <dt><b>Title: </b><dd>SOAP 1.2 DataSource</dd>
 * <p>
 * <dt><b>Description: </b><dd>Datasource capable of handling SOAP 1.2 messages with the application/soap+xml
 * content type.</dd>
 * <p>
 * </dl>
 * @author <a href="mailto:jasone@greenrivercomputing.com">Jason Essington</a>
 * @version $Revision: 1.1.4.2 $
 */
public class SOAPDataSource implements DataSource
{
   private Logger log = Logger.getLogger(getClass());
   private Message soapMessage = null;

   /**
    * constructor to create a response message (one with no action parameter)
    * @param msg soapMessage
    */
   public SOAPDataSource(Message msg)
   {
      this.soapMessage = msg;
   }

   /* (non-Javadoc)
    * @see javax.activation.DataSource#getInputStream()
    */
   public InputStream getInputStream() throws IOException
   {
      /*
       * This method will get the whole soap message, but this datasource is really only set up to handle the 
       * soap envelope sans attachments.
       * TODO handle attachments
       */
      return new ByteArrayInputStream(this.soapMessage.getSOAPPartAsBytes());
   }

   /**
    * NOT IMPLEMENTED!
    */
   public OutputStream getOutputStream() throws IOException
   {
      throw new IOException("getOutputStream() is not implemented in SOAP12DataSource.");
   }

   /* (non-Javadoc)
    * @see javax.activation.DataSource#getContentType()
    */
   public String getContentType()
   {
      SOAPEnvelope envelope = null;
      try
      {
         envelope = soapMessage.getSOAPEnvelope();
      } catch (AxisFault e)
      {
         log.warn("Unable to get SOAPEnvelope from the SOAPMessage.", e);
      }
      
      boolean soap12 = (envelope != null && envelope.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS);
       
      MessageContext mctx = this.soapMessage.getMessageContext();
      SOAPConstants sc = mctx.getSOAPConstants();

      String ct = "text/xml";
      try
      {
         ct = soapMessage.getContentType(sc);
      } catch (AxisFault e)
      {
         log.warn("Failed to determine content type from the SOAPMessage.", e);
         ct = sc.getContentType();
      } 
      
      if (soap12)
      {
         // we need to add the action parameter to the content type for SOAP 1.2 requests
         if (mctx.useSOAPAction())
         {
            ct += ct +"; action=\""+mctx.getSOAPActionURI()+"\"";
         }
      }
      
      return ct;
   }

   /* (non-Javadoc)
    * @see javax.activation.DataSource#getName()
    */
   public String getName()
   {
      // TODO return something more intelligent here
      return "SOAP Message";
   }

}
