325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.server;
325N/A
325N/Aimport com.sun.xml.internal.ws.api.message.Packet;
325N/Aimport com.sun.xml.internal.ws.api.message.AttachmentSet;
325N/Aimport com.sun.xml.internal.ws.api.message.Attachment;
325N/A
325N/Aimport java.util.*;
325N/Aimport javax.xml.ws.handler.MessageContext;
325N/Aimport javax.xml.ws.WebServiceContext;
325N/Aimport javax.activation.DataHandler;
325N/A
325N/A/**
325N/A * Implements {@link WebServiceContext}'s {@link MessageContext} on top of {@link Packet}.
325N/A *
325N/A * <p>
325N/A * This class creates a {@link Map} view for APPLICATION scoped properties that
325N/A * gets exposed to endpoint implementations during the invocation
325N/A * of web methods. The implementations access this map using
325N/A * WebServiceContext.getMessageContext().
325N/A *
325N/A * <p>
325N/A * Some of the {@link Map} methods requre this class to
325N/A * build the complete {@link Set} of properties, but we
325N/A * try to avoid that as much as possible.
325N/A *
325N/A *
325N/A * @author Jitendra Kotamraju
325N/A */
325N/A@SuppressWarnings({"SuspiciousMethodCalls"})
325N/Apublic final class EndpointMessageContextImpl extends AbstractMap<String,Object> implements MessageContext {
325N/A
325N/A /**
325N/A * Lazily computed.
325N/A */
325N/A private Set<Map.Entry<String,Object>> entrySet;
325N/A private final Packet packet;
325N/A
325N/A /**
325N/A * @param packet
325N/A * The {@link Packet} to wrap.
325N/A */
325N/A public EndpointMessageContextImpl(Packet packet) {
325N/A this.packet = packet;
325N/A }
325N/A
325N/A @Override
325N/A public Object get(Object key) {
325N/A if (packet.supports(key)) {
325N/A return packet.get(key); // strongly typed
325N/A }
325N/A if (packet.getHandlerScopePropertyNames(true).contains(key)) {
325N/A return null; // no such application-scope property
325N/A }
325N/A Object value = packet.invocationProperties.get(key);
325N/A
325N/A //add the attachments from the Message to the corresponding attachment property
325N/A if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) ||
325N/A key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){
325N/A Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
325N/A if(atts == null)
325N/A atts = new HashMap<String, DataHandler>();
325N/A AttachmentSet attSet = packet.getMessage().getAttachments();
325N/A for(Attachment att : attSet){
325N/A atts.put(att.getContentId(), att.asDataHandler());
325N/A }
325N/A return atts;
325N/A }
325N/A return value;
325N/A }
325N/A
325N/A @Override
325N/A public Object put(String key, Object value) {
325N/A if (packet.supports(key)) {
325N/A return packet.put(key, value); // strongly typed
325N/A }
325N/A Object old = packet.invocationProperties.get(key);
325N/A if (old != null) {
325N/A if (packet.getHandlerScopePropertyNames(true).contains(key)) {
325N/A throw new IllegalArgumentException("Cannot overwrite property in HANDLER scope");
325N/A }
325N/A // Overwrite existing APPLICATION scoped property
325N/A packet.invocationProperties.put(key, value);
325N/A return old;
325N/A }
325N/A // No existing property. So Add a new property
325N/A packet.invocationProperties.put(key, value);
325N/A return null;
325N/A }
325N/A
325N/A @Override
325N/A public Object remove(Object key) {
325N/A if (packet.supports(key)) {
325N/A return packet.remove(key);
325N/A }
325N/A Object old = packet.invocationProperties.get(key);
325N/A if (old != null) {
325N/A if (packet.getHandlerScopePropertyNames(true).contains(key)) {
325N/A throw new IllegalArgumentException("Cannot remove property in HANDLER scope");
325N/A }
325N/A // Remove existing APPLICATION scoped property
325N/A packet.invocationProperties.remove(key);
325N/A return old;
325N/A }
325N/A // No existing property.
325N/A return null;
325N/A }
325N/A
325N/A public Set<Map.Entry<String, Object>> entrySet() {
325N/A if (entrySet == null) {
325N/A entrySet = new EntrySet();
325N/A }
325N/A return entrySet;
325N/A }
325N/A
325N/A public void setScope(String name, MessageContext.Scope scope) {
325N/A throw new UnsupportedOperationException(
325N/A "All the properties in this context are in APPLICATION scope. Cannot do setScope().");
325N/A }
325N/A
325N/A public MessageContext.Scope getScope(String name) {
325N/A throw new UnsupportedOperationException(
325N/A "All the properties in this context are in APPLICATION scope. Cannot do getScope().");
325N/A }
325N/A
325N/A private class EntrySet extends AbstractSet<Map.Entry<String, Object>> {
325N/A
325N/A public Iterator<Map.Entry<String, Object>> iterator() {
325N/A final Iterator<Map.Entry<String, Object>> it = createBackupMap().entrySet().iterator();
325N/A
325N/A return new Iterator<Map.Entry<String, Object>>() {
325N/A Map.Entry<String, Object> cur;
325N/A
325N/A public boolean hasNext() {
325N/A return it.hasNext();
325N/A }
325N/A
325N/A public Map.Entry<String, Object> next() {
325N/A cur = it.next();
325N/A return cur;
325N/A }
325N/A
325N/A public void remove() {
325N/A it.remove();
325N/A EndpointMessageContextImpl.this.remove(cur.getKey());
325N/A }
325N/A };
325N/A }
325N/A
325N/A public int size() {
325N/A return createBackupMap().size();
325N/A }
325N/A
325N/A }
325N/A
325N/A private Map<String, Object> createBackupMap() {
325N/A Map<String, Object> backupMap = new HashMap<String, Object>();
325N/A backupMap.putAll(packet.createMapView());
325N/A Set<String> handlerProps = packet.getHandlerScopePropertyNames(true);
325N/A for(Map.Entry<String, Object> e : packet.invocationProperties.entrySet()) {
325N/A if (!handlerProps.contains(e.getKey())) {
325N/A backupMap.put(e.getKey(), e.getValue());
325N/A }
325N/A }
325N/A return backupMap;
325N/A }
325N/A
325N/A}