001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.xml.ws.handler;
037:
038: import com.sun.xml.ws.api.message.Header;
039: import com.sun.xml.ws.api.message.Message;
040: import com.sun.xml.ws.api.message.Packet;
041: import com.sun.xml.ws.api.WSBinding;
042: import com.sun.xml.ws.api.SOAPVersion;
043: import com.sun.xml.ws.message.saaj.SAAJMessage;
044:
045: import javax.xml.bind.JAXBContext;
046: import javax.xml.namespace.QName;
047: import javax.xml.soap.SOAPException;
048: import javax.xml.soap.SOAPMessage;
049: import javax.xml.ws.WebServiceException;
050: import javax.xml.ws.handler.soap.SOAPMessageContext;
051:
052: import java.util.ArrayList;
053: import java.util.Iterator;
054: import java.util.List;
055: import java.util.Set;
056:
057: /**
058: * Implementation of {@link SOAPMessageContext}. This class is used at runtime
059: * to pass to the handlers for processing soap messages.
060: *
061: * @see MessageContextImpl
062: *
063: * @author WS Development Team
064: */
065: class SOAPMessageContextImpl extends MessageUpdatableContext implements
066: SOAPMessageContext {
067:
068: private Set<String> roles;
069: private SOAPMessage soapMsg = null;
070: private WSBinding binding;
071:
072: public SOAPMessageContextImpl(WSBinding binding, Packet packet,
073: Set<String> roles) {
074: super (packet);
075: this .binding = binding;
076: this .roles = roles;
077: }
078:
079: public SOAPMessage getMessage() {
080: if (soapMsg == null) {
081: try {
082: soapMsg = packet.getMessage().readAsSOAPMessage();
083: } catch (SOAPException e) {
084: throw new WebServiceException(e);
085: }
086: }
087: return soapMsg;
088: }
089:
090: public void setMessage(SOAPMessage soapMsg) {
091: try {
092: this .soapMsg = soapMsg;
093: } catch (Exception e) {
094: throw new WebServiceException(e);
095: }
096: }
097:
098: void setPacketMessage(Message newMessage) {
099: if (newMessage != null) {
100: packet.setMessage(newMessage);
101: soapMsg = null;
102: }
103: }
104:
105: protected void updateMessage() {
106: //Check if SOAPMessage has changed, if so construct new one,
107: // Packet are handled through MessageContext
108: if (soapMsg != null) {
109: packet.setMessage(new SAAJMessage(soapMsg));
110: soapMsg = null;
111: }
112: }
113:
114: public Object[] getHeaders(QName header, JAXBContext jaxbContext,
115: boolean allRoles) {
116: SOAPVersion soapVersion = binding.getSOAPVersion();
117:
118: List<Object> beanList = new ArrayList<Object>();
119: try {
120: Iterator<Header> itr = packet.getMessage().getHeaders()
121: .getHeaders(header, false);
122: if (allRoles) {
123: while (itr.hasNext()) {
124: beanList.add(itr.next().readAsJAXB(
125: jaxbContext.createUnmarshaller()));
126: }
127: } else {
128: while (itr.hasNext()) {
129: Header soapHeader = itr.next();
130: //Check if the role is one of the roles on this Binding
131: String role = soapHeader.getRole(soapVersion);
132: if (getRoles().contains(role)) {
133: beanList.add(soapHeader.readAsJAXB(jaxbContext
134: .createUnmarshaller()));
135: }
136: }
137: }
138: return beanList.toArray();
139: } catch (Exception e) {
140: throw new WebServiceException(e);
141: }
142: }
143:
144: public Set<String> getRoles() {
145: return roles;
146: }
147: }
|