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.jaxws.handler.soap;
019:
020: import java.util.ArrayList;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Set;
025:
026: import javax.xml.bind.JAXBContext;
027: import javax.xml.bind.JAXBException;
028: import javax.xml.namespace.QName;
029: import javax.xml.soap.SOAPException;
030: import javax.xml.soap.SOAPHeader;
031: import javax.xml.soap.SOAPHeaderElement;
032: import javax.xml.soap.SOAPMessage;
033: import javax.xml.ws.WebServiceException;
034: import javax.xml.ws.handler.MessageContext;
035: import javax.xml.ws.handler.soap.SOAPMessageContext;
036:
037: import org.apache.cxf.binding.soap.SoapMessage;
038: import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
039: import org.apache.cxf.helpers.CastUtils;
040: import org.apache.cxf.jaxws.context.WrappedMessageContext;
041: import org.apache.cxf.message.Message;
042:
043: public class SOAPMessageContextImpl extends WrappedMessageContext
044: implements SOAPMessageContext {
045: private static final SAAJInInterceptor SAAJ_IN = new SAAJInInterceptor();
046:
047: private Set<String> roles = new HashSet<String>();
048:
049: public SOAPMessageContextImpl(Message m) {
050: super (m, Scope.HANDLER);
051: roles.add(getWrappedSoapMessage().getVersion().getNextRole());
052: }
053:
054: public void setMessage(SOAPMessage message) {
055: if (getWrappedMessage().getContent(Object.class) instanceof SOAPMessage) {
056: getWrappedMessage().setContent(Object.class, message);
057: } else {
058: getWrappedMessage().setContent(SOAPMessage.class, message);
059: }
060: }
061:
062: public SOAPMessage getMessage() {
063: SOAPMessage message = null;
064: if (getWrappedMessage().getContent(Object.class) instanceof SOAPMessage) {
065: message = (SOAPMessage) getWrappedMessage().getContent(
066: Object.class);
067: } else {
068: message = getWrappedMessage().getContent(SOAPMessage.class);
069: }
070:
071: //Only happens to non-Dispatch/Provider case.
072: if (null == message) {
073: Boolean outboundProperty = (Boolean) get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
074: if (!outboundProperty) {
075: //No SOAPMessage exists yet, so lets create one
076: SAAJ_IN.handleMessage(getWrappedSoapMessage());
077: message = getWrappedSoapMessage().getContent(
078: SOAPMessage.class);
079: }
080: }
081: return message;
082: }
083:
084: // TODO: handle the boolean parameter
085: public Object[] getHeaders(QName name, JAXBContext context,
086: boolean allRoles) {
087: SOAPMessage msg = getMessage();
088: SOAPHeader header;
089: try {
090: header = msg.getSOAPPart().getEnvelope().getHeader();
091: if (header == null || !header.hasChildNodes()) {
092: return new Object[0];
093: }
094: List<Object> ret = new ArrayList<Object>();
095: Iterator<SOAPHeaderElement> it = CastUtils.cast(header
096: .examineAllHeaderElements());
097: while (it.hasNext()) {
098: SOAPHeaderElement she = it.next();
099: if ((allRoles || roles.contains(she.getActor()))
100: && name.equals(she.getElementQName())) {
101:
102: ret
103: .add(context.createUnmarshaller()
104: .unmarshal(she));
105:
106: }
107: }
108: return ret.toArray(new SOAPHeaderElement[ret.size()]);
109: } catch (SOAPException e) {
110: throw new WebServiceException(e);
111: } catch (JAXBException e) {
112: throw new WebServiceException(e);
113: }
114: }
115:
116: public Set<String> getRoles() {
117: return roles;
118: }
119:
120: private SoapMessage getWrappedSoapMessage() {
121: return (SoapMessage) getWrappedMessage();
122: }
123:
124: }
|