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.http;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.net.HttpURLConnection;
023: import java.net.URL;
024: import java.net.URLConnection;
025:
026: import javax.xml.messaging.URLEndpoint;
027: import javax.xml.namespace.QName;
028: import javax.xml.transform.TransformerException;
029:
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Element;
032: import org.w3c.dom.Node;
033: import org.w3c.dom.traversal.NodeIterator;
034:
035: import junit.framework.TestCase;
036:
037: import org.apache.activemq.broker.BrokerService;
038: import org.apache.activemq.xbean.BrokerFactoryBean;
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041: import org.apache.servicemix.components.saaj.SaajBinding;
042: import org.apache.servicemix.components.util.MockServiceComponent;
043: import org.apache.servicemix.jbi.container.ActivationSpec;
044: import org.apache.servicemix.jbi.container.JBIContainer;
045: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
046: import org.apache.servicemix.jbi.jaxp.StringSource;
047: import org.apache.servicemix.jbi.util.DOMUtil;
048: import org.apache.servicemix.jbi.util.FileUtil;
049: import org.apache.xml.serialize.OutputFormat;
050: import org.apache.xml.serialize.XMLSerializer;
051: import org.apache.xpath.CachedXPathAPI;
052: import org.springframework.core.io.ClassPathResource;
053:
054: public class HttpSoapAndSaajTest extends TestCase {
055:
056: private static final int PORT_1 = 7012;
057: private static final int PORT_2 = 7012;
058: private static final int PORT_WS_1 = 7014;
059: private static final int PORT_WS_2 = 7014;
060:
061: private static transient Log log = LogFactory
062: .getLog(HttpSoapAndSaajTest.class);
063:
064: protected JBIContainer container;
065: protected BrokerService broker;
066:
067: protected void setUp() throws Exception {
068: BrokerFactoryBean bfb = new BrokerFactoryBean(
069: new ClassPathResource("broker.xml"));
070: bfb.afterPropertiesSet();
071: broker = bfb.getBroker();
072: broker.start();
073: container = new JBIContainer();
074: container.setMonitorInstallationDirectory(false);
075: container.setUseMBeanServer(false);
076: container.setCreateMBeanServer(false);
077: container.setEmbedded(true);
078: container.setFlowName("jms?jmsURL=tcp://localhost:61626");
079: container.init();
080: container.start();
081: }
082:
083: protected void tearDown() throws Exception {
084: if (container != null) {
085: container.shutDown();
086: }
087: if (broker != null) {
088: broker.stop();
089: }
090: }
091:
092: public void testInOut() throws Exception {
093: ActivationSpec as = new ActivationSpec();
094: as.setId("saaj");
095: SaajBinding saaj = new SaajBinding();
096: saaj.setSoapEndpoint(new URLEndpoint("http://localhost:"
097: + PORT_WS_1));
098: as.setComponent(saaj);
099: as.setService(new QName("saaj"));
100: container.activateComponent(as);
101:
102: as = new ActivationSpec();
103: as.setId("xfireBinding");
104: as.setComponent(new HttpSoapConnector(null, PORT_2, true));
105: as.setDestinationService(new QName("saaj"));
106: container.activateComponent(as);
107:
108: as = new ActivationSpec();
109: as.setId("webservice");
110: as.setComponent(new HttpConnector(null, PORT_WS_2));
111: as.setDestinationService(new QName("mock"));
112: container.activateComponent(as);
113:
114: as = new ActivationSpec();
115: as.setId("mock");
116: MockServiceComponent mock = new MockServiceComponent();
117: mock.setResponseResource(new ClassPathResource(
118: "soap-response.xml", getClass()));
119: as.setComponent(mock);
120: as.setService(new QName("mock"));
121: container.activateComponent(as);
122:
123: URLConnection connection = new URL("http://localhost:" + PORT_1)
124: .openConnection();
125: connection.setDoOutput(true);
126: connection.setDoInput(true);
127: ((HttpURLConnection) connection).setRequestProperty(
128: "Content-Type", "text/xml; charset=utf-8;");
129: OutputStream os = connection.getOutputStream();
130: // Post the request file.
131: InputStream fis = getClass().getResourceAsStream(
132: "soap-request.xml");
133: FileUtil.copyInputStream(fis, os);
134: // Read the response.
135: InputStream is = connection.getInputStream();
136: ByteArrayOutputStream baos = new ByteArrayOutputStream();
137: FileUtil.copyInputStream(is, baos);
138:
139: // Check xml validity
140: Node node = new SourceTransformer().toDOMNode(new StringSource(
141: baos.toString()));
142:
143: OutputFormat format = new OutputFormat((Document) node);
144: format.setLineWidth(65);
145: format.setIndenting(true);
146: format.setIndent(2);
147:
148: ByteArrayOutputStream output = new ByteArrayOutputStream();
149: XMLSerializer serializer = new XMLSerializer(output, format);
150: serializer.serialize((Document) node);
151: log.info(output.toString());
152:
153: CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
154: NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node,
155: "//*[local-name() = 'userId']");
156: Element root = (Element) iterator.nextNode();
157: QName qname = DOMUtil.createQName(root, root.getAttributeNS(
158: "http://www.w3.org/2001/XMLSchema-instance", "type"));
159: assertEquals("http://www.w3.org/2001/XMLSchema", qname
160: .getNamespaceURI());
161: assertEquals("string", qname.getLocalPart());
162:
163: }
164:
165: protected String textValueOfXPath(Node node, String xpath)
166: throws TransformerException {
167: CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
168: NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node,
169: xpath);
170: Node root = iterator.nextNode();
171: if (root instanceof Element) {
172: Element element = (Element) root;
173: if (element == null) {
174: return "";
175: }
176: String text = DOMUtil.getElementText(element);
177: return text;
178: } else {
179: return root.getNodeValue();
180: }
181: }
182: }
|