View Javadoc
1   /*
2    * Copyright 2016 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.openehealth.ipf.commons.ihe.fhir.translation;
18  
19  import org.apache.http.impl.client.HttpClientBuilder;
20  import org.openehealth.ipf.commons.ihe.core.SecurityInformation;
21  
22  import javax.net.ssl.HostnameVerifier;
23  import javax.net.ssl.SSLContext;
24  import java.security.NoSuchAlgorithmException;
25  
26  /**
27   *
28   */
29  public class FhirSecurityInformation extends SecurityInformation {
30  
31      public FhirSecurityInformation(boolean secure, SSLContext sslContext, HostnameVerifier hostnameVerifier, String username, String password) {
32          super(secure, sslContext, hostnameVerifier, username, password);
33      }
34  
35      public void configureHttpClientBuilder(HttpClientBuilder builder)  {
36          if (isSecure()) {
37              if (getSslContext() == null) {
38                  try {
39                      builder.setSSLContext(SSLContext.getDefault());
40                  } catch (NoSuchAlgorithmException e) {
41                      // Should never happen
42                      throw new RuntimeException("Could not create SSL Context", e);
43                  }
44              } else {
45                  builder.setSSLContext(getSslContext());
46              }
47              if (getHostnameVerifier() != null) {
48                  builder.setSSLHostnameVerifier(getHostnameVerifier());
49              }
50          }
51  
52          // This is currently done by the FhirProducer#getClient by attaching a ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor
53          // I wonder if the following wouldn't be better
54  
55          /*
56          if (getUsername() != null) {
57              CredentialsProvider provider = new BasicCredentialsProvider();
58              AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
59              UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(getUsername(), getPassword());
60              provider.setCredentials(scope, credentials);
61              builder.setDefaultCredentialsProvider(provider);
62          }
63          */
64      }
65  }