001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.components.util;
018    
019    import java.util.Iterator;
020    import java.util.Map;
021    
022    import javax.jbi.JBIException;
023    import javax.jbi.messaging.MessageExchange;
024    import javax.jbi.messaging.MessagingException;
025    import javax.jbi.messaging.NormalizedMessage;
026    import javax.xml.namespace.QName;
027    import javax.xml.transform.Source;
028    
029    import org.apache.servicemix.MessageExchangeListener;
030    import org.apache.servicemix.jbi.jaxp.ResourceSource;
031    import org.apache.servicemix.jbi.jaxp.StringSource;
032    import org.springframework.core.io.Resource;
033    
034    /**
035     * A simple mock service component which is hard coded with a response to give
036     * which can be very useful for mocking out a web service call with some static
037     * response. For more complex requirements consider using a Script component or
038     * maybe a Jelly based component etc.
039     * 
040     * @version $Revision: 564374 $
041     */
042    public class MockServiceComponent extends TransformComponentSupport implements MessageExchangeListener {
043        
044        private Source responseContent;
045        private String responseXml;
046        private Resource responseResource;
047        private Map responseProperties;
048    
049        public MockServiceComponent() {
050        }
051        
052        public MockServiceComponent(QName service, String endpoint) {
053            super(service, endpoint);
054        }
055        
056        public Source getResponseContent() {
057            if (responseContent == null) {
058                if (responseXml != null) {
059                    responseContent = new StringSource(responseXml);
060                } else if (responseResource != null) {
061                    return new ResourceSource(responseResource);
062                }
063            }
064            return responseContent;
065        }
066    
067        public void setResponseContent(Source responseContent) {
068            this.responseContent = responseContent;
069        }
070    
071        public Map getResponseProperties() {
072            return responseProperties;
073        }
074    
075        public void setResponseProperties(Map responseProperties) {
076            this.responseProperties = responseProperties;
077        }
078    
079        public String getResponseXml() {
080            return responseXml;
081        }
082    
083        public void setResponseXml(String responseXml) {
084            this.responseXml = responseXml;
085        }
086    
087        public Resource getResponseResource() {
088            return responseResource;
089        }
090    
091        public void setResponseResource(Resource responseResource) {
092            this.responseResource = responseResource;
093        }
094    
095        // Implementation methods
096        // -------------------------------------------------------------------------
097        protected void init() throws JBIException {
098            super.init();
099            if (getResponseContent() == null) {
100                throw new IllegalArgumentException("You must specify the 'responseContent', 'responseXml' or 'responseResource' properties");
101            }
102        }
103    
104        protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException {
105            getMessageTransformer().transform(exchange, in, out);
106            out.setContent(getResponseContent());
107            Map map = getResponseProperties();
108            if (map != null) {
109                for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
110                    Map.Entry entry = (Map.Entry) iter.next();
111                    String name = (String) entry.getKey();
112                    Object value = entry.getValue();
113                    out.setProperty(name, value);
114                }
115            }
116            return true;
117        }
118    }