src/share/jaxws_classes/com/sun/xml/internal/ws/server/EndpointMessageContextImpl.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.ws.server;
ohair@286 27
ohair@286 28 import com.sun.xml.internal.ws.api.message.Packet;
ohair@286 29 import com.sun.xml.internal.ws.api.message.AttachmentSet;
ohair@286 30 import com.sun.xml.internal.ws.api.message.Attachment;
ohair@286 31
ohair@286 32 import java.util.*;
ohair@286 33 import javax.xml.ws.handler.MessageContext;
ohair@286 34 import javax.xml.ws.WebServiceContext;
ohair@286 35 import javax.activation.DataHandler;
ohair@286 36
ohair@286 37 /**
ohair@286 38 * Implements {@link WebServiceContext}'s {@link MessageContext} on top of {@link Packet}.
ohair@286 39 *
ohair@286 40 * <p>
ohair@286 41 * This class creates a {@link Map} view for APPLICATION scoped properties that
ohair@286 42 * gets exposed to endpoint implementations during the invocation
ohair@286 43 * of web methods. The implementations access this map using
ohair@286 44 * WebServiceContext.getMessageContext().
ohair@286 45 *
ohair@286 46 * <p>
ohair@286 47 * Some of the {@link Map} methods requre this class to
ohair@286 48 * build the complete {@link Set} of properties, but we
ohair@286 49 * try to avoid that as much as possible.
ohair@286 50 *
ohair@286 51 *
ohair@286 52 * @author Jitendra Kotamraju
ohair@286 53 */
ohair@286 54 public final class EndpointMessageContextImpl extends AbstractMap<String,Object> implements MessageContext {
ohair@286 55
ohair@286 56 /**
ohair@286 57 * Lazily computed.
ohair@286 58 */
ohair@286 59 private Set<Map.Entry<String,Object>> entrySet;
ohair@286 60 private final Packet packet;
ohair@286 61
ohair@286 62 /**
ohair@286 63 * @param packet
ohair@286 64 * The {@link Packet} to wrap.
ohair@286 65 */
ohair@286 66 public EndpointMessageContextImpl(Packet packet) {
ohair@286 67 this.packet = packet;
ohair@286 68 }
ohair@286 69
ohair@286 70 @Override
alanb@368 71 @SuppressWarnings("element-type-mismatch")
ohair@286 72 public Object get(Object key) {
ohair@286 73 if (packet.supports(key)) {
ohair@286 74 return packet.get(key); // strongly typed
ohair@286 75 }
ohair@286 76 if (packet.getHandlerScopePropertyNames(true).contains(key)) {
ohair@286 77 return null; // no such application-scope property
ohair@286 78 }
ohair@286 79 Object value = packet.invocationProperties.get(key);
ohair@286 80
ohair@286 81 //add the attachments from the Message to the corresponding attachment property
ohair@286 82 if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) ||
ohair@286 83 key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){
ohair@286 84 Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
ohair@286 85 if(atts == null)
ohair@286 86 atts = new HashMap<String, DataHandler>();
ohair@286 87 AttachmentSet attSet = packet.getMessage().getAttachments();
ohair@286 88 for(Attachment att : attSet){
ohair@286 89 atts.put(att.getContentId(), att.asDataHandler());
ohair@286 90 }
ohair@286 91 return atts;
ohair@286 92 }
ohair@286 93 return value;
ohair@286 94 }
ohair@286 95
ohair@286 96 @Override
ohair@286 97 public Object put(String key, Object value) {
ohair@286 98 if (packet.supports(key)) {
ohair@286 99 return packet.put(key, value); // strongly typed
ohair@286 100 }
ohair@286 101 Object old = packet.invocationProperties.get(key);
ohair@286 102 if (old != null) {
ohair@286 103 if (packet.getHandlerScopePropertyNames(true).contains(key)) {
ohair@286 104 throw new IllegalArgumentException("Cannot overwrite property in HANDLER scope");
ohair@286 105 }
ohair@286 106 // Overwrite existing APPLICATION scoped property
ohair@286 107 packet.invocationProperties.put(key, value);
ohair@286 108 return old;
ohair@286 109 }
ohair@286 110 // No existing property. So Add a new property
ohair@286 111 packet.invocationProperties.put(key, value);
ohair@286 112 return null;
ohair@286 113 }
ohair@286 114
ohair@286 115 @Override
alanb@368 116 @SuppressWarnings("element-type-mismatch")
ohair@286 117 public Object remove(Object key) {
ohair@286 118 if (packet.supports(key)) {
ohair@286 119 return packet.remove(key);
ohair@286 120 }
ohair@286 121 Object old = packet.invocationProperties.get(key);
ohair@286 122 if (old != null) {
ohair@286 123 if (packet.getHandlerScopePropertyNames(true).contains(key)) {
ohair@286 124 throw new IllegalArgumentException("Cannot remove property in HANDLER scope");
ohair@286 125 }
ohair@286 126 // Remove existing APPLICATION scoped property
ohair@286 127 packet.invocationProperties.remove(key);
ohair@286 128 return old;
ohair@286 129 }
ohair@286 130 // No existing property.
ohair@286 131 return null;
ohair@286 132 }
ohair@286 133
ohair@286 134 public Set<Map.Entry<String, Object>> entrySet() {
ohair@286 135 if (entrySet == null) {
ohair@286 136 entrySet = new EntrySet();
ohair@286 137 }
ohair@286 138 return entrySet;
ohair@286 139 }
ohair@286 140
ohair@286 141 public void setScope(String name, MessageContext.Scope scope) {
ohair@286 142 throw new UnsupportedOperationException(
ohair@286 143 "All the properties in this context are in APPLICATION scope. Cannot do setScope().");
ohair@286 144 }
ohair@286 145
ohair@286 146 public MessageContext.Scope getScope(String name) {
ohair@286 147 throw new UnsupportedOperationException(
ohair@286 148 "All the properties in this context are in APPLICATION scope. Cannot do getScope().");
ohair@286 149 }
ohair@286 150
ohair@286 151 private class EntrySet extends AbstractSet<Map.Entry<String, Object>> {
ohair@286 152
ohair@286 153 public Iterator<Map.Entry<String, Object>> iterator() {
ohair@286 154 final Iterator<Map.Entry<String, Object>> it = createBackupMap().entrySet().iterator();
ohair@286 155
ohair@286 156 return new Iterator<Map.Entry<String, Object>>() {
ohair@286 157 Map.Entry<String, Object> cur;
ohair@286 158
ohair@286 159 public boolean hasNext() {
ohair@286 160 return it.hasNext();
ohair@286 161 }
ohair@286 162
ohair@286 163 public Map.Entry<String, Object> next() {
ohair@286 164 cur = it.next();
ohair@286 165 return cur;
ohair@286 166 }
ohair@286 167
ohair@286 168 public void remove() {
ohair@286 169 it.remove();
ohair@286 170 EndpointMessageContextImpl.this.remove(cur.getKey());
ohair@286 171 }
ohair@286 172 };
ohair@286 173 }
ohair@286 174
ohair@286 175 public int size() {
ohair@286 176 return createBackupMap().size();
ohair@286 177 }
ohair@286 178
ohair@286 179 }
ohair@286 180
ohair@286 181 private Map<String, Object> createBackupMap() {
ohair@286 182 Map<String, Object> backupMap = new HashMap<String, Object>();
ohair@286 183 backupMap.putAll(packet.createMapView());
ohair@286 184 Set<String> handlerProps = packet.getHandlerScopePropertyNames(true);
ohair@286 185 for(Map.Entry<String, Object> e : packet.invocationProperties.entrySet()) {
ohair@286 186 if (!handlerProps.contains(e.getKey())) {
ohair@286 187 backupMap.put(e.getKey(), e.getValue());
ohair@286 188 }
ohair@286 189 }
ohair@286 190 return backupMap;
ohair@286 191 }
ohair@286 192
ohair@286 193 }

mercurial