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.geronimo.test;
019:
020: import javax.xml.ws.handler.MessageContext;
021: import javax.xml.ws.handler.soap.SOAPMessageContext;
022: import javax.xml.ws.handler.soap.SOAPHandler;
023: import javax.xml.ws.WebServiceContext;
024:
025: import javax.annotation.PreDestroy;
026: import javax.annotation.PostConstruct;
027: import javax.annotation.Resource;
028:
029: import java.util.Iterator;
030: import java.util.Set;
031: import java.util.TreeSet;
032: import java.util.Map;
033:
034: import javax.xml.namespace.QName;
035:
036: import javax.xml.soap.*;
037:
038: public class GreeterSOAPHandler implements
039: SOAPHandler<SOAPMessageContext> {
040:
041: @Resource
042: WebServiceContext context;
043:
044: @Resource(name="greeting")
045: private String greeting;
046:
047: @PostConstruct
048: public void init() {
049: System.out.println(this + " init: " + context);
050: }
051:
052: @PreDestroy
053: public void destroy() {
054: System.out.println(this + " destroy");
055: }
056:
057: public void init(Map<String, Object> config) {
058: }
059:
060: public boolean handleFault(SOAPMessageContext context) {
061: System.out.println(this + " handleFault");
062: return true;
063: }
064:
065: public void close(MessageContext context) {
066: System.out.println(this + " close");
067: }
068:
069: public boolean handleMessage(SOAPMessageContext context) {
070: System.out.println(this + " handleMessage: "
071: + context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)
072: + " " + greeting);
073:
074: SOAPMessage message = context.getMessage();
075: try {
076: if ((Boolean) context
077: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)) {
078: // outbound
079:
080: } else {
081: // inbound
082: SOAPElement element = findElement(
083: message.getSOAPBody(), "arg0");
084: element.setValue("foo bar");
085:
086: // XXX: this does not work with Axis2
087: // message.getSOAPBody().getElementsByTagNameNS("*", "arg0").item(0).getFirstChild().setNodeValue("foo bar");
088:
089: message.saveChanges();
090: }
091:
092: // message.writeTo(System.out);
093: } catch (Exception e) {
094: throw new RuntimeException("handler failed", e);
095: }
096:
097: return true;
098: }
099:
100: private SOAPElement findElement(SOAPElement element, String name) {
101: Iterator iter = element.getChildElements();
102: while (iter.hasNext()) {
103: Node child = (Node) iter.next();
104: if (child instanceof SOAPElement) {
105: SOAPElement childEl = (SOAPElement) child;
106: if (name
107: .equals(childEl.getElementName().getLocalName())) {
108: return childEl;
109: } else {
110: return findElement(childEl, name);
111: }
112: }
113: }
114: return null;
115: }
116:
117: public Set<QName> getHeaders() {
118: System.out.println(this + " getHeaders");
119: return new TreeSet<QName>();
120: }
121:
122: }
|