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=========================================================
21 package org.onap.aai.schemaservice;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.security.KeyStore;
29 import javax.net.ssl.SSLContext;
31 import org.apache.http.client.HttpClient;
32 import org.apache.http.impl.client.HttpClients;
33 import org.apache.http.ssl.SSLContextBuilder;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.test.context.TestConfiguration;
38 import org.springframework.boot.web.client.RestTemplateBuilder;
39 import org.springframework.context.annotation.Bean;
40 import org.springframework.core.env.Environment;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.client.ClientHttpResponse;
43 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
44 import org.springframework.util.ResourceUtils;
45 import org.springframework.web.client.ResponseErrorHandler;
46 import org.springframework.web.client.RestTemplate;
49 public class SchemaServiceTestConfiguration {
51 private static final Logger logger =
52 LoggerFactory.getLogger(SchemaServiceTestConfiguration.class);
55 private Environment env;
58 * Create a RestTemplate bean, using the RestTemplateBuilder provided
59 * by the auto-configuration.
62 RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {
64 RestTemplate restTemplate = null;
66 if (env.acceptsProfiles("one-way-ssl", "two-way-ssl")) {
67 char[] trustStorePassword =
68 env.getProperty("server.ssl.trust-store-password").toCharArray();
69 char[] keyStorePassword =
70 env.getProperty("server.ssl.key-store-password").toCharArray();
72 String keyStore = env.getProperty("server.ssl.key-store");
73 String trustStore = env.getProperty("server.ssl.trust-store");
74 SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();
76 if (env.acceptsProfiles("two-way-ssl")) {
77 sslContextBuilder = sslContextBuilder
78 .loadKeyMaterial(loadPfx(keyStore, keyStorePassword), keyStorePassword);
81 SSLContext sslContext = sslContextBuilder
82 .loadTrustMaterial(ResourceUtils.getFile(trustStore), trustStorePassword).build();
84 HttpClient client = HttpClients.custom().setSSLContext(sslContext)
85 .setSSLHostnameVerifier((s, sslSession) -> true).build();
87 restTemplate = builder
88 .requestFactory(() -> new HttpComponentsClientHttpRequestFactory(client)).build();
90 restTemplate = builder.build();
93 restTemplate.setErrorHandler(new ResponseErrorHandler() {
95 public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
96 if (clientHttpResponse.getStatusCode() != HttpStatus.OK) {
98 logger.debug("Status code: " + clientHttpResponse.getStatusCode());
100 if (clientHttpResponse.getStatusCode() == HttpStatus.FORBIDDEN) {
101 logger.debug("Call returned a error 403 forbidden resposne ");
105 if (clientHttpResponse.getRawStatusCode() % 100 == 5) {
106 logger.debug("Call returned a error " + clientHttpResponse.getStatusText());
115 public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
122 private KeyStore loadPfx(String file, char[] password) throws Exception {
123 KeyStore keyStore = KeyStore.getInstance("PKCS12");
124 File key = ResourceUtils.getFile(file);
125 try (InputStream in = new FileInputStream(key)) {
126 keyStore.load(in, password);