View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.soapui.impl.wsdl.submit.transports.http;
    14
    15  import java.io.ByteArrayOutputStream;
    16  import java.io.IOException;
    17  import java.io.OutputStream;
    18
    19  import javax.mail.MessagingException;
    20  import javax.mail.internet.MimeMessage;
    21  import javax.mail.internet.MimeMultipart;
    22
    23  import org.apache.commons.httpclient.methods.RequestEntity;
    24
    25  import com.eviware.soapui.SoapUI;
    26  import com.eviware.soapui.impl.wsdl.WsdlInterface;
    27  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
    28
    29  /***
    30   * MimeMessage response for a WsdlMockResponse
    31   *
    32   * @author ole.matzura
    33   */
    34
    35  public class MimeMessageMockResponseEntity implements RequestEntity
    36  {
    37  	private final MimeMessage message;
    38  	private byte[] buffer;
    39  	private final boolean isXOP;
    40  	private final WsdlMockResponse wsdlRequest;
    41
    42  	public MimeMessageMockResponseEntity(MimeMessage message, boolean isXOP, WsdlMockResponse response )
    43  	{
    44  		this.message = message;
    45  		this.isXOP = isXOP;
    46  		this.wsdlRequest = response;
    47  	}
    48
    49  	public long getContentLength()
    50  	{
    51  		try
    52  		{
    53  			ByteArrayOutputStream out = new ByteArrayOutputStream();
    54  			writeRequest( out );
    55  			buffer = out.toByteArray();
    56  			return buffer.length;
    57  		}
    58  		catch (Exception e)
    59  		{
    60  			SoapUI.logError( e );
    61  			return -1;
    62  		}
    63  	}
    64
    65  	public String getContentType()
    66  	{
    67  		try
    68  		{
    69  			if( isXOP )
    70  			{
    71  				String header = message.getHeader( "Content-Type" )[0];
    72  				return AttachmentUtils.buildMTOMContentType(header, wsdlRequest.getMockOperation().getOperation().getAction(),
    73  							((WsdlInterface) wsdlRequest.getMockOperation().getOperation().getInterface()).getSoapVersion());
    74  			}
    75  			else
    76  			{
    77  				String header = message.getHeader( "Content-Type" )[0];
    78  				int ix = header.indexOf( "boundary" );
    79  				return "multipart/related; type=\"text/xml\"; start=\"" + AttachmentUtils.ROOTPART_SOAPUI_ORG + "\"; "  + header.substring( ix );
    80  			}
    81  		}
    82  		catch (MessagingException e)
    83  		{
    84  			SoapUI.logError( e );
    85  		}
    86
    87  		return null;
    88  	}
    89
    90  	public boolean isRepeatable()
    91  	{
    92  		return true;
    93  	}
    94
    95  	public void writeRequest(OutputStream arg0) throws IOException
    96  	{
    97  		try
    98  		{
    99  			if( buffer == null )
    100 			{
    101 				arg0.write( "\r\n".getBytes() );
    102 				((MimeMultipart)message.getContent()).writeTo( arg0 );
    103 			}
    104 			else
    105 				arg0.write( buffer );
    106 		}
    107 		catch (Exception e)
    108 		{
    109 			SoapUI.logError( e );
    110 		}
    111 	}
    112 }