ohair@286: /* alanb@368: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.server; ohair@286: ohair@286: import com.sun.xml.internal.ws.api.message.Packet; ohair@286: import com.sun.xml.internal.ws.api.message.AttachmentSet; ohair@286: import com.sun.xml.internal.ws.api.message.Attachment; ohair@286: ohair@286: import java.util.*; ohair@286: import javax.xml.ws.handler.MessageContext; ohair@286: import javax.xml.ws.WebServiceContext; ohair@286: import javax.activation.DataHandler; ohair@286: ohair@286: /** ohair@286: * Implements {@link WebServiceContext}'s {@link MessageContext} on top of {@link Packet}. ohair@286: * ohair@286: *

ohair@286: * This class creates a {@link Map} view for APPLICATION scoped properties that ohair@286: * gets exposed to endpoint implementations during the invocation ohair@286: * of web methods. The implementations access this map using ohair@286: * WebServiceContext.getMessageContext(). ohair@286: * ohair@286: *

ohair@286: * Some of the {@link Map} methods requre this class to ohair@286: * build the complete {@link Set} of properties, but we ohair@286: * try to avoid that as much as possible. ohair@286: * ohair@286: * ohair@286: * @author Jitendra Kotamraju ohair@286: */ ohair@286: public final class EndpointMessageContextImpl extends AbstractMap implements MessageContext { ohair@286: ohair@286: /** ohair@286: * Lazily computed. ohair@286: */ ohair@286: private Set> entrySet; ohair@286: private final Packet packet; ohair@286: ohair@286: /** ohair@286: * @param packet ohair@286: * The {@link Packet} to wrap. ohair@286: */ ohair@286: public EndpointMessageContextImpl(Packet packet) { ohair@286: this.packet = packet; ohair@286: } ohair@286: ohair@286: @Override alanb@368: @SuppressWarnings("element-type-mismatch") ohair@286: public Object get(Object key) { ohair@286: if (packet.supports(key)) { ohair@286: return packet.get(key); // strongly typed ohair@286: } ohair@286: if (packet.getHandlerScopePropertyNames(true).contains(key)) { ohair@286: return null; // no such application-scope property ohair@286: } ohair@286: Object value = packet.invocationProperties.get(key); ohair@286: ohair@286: //add the attachments from the Message to the corresponding attachment property ohair@286: if(key.equals(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS) || ohair@286: key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){ ohair@286: Map atts = (Map) value; ohair@286: if(atts == null) ohair@286: atts = new HashMap(); ohair@286: AttachmentSet attSet = packet.getMessage().getAttachments(); ohair@286: for(Attachment att : attSet){ ohair@286: atts.put(att.getContentId(), att.asDataHandler()); ohair@286: } ohair@286: return atts; ohair@286: } ohair@286: return value; ohair@286: } ohair@286: ohair@286: @Override ohair@286: public Object put(String key, Object value) { ohair@286: if (packet.supports(key)) { ohair@286: return packet.put(key, value); // strongly typed ohair@286: } ohair@286: Object old = packet.invocationProperties.get(key); ohair@286: if (old != null) { ohair@286: if (packet.getHandlerScopePropertyNames(true).contains(key)) { ohair@286: throw new IllegalArgumentException("Cannot overwrite property in HANDLER scope"); ohair@286: } ohair@286: // Overwrite existing APPLICATION scoped property ohair@286: packet.invocationProperties.put(key, value); ohair@286: return old; ohair@286: } ohair@286: // No existing property. So Add a new property ohair@286: packet.invocationProperties.put(key, value); ohair@286: return null; ohair@286: } ohair@286: ohair@286: @Override alanb@368: @SuppressWarnings("element-type-mismatch") ohair@286: public Object remove(Object key) { ohair@286: if (packet.supports(key)) { ohair@286: return packet.remove(key); ohair@286: } ohair@286: Object old = packet.invocationProperties.get(key); ohair@286: if (old != null) { ohair@286: if (packet.getHandlerScopePropertyNames(true).contains(key)) { ohair@286: throw new IllegalArgumentException("Cannot remove property in HANDLER scope"); ohair@286: } ohair@286: // Remove existing APPLICATION scoped property ohair@286: packet.invocationProperties.remove(key); ohair@286: return old; ohair@286: } ohair@286: // No existing property. ohair@286: return null; ohair@286: } ohair@286: ohair@286: public Set> entrySet() { ohair@286: if (entrySet == null) { ohair@286: entrySet = new EntrySet(); ohair@286: } ohair@286: return entrySet; ohair@286: } ohair@286: ohair@286: public void setScope(String name, MessageContext.Scope scope) { ohair@286: throw new UnsupportedOperationException( ohair@286: "All the properties in this context are in APPLICATION scope. Cannot do setScope()."); ohair@286: } ohair@286: ohair@286: public MessageContext.Scope getScope(String name) { ohair@286: throw new UnsupportedOperationException( ohair@286: "All the properties in this context are in APPLICATION scope. Cannot do getScope()."); ohair@286: } ohair@286: ohair@286: private class EntrySet extends AbstractSet> { ohair@286: ohair@286: public Iterator> iterator() { ohair@286: final Iterator> it = createBackupMap().entrySet().iterator(); ohair@286: ohair@286: return new Iterator>() { ohair@286: Map.Entry cur; ohair@286: ohair@286: public boolean hasNext() { ohair@286: return it.hasNext(); ohair@286: } ohair@286: ohair@286: public Map.Entry next() { ohair@286: cur = it.next(); ohair@286: return cur; ohair@286: } ohair@286: ohair@286: public void remove() { ohair@286: it.remove(); ohair@286: EndpointMessageContextImpl.this.remove(cur.getKey()); ohair@286: } ohair@286: }; ohair@286: } ohair@286: ohair@286: public int size() { ohair@286: return createBackupMap().size(); ohair@286: } ohair@286: ohair@286: } ohair@286: ohair@286: private Map createBackupMap() { ohair@286: Map backupMap = new HashMap(); ohair@286: backupMap.putAll(packet.createMapView()); ohair@286: Set handlerProps = packet.getHandlerScopePropertyNames(true); ohair@286: for(Map.Entry e : packet.invocationProperties.entrySet()) { ohair@286: if (!handlerProps.contains(e.getKey())) { ohair@286: backupMap.put(e.getKey(), e.getValue()); ohair@286: } ohair@286: } ohair@286: return backupMap; ohair@286: } ohair@286: ohair@286: }