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: package com.sun.xml.ws.server;
037:
038: import com.sun.xml.ws.api.message.Packet;
039: import com.sun.xml.ws.api.message.AttachmentSet;
040: import com.sun.xml.ws.api.message.Attachment;
041:
042: import java.util.*;
043: import javax.xml.ws.handler.MessageContext;
044: import javax.xml.ws.WebServiceContext;
045: import javax.activation.DataHandler;
046:
047: /**
048: * Implements {@link WebServiceContext}'s {@link MessageContext} on top of {@link Packet}.
049: *
050: * <p>
051: * This class creates a {@link Map} view for APPLICATION scoped properties that
052: * gets exposed to endpoint implementations during the invocation
053: * of web methods. The implementations access this map using
054: * WebServiceContext.getMessageContext().
055: *
056: * <p>
057: * Some of the {@link Map} methods requre this class to
058: * build the complete {@link Set} of properties, but we
059: * try to avoid that as much as possible.
060: *
061: *
062: * @author Jitendra Kotamraju
063: */
064: @SuppressWarnings({"SuspiciousMethodCalls"})
065: public final class EndpointMessageContextImpl extends
066: AbstractMap<String, Object> implements MessageContext {
067:
068: /**
069: * Lazily computed.
070: */
071: private Set<Map.Entry<String, Object>> entrySet;
072: private final Packet packet;
073:
074: /**
075: * @param packet
076: * The {@link Packet} to wrap.
077: */
078: public EndpointMessageContextImpl(Packet packet) {
079: this .packet = packet;
080: }
081:
082: @Override
083: public Object get(Object key) {
084: if (packet.supports(key)) {
085: return packet.get(key); // strongly typed
086: }
087: if (packet.getHandlerScopePropertyNames(true).contains(key)) {
088: return null; // no such application-scope property
089: }
090: Object value = packet.invocationProperties.get(key);
091:
092: //add the attachments from the Message to the corresponding attachment property
093: if (key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS)
094: || key
095: .equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)) {
096: Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
097: if (atts == null)
098: atts = new HashMap<String, DataHandler>();
099: AttachmentSet attSet = packet.getMessage().getAttachments();
100: for (Attachment att : attSet) {
101: atts.put(att.getContentId(), att.asDataHandler());
102: }
103: return atts;
104: }
105: return value;
106: }
107:
108: @Override
109: public Object put(String key, Object value) {
110: if (packet.supports(key)) {
111: return packet.put(key, value); // strongly typed
112: }
113: Object old = packet.invocationProperties.get(key);
114: if (old != null) {
115: if (packet.getHandlerScopePropertyNames(true).contains(key)) {
116: throw new IllegalArgumentException(
117: "Cannot overwrite property in HANDLER scope");
118: }
119: // Overwrite existing APPLICATION scoped property
120: packet.invocationProperties.put(key, value);
121: return old;
122: }
123: // No existing property. So Add a new property
124: packet.invocationProperties.put(key, value);
125: return null;
126: }
127:
128: @Override
129: public Object remove(Object key) {
130: if (packet.supports(key)) {
131: return packet.remove(key);
132: }
133: Object old = packet.invocationProperties.get(key);
134: if (old != null) {
135: if (packet.getHandlerScopePropertyNames(true).contains(key)) {
136: throw new IllegalArgumentException(
137: "Cannot remove property in HANDLER scope");
138: }
139: // Remove existing APPLICATION scoped property
140: packet.invocationProperties.remove(key);
141: return old;
142: }
143: // No existing property.
144: return null;
145: }
146:
147: public Set<Map.Entry<String, Object>> entrySet() {
148: if (entrySet == null) {
149: entrySet = new EntrySet();
150: }
151: return entrySet;
152: }
153:
154: public void setScope(String name, MessageContext.Scope scope) {
155: throw new UnsupportedOperationException(
156: "All the properties in this context are in APPLICATION scope. Cannot do setScope().");
157: }
158:
159: public MessageContext.Scope getScope(String name) {
160: throw new UnsupportedOperationException(
161: "All the properties in this context are in APPLICATION scope. Cannot do getScope().");
162: }
163:
164: private class EntrySet extends
165: AbstractSet<Map.Entry<String, Object>> {
166:
167: public Iterator<Map.Entry<String, Object>> iterator() {
168: final Iterator<Map.Entry<String, Object>> it = createBackupMap()
169: .entrySet().iterator();
170:
171: return new Iterator<Map.Entry<String, Object>>() {
172: Map.Entry<String, Object> cur;
173:
174: public boolean hasNext() {
175: return it.hasNext();
176: }
177:
178: public Map.Entry<String, Object> next() {
179: cur = it.next();
180: return cur;
181: }
182:
183: public void remove() {
184: it.remove();
185: EndpointMessageContextImpl.this
186: .remove(cur.getKey());
187: }
188: };
189: }
190:
191: public int size() {
192: return createBackupMap().size();
193: }
194:
195: }
196:
197: private Map<String, Object> createBackupMap() {
198: Map<String, Object> backupMap = new HashMap<String, Object>();
199: backupMap.putAll(packet.createMapView());
200: Set<String> handlerProps = packet
201: .getHandlerScopePropertyNames(true);
202: for (Map.Entry<String, Object> e : packet.invocationProperties
203: .entrySet()) {
204: if (!handlerProps.contains(e.getKey())) {
205: backupMap.put(e.getKey(), e.getValue());
206: }
207: }
208: return backupMap;
209: }
210:
211: }
|