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:
037: package com.sun.xml.ws.handler;
038:
039: import com.sun.xml.ws.api.message.Attachment;
040: import com.sun.xml.ws.api.message.AttachmentSet;
041: import com.sun.xml.ws.api.message.Packet;
042: import com.sun.xml.ws.util.ReadOnlyPropertyException;
043:
044: import javax.activation.DataHandler;
045: import javax.xml.ws.handler.MessageContext;
046: import java.util.Collection;
047: import java.util.HashMap;
048: import java.util.Map;
049: import java.util.Set;
050:
051: /**
052: *
053: * @author WS Development Team
054: */
055:
056: class MessageContextImpl implements MessageContext {
057: private Map<String, Object> fallbackMap = null;
058: private Set<String> handlerScopeProps;
059: Packet packet;
060:
061: void fallback() {
062: if (fallbackMap == null) {
063: fallbackMap = new HashMap<String, Object>();
064: fallbackMap.putAll(packet.createMapView());
065: fallbackMap.putAll(packet.invocationProperties);
066: }
067: }
068:
069: /** Creates a new instance of MessageContextImpl */
070: public MessageContextImpl(Packet packet) {
071: this .packet = packet;
072: handlerScopeProps = packet.getHandlerScopePropertyNames(false);
073: }
074:
075: protected void updatePacket() {
076: throw new UnsupportedOperationException("wrong call");
077: }
078:
079: public void setScope(String name, Scope scope) {
080: if (!containsKey(name))
081: throw new IllegalArgumentException("Property " + name
082: + " does not exist.");
083: if (scope == Scope.APPLICATION) {
084: handlerScopeProps.remove(name);
085: } else {
086: handlerScopeProps.add(name);
087:
088: }
089: }
090:
091: public Scope getScope(String name) {
092: if (!containsKey(name))
093: throw new IllegalArgumentException("Property " + name
094: + " does not exist.");
095: if (handlerScopeProps.contains(name)) {
096: return Scope.HANDLER;
097: } else {
098: return Scope.APPLICATION;
099: }
100: }
101:
102: public int size() {
103: fallback();
104: return fallbackMap.size();
105: }
106:
107: public boolean isEmpty() {
108: fallback();
109: return fallbackMap.isEmpty();
110: }
111:
112: public boolean containsKey(Object key) {
113: if (fallbackMap == null) {
114: if (packet.supports(key))
115: return true;
116: return packet.invocationProperties.containsKey(key);
117: } else {
118: fallback();
119: return fallbackMap.containsKey(key);
120: }
121: }
122:
123: public boolean containsValue(Object value) {
124: fallback();
125: return fallbackMap.containsValue(value);
126: }
127:
128: public Object put(String key, Object value) {
129: if (fallbackMap == null) {
130: if (packet.supports(key)) {
131: return packet.put(key, value); // strongly typed
132: }
133: if (!packet.invocationProperties.containsKey(key)) {
134: //New property, default to Scope.HANDLER
135: handlerScopeProps.add(key);
136: }
137: return packet.invocationProperties.put(key, value);
138:
139: } else {
140: fallback();
141: if (!fallbackMap.containsKey(key)) {
142: //new property, default to Scope.HANDLER
143: handlerScopeProps.add(key);
144: }
145: return fallbackMap.put(key, value);
146: }
147: }
148:
149: public Object get(Object key) {
150: if (key == null)
151: return null;
152: Object value;
153: if (fallbackMap == null) {
154: if (packet.supports(key)) {
155: value = packet.get(key); // strongly typed
156: } else {
157: value = packet.invocationProperties.get(key);
158: }
159: } else {
160: fallback();
161: value = fallbackMap.get(key);
162: }
163: //add the attachments from the Message to the corresponding attachment property
164: if (key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS)
165: || key
166: .equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)) {
167: Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
168: if (atts == null)
169: atts = new HashMap<String, DataHandler>();
170: AttachmentSet attSet = packet.getMessage().getAttachments();
171: for (Attachment att : attSet) {
172: atts.put(att.getContentId(), att.asDataHandler());
173: }
174: return atts;
175: }
176: return value;
177: }
178:
179: public void putAll(Map<? extends String, ? extends Object> t) {
180: fallback();
181: for (String key : t.keySet()) {
182: if (!fallbackMap.containsKey(key)) {
183: //new property, default to Scope.HANDLER
184: handlerScopeProps.add(key);
185: }
186: }
187: fallbackMap.putAll(t);
188: }
189:
190: public void clear() {
191: fallback();
192: fallbackMap.clear();
193: }
194:
195: public Object remove(Object key) {
196: fallback();
197: handlerScopeProps.remove(key);
198: return fallbackMap.remove(key);
199: }
200:
201: public Set<String> keySet() {
202: fallback();
203: return fallbackMap.keySet();
204: }
205:
206: public Set<Map.Entry<String, Object>> entrySet() {
207: fallback();
208: return fallbackMap.entrySet();
209: }
210:
211: public Collection<Object> values() {
212: fallback();
213: return fallbackMap.values();
214: }
215:
216: /**
217: * Fill a {@link Packet} with values of this {@link MessageContext}.
218: */
219: void fill(Packet packet) {
220: if (fallbackMap != null) {
221: for (Entry<String, Object> entry : fallbackMap.entrySet()) {
222: String key = entry.getKey();
223: if (packet.supports(key)) {
224: try {
225: packet.put(key, entry.getValue());
226: } catch (ReadOnlyPropertyException e) {
227: // Nothing to do
228: }
229: } else {
230: packet.invocationProperties.put(key, entry
231: .getValue());
232: }
233: }
234:
235: //Remove properties which are removed by user.
236: packet.createMapView().keySet().retainAll(
237: fallbackMap.keySet());
238: packet.invocationProperties.keySet().retainAll(
239: fallbackMap.keySet());
240: }
241: }
242:
243: }
|