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.net.URI;
021: import java.util.HashSet;
022: import java.util.Set;
023:
024: import javax.activation.DataHandler;
025: import javax.mail.util.ByteArrayDataSource;
026: import javax.xml.namespace.QName;
027: import javax.xml.stream.XMLInputFactory;
028: import javax.xml.stream.XMLStreamReader;
029:
030: import org.apache.cxf.Bus;
031: import org.apache.cxf.BusFactory;
032: import org.apache.cxf.attachment.AttachmentImpl;
033: import org.apache.cxf.attachment.AttachmentUtil;
034: import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
035: import org.apache.cxf.binding.soap.interceptor.MustUnderstandInterceptor;
036: import org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor;
037: import org.apache.cxf.message.Attachment;
038: import org.apache.cxf.service.model.BindingInfo;
039: import org.apache.cxf.service.model.BindingOperationInfo;
040: import org.apache.cxf.service.model.ServiceInfo;
041: import org.junit.Before;
042: import org.junit.Test;
043:
044: public class MustUnderstandInterceptorTest extends TestBase {
045:
046: private static final QName RESERVATION = new QName(
047: "http://travelcompany.example.org/reservation",
048: "reservation");
049: private static final QName PASSENGER = new QName(
050: "http://mycompany.example.com/employees", "passenger");
051:
052: private MustUnderstandInterceptor mui;
053: private DummySoapInterceptor dsi;
054: private ReadHeadersInterceptor rhi;
055:
056: @Before
057: public void setUp() throws Exception {
058:
059: super .setUp();
060:
061: Bus bus = BusFactory.getDefaultBus();
062:
063: rhi = new ReadHeadersInterceptor(bus, "phase1");
064: chain.add(rhi);
065:
066: mui = new MustUnderstandInterceptor("phase2");
067: chain.add(mui);
068:
069: dsi = new DummySoapInterceptor("phase3");
070: chain.add(dsi);
071: }
072:
073: @Test
074: public void testHandleMessageSucc() throws Exception {
075: prepareSoapMessage();
076: dsi.getUnderstoodHeaders().add(RESERVATION);
077: dsi.getUnderstoodHeaders().add(PASSENGER);
078:
079: soapMessage.getInterceptorChain().doIntercept(soapMessage);
080: assertEquals("DummaySoapInterceptor getRoles has been called!",
081: true, dsi.isCalledGetRoles());
082: assertEquals(
083: "DummaySoapInterceptor getUnderstood has been called!",
084: true, dsi.isCalledGetUnderstood());
085: }
086:
087: @Test
088: public void testHandleMessageFail() throws Exception {
089: prepareSoapMessage();
090:
091: dsi.getUnderstoodHeaders().add(RESERVATION);
092:
093: soapMessage.getInterceptorChain().doIntercept(soapMessage);
094:
095: assertEquals("DummaySoapInterceptor getRoles has been called!",
096: true, dsi.isCalledGetRoles());
097: assertEquals(
098: "DummaySoapInterceptor getUnderstood has been called!",
099: true, dsi.isCalledGetUnderstood());
100:
101: SoapFault ie = (SoapFault) soapMessage
102: .getContent(Exception.class);
103: if (ie == null) {
104: fail("InBound Exception Missing! Exception should be Can't understands QNames: "
105: + PASSENGER);
106: } else {
107: assertEquals(soapMessage.getVersion().getMustUnderstand(),
108: ie.getFaultCode());
109: assertEquals("Can not understand QNames: " + PASSENGER, ie
110: .getMessage().toString());
111: }
112: }
113:
114: @Test
115: public void testHandleMessageWithHeaderParam() throws Exception {
116: prepareSoapMessage();
117: dsi.getUnderstoodHeaders().add(RESERVATION);
118: ServiceInfo serviceInfo = getMockedServiceModel(getClass()
119: .getResource("test-soap-header.wsdl").toString());
120:
121: BindingInfo binding = serviceInfo.getBinding(new QName(
122: "http://org.apache.cxf/headers",
123: "headerTesterSOAPBinding"));
124: BindingOperationInfo bop = binding.getOperation(new QName(
125: "http://org.apache.cxf/headers", "inHeader"));
126: soapMessage.getExchange().put(BindingOperationInfo.class, bop);
127:
128: soapMessage.getInterceptorChain().doIntercept(soapMessage);
129: assertEquals("DummaySoapInterceptor getRoles has been called!",
130: true, dsi.isCalledGetRoles());
131: assertEquals(
132: "DummaySoapInterceptor getUnderstood has been called!",
133: true, dsi.isCalledGetUnderstood());
134: }
135:
136: private void prepareSoapMessage() throws Exception {
137:
138: soapMessage = TestUtil.createEmptySoapMessage(Soap12
139: .getInstance(), chain);
140: ByteArrayDataSource bads = new ByteArrayDataSource(
141: this .getClass().getResourceAsStream(
142: "test-soap-header.xml"), "Application/xop+xml");
143: String cid = AttachmentUtil
144: .createContentID("http://cxf.apache.org");
145: soapMessage.setContent(Attachment.class, new AttachmentImpl(
146: cid, new DataHandler(bads)));
147: soapMessage.setContent(XMLStreamReader.class, XMLInputFactory
148: .newInstance().createXMLStreamReader(
149: bads.getInputStream()));
150:
151: }
152:
153: private class DummySoapInterceptor extends AbstractSoapInterceptor {
154:
155: private boolean calledGetRoles;
156: private boolean calledGetUnderstood;
157:
158: private Set<URI> roles = new HashSet<URI>();
159: private Set<QName> understood = new HashSet<QName>();
160:
161: public DummySoapInterceptor() {
162: super ("");
163: }
164:
165: public DummySoapInterceptor(String phase) {
166: super (phase);
167: }
168:
169: public void handleMessage(SoapMessage messageParam) {
170: }
171:
172: public Set<URI> getRoles() {
173: calledGetRoles = true;
174: if (roles.size() == 0) {
175: try {
176: roles
177: .add(new URI(
178: "http://www.w3.org/2003/05/soap-envelope/role/next"));
179: roles
180: .add(new URI(
181: "http://www.w3.org/2003/05/soap-envelope/role/none"));
182: roles
183: .add(new URI(
184: "http://www.w3.org/2003/05/soap-envelope/role/ultimateReceiver"));
185: } catch (Exception e) {
186: return null;
187: }
188: }
189: return roles;
190: }
191:
192: public Set<QName> getUnderstoodHeaders() {
193: calledGetUnderstood = true;
194: return understood;
195: }
196:
197: public boolean isCalledGetRoles() {
198: return calledGetRoles;
199: }
200:
201: public boolean isCalledGetUnderstood() {
202: return calledGetUnderstood;
203: }
204:
205: }
206:
207: }
|