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  
13  package com.eviware.soapui.impl.wsdl.submit.transports.jms;
14  
15  import hermes.Domain;
16  import hermes.Hermes;
17  
18  import java.util.Enumeration;
19  import java.util.List;
20  
21  import javax.jms.JMSException;
22  import javax.jms.Message;
23  import javax.naming.NamingException;
24  
25  import com.eviware.soapui.SoapUI;
26  import com.eviware.soapui.config.JMSPropertyConfig;
27  import com.eviware.soapui.impl.support.AbstractHttpRequest;
28  import com.eviware.soapui.impl.wsdl.support.jms.header.JMSHeaderConfig;
29  import com.eviware.soapui.impl.wsdl.support.jms.property.JMSPropertiesConfig;
30  import com.eviware.soapui.model.iface.Request;
31  import com.eviware.soapui.model.iface.SubmitContext;
32  import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
33  import com.eviware.soapui.support.types.StringToStringMap;
34  import com.eviware.soapui.support.types.StringToStringsMap;
35  
36  /***
37   * @author nebojsa.tasic
38   * 
39   */
40  public class JMSHeader
41  {
42  	public static final String JMSCORRELATIONID = "JMSCorrelationID";
43  	public static final String JMSREPLYTO = "JMSReplyTo";
44  	public static final String TIMETOLIVE = "timeToLive";
45  	public static final String JMSTYPE = "JMSType";
46  	public static final String JMSPRIORITY = "JMSPriority";
47  	public static final String JMSDELIVERYMODE = "JMSDeliveryMode";
48  	public static final String JMSEXPIRATION = "JMSExpiration";
49  	public static final String JMSMESSAGEID = "JMSMessageID";
50  	public static final String JMSTIMESTAMP = "JMSTimestamp";
51  	public static final String JMSREDELIVERED = "JMSRedelivered";
52  	public static final String JMSDESTINATION = "JMSDestination";
53  	public static final String DURABLE_SUBSCRIPTION_NAME = "durableSubscriptionName";
54  	public static final String MESSAGE_SELECTOR = "messageSelector";
55  	public static final String CLIENT_ID = "clientID";
56  	public static final String SEND_AS_BYTESMESSAGE = "sendAsBytesMessage";
57  	public static final String SOAP_ACTION_ADD = "soapActionAdd";
58  	public static final String SOAP_ACTION = "SoapAction";
59  	public static final String SOAPJMS_SOAP_ACTION = "SOAPJMS_soapAction";
60  
61  	private long timeTolive = Message.DEFAULT_TIME_TO_LIVE;
62  
63  	public void setMessageHeaders( Message message, Request request, Hermes hermes, SubmitContext submitContext )
64  	{
65  		if( request instanceof AbstractHttpRequest )
66  		{
67  			JMSHeaderConfig jmsConfig = ( ( AbstractHttpRequest<?> )request ).getJMSHeaderConfig();
68  			try
69  			{
70  				// JMSCORRELATIONID
71  				if( jmsConfig.getJMSCorrelationID() != null && !jmsConfig.getJMSCorrelationID().equals( "" ) )
72  				{
73  					message.setJMSCorrelationID( PropertyExpander.expandProperties( submitContext, jmsConfig
74  							.getJMSCorrelationID() ) );
75  				}
76  
77  				// JMSREPLYTO
78  				if( jmsConfig.getJMSReplyTo() != null && !jmsConfig.getJMSReplyTo().equals( "" ) )
79  				{
80  					message.setJMSReplyTo( hermes.getDestination( PropertyExpander.expandProperties( submitContext,
81  							jmsConfig.getJMSReplyTo() ), Domain.QUEUE ) );
82  				}
83  
84  				// TIMETOLIVE
85  				if( jmsConfig.getTimeToLive() != null && !jmsConfig.getTimeToLive().equals( "" ) )
86  				{
87  					setTimeTolive( Long.parseLong( PropertyExpander.expandProperties( submitContext, jmsConfig
88  							.getTimeToLive() ) ) );
89  				}
90  				else
91  				{
92  					setTimeTolive( Message.DEFAULT_TIME_TO_LIVE );
93  				}
94  
95  				// JMSTYPE
96  				if( jmsConfig.getJMSType() != null && !jmsConfig.getJMSType().equals( "" ) )
97  				{
98  					message.setJMSType( PropertyExpander.expandProperties( submitContext, jmsConfig.getJMSType() ) );
99  				}
100 
101 				// JMSPRIORITY
102 				if( jmsConfig.getJMSPriority() != null && !jmsConfig.getJMSPriority().equals( "" ) )
103 				{
104 					message.setJMSPriority( Integer.parseInt( PropertyExpander.expandProperties( submitContext, jmsConfig
105 							.getJMSPriority() ) ) );
106 				}
107 				else
108 				{
109 					message.setJMSPriority( Message.DEFAULT_PRIORITY );
110 				}
111 
112 				// JMSDELIVERYMODE
113 				if( jmsConfig.getJMSDeliveryMode() != null && !jmsConfig.getJMSDeliveryMode().equals( "" ) )
114 				{
115 					int deliveryMode = jmsConfig.getJMSDeliveryMode().equals( "PERSISTENT" ) ? javax.jms.DeliveryMode.PERSISTENT
116 							: javax.jms.DeliveryMode.NON_PERSISTENT;
117 					message.setJMSDeliveryMode( deliveryMode );
118 				}
119 				else
120 				{
121 					message.setJMSDeliveryMode( Message.DEFAULT_DELIVERY_MODE );
122 				}
123 
124 			}
125 			catch( NamingException e )
126 			{
127 				SoapUI.logError( e, "Message header JMSReplyTo = "
128 						+ PropertyExpander.expandProperties( submitContext, jmsConfig.getJMSReplyTo() )
129 						+ "destination not exists!" );
130 			}
131 			catch( Exception e )
132 			{
133 				SoapUI.logError( e, "error while seting message header properties!" );
134 			}
135 		}
136 	}
137 
138 	public static void setMessageProperties( Message message, Request request, Hermes hermes, SubmitContext submitContext )
139 	{
140 		if( request instanceof AbstractHttpRequest )
141 		{
142 			JMSPropertiesConfig jmsPropertyConfig = ( ( AbstractHttpRequest<?> )request ).getJMSPropertiesConfig();
143 			try
144 			{
145 				List<JMSPropertyConfig> propertyList = jmsPropertyConfig.getJMSProperties();
146 				StringToStringMap stringToStringMap = new StringToStringMap( propertyList.size() );
147 				for( JMSPropertyConfig jmsProperty : propertyList )
148 				{
149 					stringToStringMap.put( jmsProperty.getName(), jmsProperty.getValue() );
150 				}
151 
152 				// CUSTOM PROPERTIES
153 				String keys[] = stringToStringMap.getKeys();
154 				for( String key : keys )
155 				{
156 					if( !key.equals( JMSCORRELATIONID ) && !key.equals( JMSREPLYTO ) && !key.equals( TIMETOLIVE )
157 							&& !key.equals( JMSTYPE ) && !key.equals( JMSPRIORITY ) && !key.equals( JMSDELIVERYMODE ) )
158 					{
159 						message.setStringProperty( key, PropertyExpander.expandProperties( submitContext, stringToStringMap
160 								.get( key ) ) );
161 					}
162 				}
163 			}
164 
165 			catch( Exception e )
166 			{
167 				SoapUI.logError( e, "error while seting jms message properties!" );
168 			}
169 		}
170 	}
171 
172 	public long getTimeTolive()
173 	{
174 		return timeTolive;
175 	}
176 
177 	public void setTimeTolive( long timeTolive )
178 	{
179 		this.timeTolive = timeTolive;
180 	}
181 
182 	public static StringToStringsMap getMessageHeadersAndProperties( Message message )
183 	{
184 		StringToStringsMap headermap = new StringToStringsMap();
185 		try
186 		{
187 			headermap.put( JMSDELIVERYMODE, String.valueOf( message.getJMSDeliveryMode() ) );
188 			headermap.put( JMSEXPIRATION, String.valueOf( message.getJMSExpiration() ) );
189 			headermap.put( JMSPRIORITY, String.valueOf( message.getJMSPriority() ) );
190 			headermap.put( JMSTIMESTAMP, String.valueOf( message.getJMSTimestamp() ) );
191 			headermap.put( JMSREDELIVERED, String.valueOf( message.getJMSRedelivered() ) );
192 
193 			if( message.getJMSDestination() != null )
194 				headermap.put( JMSDESTINATION, String.valueOf( message.getJMSDestination() ) );
195 
196 			if( message.getJMSMessageID() != null )
197 				headermap.put( JMSMESSAGEID, message.getJMSMessageID() );
198 
199 			if( message.getJMSCorrelationID() != null )
200 				headermap.put( JMSCORRELATIONID, message.getJMSCorrelationID() );
201 
202 			if( message.getJMSReplyTo() != null )
203 				headermap.put( JMSREPLYTO, String.valueOf( message.getJMSReplyTo() ) );
204 
205 			if( message.getJMSType() != null )
206 				headermap.put( JMSTYPE, message.getJMSType() );
207 
208 			Enumeration<?> properties = message.getPropertyNames();
209 			while( properties.hasMoreElements() )
210 			{
211 				String key = ( String )properties.nextElement();
212 				headermap.put( key, message.getStringProperty( key ) );
213 			}
214 
215 		}
216 		catch( JMSException e )
217 		{
218 			SoapUI.logError( e );
219 		}
220 		return headermap;
221 	}
222 }