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

changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
equal deleted inserted replaced
366:8c0b6bccfe47 368:0989ad8c0860
1 /* 1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
26 package com.sun.xml.internal.ws.handler; 26 package com.sun.xml.internal.ws.handler;
27 27
28 import com.sun.xml.internal.ws.api.message.Attachment; 28 import com.sun.xml.internal.ws.api.message.Attachment;
29 import com.sun.xml.internal.ws.api.message.AttachmentSet; 29 import com.sun.xml.internal.ws.api.message.AttachmentSet;
30 import com.sun.xml.internal.ws.api.message.Packet; 30 import com.sun.xml.internal.ws.api.message.Packet;
31 import com.sun.xml.internal.ws.util.ReadOnlyPropertyException;
32 31
33 import javax.activation.DataHandler; 32 import javax.activation.DataHandler;
34 import javax.xml.ws.handler.MessageContext; 33 import javax.xml.ws.handler.MessageContext;
35 import java.util.Collection; 34 import java.util.Collection;
36 import java.util.HashMap; 35 import java.util.HashMap;
37 import java.util.Map; 36 import java.util.Map;
38 import java.util.Set; 37 import java.util.Set;
39 38
39
40 /** 40 /**
41 * 41 *
42 * @author WS Development Team 42 * @author WS Development Team
43 */ 43 */
44 44
45 class MessageContextImpl implements MessageContext { 45 class MessageContextImpl implements MessageContext {
46 private Map<String,Object> fallbackMap = null; 46 private final Set<String> handlerScopeProps;
47 private Set<String> handlerScopeProps; 47 private final Packet packet;
48 Packet packet; 48 private final Map<String, Object> asMapIncludingInvocationProperties;
49 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 */ 50 /** Creates a new instance of MessageContextImpl */
59 public MessageContextImpl(Packet packet) { 51 public MessageContextImpl(Packet packet) {
60 this.packet = packet; 52 this.packet = packet;
61 handlerScopeProps = packet.getHandlerScopePropertyNames(false); 53 this.asMapIncludingInvocationProperties = packet.asMapIncludingInvocationProperties();
54 this.handlerScopeProps = packet.getHandlerScopePropertyNames(false);
62 } 55 }
56
63 protected void updatePacket() { 57 protected void updatePacket() {
64 throw new UnsupportedOperationException("wrong call"); 58 throw new UnsupportedOperationException("wrong call");
65 } 59 }
66 60
67 public void setScope(String name, Scope scope) { 61 public void setScope(String name, Scope scope) {
84 return Scope.APPLICATION; 78 return Scope.APPLICATION;
85 } 79 }
86 } 80 }
87 81
88 public int size() { 82 public int size() {
89 fallback(); 83 return asMapIncludingInvocationProperties.size();
90 return fallbackMap.size();
91 } 84 }
92 85
93 public boolean isEmpty() { 86 public boolean isEmpty() {
94 fallback(); 87 return asMapIncludingInvocationProperties.isEmpty();
95 return fallbackMap.isEmpty();
96 } 88 }
97 89
98 public boolean containsKey(Object key) { 90 public boolean containsKey(Object key) {
99 if(fallbackMap == null) { 91 return asMapIncludingInvocationProperties.containsKey(key);
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 } 92 }
108 93
109 public boolean containsValue(Object value) { 94 public boolean containsValue(Object value) {
110 fallback(); 95 return asMapIncludingInvocationProperties.containsValue(value);
111 return fallbackMap.containsValue(value);
112 } 96 }
113 97
114 public Object put(String key, Object value) { 98 public Object put(String key, Object value) {
115 if (fallbackMap == null) { 99 if (!asMapIncludingInvocationProperties.containsKey(key)) {
116 if (packet.supports(key)) { 100 //new property, default to Scope.HANDLER
117 return packet.put(key, value); // strongly typed 101 handlerScopeProps.add(key);
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 } 102 }
103 return asMapIncludingInvocationProperties.put(key, value);
133 } 104 }
134 public Object get(Object key) { 105 public Object get(Object key) {
135 if(key == null) 106 if(key == null)
136 return null; 107 return null;
137 Object value; 108 Object value = asMapIncludingInvocationProperties.get(key);
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 109 //add the attachments from the Message to the corresponding attachment property
149 if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) || 110 if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) ||
150 key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){ 111 key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){
151 Map<String, DataHandler> atts = (Map<String, DataHandler>) value; 112 Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
152 if(atts == null) 113 if(atts == null)
153 atts = new HashMap<String, DataHandler>(); 114 atts = new HashMap<String, DataHandler>();
154 AttachmentSet attSet = packet.getMessage().getAttachments(); 115 AttachmentSet attSet = packet.getMessage().getAttachments();
155 for(Attachment att : attSet){ 116 for(Attachment att : attSet){
156 atts.put(att.getContentId(), att.asDataHandler()); 117 String cid = att.getContentId();
118 if (cid.indexOf("@jaxws.sun.com") == -1) {
119 Object a = atts.get(cid);
120 if (a == null) {
121 a = atts.get("<" + cid + ">");
122 if (a == null) atts.put(att.getContentId(), att.asDataHandler());
123 }
124 } else {
125 atts.put(att.getContentId(), att.asDataHandler());
126 }
157 } 127 }
158 return atts; 128 return atts;
159 } 129 }
160 return value; 130 return value;
161 } 131 }
162 132
163 public void putAll(Map<? extends String, ? extends Object> t) { 133 public void putAll(Map<? extends String, ? extends Object> t) {
164 fallback();
165 for(String key: t.keySet()) { 134 for(String key: t.keySet()) {
166 if(!fallbackMap.containsKey(key)) { 135 if(!asMapIncludingInvocationProperties.containsKey(key)) {
167 //new property, default to Scope.HANDLER 136 //new property, default to Scope.HANDLER
168 handlerScopeProps.add(key); 137 handlerScopeProps.add(key);
169 } 138 }
170 } 139 }
171 fallbackMap.putAll(t); 140 asMapIncludingInvocationProperties.putAll(t);
172 } 141 }
173 142
174 public void clear() { 143 public void clear() {
175 fallback(); 144 asMapIncludingInvocationProperties.clear();
176 fallbackMap.clear();
177 } 145 }
178 public Object remove(Object key){ 146 public Object remove(Object key){
179 fallback();
180 handlerScopeProps.remove(key); 147 handlerScopeProps.remove(key);
181 return fallbackMap.remove(key); 148 return asMapIncludingInvocationProperties.remove(key);
182 } 149 }
183 public Set<String> keySet() { 150 public Set<String> keySet() {
184 fallback(); 151 return asMapIncludingInvocationProperties.keySet();
185 return fallbackMap.keySet();
186 } 152 }
187 public Set<Map.Entry<String, Object>> entrySet(){ 153 public Set<Map.Entry<String, Object>> entrySet(){
188 fallback(); 154 return asMapIncludingInvocationProperties.entrySet();
189 return fallbackMap.entrySet();
190 } 155 }
191 public Collection<Object> values() { 156 public Collection<Object> values() {
192 fallback(); 157 return asMapIncludingInvocationProperties.values();
193 return fallbackMap.values();
194 } 158 }
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 } 159 }

mercurial