Newer
Older
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain
* a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
*
* The Original Code is .NET RASP toolkit.
*
* The Initial Developer of the Original Code is Accenture and Avanade.
* Portions created by Accenture and Avanade are Copyright (C) 2009
* Danish National IT and Telecom Agency (http://www.itst.dk).
* All Rights Reserved.
*
* Contributor(s):
* Gert Sylvest, Avanade
* Jesper Jensen, Avanade
* Ramzi Fadel, Avanade
* Patrik Johansson, Accenture
* Christian Pedersen, Accenture
* Martin Bentzen, Accenture
* Mikkel Hippe Brun, ITST
* Finn Hartmann Jordal, ITST
* Christian Lanng, ITST
*
*/
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Security;
using System.Text;
using System.Xml;
using dk.gov.oiosi.communication.client;
using dk.gov.oiosi.extension.wcf.Behavior;
using dk.gov.oiosi.extension.wcf.EmailTransport;
using dk.gov.oiosi.extension.wcf.Interceptor.UbiquitousProperties;
using dk.gov.oiosi.security.oces;
using dk.gov.oiosi.communication.handlers.email;
using System.Diagnostics;
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
namespace dk.gov.oiosi.communication
{
/// <summary>
/// Represents a request to an OIOSI http or email endpoint.
/// </summary>
public class Request : dk.gov.oiosi.communication.IRequest
{
// The proxy used for service calls
private ClientProxy proxy;
// The delegate used to call our synchronous sending method asynchronously
private delegate void AsyncGetResponse(OiosiMessage message, out Response response);
/// <summary>
/// Constant for http endpoint configuration name
/// </summary>
public const string HttpEndpointConfigurationName = "OiosiHttpEndpoint";
/// <summary>
/// The name of the WCF endpoint configuration
/// </summary>
protected string endpointConfigurationName;
private Credentials credentials;
private Uri requestUri;
private SendPolicy policy = new SendPolicy();
#region public methods
/// <summary>
/// Constructor
/// </summary>
/// <param name="endpointAddress"></param>
public Request(Uri endpointAddress)
{
// Test that a valid endpoint address was given
this.TestEndpointAddressCompability(endpointAddress);
this.requestUri = endpointAddress;
}
/// <summary>
/// Request
/// </summary>
/// <param name="endpointAddress">The endpoint address</param>
/// <param name="credentials">Credentials</param>
public Request(Uri endpointAddress, Credentials credentials)
: this(endpointAddress)
{
this.credentials = credentials;
}
/// <summary>
/// Creates a new Request instance bound to the specific endpoint. During construction,
/// the type of endpoint is inferred from the Uri scheme. After construction, additional
/// properties may be set for the request, such as credentials
/// </summary>
/// <param name="credentials">Overrides the credentials set in config</param>
/// <param name="endpointAddress"></param>
/// <param name="sendPolicy">The send policy of the request</param>
public Request(Uri endpointAddress, Credentials credentials, SendPolicy sendPolicy)
: this(endpointAddress, credentials)
{
this.policy = sendPolicy;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="endpointConfigurationName">The name of the endpoint configuration</param>
public Request(string endpointConfigurationName)
{
if (string.IsNullOrEmpty(endpointConfigurationName))
{
//E-RSP31001
throw new NoEndpointGivenException();
}
this.endpointConfigurationName = endpointConfigurationName;
}
/// <summary>
/// Tests that and endpoint address is compatible with Request
/// </summary>
protected void TestEndpointAddressCompability(Uri endpointAddress)
{
if (endpointAddress == null)
{
//E-RSP31001
throw new NoEndpointGivenException();
}
// We only support 'http' and 'mailto'
if (endpointAddress.Scheme != "mailto" && endpointAddress.Scheme != "http")
{
//E-RSP31002
throw new NotSupportedSchemeException(endpointAddress.Scheme);
}
}
/// <summary>
/// Creates the proxy object
/// </summary>
protected void CreateProxy()
{
logging.WCFLogger.Write(System.Diagnostics.TraceEventType.Start, "Proxy being created");
// Try to create a proxy
try
{
// Is an endpoint in configuration given?
if (!string.IsNullOrEmpty(endpointConfigurationName))
// ToDo: Should the implementing class not be configurable? The interface is defined in App.Config.....
this.proxy = new ClientProxy(endpointConfigurationName);
this.requestUri = proxy.Endpoint.Address.Uri;
logging.WCFLogger.Write(System.Diagnostics.TraceEventType.Verbose, "Endpoint configuration section '" + endpointConfigurationName + "' used to create proxy object");
}
// ... if not, infer the endpoint config from the URI type
else
{
switch (this.requestUri.Scheme)
{
case "http":
{
logging.WCFLogger.Write(System.Diagnostics.TraceEventType.Verbose, "HTTP transport inferred from request URI '" + RequestUri + "'");
this.proxy = new ClientProxy(HttpEndpointConfigurationName);
logging.WCFLogger.Write(System.Diagnostics.TraceEventType.Verbose, "Default HTTP endpoint configuration section '" + HttpEndpointConfigurationName + "' used to create proxy object");
break;
}
default:
{
//E-RSP31002
throw new NotSupportedSchemeException(this.requestUri.Scheme);
}
}
}
// Add the behavior that will encrypt the bodies of WS-RM messages
this.proxy.Endpoint.Behaviors.Add(new EncryptRmBodiesBehavior());
logging.WCFLogger.Write(System.Diagnostics.TraceEventType.Verbose, "Behavior to encrypt the body of RM messages added to proxy");
this.SetCredentials();
logging.WCFLogger.Write(System.Diagnostics.TraceEventType.Stop, "Proxy finished being created");
}
catch (Exception e)
{
Debug.Fail("User the 'dk.gov.oiosi.communication.service.IServiceContract' contract instead of 'dk.gov.oiosi.communication.client.IClientProxyContract'.");
//E-RSP31003
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
throw new ProxyGenerationException(e);
}
}
private void SetCredentials()
{
string dnsId = null;
// Do we have programatically set credentials?
if (this.credentials != null)
{
logging.WCFLogger.Write(System.Diagnostics.TraceEventType.Start, "Credentials added programatically. Starting to override proxy settings.");
// Override client cert
if (this.credentials.ClientCertificate != null)
{
this.proxy.ClientCredentials.ClientCertificate.Certificate = this.credentials.ClientCertificate.Certificate;
logging.WCFLogger.Write(System.Diagnostics.TraceEventType.Start, "Client certificate overridden with '" + this.credentials.ClientCertificate.Certificate.FriendlyName + "'");
}
// Override server cert
if (this.credentials.ServerCertificate != null)
{
this.proxy.ClientCredentials.ServiceCertificate.DefaultCertificate = this.credentials.ServerCertificate.Certificate;
logging.WCFLogger.Write(System.Diagnostics.TraceEventType.Start, "Server certificate overridden with '" + this.credentials.ServerCertificate.Certificate.FriendlyName + "'");
}
// Get the endpoint DNS Identity
dnsId = this.proxy.ClientCredentials.ServiceCertificate.DefaultCertificate.GetNameInfo(X509NameType.DnsName, false);
logging.WCFLogger.Write(System.Diagnostics.TraceEventType.Stop, "Finished overriding credentials.");
}
// The credentials were not set programatically...
// If client credentials have been set by config, set the local property to point to them
else if (proxy.ClientCredentials != null)
{
X509Certificate2 clientCert = null;
X509Certificate2 serverCert = null;
if (this.proxy.ClientCredentials.ClientCertificate != null)
{
clientCert = proxy.ClientCredentials.ClientCertificate.Certificate;
}
if (proxy.ClientCredentials.ServiceCertificate != null)
{
serverCert = proxy.ClientCredentials.ServiceCertificate.DefaultCertificate;
}
// Local credentials should be set to same as the ones gotten from app.config
this.credentials = new Credentials(
new OcesX509Certificate(clientCert),
new OcesX509Certificate(serverCert));
// Get the endpoint DNS Identity
dnsId = this.proxy.ClientCredentials.ServiceCertificate.DefaultCertificate.GetNameInfo(X509NameType.DnsName, false);
}
// Set the endpoint address
if (dnsId != null)
{
this.proxy.Endpoint.Address = new EndpointAddress(this.requestUri, new DnsEndpointIdentity(dnsId), new AddressHeaderCollection());
}
else
{
proxy.Endpoint.Address = new EndpointAddress(this.requestUri);
}
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
/// <summary>
/// Synchronously sends a request and gets a response
/// </summary>
/// <param name="message">Request message</param>
/// <returns>Response message</returns>
[Obsolete("void GetResponse(OiosiMessage message, Response response) should be used instead", false)]
public Response GetResponse(OiosiMessage message)
{
System.Diagnostics.Debug.WriteLine(DateTime.Now + " " + this.ToString() + ".GetResponse()");
Response response = null;
this.OpenProxy();
try
{
this.SendMessage(message, out response);
}
catch
{
throw;
}
finally
{
this.CloseProxy();
}
return response;
}
/// <summary>
/// Synchronously sends a request and gets a response
/// </summary>
/// <example>
/// Response response;
/// try{
/// GetResponse(request, out response);
/// }
/// catch(RequestShutdownException){
/// // Sending went well, and we can continue even though we didn't get a neat shutdown
/// }
/// catch(Exception e){
/// // Sending did not go well
/// trow;
/// }
/// </example>
/// <param name="response">The response. If this parameter is set the sending went well and the response is safe to use</param>
/// <param name="request">Request message</param>
public void GetResponse(OiosiMessage request, out Response response)
{
response = null;
this.OpenProxy();
try
{
this.SendMessage(request, out response);
}
catch
{
throw;
}
finally
{
this.CloseProxy();
}
}
/// <summary>
/// Opens a proxy connection to the remote endpoint
/// </summary>
private void OpenProxy()
{
this.CreateProxy();
// Test that an inner channel was created
if (proxy.InnerChannel == null)
{
//E-RSP31004
throw new CreatingCommunicationChannelFailedException();
}
}
/// <summary>
/// Closes the proxy connection to the remote endpoint
/// </summary>
private void CloseProxy()
{
if (this.proxy != null && this.proxy.State != CommunicationState.Faulted)
{
try
{
this.proxy.Close();
}
{
// The Communication Object hass already been aborted, so we can not close it
// Exception should no be handled.
}
catch (Exception ex)
{
//E-RSP31005
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
throw new RequestShutdownException(ex);
}
}
}
/// <summary>
/// Converts the message to a wcf message and sends it via the proxy
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
private void SendMessage(OiosiMessage message, out Response response)
{
response = null;
try
{
// Convert to WCF message
Message wcfMessage = null;
if (message.HasBody)
{
XmlReader xmlBody = message.GetMessageXmlReader();
wcfMessage = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, message.RequestAction, xmlBody);
}
else
{
wcfMessage = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, message.RequestAction);
}
// Adds properties from the Message to the WCF message
foreach (KeyValuePair<string, object> p in message.Properties)
{
wcfMessage.Properties.Add(p.Key, p.Value);
}
// Do we have any properties that should be added to WS-RM messages as well?
if (message.UbiquitousProperties.Count > 0)
{
try
{
UbiquitousPropertiesBindingElement interceptor = (((CustomBinding)proxy.ChannelFactory.Endpoint.Binding).Elements.Find<UbiquitousPropertiesBindingElement>());
interceptor.SetProperties(message.UbiquitousProperties);
}
catch (NullReferenceException e)
{
//E-RSP31006
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
throw new MissingStackElementException("UbiquitousPropertiesBindingElement", e);
}
}
// Adds custom headers
foreach (KeyValuePair<XmlQualifiedName, MessageHeader> header in message.MessageHeaders)
{
wcfMessage.Headers.Add(header.Value);
}
// Sends
Message wcfMessageResponse = this.proxy.RequestRespond(wcfMessage);
// Make sure we dind't receive a fault
if (wcfMessageResponse.IsFault)
{
throw this.CreateFaultWasReceivedException(new FaultException(MessageFault.CreateFault(wcfMessageResponse, int.MaxValue)));
}
// Convert back to oiosi message
response = new Response(wcfMessageResponse);
// If any properties with the attribute MessageProperty were sent with the message
// they should be attached to the ListenerRequest message as well
foreach (object o in wcfMessageResponse.Properties.Values)
{
object[] attributes = o.GetType().GetCustomAttributes(typeof(dk.gov.oiosi.extension.wcf.OiosiMessagePropertyAttribute), false);
if (attributes.Length > 0)
{
response.AddProperty(o);
}
}
}
catch (ProtocolException e)
{
// Minor hack to fix interop problems with the Java/Axis2 1.2 NemHandel stack
// SOAP faults might be returned with a http code 400 (Bad request),
// if that is the case we need to manually get the SOAP fault from the WebException
if (e.InnerException is System.Net.WebException)
{
throw this.GetSoapFaultFromHttpException(e.InnerException as System.Net.WebException);
}
else
{
throw new ProtocolMismatchException(e);
}
}
catch (MessageSecurityException e)
{
// If the execption was not due to a fault
if (!(e.InnerException is FaultException))
{
throw new ProtocolMismatchException(e);
}
else
{
throw this.CreateFaultWasReceivedException((FaultException)e.InnerException);
}
}
catch
{
throw;
}
}
/// <summary>
/// Minor hack to fix interop problems with the Java/Axis2 1.2 NemHandel stack
/// SOAP faults might be returned with a http code 400 (Bad request),
/// if that is the case we need to manually get the SOAP fault from the WebException
/// </summary>
private Exception GetSoapFaultFromHttpException(System.Net.WebException e)
{
// ToDo : should this string builder has ha max size? ~ new StringBuilder("", 65536);
StringBuilder sb = new StringBuilder();
Stream s = e.Response.GetResponseStream();
//e.
// Try to read the fault
try
{
byte[] readBuffer = new byte[1000];
int count = 0;
bool finish = false;
while (!finish)
{
count = s.Read(readBuffer, 0, readBuffer.Length);
if (count == 0)
{
// EOF
finish = true;
}
else
{
sb.Append(System.Text.Encoding.UTF8.GetString(readBuffer, 0, count));
}
}
}
catch
{
return e;
}
finally
{
s.Close();
}
// Try to make it a SOAP faultobject
// move declarion here, for easy debugging
MemoryStream memStream = null;
Message message;
MessageFault msgFault;
Exception exception;
try
{
;
using (memStream = new MemoryStream(Encoding.Default.GetBytes(sb.ToString())))
{
XmlTextReader xmlReader = new XmlTextReader(memStream);
message = Message.CreateMessage(xmlReader, int.MaxValue, MessageVersion.Soap12WSAddressing10);
}
//OiosiMessage oio = new OiosiMessage(message);
msgFault = MessageFault.CreateFault(message, int.MaxValue);
exception = CreateFaultWasReceivedException(new FaultException(msgFault));
}
string errorMessage = sb.ToString();
if (string.IsNullOrEmpty(errorMessage))
{
if (e.GetBaseException() != null)
{
// The e.Message is just: 'Protocol Error'.
// Better error description here:
errorMessage = e.GetBaseException().Message;
}
else
{
errorMessage = e.Message;
}
}
return new Exception(errorMessage);
}
return exception;
}
/// <summary>
/// Creates the appropriate exception when a fault was received
/// </summary>
private Exception CreateFaultWasReceivedException(FaultException e)
{
// Time for blaming. Who's fault was it?
if (e.Code.IsSenderFault)
{
//E-RSP31007
return new FaultReturnedException(e, "dig");
}
else
{
//E-RSP31007
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
return new FaultReturnedException(e, "serveren");
}
}
/// <summary>
/// Asynchronously starts sending a request
/// </summary>
/// <param name="message">Request message</param>
/// <param name="callback">The asynchronous callback</param>
/// <returns>Returns an IAsyncResult object</returns>
public IAsyncResult BeginGetResponse(OiosiMessage message, out Response response, AsyncCallback callback)
{
AsyncGetResponse asyncGetResponse = new AsyncGetResponse(GetResponse);
IAsyncResult result = asyncGetResponse.BeginInvoke(message, out response, callback, asyncGetResponse);
return result;
}
/// <summary>
/// Asynchronously ends sending a request
/// </summary>
/// <returns>Response message</returns>
public void EndGetResponse(IAsyncResult asyncResult, out Response response)
{
//Response r;
try
{
((AsyncGetResponse)asyncResult.AsyncState).EndInvoke(out response, asyncResult);
}
catch (InvalidOperationException)
{
throw;
}
//return r;
}
#region properties
/// <summary>
/// Property for credentials
/// </summary>
public Credentials Credentials
{
get { return this.credentials; }
set { this.credentials = value; }
}
/// <summary>
/// Property for the request uri
/// </summary>
public Uri RequestUri
{
get { return this.requestUri; }
}
/// <summary>
/// Property for the send policy
/// </summary>
public SendPolicy Policy
{
get { return this.policy; }
set { this.policy = value; }
}
#endregion
/// <summary>
/// Closes the request
/// </summary>
public void Close()
{
this.CloseProxy();
}
/// <summary>
/// Aborts the request
/// </summary>
public void Abort()
{
if (this.proxy != null)
{
proxy.Abort();
}
}
#endregion
}
}