Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Clients.Common/3.3/ClientFactory.cs @ 15624

Last change on this file since 15624 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Security.Cryptography.X509Certificates;
24using System.ServiceModel;
25using System.ServiceModel.Description;
26using System.ServiceModel.Security;
27using HeuristicLab.Clients.Common.Properties;
28
29namespace HeuristicLab.Clients.Common {
30  public static class ClientFactory {
31    #region CreateClient Methods
32    public static T CreateClient<T, I>()
33      where T : ClientBase<I>, I
34      where I : class {
35      return CreateClient<T, I>(null, null);
36    }
37    public static T CreateClient<T, I>(string endpointConfigurationName)
38      where T : ClientBase<I>, I
39      where I : class {
40      return CreateClient<T, I>(endpointConfigurationName, null);
41    }
42    public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress)
43      where T : ClientBase<I>, I
44      where I : class {
45      return CreateClient<T, I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, CryptoService.DecryptString(Settings.Default.Password));
46    }
47    public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
48      where T : ClientBase<I>, I
49      where I : class {
50      T client;
51      if (string.IsNullOrEmpty(endpointConfigurationName)) {
52        client = Activator.CreateInstance<T>();
53      } else {
54        client = (T)Activator.CreateInstance(typeof(T), endpointConfigurationName);
55      }
56
57      if (!string.IsNullOrEmpty(remoteAddress)) {
58        SetEndpointAddress(client.Endpoint, remoteAddress);
59      }
60
61      client.ClientCredentials.UserName.UserName = userName;
62      client.ClientCredentials.UserName.Password = password;
63      client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.ChainTrust;
64
65      // we (jkarder + abeham) have disabled the revocation check for now
66      // the certificate requires OCSP instead of CRL for revocation checks, but the OCSP check fails
67      // we currently don't know why this is the case, because we observed a valid OCSP request/response using wireshark
68      client.ClientCredentials.ServiceCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;
69      return client;
70    }
71    #endregion
72
73    #region CreateChannelFactory Methods
74    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName)
75      where I : class {
76      return CreateChannelFactory<I>(endpointConfigurationName, null);
77    }
78    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress)
79      where I : class {
80      return CreateChannelFactory<I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, CryptoService.DecryptString(Settings.Default.Password));
81    }
82    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
83      where I : class {
84      ChannelFactory<I> channelFactory = new ChannelFactory<I>(endpointConfigurationName);
85
86      if (!string.IsNullOrEmpty(remoteAddress)) {
87        SetEndpointAddress(channelFactory.Endpoint, remoteAddress);
88      }
89
90      channelFactory.Credentials.UserName.UserName = userName;
91      channelFactory.Credentials.UserName.Password = password;
92      channelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.ChainTrust;
93
94      // we (jkarder + abeham) have disabled the revocation check for now
95      // the certificate requires OCSP instead of CRL for revocation checks, but the OCSP check fails
96      // we currently don't know why this is the case, because we observed a valid OCSP request/response using wireshark
97      channelFactory.Credentials.ServiceCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;
98      return channelFactory;
99    }
100    #endregion
101
102    #region Helpers
103    private static void SetEndpointAddress(ServiceEndpoint endpoint, string remoteAddress) {
104      // change the endpoint address and preserve the identity certificate defined in the config file
105      EndpointAddressBuilder endpointAddressBuilder = new EndpointAddressBuilder(endpoint.Address);
106      UriBuilder uriBuilder = new UriBuilder(endpointAddressBuilder.Uri);
107      uriBuilder.Host = remoteAddress;
108      endpointAddressBuilder.Uri = uriBuilder.Uri;
109      endpoint.Address = endpointAddressBuilder.ToEndpointAddress();
110    }
111    #endregion
112  }
113}
Note: See TracBrowser for help on using the repository browser.