View Javadoc

1   /*
2    * $Id: OutboundRoutingTestEvent.java 22597 2011-08-05 20:40:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.routing.outbound;
12  
13  import org.mule.MessageExchangePattern;
14  import org.mule.api.DefaultMuleException;
15  import org.mule.api.MuleContext;
16  import org.mule.api.MuleEvent;
17  import org.mule.api.MuleException;
18  import org.mule.api.MuleMessage;
19  import org.mule.api.MuleSession;
20  import org.mule.api.construct.FlowConstruct;
21  import org.mule.api.endpoint.InboundEndpoint;
22  import org.mule.api.security.Credentials;
23  import org.mule.api.transformer.DataType;
24  import org.mule.api.transformer.TransformerException;
25  import org.mule.api.transport.ReplyToHandler;
26  import org.mule.management.stats.ProcessingTime;
27  import org.mule.tck.MuleTestUtils;
28  import org.mule.transformer.types.DataTypeFactory;
29  import org.mule.util.UUID;
30  
31  import java.io.OutputStream;
32  import java.io.UnsupportedEncodingException;
33  import java.net.URI;
34  
35  /** An event used for outbound routing tests.  It is not fully fleshed out, containing only the information needed for
36   * routing.
37   */
38  public class OutboundRoutingTestEvent implements MuleEvent
39  {
40      private MuleMessage message;
41      private MuleSession session;
42      private String id = UUID.getUUID();
43      private boolean stopFurtherProcessing;
44      int timeout = -1;
45      private InboundEndpoint endpoint;
46      
47      public OutboundRoutingTestEvent(MuleMessage message, MuleSession session, MuleContext muleContext) throws Exception
48      {
49          this.message = message;
50          this.session = session;
51          this.endpoint = MuleTestUtils.getTestInboundEndpoint(MessageExchangePattern.REQUEST_RESPONSE,
52              muleContext);
53      }
54  
55      @Override
56      public MuleMessage getMessage()
57      {
58          return message;
59      }
60  
61      @Override
62      public MuleSession getSession()
63      {
64          return session;
65      }
66  
67      @Override
68      public Credentials getCredentials()
69      {
70          return null;
71      }
72  
73      @Override
74      public byte[] getMessageAsBytes() throws MuleException
75      {
76          try
77          {
78              return message.getPayloadAsBytes();
79          }
80          catch (Exception e)
81          {
82              throw new DefaultMuleException(e);
83          }
84      }
85  
86      @Override
87      public String getMessageAsString() throws MuleException
88      {
89          try
90          {
91              return message.getPayloadAsString();
92          }
93          catch (Exception e)
94          {
95              throw new DefaultMuleException(e);
96          }
97      }
98  
99      @Override
100     public String getMessageAsString(String encoding) throws MuleException
101     {
102         try
103         {
104             return message.getPayloadAsString(encoding);
105         }
106         catch (Exception e)
107         {
108             throw new DefaultMuleException(e);
109         }
110     }
111 
112     @Override
113     public Object transformMessage() throws TransformerException
114     {
115         throw new UnsupportedOperationException();
116     }
117 
118     @Override
119     public <T> T transformMessage(Class<T> outputType) throws TransformerException
120     {
121         return transformMessage(DataTypeFactory.create(outputType));
122     }
123 
124     @Override
125     public <T> T transformMessage(DataType<T> outputType) throws TransformerException
126     {
127         throw new UnsupportedOperationException();
128     }
129 
130     @Override
131     @Deprecated
132     public byte[] transformMessageToBytes() throws TransformerException
133     {
134         return transformMessage(DataType.BYTE_ARRAY_DATA_TYPE);
135     }
136 
137     @Override
138     public String transformMessageToString() throws TransformerException
139     {
140         try
141         {
142             return new String(transformMessageToBytes(), getEncoding());
143         }
144         catch (UnsupportedEncodingException e)
145         {
146             return "Unsupported Encoding";
147         }
148     }
149 
150     @Override
151     public String getId()
152     {
153         return id;
154     }
155 
156     @Override
157     public Object getProperty(String name)
158     {
159         return null;
160     }
161 
162     @Override
163     public Object getProperty(String name, Object defaultValue)
164     {
165         return defaultValue;
166     }
167 
168     @Override
169     public boolean isStopFurtherProcessing()
170     {
171         return stopFurtherProcessing;
172     }
173 
174     @Override
175     public void setStopFurtherProcessing(boolean stopFurtherProcessing)
176     {
177         this.stopFurtherProcessing = stopFurtherProcessing;
178     }
179 
180     @Override
181     public int getTimeout()
182     {
183         return timeout;
184     }
185 
186     @Override
187     public void setTimeout(int timeout)
188     {
189         this.timeout = timeout;
190     }
191 
192     @Override
193     public OutputStream getOutputStream()
194     {
195         return null;
196     }
197 
198     @Override
199     public String getEncoding()
200     {
201         return message.getEncoding();
202     }
203 
204     @Override
205     public MuleContext getMuleContext()
206     {
207         return null;
208     }
209 
210     @Override
211     public FlowConstruct getFlowConstruct()
212     {
213         try
214         {
215             return session == null
216                                   ? MuleTestUtils.getTestService(message.getMuleContext())
217                                   : session.getFlowConstruct();
218         }
219         catch (Exception e)
220         {
221             throw new RuntimeException(e);
222         }
223     }
224 
225     @Override
226     public ProcessingTime getProcessingTime()
227     {
228         return null;
229     }
230 
231     @Override
232     public MessageExchangePattern getExchangePattern()
233     {
234         return endpoint.getExchangePattern();
235     }
236 
237     @Override
238     public boolean isTransacted()
239     {
240         return false;
241     }
242     
243     @Override
244     public URI getMessageSourceURI()
245     {
246         return URI.create("test://test");
247     }
248     
249     @Override
250     public String getMessageSourceName()
251     {
252         return "test";
253     }
254     
255     @Override
256     public ReplyToHandler getReplyToHandler()
257     {
258         return null;
259     }
260 
261     @Override
262     public Object getReplyToDestination()
263     {
264         return null;
265     }
266 
267     @Override
268     public void captureReplyToDestination()
269     {
270     }
271 
272     @Override
273     public boolean isSynchronous()
274     {
275         return false;
276     }
277 }