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

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 637
9c07ef4934dd
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

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

mercurial