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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, 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 @SuppressWarnings({"SuspiciousMethodCalls"})
ohair@286 55 public final class EndpointMessageContextImpl extends AbstractMap<String,Object> implements MessageContext {
ohair@286 56
ohair@286 57 /**
ohair@286 58 * Lazily computed.
ohair@286 59 */
ohair@286 60 private Set<Map.Entry<String,Object>> entrySet;
ohair@286 61 private final Packet packet;
ohair@286 62
ohair@286 63 /**
ohair@286 64 * @param packet
ohair@286 65 * The {@link Packet} to wrap.
ohair@286 66 */
ohair@286 67 public EndpointMessageContextImpl(Packet packet) {
ohair@286 68 this.packet = packet;
ohair@286 69 }
ohair@286 70
ohair@286 71 @Override
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
ohair@286 116 public Object remove(Object key) {
ohair@286 117 if (packet.supports(key)) {
ohair@286 118 return packet.remove(key);
ohair@286 119 }
ohair@286 120 Object old = packet.invocationProperties.get(key);
ohair@286 121 if (old != null) {
ohair@286 122 if (packet.getHandlerScopePropertyNames(true).contains(key)) {
ohair@286 123 throw new IllegalArgumentException("Cannot remove property in HANDLER scope");
ohair@286 124 }
ohair@286 125 // Remove existing APPLICATION scoped property
ohair@286 126 packet.invocationProperties.remove(key);
ohair@286 127 return old;
ohair@286 128 }
ohair@286 129 // No existing property.
ohair@286 130 return null;
ohair@286 131 }
ohair@286 132
ohair@286 133 public Set<Map.Entry<String, Object>> entrySet() {
ohair@286 134 if (entrySet == null) {
ohair@286 135 entrySet = new EntrySet();
ohair@286 136 }
ohair@286 137 return entrySet;
ohair@286 138 }
ohair@286 139
ohair@286 140 public void setScope(String name, MessageContext.Scope scope) {
ohair@286 141 throw new UnsupportedOperationException(
ohair@286 142 "All the properties in this context are in APPLICATION scope. Cannot do setScope().");
ohair@286 143 }
ohair@286 144
ohair@286 145 public MessageContext.Scope getScope(String name) {
ohair@286 146 throw new UnsupportedOperationException(
ohair@286 147 "All the properties in this context are in APPLICATION scope. Cannot do getScope().");
ohair@286 148 }
ohair@286 149
ohair@286 150 private class EntrySet extends AbstractSet<Map.Entry<String, Object>> {
ohair@286 151
ohair@286 152 public Iterator<Map.Entry<String, Object>> iterator() {
ohair@286 153 final Iterator<Map.Entry<String, Object>> it = createBackupMap().entrySet().iterator();
ohair@286 154
ohair@286 155 return new Iterator<Map.Entry<String, Object>>() {
ohair@286 156 Map.Entry<String, Object> cur;
ohair@286 157
ohair@286 158 public boolean hasNext() {
ohair@286 159 return it.hasNext();
ohair@286 160 }
ohair@286 161
ohair@286 162 public Map.Entry<String, Object> next() {
ohair@286 163 cur = it.next();
ohair@286 164 return cur;
ohair@286 165 }
ohair@286 166
ohair@286 167 public void remove() {
ohair@286 168 it.remove();
ohair@286 169 EndpointMessageContextImpl.this.remove(cur.getKey());
ohair@286 170 }
ohair@286 171 };
ohair@286 172 }
ohair@286 173
ohair@286 174 public int size() {
ohair@286 175 return createBackupMap().size();
ohair@286 176 }
ohair@286 177
ohair@286 178 }
ohair@286 179
ohair@286 180 private Map<String, Object> createBackupMap() {
ohair@286 181 Map<String, Object> backupMap = new HashMap<String, Object>();
ohair@286 182 backupMap.putAll(packet.createMapView());
ohair@286 183 Set<String> handlerProps = packet.getHandlerScopePropertyNames(true);
ohair@286 184 for(Map.Entry<String, Object> e : packet.invocationProperties.entrySet()) {
ohair@286 185 if (!handlerProps.contains(e.getKey())) {
ohair@286 186 backupMap.put(e.getKey(), e.getValue());
ohair@286 187 }
ohair@286 188 }
ohair@286 189 return backupMap;
ohair@286 190 }
ohair@286 191
ohair@286 192 }

mercurial