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.systest.outofband.header;
019:
020: import java.lang.reflect.InvocationHandler;
021: import java.lang.reflect.Proxy;
022: import java.net.URL;
023: import java.util.ArrayList;
024:
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028:
029: import javax.xml.bind.JAXBContext;
030: import javax.xml.bind.JAXBElement;
031: import javax.xml.bind.JAXBException;
032: import javax.xml.namespace.QName;
033: import javax.xml.ws.BindingProvider;
034: import javax.xml.ws.Holder;
035: import org.w3c.dom.Node;
036:
037: import org.apache.cxf.BusFactory;
038:
039: import org.apache.cxf.bus.spring.SpringBusFactory;
040: import org.apache.cxf.headers.Header;
041: import org.apache.cxf.jaxb.JAXBDataBinding;
042:
043: import org.apache.cxf.outofband.header.ObjectFactory;
044: import org.apache.cxf.outofband.header.OutofBandHeader;
045: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
046:
047: import org.apache.hello_world_doc_lit_bare.PutLastTradedPricePortType;
048: import org.apache.hello_world_doc_lit_bare.SOAPService;
049: import org.apache.hello_world_doc_lit_bare.types.TradePriceData;
050: import org.junit.BeforeClass;
051: import org.junit.Test;
052:
053: public class OOBHeaderTest extends AbstractBusClientServerTestBase {
054:
055: public static final String CONFIG_FILE = "org/apache/cxf/systest/outofband/header/cxf.xml";
056:
057: public static final String TEST_HDR_NS = "http://cxf.apache.org/outofband/Header";
058: public static final String TEST_HDR_REQUEST_ELEM = "outofbandHeader";
059: public static final String TEST_HDR_RESPONSE_ELEM = "outofbandHeader";
060:
061: private final QName serviceName = new QName(
062: "http://apache.org/hello_world_doc_lit_bare", "SOAPService");
063: private final QName portName = new QName(
064: "http://apache.org/hello_world_doc_lit_bare", "SoapPort");
065:
066: @BeforeClass
067: public static void startServers() throws Exception {
068: System.setProperty("org.apache.cxf.bus.factory",
069: "org.apache.cxf.bus.CXFBusFactory");
070: System.setProperty("cxf.config.file",
071: "org/apache/cxf/systest/outofband/header/cxf.xml");
072:
073: defaultConfigFileName = CONFIG_FILE;
074: SpringBusFactory bf = new SpringBusFactory();
075: staticBus = bf.createBus(defaultConfigFileName);
076: BusFactory.setDefaultBus(staticBus);
077: assertTrue("server did not launch correctly", launchServer(
078: Server.class, true));
079: }
080:
081: private void addOutOfBoundHeader(PutLastTradedPricePortType portType) {
082: InvocationHandler handler = Proxy
083: .getInvocationHandler(portType);
084: BindingProvider bp = null;
085:
086: try {
087: if (handler instanceof BindingProvider) {
088: bp = (BindingProvider) handler;
089: Map<String, Object> requestContext = bp
090: .getRequestContext();
091:
092: OutofBandHeader ob = new OutofBandHeader();
093: ob.setName("testOobHeader");
094: ob.setValue("testOobHeaderValue");
095: ob.setHdrAttribute("testHdrAttribute");
096:
097: JAXBElement<OutofBandHeader> job = new JAXBElement<OutofBandHeader>(
098: new QName(TEST_HDR_NS, TEST_HDR_REQUEST_ELEM),
099: OutofBandHeader.class, null, ob);
100: Header hdr = new Header(new QName(TEST_HDR_NS,
101: TEST_HDR_REQUEST_ELEM), job,
102: new JAXBDataBinding(ob.getClass()));
103:
104: List<Header> holder = new ArrayList<Header>();
105: holder.add(hdr);
106:
107: //Add List of headerHolders to requestContext.
108: requestContext.put(Header.HEADER_LIST, holder);
109: }
110: } catch (JAXBException ex) {
111: //System.out.println("failed to insert header into request context :" + ex);
112: }
113:
114: }
115:
116: private void checkReturnedOOBHeader(
117: PutLastTradedPricePortType portType) {
118: InvocationHandler handler = Proxy
119: .getInvocationHandler(portType);
120: BindingProvider bp = null;
121: if (handler instanceof BindingProvider) {
122: bp = (BindingProvider) handler;
123: Map<String, Object> responseContext = bp
124: .getResponseContext();
125: OutofBandHeader hdrToTest = null;
126: List oobHdr = (List) responseContext
127: .get(Header.HEADER_LIST);
128: if (oobHdr == null) {
129: fail("Should have got List of out-of-band headers ..");
130: }
131:
132: assertTrue(
133: "HeaderHolder list expected to conain 1 object received "
134: + oobHdr.size(), oobHdr.size() == 1);
135:
136: if (oobHdr != null & oobHdr instanceof List) {
137: Iterator iter = oobHdr.iterator();
138: while (iter.hasNext()) {
139: Object hdr = iter.next();
140: if (hdr instanceof Header) {
141: Header hdr1 = (Header) hdr;
142: if (hdr1.getObject() instanceof Node) {
143: //System.out.println("Node conains : " + hdr1.getObject().toString());
144: try {
145: JAXBElement job = (JAXBElement) JAXBContext
146: .newInstance(
147: ObjectFactory.class)
148: .createUnmarshaller()
149: .unmarshal(
150: (Node) hdr1.getObject());
151: hdrToTest = (OutofBandHeader) job
152: .getValue();
153: // System.out.println("oob-hdr contains : \nname = "
154: // + hdrToTest.getName()
155: // + " \nvalue = " + hdrToTest.getValue()
156: // + " \natribute = " + hdrToTest.getHdrAttribute());
157: } catch (JAXBException ex) {
158: //
159: ex.printStackTrace();
160: }
161: }
162: }
163: }
164: }
165:
166: assertNotNull("out-of-band header should not be null",
167: hdrToTest);
168:
169: assertTrue(
170: "Expected out-of-band Header name testOobReturnHeaderName recevied :"
171: + hdrToTest.getName(),
172: "testOobReturnHeaderName".equals(hdrToTest
173: .getName()));
174: assertTrue(
175: "Expected out-of-band Header value testOobReturnHeaderValue recevied :"
176: + hdrToTest.getValue(),
177: "testOobReturnHeaderValue".equals(hdrToTest
178: .getValue()));
179: assertTrue(
180: "Expected out-of-band Header attribute testReturnHdrAttribute recevied :"
181: + hdrToTest.getHdrAttribute(),
182: "testReturnHdrAttribute".equals(hdrToTest
183: .getHdrAttribute()));
184: }
185: }
186:
187: @Test
188: public void testBasicConnection() throws Exception {
189: URL wsdl = getClass().getResource("/wsdl/doc_lit_bare.wsdl");
190: assertNotNull("WSDL is null", wsdl);
191:
192: SOAPService service = new SOAPService(wsdl, serviceName);
193: assertNotNull("Service is null", service);
194:
195: PutLastTradedPricePortType putLastTradedPrice = service
196: .getPort(portName, PutLastTradedPricePortType.class);
197:
198: TradePriceData priceData = new TradePriceData();
199: priceData.setTickerPrice(1.0f);
200: priceData.setTickerSymbol("CELTIX");
201: Holder<TradePriceData> holder = new Holder<TradePriceData>(
202: priceData);
203:
204: addOutOfBoundHeader(putLastTradedPrice);
205:
206: putLastTradedPrice.sayHi(holder);
207:
208: checkReturnedOOBHeader(putLastTradedPrice);
209: }
210: }
|