1 17 package org.apache.servicemix.components.http; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.net.HttpURLConnection ; 23 import java.net.URL ; 24 import java.net.URLConnection ; 25 26 import javax.xml.messaging.URLEndpoint; 27 import javax.xml.namespace.QName ; 28 import javax.xml.transform.TransformerException ; 29 30 import junit.framework.TestCase; 31 32 import org.apache.activemq.broker.BrokerService; 33 import org.apache.activemq.xbean.BrokerFactoryBean; 34 import org.apache.servicemix.components.saaj.SaajBinding; 35 import org.apache.servicemix.components.util.MockServiceComponent; 36 import org.apache.servicemix.jbi.container.ActivationSpec; 37 import org.apache.servicemix.jbi.container.JBIContainer; 38 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 39 import org.apache.servicemix.jbi.jaxp.StringSource; 40 import org.apache.servicemix.jbi.util.DOMUtil; 41 import org.apache.servicemix.jbi.util.FileUtil; 42 import org.apache.xml.serialize.OutputFormat; 43 import org.apache.xml.serialize.XMLSerializer; 44 import org.apache.xpath.CachedXPathAPI; 45 import org.springframework.core.io.ClassPathResource; 46 import org.w3c.dom.Document ; 47 import org.w3c.dom.Element ; 48 import org.w3c.dom.Node ; 49 import org.w3c.dom.traversal.NodeIterator; 50 51 public class HttpSoapAndSaajTest extends TestCase { 52 53 private static final int PORT_1 = 7012; 54 private static final int PORT_2 = 7012; 55 private static final int PORT_WS_1 = 7014; 56 private static final int PORT_WS_2 = 7014; 57 58 protected JBIContainer container; 59 protected BrokerService broker; 60 61 protected void setUp() throws Exception { 62 BrokerFactoryBean bfb = new BrokerFactoryBean(new ClassPathResource("broker.xml")); 63 bfb.afterPropertiesSet(); 64 broker = bfb.getBroker(); 65 broker.start(); 66 container = new JBIContainer(); 67 container.setMonitorInstallationDirectory(false); 68 container.setUseMBeanServer(false); 69 container.setCreateMBeanServer(false); 70 container.setEmbedded(true); 71 container.setFlowName("jms?jmsURL=tcp://localhost:61626"); 72 container.init(); 73 container.start(); 74 } 75 76 protected void tearDown() throws Exception { 77 if (container != null) { 78 container.shutDown(); 79 } 80 if (broker != null) { 81 broker.stop(); 82 } 83 } 84 85 public void testInOut() throws Exception { 86 ActivationSpec as = new ActivationSpec(); 87 as.setId("saaj"); 88 SaajBinding saaj = new SaajBinding(); 89 saaj.setSoapEndpoint(new URLEndpoint("http://localhost:" + PORT_WS_1)); 90 as.setComponent(saaj); 91 as.setService(new QName ("saaj")); 92 container.activateComponent(as); 93 94 as = new ActivationSpec(); 95 as.setId("xfireBinding"); 96 as.setComponent(new HttpSoapConnector(null, PORT_2, true)); 97 as.setDestinationService(new QName ("saaj")); 98 container.activateComponent(as); 99 100 as = new ActivationSpec(); 101 as.setId("webservice"); 102 as.setComponent(new HttpConnector(null, PORT_WS_2)); 103 as.setDestinationService(new QName ("mock")); 104 container.activateComponent(as); 105 106 as = new ActivationSpec(); 107 as.setId("mock"); 108 MockServiceComponent mock = new MockServiceComponent(); 109 mock.setResponseResource(new ClassPathResource("soap-response.xml", getClass())); 110 as.setComponent(mock); 111 as.setService(new QName ("mock")); 112 container.activateComponent(as); 113 114 URLConnection connection = new URL ("http://localhost:" + PORT_1).openConnection(); 115 connection.setDoOutput(true); 116 connection.setDoInput(true); 117 ((HttpURLConnection ) connection).setRequestProperty("Content-Type", "text/xml; charset=utf-8;"); 118 OutputStream os = connection.getOutputStream(); 119 InputStream fis = getClass().getResourceAsStream("soap-request.xml"); 121 FileUtil.copyInputStream(fis, os); 122 InputStream is = connection.getInputStream(); 124 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 125 FileUtil.copyInputStream(is, baos); 126 127 Node node = new SourceTransformer().toDOMNode(new StringSource(baos.toString())); 129 130 OutputFormat format = new OutputFormat((Document ) node); 131 format.setLineWidth(65); 132 format.setIndenting(true); 133 format.setIndent(2); 134 XMLSerializer serializer = new XMLSerializer(System.err, format); 135 System.err.println(); 136 serializer.serialize((Document ) node); 137 138 CachedXPathAPI cachedXPathAPI = new CachedXPathAPI(); 139 NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, "//*[local-name() = 'userId']"); 140 Element root = (Element ) iterator.nextNode(); 141 QName qname = DOMUtil.createQName(root, root.getAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "type")); 142 assertEquals("http://www.w3.org/2001/XMLSchema", qname.getNamespaceURI()); 143 assertEquals("string", qname.getLocalPart()); 144 145 } 146 147 protected String textValueOfXPath(Node node, String xpath) throws TransformerException { 148 CachedXPathAPI cachedXPathAPI = new CachedXPathAPI(); 149 NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, xpath); 150 Node root = iterator.nextNode(); 151 if (root instanceof Element ) { 152 Element element = (Element ) root; 153 if (element == null) { 154 return ""; 155 } 156 String text = DOMUtil.getElementText(element); 157 return text; 158 } 159 else { 160 return root.getNodeValue(); 161 } 162 } 163 } 164 | Popular Tags |