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.util.Iterator;
021: import java.util.List;
022:
023: import javax.annotation.Resource;
024: import javax.xml.bind.JAXBContext;
025: import javax.xml.bind.JAXBElement;
026: import javax.xml.bind.JAXBException;
027: import javax.xml.namespace.QName;
028: import javax.xml.ws.Holder;
029: import javax.xml.ws.WebServiceContext;
030: import javax.xml.ws.handler.MessageContext;
031: import org.w3c.dom.Node;
032:
033: import org.apache.cxf.headers.Header;
034: import org.apache.cxf.helpers.CastUtils;
035: import org.apache.cxf.jaxb.JAXBDataBinding;
036: import org.apache.cxf.outofband.header.ObjectFactory;
037: import org.apache.cxf.outofband.header.OutofBandHeader;
038: import org.apache.hello_world_doc_lit_bare.PutLastTradedPricePortType;
039: import org.apache.hello_world_doc_lit_bare.types.TradePriceData;
040:
041: @javax.jws.WebService(serviceName="SOAPService",portName="SoapPort",endpointInterface="org.apache.hello_world_doc_lit_bare.PutLastTradedPricePortType",targetNamespace="http://apache.org/hello_world_doc_lit_bare",wsdlLocation="testutils/doc_lit_bare.wsdl")
042: public class OOBHdrServiceImpl implements PutLastTradedPricePortType {
043: @Resource
044: private WebServiceContext context;
045:
046: public void sayHi(Holder<TradePriceData> inout) {
047: inout.value.setTickerPrice(4.5f);
048: inout.value.setTickerSymbol("APACHE");
049: if (checkContext()) {
050: //System.out.println("Received out-of-band header as expected..");
051: sendReturnOOBHeader();
052: }
053: }
054:
055: private void sendReturnOOBHeader() {
056: if (context != null) {
057: MessageContext ctx = context.getMessageContext();
058: if (ctx != null) {
059: try {
060: // Create out-of-band header object.
061: OutofBandHeader ob = new OutofBandHeader();
062: ob.setName("testOobReturnHeaderName");
063: ob.setValue("testOobReturnHeaderValue");
064: ob.setHdrAttribute("testReturnHdrAttribute");
065: // Add Out-of-band header object to HeaderHolder.
066:
067: JAXBElement<OutofBandHeader> job = new JAXBElement<OutofBandHeader>(
068: new QName(
069: OOBHeaderTest.TEST_HDR_NS,
070: OOBHeaderTest.TEST_HDR_RESPONSE_ELEM),
071: OutofBandHeader.class, null, ob);
072: Header hdr = new Header(new QName(
073: OOBHeaderTest.TEST_HDR_NS,
074: OOBHeaderTest.TEST_HDR_RESPONSE_ELEM), job,
075: new JAXBDataBinding(ob.getClass()));
076: List<Header> hdrList = CastUtils.cast((List<?>) ctx
077: .get(Header.HEADER_LIST));
078: hdrList.add(hdr);
079: //Add headerHolder to requestContext.
080: // ctx.put(Header.HEADER_LIST, hdrList);
081: //System.out.println("Completed adding list to context");
082: } catch (Exception ex) {
083: ex.printStackTrace();
084: }
085: }
086: }
087: }
088:
089: public void putLastTradedPrice(TradePriceData body) {
090: //System.out.println("-----TradePriceData TickerPrice : ----- " + body.getTickerPrice());
091: //System.out.println("-----TradePriceData TickerSymbol : ----- " + body.getTickerSymbol());
092: }
093:
094: private boolean checkContext() {
095: boolean success = false;
096: MessageContext ctx = context == null ? null : context
097: .getMessageContext();
098: if (ctx.containsKey(Header.HEADER_LIST)) {
099: List oobHdr = (List) ctx.get(Header.HEADER_LIST);
100:
101: if (oobHdr instanceof List) {
102: Iterator iter = oobHdr.iterator();
103: while (iter.hasNext()) {
104: Object hdr = iter.next();
105: if (hdr instanceof Header
106: && ((Header) hdr).getObject() instanceof Node) {
107: Header hdr1 = (Header) hdr;
108: //System.out.println("Node conains : " + hdr1.getObject().toString());
109: try {
110: JAXBElement job = (JAXBElement) JAXBContext
111: .newInstance(ObjectFactory.class)
112: .createUnmarshaller().unmarshal(
113: (Node) hdr1.getObject());
114: OutofBandHeader ob = (OutofBandHeader) job
115: .getValue();
116: if ("testOobHeader".equals(ob.getName())
117: && "testOobHeaderValue".equals(ob
118: .getValue())
119: && "testHdrAttribute".equals(ob
120: .getHdrAttribute())) {
121: success = true;
122: } else {
123: throw new RuntimeException(
124: "test failed");
125: }
126: } catch (JAXBException ex) {
127: //
128: ex.printStackTrace();
129: }
130: }
131: }
132: } else {
133: throw new RuntimeException("Header should not be null"
134: + "and should be of type JAXBHeaderHolder");
135: }
136: } else {
137: throw new RuntimeException(
138: "MessageContext is null or doesnot contain OOBHeaders");
139: }
140:
141: return success;
142: }
143:
144: public String bareNoParam() {
145: return "testResponse";
146: }
147:
148: }
|