001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.binding.soap;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.activation.DataHandler;
027: import javax.mail.util.ByteArrayDataSource;
028: import javax.xml.stream.XMLStreamReader;
029:
030: import org.w3c.dom.Element;
031:
032: import org.apache.cxf.BusFactory;
033: import org.apache.cxf.attachment.AttachmentImpl;
034: import org.apache.cxf.attachment.AttachmentUtil;
035: import org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor;
036: import org.apache.cxf.headers.Header;
037: import org.apache.cxf.interceptor.StaxInInterceptor;
038: import org.apache.cxf.message.Attachment;
039: import org.junit.Before;
040: import org.junit.Test;
041:
042: public class ReadHeaderInterceptorTest extends TestBase {
043:
044: private ReadHeadersInterceptor rhi;
045: private StaxInInterceptor staxIntc = new StaxInInterceptor();
046:
047: @Before
048: public void setUp() throws Exception {
049: super .setUp();
050:
051: rhi = new ReadHeadersInterceptor(BusFactory.getDefaultBus(),
052: "phase1");
053: chain.add(rhi);
054: }
055:
056: @Test
057: public void testBadSOAPEnvelopeNamespace() throws Exception {
058: soapMessage = TestUtil.createEmptySoapMessage(Soap12
059: .getInstance(), chain);
060: InputStream in = getClass().getResourceAsStream(
061: "test-bad-env.xml");
062: assertNotNull(in);
063: ByteArrayDataSource bads = new ByteArrayDataSource(in,
064: "test/xml");
065: soapMessage
066: .setContent(InputStream.class, bads.getInputStream());
067:
068: ReadHeadersInterceptor r = new ReadHeadersInterceptor(
069: BusFactory.getDefaultBus());
070: try {
071: r.handleMessage(soapMessage);
072: fail("Did not throw exception");
073: } catch (SoapFault f) {
074: assertEquals(Soap11.getInstance().getVersionMismatch(), f
075: .getFaultCode());
076: }
077: }
078:
079: @Test
080: public void testHandleHeader() {
081: try {
082: prepareSoapMessage("test-soap-header.xml");
083: } catch (IOException ioe) {
084: fail("Failed in creating soap message");
085: }
086:
087: staxIntc.handleMessage(soapMessage);
088: soapMessage.getInterceptorChain().doIntercept(soapMessage);
089: // check the xmlReader should be placed on the first entry of the body element
090: XMLStreamReader xmlReader = soapMessage
091: .getContent(XMLStreamReader.class);
092: assertEquals("check the first entry of body", "itinerary",
093: xmlReader.getLocalName());
094:
095: List<Header> eleHeaders = soapMessage.getHeaders();
096:
097: List<Element> headerChilds = new ArrayList<Element>();
098: Iterator<Header> iter = eleHeaders.iterator();
099: while (iter.hasNext()) {
100: Header hdr = iter.next();
101:
102: if (hdr.getObject() instanceof Element) {
103: headerChilds.add((Element) hdr.getObject());
104: }
105: }
106: // for (int i = 0; i < eleHeaders.getChildNodes().getLength(); i++) {
107: // if (eleHeaders.getChildNodes().item(i) instanceof Element) {
108: // Element element = (Element)eleHeaders.getChildNodes().item(i);
109: // headerChilds.add(element);
110: // }
111: // }
112:
113: assertEquals(2, headerChilds.size());
114: for (int i = 0; i < headerChilds.size(); i++) {
115: Element ele = headerChilds.get(i);
116: if (ele.getLocalName().equals("reservation")) {
117: Element reservation = ele;
118: List<Element> reservationChilds = new ArrayList<Element>();
119: for (int j = 0; j < reservation.getChildNodes()
120: .getLength(); j++) {
121: if (reservation.getChildNodes().item(j) instanceof Element) {
122: Element element = (Element) reservation
123: .getChildNodes().item(j);
124: reservationChilds.add(element);
125: }
126: }
127: assertEquals(2, reservationChilds.size());
128: assertEquals("reference", reservationChilds.get(0)
129: .getLocalName());
130: assertEquals(
131: "uuid:093a2da1-q345-739r-ba5d-pqff98fe8j7d",
132: ((Element) reservationChilds.get(0))
133: .getTextContent());
134: assertEquals("dateAndTime",
135: ((Element) reservationChilds.get(1))
136: .getLocalName());
137: assertEquals("2001-11-29T13:20:00.000-05:00",
138: ((Element) reservationChilds.get(1))
139: .getTextContent());
140:
141: }
142: if (ele.getLocalName().equals("passenger")) {
143: Element passenger = ele;
144: assertNotNull(passenger);
145: Element child = null;
146: for (int j = 0; j < passenger.getChildNodes()
147: .getLength(); j++) {
148: if (passenger.getChildNodes().item(j) instanceof Element) {
149: child = (Element) passenger.getChildNodes()
150: .item(j);
151: }
152: }
153: assertNotNull("passenger should has child element",
154: child);
155: assertEquals("name", child.getLocalName());
156: assertEquals("Bob", child.getTextContent());
157: }
158:
159: }
160: }
161:
162: private void prepareSoapMessage(String message) throws IOException {
163:
164: soapMessage = TestUtil.createEmptySoapMessage(Soap12
165: .getInstance(), chain);
166: ByteArrayDataSource bads = new ByteArrayDataSource(this
167: .getClass().getResourceAsStream(message),
168: "Application/xop+xml");
169: String cid = AttachmentUtil
170: .createContentID("http://cxf.apache.org");
171: soapMessage.setContent(Attachment.class, new AttachmentImpl(
172: cid, new DataHandler(bads)));
173: soapMessage
174: .setContent(InputStream.class, bads.getInputStream());
175:
176: }
177: }
|