001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.xml.internal.ws.handler;
026:
027: import com.sun.xml.internal.ws.pept.ept.MessageInfo;
028: import com.sun.xml.internal.ws.encoding.jaxb.JAXBBeanInfo;
029: import com.sun.xml.internal.ws.encoding.jaxb.JAXBTypeSerializer;
030: import com.sun.xml.internal.ws.encoding.soap.SOAPEPTFactory;
031: import com.sun.xml.internal.ws.encoding.soap.internal.InternalMessage;
032:
033: import javax.xml.bind.JAXBContext;
034: import javax.xml.namespace.QName;
035: import javax.xml.soap.Name;
036: import javax.xml.soap.SOAPFactory;
037: import javax.xml.soap.SOAPHeader;
038: import javax.xml.soap.SOAPHeaderElement;
039: import javax.xml.soap.SOAPMessage;
040: import javax.xml.transform.Source;
041: import javax.xml.transform.dom.DOMSource;
042: import javax.xml.ws.WebServiceException;
043: import javax.xml.ws.handler.MessageContext;
044: import javax.xml.ws.handler.soap.SOAPMessageContext;
045:
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import java.util.HashMap;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.Map;
052: import java.util.Set;
053:
054: import org.xml.sax.InputSource;
055:
056: /**
057: * Implementation of SOAPMessageContext. This class is used at runtime
058: * to pass to the handlers for processing soap messages.
059: *
060: * @see MessageContextImpl
061: *
062: * @author WS Development Team
063: */
064: public class SOAPMessageContextImpl implements SOAPMessageContext {
065:
066: private SOAPHandlerContext handlerCtxt;
067: private MessageContext ctxt;
068: private Set<String> roles;
069: private static Map<String, Class> allowedTypes = null;
070: private boolean failure;
071:
072: public SOAPMessageContextImpl(SOAPHandlerContext handlerCtxt) {
073: this .handlerCtxt = handlerCtxt;
074: this .ctxt = handlerCtxt.getMessageContext();
075: if (allowedTypes == null) {
076: allowedTypes = new HashMap<String, Class>();
077: allowedTypes.put(
078: MessageContext.INBOUND_MESSAGE_ATTACHMENTS,
079: Map.class);
080: allowedTypes.put(
081: MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS,
082: Map.class);
083: allowedTypes.put(MessageContext.WSDL_DESCRIPTION,
084: InputSource.class);
085: allowedTypes.put(MessageContext.WSDL_SERVICE, QName.class);
086: allowedTypes.put(MessageContext.WSDL_PORT, QName.class);
087: allowedTypes
088: .put(MessageContext.WSDL_INTERFACE, QName.class);
089: allowedTypes
090: .put(MessageContext.WSDL_OPERATION, QName.class);
091: allowedTypes.put(MessageContext.MESSAGE_OUTBOUND_PROPERTY,
092: Boolean.class);
093: }
094: }
095:
096: public SOAPMessage getMessage() {
097: SOAPMessage soap = handlerCtxt.getSOAPMessage();
098: InternalMessage intr = handlerCtxt.getInternalMessage();
099: if (intr == null && soap != null) {
100: // Not much to do
101: } else if (intr != null && soap != null) {
102: // Overlay BodyBlock of InternalMessage on top of existing SOAPMessage
103: MessageInfo messageInfo = handlerCtxt.getMessageInfo();
104: SOAPEPTFactory eptf = (SOAPEPTFactory) messageInfo
105: .getEPTFactory();
106: soap = eptf.getSOAPEncoder().toSOAPMessage(intr, soap);
107: setMessage(soap); // It also sets InernalMessage to null
108: } else if (intr != null && soap == null) {
109: // Convert InternalMessage to a SOAPMessage
110: MessageInfo messageInfo = handlerCtxt.getMessageInfo();
111: SOAPEPTFactory eptf = (SOAPEPTFactory) messageInfo
112: .getEPTFactory();
113: soap = eptf.getSOAPEncoder().toSOAPMessage(intr,
114: messageInfo);
115: setMessage(soap); // It also sets InernalMessage to null
116: } else {
117: throw new WebServiceException("Don't have SOAPMessage");
118: }
119: return soap;
120: }
121:
122: public void setMessage(SOAPMessage soapMessage) {
123: handlerCtxt.setSOAPMessage(soapMessage);
124: // current InternalMessage is not valid anymore. So reset it.
125: handlerCtxt.setInternalMessage(null);
126: }
127:
128: public Object[] getHeaders(QName header, JAXBContext jaxbContext,
129: boolean allRoles) {
130: try {
131: List beanList = new ArrayList();
132: SOAPMessage msg = getMessage();
133: SOAPHeader sHeader = msg.getSOAPHeader();
134: if (sHeader == null) {
135: return new Object[0];
136: }
137: Iterator i = sHeader.getChildElements(header);
138: while (i.hasNext()) {
139: SOAPHeaderElement child = (SOAPHeaderElement) i.next();
140: if (allRoles) {
141: //If allRoles is true, add all headers
142: Source source = new DOMSource(child);
143: beanList.add(JAXBTypeSerializer.deserialize(source,
144: jaxbContext));
145: } else {
146: //If allRoles is false, add only headers with matching roles and headers with no role
147: if ((child.getActor() == null)
148: || (getRoles().contains(child.getActor()))) {
149: Source source = new DOMSource(child);
150: beanList.add(JAXBTypeSerializer.deserialize(
151: source, jaxbContext));
152: }
153: }
154: }
155: return beanList.toArray();
156: } catch (Exception e) {
157: throw new WebServiceException(e);
158: }
159: }
160:
161: public Set<String> getRoles() {
162: return roles;
163: }
164:
165: void setRoles(Set<String> roles) {
166: this .roles = roles;
167: }
168:
169: private boolean validateProperty(String name, Object value) {
170: if (allowedTypes.containsKey(name)) {
171: Class clazz = allowedTypes.get(name);
172: if (!(clazz.isInstance(value)))
173: throw new HandlerException(
174: "handler.messageContext.invalid.class",
175: new Object[] { value, name });
176: }
177:
178: return true;
179: }
180:
181: public void setScope(String name, Scope scope) {
182: ctxt.setScope(name, scope);
183: }
184:
185: public Scope getScope(String name) {
186: return ctxt.getScope(name);
187: }
188:
189: /* java.util.Map methods below here */
190:
191: public void clear() {
192: ctxt.clear();
193: }
194:
195: public boolean containsKey(Object obj) {
196: return ctxt.containsKey(obj);
197: }
198:
199: public boolean containsValue(Object obj) {
200: return ctxt.containsValue(obj);
201: }
202:
203: public Set<Entry<String, Object>> entrySet() {
204: return ctxt.entrySet();
205: }
206:
207: public Object get(Object obj) {
208: return ctxt.get(obj);
209: }
210:
211: public boolean isEmpty() {
212: return ctxt.isEmpty();
213: }
214:
215: public Set<String> keySet() {
216: return ctxt.keySet();
217: }
218:
219: public Object put(String str, Object obj) {
220: return ctxt.put(str, obj);
221: }
222:
223: public void putAll(Map<? extends String, ? extends Object> map) {
224: ctxt.putAll(map);
225: }
226:
227: public Object remove(Object obj) {
228: return ctxt.remove(obj);
229: }
230:
231: public int size() {
232: return ctxt.size();
233: }
234:
235: public Collection<Object> values() {
236: return ctxt.values();
237: }
238:
239: }
|