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.jaxb;
019:
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import javax.xml.bind.JAXBContext;
026: import javax.xml.namespace.QName;
027: import javax.xml.stream.XMLInputFactory;
028: import javax.xml.stream.XMLStreamReader;
029: import javax.xml.stream.XMLStreamWriter;
030:
031: import org.apache.cxf.Bus;
032: import org.apache.cxf.BusFactory;
033: import org.apache.cxf.binding.Binding;
034: import org.apache.cxf.binding.BindingFactory;
035: import org.apache.cxf.binding.BindingFactoryManager;
036: import org.apache.cxf.endpoint.Endpoint;
037: import org.apache.cxf.endpoint.EndpointImpl;
038: import org.apache.cxf.interceptor.BareInInterceptor;
039: import org.apache.cxf.interceptor.Interceptor;
040: import org.apache.cxf.message.Exchange;
041: import org.apache.cxf.message.ExchangeImpl;
042: import org.apache.cxf.message.Message;
043: import org.apache.cxf.message.MessageImpl;
044: import org.apache.cxf.phase.PhaseInterceptorChain;
045: import org.apache.cxf.service.Service;
046: import org.apache.cxf.service.model.BindingInfo;
047: import org.apache.cxf.service.model.BindingOperationInfo;
048: import org.apache.cxf.service.model.EndpointInfo;
049: import org.apache.cxf.service.model.ServiceInfo;
050: import org.apache.cxf.staxutils.StaxUtils;
051: import org.apache.cxf.wsdl11.WSDLServiceFactory;
052: import org.apache.hello_world_doc_lit_bare.types.TradePriceData;
053: import org.apache.hello_world_soap_http.types.GreetMe;
054: import org.apache.hello_world_soap_http.types.GreetMeResponse;
055: import org.easymock.classextension.IMocksControl;
056: import org.junit.Assert;
057: import org.junit.Before;
058: import org.junit.Test;
059:
060: import static org.easymock.EasyMock.expect;
061: import static org.easymock.classextension.EasyMock.createNiceControl;
062:
063: public class BareInInterceptorTest extends Assert {
064:
065: PhaseInterceptorChain chain;
066: MessageImpl message;
067: Bus bus;
068: ServiceInfo serviceInfo;
069: BindingInfo bindingInfo;
070: Service service;
071: EndpointInfo endpointInfo;
072: EndpointImpl endpoint;
073: BindingOperationInfo operation;
074:
075: @Before
076: public void setUp() throws Exception {
077: bus = BusFactory.newInstance().createBus();
078:
079: BindingFactoryManager bfm = bus
080: .getExtension(BindingFactoryManager.class);
081:
082: IMocksControl control = createNiceControl();
083: BindingFactory bf = control.createMock(BindingFactory.class);
084: Binding binding = control.createMock(Binding.class);
085: expect(bf.createBinding(null)).andStubReturn(binding);
086: expect(binding.getInFaultInterceptors()).andStubReturn(
087: new ArrayList<Interceptor>());
088: expect(binding.getOutFaultInterceptors()).andStubReturn(
089: new ArrayList<Interceptor>());
090:
091: bfm.registerBindingFactory(
092: "http://schemas.xmlsoap.org/wsdl/soap/", bf);
093:
094: }
095:
096: @Test
097: public void testInterceptorInbound() throws Exception {
098: setUpUsingHelloWorld();
099:
100: BareInInterceptor interceptor = new BareInInterceptor();
101: message.setContent(XMLStreamReader.class, XMLInputFactory
102: .newInstance().createXMLStreamReader(
103: getTestStream(getClass(),
104: "resources/GreetMeDocLiteralReq.xml")));
105:
106: message.put(Message.INBOUND_MESSAGE, Message.INBOUND_MESSAGE);
107:
108: interceptor.handleMessage(message);
109:
110: assertNull(message.getContent(Exception.class));
111:
112: List<?> parameters = message.getContent(List.class);
113: assertEquals(1, parameters.size());
114:
115: Object obj = parameters.get(0);
116: assertTrue(obj instanceof GreetMe);
117: GreetMe greet = (GreetMe) obj;
118: assertEquals("TestSOAPInputPMessage", greet.getRequestType());
119: }
120:
121: @Test
122: public void testInterceptorInbound1() throws Exception {
123: setUpUsingDocLit();
124:
125: BareInInterceptor interceptor = new BareInInterceptor();
126: message.setContent(XMLStreamReader.class, XMLInputFactory
127: .newInstance().createXMLStreamReader(
128: getTestStream(getClass(),
129: "resources/sayHiDocLitBareReq.xml")));
130:
131: message.put(Message.INBOUND_MESSAGE, Message.INBOUND_MESSAGE);
132:
133: interceptor.handleMessage(message);
134:
135: assertNull(message.getContent(Exception.class));
136:
137: List<?> parameters = message.getContent(List.class);
138: assertEquals(1, parameters.size());
139:
140: Object obj = parameters.get(0);
141: assertTrue(obj instanceof TradePriceData);
142: TradePriceData greet = (TradePriceData) obj;
143: assertTrue(1.0 == greet.getTickerPrice());
144: assertEquals("CXF", greet.getTickerSymbol());
145: }
146:
147: @Test
148: public void testInterceptorInboundBareNoParameter()
149: throws Exception {
150: setUpUsingDocLit();
151:
152: BareInInterceptor interceptor = new BareInInterceptor();
153: message
154: .setContent(
155: XMLStreamReader.class,
156: XMLInputFactory
157: .newInstance()
158: .createXMLStreamReader(
159: getTestStream(getClass(),
160: "resources/bareNoParamDocLitBareReq.xml")));
161:
162: XMLStreamReader reader = (XMLStreamReader) message
163: .getContent(XMLStreamReader.class);
164: // skip to the end element of soap body, so that we can serve an empty request to
165: // interceptor
166: StaxUtils.skipToStartOfElement(reader);
167: StaxUtils.nextEvent(reader);
168:
169: message.put(Message.INBOUND_MESSAGE, Message.INBOUND_MESSAGE);
170:
171: interceptor.handleMessage(message);
172:
173: assertNull(message.getContent(Exception.class));
174:
175: List<?> parameters = message.getContent(List.class);
176: assertNull(parameters);
177: }
178:
179: @Test
180: public void testInterceptorOutbound() throws Exception {
181: setUpUsingHelloWorld();
182:
183: BareInInterceptor interceptor = new BareInInterceptor();
184:
185: message
186: .setContent(
187: XMLStreamReader.class,
188: XMLInputFactory
189: .newInstance()
190: .createXMLStreamReader(
191: getTestStream(getClass(),
192: "resources/GreetMeDocLiteralResp.xml")));
193: message.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
194: interceptor.handleMessage(message);
195:
196: List<?> parameters = message.getContent(List.class);
197: assertEquals(1, parameters.size());
198:
199: Object obj = parameters.get(0);
200:
201: assertTrue(obj instanceof GreetMeResponse);
202: GreetMeResponse greet = (GreetMeResponse) obj;
203: assertEquals("TestSOAPOutputPMessage", greet.getResponseType());
204: }
205:
206: //TODO: remove duplicate code in setUpUsingHelloWorld and setUpUsingDocLit
207: private void setUpUsingHelloWorld() throws Exception {
208: String ns = "http://apache.org/hello_world_soap_http";
209: WSDLServiceFactory factory = new WSDLServiceFactory(bus,
210: getClass().getResource("/wsdl/hello_world.wsdl"),
211: new QName(ns, "SOAPService"));
212:
213: service = factory.create();
214: endpointInfo = service.getServiceInfos().get(0).getEndpoint(
215: new QName(ns, "SoapPort"));
216: endpoint = new EndpointImpl(bus, service, endpointInfo);
217: JAXBDataBinding db = new JAXBDataBinding();
218: db.setContext(JAXBContext.newInstance(new Class[] {
219: GreetMe.class, GreetMeResponse.class }));
220: service.setDataBinding(db);
221:
222: operation = endpointInfo.getBinding().getOperation(
223: new QName(ns, "greetMe"));
224: operation.getOperationInfo().getInput()
225: .getMessagePartByIndex(0).setTypeClass(GreetMe.class);
226: operation.getOperationInfo().getOutput().getMessagePartByIndex(
227: 0).setTypeClass(GreetMeResponse.class);
228:
229: message = new MessageImpl();
230: Exchange exchange = new ExchangeImpl();
231: message.setExchange(exchange);
232:
233: exchange.put(Service.class, service);
234: exchange.put(Endpoint.class, endpoint);
235: exchange.put(Binding.class, endpoint.getBinding());
236: }
237:
238: private void setUpUsingDocLit() throws Exception {
239: String ns = "http://apache.org/hello_world_doc_lit_bare";
240: WSDLServiceFactory factory = new WSDLServiceFactory(bus,
241: getClass().getResource("/wsdl/doc_lit_bare.wsdl"),
242: new QName(ns, "SOAPService"));
243:
244: service = factory.create();
245: endpointInfo = service.getServiceInfos().get(0).getEndpoint(
246: new QName(ns, "SoapPort"));
247: endpoint = new EndpointImpl(bus, service, endpointInfo);
248: JAXBDataBinding db = new JAXBDataBinding();
249: db.setContext(JAXBContext
250: .newInstance(new Class[] { TradePriceData.class }));
251: service.setDataBinding(db);
252:
253: operation = endpointInfo.getBinding().getOperation(
254: new QName(ns, "SayHi"));
255: operation.getOperationInfo().getInput()
256: .getMessagePartByIndex(0).setTypeClass(
257: TradePriceData.class);
258: operation.getOperationInfo().getOutput().getMessagePartByIndex(
259: 0).setTypeClass(TradePriceData.class);
260:
261: message = new MessageImpl();
262: Exchange exchange = new ExchangeImpl();
263: message.setExchange(exchange);
264:
265: exchange.put(Service.class, service);
266: exchange.put(Endpoint.class, endpoint);
267: exchange.put(Binding.class, endpoint.getBinding());
268: }
269:
270: public InputStream getTestStream(Class<?> clz, String file) {
271: return clz.getResourceAsStream(file);
272: }
273:
274: public XMLStreamReader getXMLStreamReader(InputStream is) {
275: return StaxUtils.createXMLStreamReader(is);
276: }
277:
278: public XMLStreamWriter getXMLStreamWriter(OutputStream os) {
279: return StaxUtils.createXMLStreamWriter(os);
280: }
281:
282: }
|