src/share/jaxws_classes/com/sun/xml/internal/ws/handler/MessageContextImpl.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
equal deleted inserted replaced
284:88b85470e72c 286:f50545b5e2f1
1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.xml.internal.ws.handler;
27
28 import com.sun.xml.internal.ws.api.message.Attachment;
29 import com.sun.xml.internal.ws.api.message.AttachmentSet;
30 import com.sun.xml.internal.ws.api.message.Packet;
31 import com.sun.xml.internal.ws.util.ReadOnlyPropertyException;
32
33 import javax.activation.DataHandler;
34 import javax.xml.ws.handler.MessageContext;
35 import java.util.Collection;
36 import java.util.HashMap;
37 import java.util.Map;
38 import java.util.Set;
39
40 /**
41 *
42 * @author WS Development Team
43 */
44
45 class MessageContextImpl implements MessageContext {
46 private Map<String,Object> fallbackMap = null;
47 private Set<String> handlerScopeProps;
48 Packet packet;
49
50
51 void fallback() {
52 if(fallbackMap == null) {
53 fallbackMap = new HashMap<String,Object>();
54 fallbackMap.putAll(packet.createMapView());
55 fallbackMap.putAll(packet.invocationProperties);
56 }
57 }
58 /** Creates a new instance of MessageContextImpl */
59 public MessageContextImpl(Packet packet) {
60 this.packet = packet;
61 handlerScopeProps = packet.getHandlerScopePropertyNames(false);
62 }
63 protected void updatePacket() {
64 throw new UnsupportedOperationException("wrong call");
65 }
66
67 public void setScope(String name, Scope scope) {
68 if(!containsKey(name))
69 throw new IllegalArgumentException("Property " + name + " does not exist.");
70 if(scope == Scope.APPLICATION) {
71 handlerScopeProps.remove(name);
72 } else {
73 handlerScopeProps.add(name);
74
75 }
76 }
77
78 public Scope getScope(String name) {
79 if(!containsKey(name))
80 throw new IllegalArgumentException("Property " + name + " does not exist.");
81 if(handlerScopeProps.contains(name)) {
82 return Scope.HANDLER;
83 } else {
84 return Scope.APPLICATION;
85 }
86 }
87
88 public int size() {
89 fallback();
90 return fallbackMap.size();
91 }
92
93 public boolean isEmpty() {
94 fallback();
95 return fallbackMap.isEmpty();
96 }
97
98 public boolean containsKey(Object key) {
99 if(fallbackMap == null) {
100 if(packet.supports(key))
101 return true;
102 return packet.invocationProperties.containsKey(key);
103 } else {
104 fallback();
105 return fallbackMap.containsKey(key);
106 }
107 }
108
109 public boolean containsValue(Object value) {
110 fallback();
111 return fallbackMap.containsValue(value);
112 }
113
114 public Object put(String key, Object value) {
115 if (fallbackMap == null) {
116 if (packet.supports(key)) {
117 return packet.put(key, value); // strongly typed
118 }
119 if (!packet.invocationProperties.containsKey(key)) {
120 //New property, default to Scope.HANDLER
121 handlerScopeProps.add(key);
122 }
123 return packet.invocationProperties.put(key, value);
124
125 } else {
126 fallback();
127 if (!fallbackMap.containsKey(key)) {
128 //new property, default to Scope.HANDLER
129 handlerScopeProps.add(key);
130 }
131 return fallbackMap.put(key, value);
132 }
133 }
134 public Object get(Object key) {
135 if(key == null)
136 return null;
137 Object value;
138 if(fallbackMap == null) {
139 if (packet.supports(key)) {
140 value = packet.get(key); // strongly typed
141 } else {
142 value = packet.invocationProperties.get(key);
143 }
144 } else {
145 fallback();
146 value = fallbackMap.get(key);
147 }
148 //add the attachments from the Message to the corresponding attachment property
149 if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) ||
150 key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){
151 Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
152 if(atts == null)
153 atts = new HashMap<String, DataHandler>();
154 AttachmentSet attSet = packet.getMessage().getAttachments();
155 for(Attachment att : attSet){
156 atts.put(att.getContentId(), att.asDataHandler());
157 }
158 return atts;
159 }
160 return value;
161 }
162
163 public void putAll(Map<? extends String, ? extends Object> t) {
164 fallback();
165 for(String key: t.keySet()) {
166 if(!fallbackMap.containsKey(key)) {
167 //new property, default to Scope.HANDLER
168 handlerScopeProps.add(key);
169 }
170 }
171 fallbackMap.putAll(t);
172 }
173
174 public void clear() {
175 fallback();
176 fallbackMap.clear();
177 }
178 public Object remove(Object key){
179 fallback();
180 handlerScopeProps.remove(key);
181 return fallbackMap.remove(key);
182 }
183 public Set<String> keySet() {
184 fallback();
185 return fallbackMap.keySet();
186 }
187 public Set<Map.Entry<String, Object>> entrySet(){
188 fallback();
189 return fallbackMap.entrySet();
190 }
191 public Collection<Object> values() {
192 fallback();
193 return fallbackMap.values();
194 }
195
196
197 /**
198 * Fill a {@link Packet} with values of this {@link MessageContext}.
199 */
200 void fill(Packet packet) {
201 if(fallbackMap != null) {
202 for (Entry<String, Object> entry : fallbackMap.entrySet()) {
203 String key = entry.getKey();
204 if (packet.supports(key)) {
205 try {
206 packet.put(key, entry.getValue());
207 } catch (ReadOnlyPropertyException e) {
208 // Nothing to do
209 }
210 } else {
211 packet.invocationProperties.put(key, entry.getValue());
212 }
213 }
214
215 //Remove properties which are removed by user.
216 packet.createMapView().keySet().retainAll(fallbackMap.keySet());
217 packet.invocationProperties.keySet().retainAll(fallbackMap.keySet());
218 }
219 }
220
221 }

mercurial