File: CommonMessageContext.java

package info (click to toggle)
libjboss-web-services-java 0.0%2Bsvn5660%2Bdak2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 7,252 kB
  • ctags: 12,475
  • sloc: java: 79,207; xml: 29; makefile: 19; sh: 16
file content (418 lines) | stat: -rw-r--r-- 11,587 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.ws.core;

// $Id: CommonMessageContext.java 4767 2007-10-15 13:37:59Z heiko.braun@jboss.com $

import org.jboss.logging.Logger;
import org.jboss.ws.core.binding.SerializationContext;
import org.jboss.ws.core.soap.attachment.SwapableMemoryDataSource;
import org.jboss.ws.extensions.xop.XOPContext;
import org.jboss.ws.metadata.umdm.EndpointMetaData;
import org.jboss.ws.metadata.umdm.OperationMetaData;
import org.jboss.xb.binding.NamespaceRegistry;

import javax.xml.soap.AttachmentPart;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext.Scope;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * The common JAXRPC/JAXWS MessageContext
 * 
 * @author Thomas.Diesler@jboss.org
 * @since 1-Sep-2006
 */
public abstract class CommonMessageContext implements Map<String, Object>
{
   private static Logger log = Logger.getLogger(CommonMessageContext.class);

   // expandToDOM in the SOAPContentElement should not happen during normal operation 
   // This property should be set the message context when it is ok to do so.
   public static String ALLOW_EXPAND_TO_DOM = "org.jboss.ws.allow.expand.dom";

   public static String REMOTING_METADATA = "org.jboss.ws.remoting.metadata";

   // The serialization context for this message ctx
   private SerializationContext serContext;
   // The operation for this message ctx
   private EndpointMetaData epMetaData;
   // The operation for this message ctx
   private OperationMetaData opMetaData;
   // The Message in this message context
   private MessageAbstraction message;
   // The map of scoped properties
   protected Map<String, ScopedProperty> scopedProps = new HashMap<String, ScopedProperty>();
   // The current property scope
   protected Scope currentScope = Scope.APPLICATION;

   private boolean isModified;

   public CommonMessageContext()
   {
   }

   // Copy constructor
   public CommonMessageContext(CommonMessageContext msgContext)
   {
      this.epMetaData = msgContext.epMetaData;
      this.opMetaData = msgContext.opMetaData;
      this.message = msgContext.message;
      this.serContext = msgContext.serContext;
      this.scopedProps = new HashMap<String, ScopedProperty>(msgContext.scopedProps);
      this.currentScope = msgContext.currentScope;
   }

   public Scope getCurrentScope()
   {
      return currentScope;
   }

   public void setCurrentScope(Scope currentScope)
   {
      this.currentScope = currentScope;
   }

   public EndpointMetaData getEndpointMetaData()
   {
      if (epMetaData == null && opMetaData != null)
         epMetaData = opMetaData.getEndpointMetaData();

      return epMetaData;
   }

   public void setEndpointMetaData(EndpointMetaData epMetaData)
   {
      this.epMetaData = epMetaData;
   }

   public OperationMetaData getOperationMetaData()
   {
      return opMetaData;
   }

   public void setOperationMetaData(OperationMetaData opMetaData)
   {
      this.opMetaData = opMetaData;
   }

   public SOAPMessage getSOAPMessage()
   {
      if(message!=null && ((message instanceof SOAPMessage) == false))
         throw new UnsupportedOperationException("No SOAPMessage avilable. Current message context carries " + message.getClass());
      return (SOAPMessage)message;
   }

   public void setSOAPMessage(SOAPMessage soapMessage)
   {
      this.message = (MessageAbstraction)soapMessage;
      this.setModified(true);
   }

   public MessageAbstraction getMessageAbstraction()
   {
      return message;
   }

   public void setMessageAbstraction(MessageAbstraction message)
   {
      this.message = message;
   }

   public SerializationContext getSerializationContext()
   {
      if (serContext == null)
      {
         serContext = createSerializationContext();
      }
      return serContext;
   }

   public abstract SerializationContext createSerializationContext();

   public void setSerializationContext(SerializationContext serContext)
   {
      this.serContext = serContext;
   }

   /** Gets the namespace registry for this message context */
   public NamespaceRegistry getNamespaceRegistry()
   {
      return getSerializationContext().getNamespaceRegistry();
   }

   // Map interface

   public int size()
   {
      return scopedProps.size();
   }

   public boolean isEmpty()
   {
      return scopedProps.isEmpty();
   }

   public boolean containsKey(Object key)
   {
      ScopedProperty prop = scopedProps.get(key);
      return isValidInScope(prop);
   }

   public boolean containsValue(Object value)
   {
      boolean valueFound = false;
      for (ScopedProperty prop : scopedProps.values())
      {
         if (prop.getValue().equals(value) && isValidInScope(prop))
         {
            valueFound = true;
            break;
         }
      }
      return valueFound;
   }

   public Object get(Object key)
   {
      Object value = null;

      ScopedProperty scopedProp = scopedProps.get(key);
      if (log.isTraceEnabled())
         log.trace("get(" + key + "): " + scopedProp);

      if (isValidInScope(scopedProp))
         value = scopedProp.getValue();

      return value;
   }

   public Object put(String key, Object value)
   {
      ScopedProperty prevProp = scopedProps.get(key);
      if (prevProp != null && !isValidInScope(prevProp))
         throw new IllegalArgumentException("Cannot set value for HANDLER scoped property: " + key);

      ScopedProperty newProp = new ScopedProperty(key, value, currentScope);
      if (log.isTraceEnabled())
         log.trace("put: " + newProp);

      scopedProps.put(key, newProp);
      return prevProp != null ? prevProp.getValue() : null;
   }

   public Object remove(Object key)
   {
      ScopedProperty prevProp = scopedProps.get(key);
      if (prevProp != null && !isValidInScope(prevProp))
         throw new IllegalArgumentException("Cannot set remove for HANDLER scoped property: " + key);

      return scopedProps.remove(key);
   }

   public void putAll(Map<? extends String, ? extends Object> srcMap)
   {
      for (String key : srcMap.keySet())
      {
         try
         {
            Object value = srcMap.get(key);
            put(key, value);
         }
         catch (IllegalArgumentException ex)
         {
            log.debug("Ignore: " + ex.getMessage());
         }
      }
   }

   public void clear()
   {
      scopedProps.clear();
   }

   public boolean isModified()
   {
      // skip changes from XOP handler interactions
      if (XOPContext.isXOPEncodedRequest() && !XOPContext.isXOPMessage())
      {
         log.debug("Disregard changes from XOP/Handler interactions");
         return false;
      }
      return isModified;
   }

   /**
    * Mark a message as 'modified' when the SAAJ model becomes stale.    
    * This may be the case when:
    * <ul>
    * <li>the complete message is replaced at MessageContext level
    * <li>the payload is set on a LogicalMessage
    * <li>The SAAJ model is changed though the DOM or SAAJ API (handler)
    * </ul>
    *
    * In any of these cases another 'unbind' invocation is required.
    */
   public void setModified(boolean modified)
   {
      isModified = modified;
   }

   public Set<String> keySet()
   {
      Set<String> keys = new HashSet<String>(scopedProps.size());
      for (ScopedProperty prop : scopedProps.values())
      {
         if (isValidInScope(prop))
            keys.add(prop.getName());
      }
      return keys;
   }

   public Collection<Object> values()
   {
      Collection<Object> values = new HashSet<Object>(scopedProps.size());
      for (ScopedProperty prop : scopedProps.values())
      {
         if (isValidInScope(prop))
            values.add(prop.getValue());
      }
      return values;
   }

   public Set<Entry<String, Object>> entrySet()
   {
      Set<Entry<String, Object>> entries = new HashSet<Entry<String, Object>>();
      for (ScopedProperty prop : scopedProps.values())
      {
         if (isValidInScope(prop))
         {
            String name = prop.getName();
            Object value = prop.getValue();
            Entry<String, Object> entry = new ImmutableEntry<String, Object>(name, value);
            entries.add(entry);
         }
      }
      return entries;
   }

   private boolean isValidInScope(ScopedProperty prop)
   {
      // A property of scope APPLICATION is always visible
      boolean valid = (prop != null && (prop.getScope() == Scope.APPLICATION || currentScope == Scope.HANDLER));
      return valid;
   }

   public static void cleanupAttachments(CommonMessageContext messageContext)
   {
      // cleanup attachments
      MessageAbstraction msg = messageContext.getMessageAbstraction();

      if(msg!=null && (msg instanceof SOAPMessage)) // in case of http binding
      {
         Iterator it = ((SOAPMessage)msg).getAttachments();
         while(it.hasNext())
         {
            AttachmentPart attachment = (AttachmentPart)it.next();
            try
            {
               if(attachment.getDataHandler().getDataSource() instanceof SwapableMemoryDataSource)
               {
                  SwapableMemoryDataSource swapFile = (SwapableMemoryDataSource)attachment.getDataHandler().getDataSource();
                  swapFile.cleanup();
               }
            }
            catch (SOAPException e)
            {
               log.warn("Failed to cleanup attachment part", e);
            }
         }
      }
   }

   private static class ImmutableEntry<K, V> implements Map.Entry<K, V>
   {
      final K k;
      final V v;

      ImmutableEntry(K key, V value)
      {
         k = key;
         v = value;
      }

      public K getKey()
      {
         return k;
      }

      public V getValue()
      {
         return v;
      }

      public V setValue(V value)
      {
         throw new UnsupportedOperationException();
      }
   }

   public static class ScopedProperty
   {
      private Scope scope;
      private String name;
      private Object value;

      public ScopedProperty(String name, Object value, Scope scope)
      {
         this.scope = scope;
         this.name = name;
         this.value = value;
      }

      public String getName()
      {
         return name;
      }

      public Scope getScope()
      {
         return scope;
      }

      public Object getValue()
      {
         return value;
      }

      public String toString()
      {
         return scope + ":" + name + "=" + value;
      }
   }
}