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.InputStream;
    17  import java.util.ArrayList;
    18  import java.util.List;
    19
    20  import javax.activation.DataSource;
    21  import javax.mail.BodyPart;
    22  import javax.mail.MessagingException;
    23  import javax.mail.internet.MimeMessage;
    24  import javax.mail.internet.MimeMultipart;
    25
    26  import com.eviware.soapui.SoapUI;
    27  import com.eviware.soapui.impl.wsdl.WsdlOperation;
    28  import com.eviware.soapui.model.iface.Attachment;
    29  import com.eviware.soapui.settings.WsdlSettings;
    30  import com.eviware.soapui.support.StringUtils;
    31  import com.eviware.soapui.support.Tools;
    32  import com.eviware.soapui.support.xml.XmlUtils;
    33
    34  /***
    35   * Utility class for managing large MultiParts
    36   *
    37   * @author ole.matzura
    38   */
    39
    40  public class MultipartMessageSupport
    41  {
    42  	private final List<BodyPartAttachment> attachments = new ArrayList<BodyPartAttachment>();
    43  	private Attachment rootPart;
    44  	private MimeMessage message;
    45  	private String responseContent;
    46
    47  	public MultipartMessageSupport( DataSource dataSource, String rootPartId, WsdlOperation operation, boolean isRequest ) throws MessagingException
    48  	{
    49  		MimeMultipart mp = new MimeMultipart( dataSource);
    50  		message = new MimeMessage( AttachmentUtils.JAVAMAIL_SESSION );
    51  		message.setContent( mp );
    52
    53  		for( int c = 0; c < mp.getCount(); c++ )
    54  		{
    55  			BodyPart bodyPart = mp.getBodyPart( c );
    56
    57  			if( bodyPart.getContentType().toUpperCase().startsWith( "MULTIPART/"))
    58  			{
    59  				MimeMultipart mp2 = new MimeMultipart( new BodyPartDataSource( bodyPart ));
    60  				for( int i = 0; i < mp2.getCount(); i++ )
    61  				{
    62  					attachments.add( new BodyPartAttachment( mp2.getBodyPart(i), operation, isRequest ));
    63  				}
    64  			}
    65  			else
    66  			{
    67  				BodyPartAttachment attachment = new BodyPartAttachment( bodyPart, operation, isRequest );
    68
    69  				String[] contentIdHeaders = bodyPart.getHeader( "Content-ID");
    70  				if( contentIdHeaders != null && contentIdHeaders.length > 0 && contentIdHeaders[0].equals( rootPartId ))
    71  				{
    72  					rootPart = attachment;
    73  				}
    74  				else
    75  					attachments.add( attachment );
    76  			}
    77  		}
    78
    79  		// if no explicit root part has been set, use the first one in the result
    80  		if( rootPart == null )
    81  			rootPart = attachments.remove( 0 );
    82  	}
    83
    84  	public void setOperation( WsdlOperation operation )
    85  	{
    86  		for( BodyPartAttachment attachment : attachments )
    87  		{
    88  			attachment.setOperation( operation );
    89  		}
    90  	}
    91
    92  	public Attachment[] getAttachments()
    93  	{
    94  		return attachments.toArray( new Attachment[attachments.size()]);
    95  	}
    96
    97  	public Attachment getRootPart()
    98  	{
    99  		return rootPart;
    100 	}
    101
    102 	public Attachment[] getAttachmentsForPart(String partName)
    103 	{
    104 		List<Attachment> results = new ArrayList<Attachment>();
    105
    106 		for( Attachment attachment : attachments )
    107 		{
    108 			if( attachment.getPart().equals( partName ))
    109 				results.add( attachment );
    110 		}
    111
    112 		return results.toArray( new Attachment[results.size()]);
    113 	}
    114
    115 	public String getContentAsString()
    116 	{
    117 		if( rootPart == null )
    118 			return null;
    119
    120 		if( responseContent == null )
    121 		{
    122 			try
    123 			{
    124 				InputStream in = rootPart.getInputStream();
    125 				ByteArrayOutputStream data = Tools.readAll( in, Tools.READ_ALL );
    126
    127 				String contentType = rootPart.getContentType();
    128 				if( contentType != null && contentType.indexOf( "charset=" ) > 0 )
    129 				{
    130 					try
    131 					{
    132 						int ix = contentType.indexOf( "charset=" );
    133 						int ix2 = contentType.indexOf( ";", ix );
    134
    135 						String charset = ix2 == -1 ? contentType.substring( ix+8 ) :
    136 							contentType.substring( ix+8, ix2 );
    137
    138 						responseContent = data.toString( StringUtils.unquote( charset ) );
    139 					}
    140 					catch( Throwable e )
    141 					{
    142 						SoapUI.logError( e );
    143 					}
    144 				}
    145
    146 				if( responseContent == null )
    147 				{
    148 					responseContent = data.toString();
    149 				}
    150
    151 				if( SoapUI.getSettings().getBoolean( WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES ))
    152 		   	{
    153 					responseContent = XmlUtils.prettyPrintXml( responseContent );
    154 		   	}
    155
    156 				return responseContent;
    157 			}
    158 			catch (Exception e)
    159 			{
    160 				SoapUI.logError( e );
    161 			}
    162 		}
    163
    164 		return responseContent;
    165 	}
    166
    167 	public void setResponseContent(String responseContent)
    168 	{
    169 		this.responseContent = responseContent;
    170 	}
    171
    172 	public Attachment getAttachmentWithContentId( String contentId )
    173 	{
    174 		for( Attachment attachment : attachments)
    175 		   if( contentId.equals( attachment.getContentID() ))
    176 		   	return attachment;
    177
    178 		return null;
    179 	}
    180 }