1 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 ; 29 30 import java.util.Iterator ; 31 import java.util.Map ; 32 33 41 public class MockServiceComponent extends TransformComponentSupport implements MessageExchangeListener { 42 private Source responseContent; 43 private String responseXml; 44 private Resource responseResource; 45 private Map 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 getResponseProperties() { 64 return responseProperties; 65 } 66 67 public void setResponseProperties(Map responseProperties) { 68 this.responseProperties = responseProperties; 69 } 70 71 public String getResponseXml() { 72 return responseXml; 73 } 74 75 public void setResponseXml(String 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 protected void init() throws JBIException { 90 super.init(); 91 if (getResponseContent() == null) { 92 throw new IllegalArgumentException ("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 map = getResponseProperties(); 100 if (map != null) { 101 for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { 102 Map.Entry entry = (Map.Entry ) iter.next(); 103 String name = (String ) entry.getKey(); 104 Object value = entry.getValue(); 105 out.setProperty(name, value); 106 } 107 } 108 return true; 109 } 110 } 111 | Popular Tags |