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