KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > util > MockServiceComponent


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.util;
18
19 import org.apache.servicemix.MessageExchangeListener;
20 import org.apache.servicemix.jbi.jaxp.ResourceSource;
21 import org.apache.servicemix.jbi.jaxp.StringSource;
22 import org.springframework.core.io.Resource;
23
24 import javax.jbi.JBIException;
25 import javax.jbi.messaging.MessageExchange;
26 import javax.jbi.messaging.MessagingException;
27 import javax.jbi.messaging.NormalizedMessage;
28 import javax.xml.transform.Source JavaDoc;
29
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * A simple mock service component which is hard coded with a response to give
35  * which can be very useful for mocking out a web service call with some static
36  * response. For more complex requirements consider using a Script component or
37  * maybe a Jelly based component etc.
38  *
39  * @version $Revision: 426415 $
40  */

41 public class MockServiceComponent extends TransformComponentSupport implements MessageExchangeListener {
42     private Source responseContent;
43     private String JavaDoc responseXml;
44     private Resource responseResource;
45     private Map JavaDoc responseProperties;
46
47     public Source getResponseContent() {
48         if (responseContent == null) {
49             if (responseXml != null) {
50                 responseContent = new StringSource(responseXml);
51             }
52             else if (responseResource != null) {
53                 return new ResourceSource(responseResource);
54             }
55         }
56         return responseContent;
57     }
58
59     public void setResponseContent(Source responseContent) {
60         this.responseContent = responseContent;
61     }
62
63     public Map JavaDoc getResponseProperties() {
64         return responseProperties;
65     }
66
67     public void setResponseProperties(Map JavaDoc responseProperties) {
68         this.responseProperties = responseProperties;
69     }
70
71     public String JavaDoc getResponseXml() {
72         return responseXml;
73     }
74
75     public void setResponseXml(String JavaDoc responseXml) {
76         this.responseXml = responseXml;
77     }
78
79     public Resource getResponseResource() {
80         return responseResource;
81     }
82
83     public void setResponseResource(Resource responseResource) {
84         this.responseResource = responseResource;
85     }
86
87     // Implementation methods
88
// -------------------------------------------------------------------------
89
protected void init() throws JBIException {
90         super.init();
91         if (getResponseContent() == null) {
92             throw new IllegalArgumentException JavaDoc("You must specify the 'responseContent', 'responseXml' or 'responseResource' properties");
93         }
94     }
95
96     protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException {
97         getMessageTransformer().transform(exchange, in, out);
98         out.setContent(getResponseContent());
99         Map JavaDoc map = getResponseProperties();
100         if (map != null) {
101             for (Iterator JavaDoc iter = map.entrySet().iterator(); iter.hasNext();) {
102                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
103                 String JavaDoc name = (String JavaDoc) entry.getKey();
104                 Object JavaDoc value = entry.getValue();
105                 out.setProperty(name, value);
106             }
107         }
108         return true;
109     }
110 }
111
Popular Tags