/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openehealth.ipf.commons.ihe.fhir.translation;

import org.apache.http.impl.client.HttpClientBuilder;
import org.openehealth.ipf.commons.ihe.core.SecurityInformation;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.security.NoSuchAlgorithmException;

/**
 *
 */
public class FhirSecurityInformation extends SecurityInformation {

    public FhirSecurityInformation(boolean secure, SSLContext sslContext, HostnameVerifier hostnameVerifier, String username, String password) {
        super(secure, sslContext, hostnameVerifier, username, password);
    }

    public void configureHttpClientBuilder(HttpClientBuilder builder)  {
        if (isSecure() && getSslContext() == null) {
            try {
                builder.setSSLContext(SSLContext.getDefault());
            } catch (NoSuchAlgorithmException e) {
                // Should never happen
                throw new RuntimeException("Could not create SSL Context", e);
            }
        } else {
            builder.setSSLContext(getSslContext());
        }
        if (getHostnameVerifier() != null) {
            builder.setSSLHostnameVerifier(getHostnameVerifier());
        }

        // This is currently done by the FhirProducer#getClient by attaching a ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor
        // I wonder if the following wouldn't be better

        /*
        if (getUsername() != null) {
            CredentialsProvider provider = new BasicCredentialsProvider();
            AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(getUsername(), getPassword());
            provider.setCredentials(scope, credentials);
            builder.setDefaultCredentialsProvider(provider);
        }
        */
    }
}

@@ -1313,12 +1313,28 @@
 re()
- && 
+) %7B%0A            if (
 getS
@@ -1372,14 +1372,22 @@
     
+    
 try %7B%0A
+    
     
@@ -1458,16 +1458,20 @@
         
+  
+  
 %7D catch 
@@ -1517,16 +1517,20 @@
         
+    
 // Shoul
@@ -1560,16 +1560,20 @@
         
+   
+ 
 throw ne
@@ -1631,34 +1631,42 @@
 e);%0A            
-%7D%0A
+    %7D%0A    
         %7D else %7B
@@ -1670,32 +1670,36 @@
 e %7B%0A            
+ 
+   
 builder.setSSLCo
@@ -1730,26 +1730,33 @@
         
-%7D%0A
+    %7D%0A   
         
 if (getH
@@ -1743,24 +1743,25 @@
 %0A           
+ 
 if (getHostn
@@ -1777,32 +1777,36 @@
 er() != null) %7B%0A
+    
             buil
@@ -1852,24 +1852,38 @@
 erifier());%0A
+            %7D%0A
         %7D%0A%0A 

fix condition