Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
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 */
26 package com.sun.xml.internal.ws.server;
28 import com.sun.xml.internal.ws.api.message.Packet;
29 import com.sun.xml.internal.ws.api.message.AttachmentSet;
30 import com.sun.xml.internal.ws.api.message.Attachment;
32 import java.util.*;
33 import javax.xml.ws.handler.MessageContext;
34 import javax.xml.ws.WebServiceContext;
35 import javax.activation.DataHandler;
37 /**
38 * Implements {@link WebServiceContext}'s {@link MessageContext} on top of {@link Packet}.
39 *
40 * <p>
41 * This class creates a {@link Map} view for APPLICATION scoped properties that
42 * gets exposed to endpoint implementations during the invocation
43 * of web methods. The implementations access this map using
44 * WebServiceContext.getMessageContext().
45 *
46 * <p>
47 * Some of the {@link Map} methods requre this class to
48 * build the complete {@link Set} of properties, but we
49 * try to avoid that as much as possible.
50 *
51 *
52 * @author Jitendra Kotamraju
53 */
54 @SuppressWarnings({"SuspiciousMethodCalls"})
55 public final class EndpointMessageContextImpl extends AbstractMap<String,Object> implements MessageContext {
57 /**
58 * Lazily computed.
59 */
60 private Set<Map.Entry<String,Object>> entrySet;
61 private final Packet packet;
63 /**
64 * @param packet
65 * The {@link Packet} to wrap.
66 */
67 public EndpointMessageContextImpl(Packet packet) {
68 this.packet = packet;
69 }
71 @Override
72 public Object get(Object key) {
73 if (packet.supports(key)) {
74 return packet.get(key); // strongly typed
75 }
76 if (packet.getHandlerScopePropertyNames(true).contains(key)) {
77 return null; // no such application-scope property
78 }
79 Object value = packet.invocationProperties.get(key);
81 //add the attachments from the Message to the corresponding attachment property
82 if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) ||
83 key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){
84 Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
85 if(atts == null)
86 atts = new HashMap<String, DataHandler>();
87 AttachmentSet attSet = packet.getMessage().getAttachments();
88 for(Attachment att : attSet){
89 atts.put(att.getContentId(), att.asDataHandler());
90 }
91 return atts;
92 }
93 return value;
94 }
96 @Override
97 public Object put(String key, Object value) {
98 if (packet.supports(key)) {
99 return packet.put(key, value); // strongly typed
100 }
101 Object old = packet.invocationProperties.get(key);
102 if (old != null) {
103 if (packet.getHandlerScopePropertyNames(true).contains(key)) {
104 throw new IllegalArgumentException("Cannot overwrite property in HANDLER scope");
105 }
106 // Overwrite existing APPLICATION scoped property
107 packet.invocationProperties.put(key, value);
108 return old;
109 }
110 // No existing property. So Add a new property
111 packet.invocationProperties.put(key, value);
112 return null;
113 }
115 @Override
116 public Object remove(Object key) {
117 if (packet.supports(key)) {
118 return packet.remove(key);
119 }
120 Object old = packet.invocationProperties.get(key);
121 if (old != null) {
122 if (packet.getHandlerScopePropertyNames(true).contains(key)) {
123 throw new IllegalArgumentException("Cannot remove property in HANDLER scope");
124 }
125 // Remove existing APPLICATION scoped property
126 packet.invocationProperties.remove(key);
127 return old;
128 }
129 // No existing property.
130 return null;
131 }
133 public Set<Map.Entry<String, Object>> entrySet() {
134 if (entrySet == null) {
135 entrySet = new EntrySet();
136 }
137 return entrySet;
138 }
140 public void setScope(String name, MessageContext.Scope scope) {
141 throw new UnsupportedOperationException(
142 "All the properties in this context are in APPLICATION scope. Cannot do setScope().");
143 }
145 public MessageContext.Scope getScope(String name) {
146 throw new UnsupportedOperationException(
147 "All the properties in this context are in APPLICATION scope. Cannot do getScope().");
148 }
150 private class EntrySet extends AbstractSet<Map.Entry<String, Object>> {
152 public Iterator<Map.Entry<String, Object>> iterator() {
153 final Iterator<Map.Entry<String, Object>> it = createBackupMap().entrySet().iterator();
155 return new Iterator<Map.Entry<String, Object>>() {
156 Map.Entry<String, Object> cur;
158 public boolean hasNext() {
159 return it.hasNext();
160 }
162 public Map.Entry<String, Object> next() {
163 cur = it.next();
164 return cur;
165 }
167 public void remove() {
168 it.remove();
169 EndpointMessageContextImpl.this.remove(cur.getKey());
170 }
171 };
172 }
174 public int size() {
175 return createBackupMap().size();
176 }
178 }
180 private Map<String, Object> createBackupMap() {
181 Map<String, Object> backupMap = new HashMap<String, Object>();
182 backupMap.putAll(packet.createMapView());
183 Set<String> handlerProps = packet.getHandlerScopePropertyNames(true);
184 for(Map.Entry<String, Object> e : packet.invocationProperties.entrySet()) {
185 if (!handlerProps.contains(e.getKey())) {
186 backupMap.put(e.getKey(), e.getValue());
187 }
188 }
189 return backupMap;
190 }
192 }