2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.schemaservice;
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.apache.http.client.HttpClient;
25 import org.apache.http.impl.client.HttpClients;
26 import org.apache.http.ssl.SSLContextBuilder;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.boot.test.context.TestConfiguration;
29 import org.springframework.boot.web.client.RestTemplateBuilder;
30 import org.springframework.context.annotation.Bean;
31 import org.springframework.core.env.Environment;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.client.ClientHttpResponse;
34 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
35 import org.springframework.util.ResourceUtils;
36 import org.springframework.web.client.ResponseErrorHandler;
37 import org.springframework.web.client.RestTemplate;
39 import javax.net.ssl.SSLContext;
41 import java.io.FileInputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.security.KeyStore;
47 public class SchemaServiceTestConfiguration {
49 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SchemaServiceTestConfiguration.class);
52 private Environment env;
55 * Create a RestTemplate bean, using the RestTemplateBuilder provided
56 * by the auto-configuration.
59 RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {
61 char[] trustStorePassword = env.getProperty("server.ssl.trust-store-password").toCharArray();
62 char[] keyStorePassword = env.getProperty("server.ssl.key-store-password").toCharArray();
64 String keyStore = env.getProperty("server.ssl.key-store");
65 String trustStore = env.getProperty("server.ssl.trust-store");
67 SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();
69 if(env.acceptsProfiles("two-way-ssl")){
70 sslContextBuilder = sslContextBuilder.loadKeyMaterial(loadPfx(keyStore, keyStorePassword), keyStorePassword);
73 SSLContext sslContext = sslContextBuilder
74 .loadTrustMaterial(ResourceUtils.getFile(trustStore), trustStorePassword)
77 HttpClient client = HttpClients.custom()
78 .setSSLContext(sslContext)
79 .setSSLHostnameVerifier((s, sslSession) -> true)
82 RestTemplate restTemplate = builder
83 .requestFactory(new HttpComponentsClientHttpRequestFactory(client))
86 restTemplate.setErrorHandler(new ResponseErrorHandler() {
88 public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
89 if (clientHttpResponse.getStatusCode() != HttpStatus.OK) {
91 logger.debug("Status code: " + clientHttpResponse.getStatusCode());
93 if (clientHttpResponse.getStatusCode() == HttpStatus.FORBIDDEN) {
94 logger.debug("Call returned a error 403 forbidden resposne ");
98 if(clientHttpResponse.getRawStatusCode() % 100 == 5){
99 logger.debug("Call returned a error " + clientHttpResponse.getStatusText());
108 public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
115 private KeyStore loadPfx(String file, char[] password) throws Exception {
116 KeyStore keyStore = KeyStore.getInstance("PKCS12");
117 File key = ResourceUtils.getFile(file);
118 try (InputStream in = new FileInputStream(key)) {
119 keyStore.load(in, password);