2008-11-01 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / OperationContext.cs
1 //
2 // OperationContext.cs
3 //
4 // Author: Atsushi Enomoto (atsushi@ximian.com)
5 //
6 // Copyright (C) 2005,2007 Novell, Inc (http://www.novell.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 using System;
28 using System.Collections.Generic;
29 using System.Collections.ObjectModel;
30 using System.ServiceModel.Channels;
31 using System.ServiceModel.Dispatcher;
32 using System.ServiceModel.Security;
33 using System.Threading;
34
35 namespace System.ServiceModel
36 {
37         public sealed class OperationContext : IExtensibleObject<OperationContext>
38         {
39                 // generated guid (no special meaning)
40                 const string operation_context_name = "c15795e2-bb44-4cfb-a89c-8529feb170cb";
41                 Message incoming_message;
42                 IDefaultCommunicationTimeouts timeouts;
43
44                 public static OperationContext Current {
45                         get { return Thread.GetData (Thread.GetNamedDataSlot (operation_context_name)) as OperationContext; }
46                         set { Thread.SetData (Thread.GetNamedDataSlot (operation_context_name), value); }
47                 }
48
49                 EndpointDispatcher dispatcher;
50                 IContextChannel channel;
51                 RequestContext request_ctx;
52                 ExtensionCollection<OperationContext> extensions;
53                 MessageHeaders outgoing_headers;
54                 MessageProperties outgoing_properties;
55                 InstanceContext instance_context;
56
57                 public OperationContext (IContextChannel channel)
58                 {
59                         if (channel == null)
60                                 throw new ArgumentNullException ("channel");
61                         this.channel = channel;
62                 }
63
64                 public event EventHandler OperationCompleted;
65
66                 public IContextChannel Channel {
67                         get { return channel; }
68                 }
69
70                 public EndpointDispatcher EndpointDispatcher {
71                         get { return dispatcher; }
72                         set { dispatcher = value; }
73                 }
74
75                 public IExtensionCollection<OperationContext> Extensions {
76                         get {
77                                 if (extensions == null)
78                                         extensions = new ExtensionCollection<OperationContext> (this);
79                                 return extensions;
80                         }
81                 }
82
83                 public bool HasSupportingTokens {
84                         get { return SupportingTokens != null ? SupportingTokens.Count > 0 : false; }
85                 }
86
87                 public ServiceHostBase Host {
88                         get { return dispatcher != null ? dispatcher.ChannelDispatcher.Host : null; }
89                 }
90
91                 public MessageHeaders IncomingMessageHeaders {
92                         get { return request_ctx != null ? request_ctx.RequestMessage.Headers : null; }
93                 }
94
95                 public MessageProperties IncomingMessageProperties {
96                         get { return request_ctx != null ? request_ctx.RequestMessage.Properties : null; }
97                 }
98
99                 public MessageVersion IncomingMessageVersion {
100                         get { return request_ctx != null ? request_ctx.RequestMessage.Version : null; }
101                 }
102
103                 [MonoTODO]
104                 public InstanceContext InstanceContext {
105                         get {                           
106                                 return instance_context;
107                         }
108                         internal set {
109                                 instance_context = value;
110                         }
111                 }
112
113                 [MonoTODO]
114                 public bool IsUserContext {
115                         get { throw new NotImplementedException (); }
116                 }
117
118                 public MessageHeaders OutgoingMessageHeaders {
119                         get {
120                                 if (outgoing_headers == null)
121                                         outgoing_headers = new MessageHeaders (MessageVersion.Default);
122                                 return outgoing_headers;
123                         }
124                 }
125
126                 public MessageProperties OutgoingMessageProperties {
127                         get {
128                                 if (outgoing_properties == null)
129                                         outgoing_properties = new MessageProperties ();
130                                 return outgoing_properties;
131                         }
132                 }
133
134                 public RequestContext RequestContext {
135                         get { return request_ctx; }
136                         set { request_ctx = value; }
137                 }
138
139                 public ServiceSecurityContext ServiceSecurityContext {
140                         get { return IncomingMessageProperties != null ? IncomingMessageProperties.Security.ServiceSecurityContext : null; }
141                 }
142
143                 [MonoTODO]
144                 public string SessionId {
145                         get { throw new NotImplementedException (); }
146                 }
147
148                 public ICollection<SupportingTokenSpecification> SupportingTokens {
149                         get { return IncomingMessageProperties != null ? IncomingMessageProperties.Security.IncomingSupportingTokens : null; }
150                 }
151
152                 public T GetCallbackChannel<T> ()
153                 {
154                         if (!(channel is IDuplexContextChannel))
155                                 return default (T);
156                         IDuplexContextChannel duplex = (IDuplexContextChannel) channel;
157                         foreach (IChannel ch in duplex.CallbackInstance.IncomingChannels)
158                                 if (typeof (T).IsAssignableFrom (ch.GetType ()))
159                                         return (T) (object) ch;
160                         foreach (IChannel ch in duplex.CallbackInstance.OutgoingChannels)
161                                 if (typeof (T).IsAssignableFrom (ch.GetType ()))
162                                         return (T) (object) ch;
163                         return default (T);
164                 }
165
166                 [MonoTODO]
167                 public void SetTransactionComplete ()
168                 {
169                         throw new NotImplementedException ();
170                 }
171
172                 internal Message IncomingMessage {
173                         get {
174                                 return incoming_message;
175                         }
176                         set {
177                                 incoming_message = value;
178                         }
179                 }
180
181                 internal IDefaultCommunicationTimeouts CommunicationTimeouts
182                 {
183                         get {
184                                 return timeouts;
185                         }
186                         set {
187                                 timeouts = value;
188                         }
189                 }
190         }
191 }