View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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  package com.eviware.soapui.impl.wsdl.submit.transports.jms.util;
13  
14  import java.util.Enumeration;
15  
16  import javax.jms.BytesMessage;
17  import javax.jms.JMSException;
18  import javax.jms.MapMessage;
19  
20  import com.eviware.soapui.SoapUI;
21  import com.eviware.soapui.impl.wsdl.submit.transports.jms.JMSEndpoint;
22  import com.eviware.soapui.impl.wsdl.support.MessageExchangeModelItem;
23  import com.eviware.soapui.model.ModelItem;
24  import com.eviware.soapui.model.iface.MessageExchange;
25  import com.eviware.soapui.model.iface.Request;
26  import com.eviware.soapui.support.StringUtils;
27  import com.eviware.soapui.support.types.StringToStringMap;
28  import com.eviware.soapui.support.xml.XmlUtils;
29  
30  public class JMSUtils
31  {
32  
33  	private static boolean checkIfJMS( Request request )
34  	{
35  		try
36  		{
37  			String endpoint = request.getEndpoint();
38  			return StringUtils.hasContent( endpoint ) && endpoint.startsWith( JMSEndpoint.JMS_ENDPIONT_PREFIX );
39  		}
40  		catch( NullPointerException e )
41  		{
42  			SoapUI.logError( e );
43  		}
44  		return false;
45  	}
46  
47  	private static boolean checkIfJMS( MessageExchangeModelItem messageExchange )
48  	{
49  		try
50  		{
51  			MessageExchange me = ( ( MessageExchangeModelItem )messageExchange ).getMessageExchange();
52  			if( me != null )
53  			{
54  				StringToStringMap strmap = me.getProperties();
55  				if( strmap != null && strmap.containsKey( "Endpoint" ) )
56  				{
57  					String r = me.getProperty( "Endpoint" );
58  					return r != null && r.startsWith( JMSEndpoint.JMS_ENDPIONT_PREFIX );
59  				}
60  				else
61  				{
62  					return false;
63  				}
64  			}
65  			else
66  			{
67  				return false;
68  			}
69  		}
70  		catch( NullPointerException e )
71  		{
72  			SoapUI.logError( e );
73  		}
74  		return false;
75  	}
76  
77  	public static boolean checkIfJMS( ModelItem modelItem )
78  	{
79  		if( modelItem instanceof Request )
80  		{
81  			return checkIfJMS( ( Request )modelItem );
82  		}
83  		else
84  		{
85  			if( modelItem instanceof MessageExchangeModelItem )
86  			{
87  				return checkIfJMS( ( MessageExchangeModelItem )modelItem );
88  			}
89  		}
90  		return false;
91  	}
92  
93  	public static String extractMapMessagePayloadToString( MapMessage mapMessage ) throws JMSException
94  	{
95  		StringBuffer sb = new StringBuffer();
96  
97  		Enumeration<?> mapNames = mapMessage.getMapNames();
98  
99  		while( mapNames.hasMoreElements() )
100 		{
101 			String key = ( String )mapNames.nextElement();
102 			String value = mapMessage.getString( key );
103 			sb.append( key + ": " + value );
104 		}
105 
106 		return sb.toString();
107 	}
108 
109 	public static String extractMapMessagePayloadToXML( MapMessage mapMessage ) throws JMSException
110 	{
111 		StringBuffer sb = new StringBuffer( "<message>\n" );
112 
113 		Enumeration<?> mapNames = mapMessage.getMapNames();
114 
115 		while( mapNames.hasMoreElements() )
116 		{
117 			String key = ( String )mapNames.nextElement();
118 			String value = mapMessage.getString( key );
119 			sb.append( "<" + key + ">" + XmlUtils.entitize( value ) + "</" + key + ">\n" );
120 		}
121 		sb.append( "</message>" );
122 		return sb.toString();
123 	}
124 
125 	public static byte[] extractByteArrayFromMessage( BytesMessage message ) throws JMSException
126 	{
127 		byte[] bytes = new byte[( int )message.getBodyLength()];
128 		message.readBytes( bytes );
129 		return bytes;
130 	}
131 }