2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.apache.http.client.HttpClient;
27 import org.apache.http.impl.client.HttpClients;
28 import org.apache.http.ssl.SSLContextBuilder;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.boot.test.context.TestConfiguration;
31 import org.springframework.boot.web.client.RestTemplateBuilder;
32 import org.springframework.context.annotation.Bean;
33 import org.springframework.core.env.Environment;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.client.ClientHttpResponse;
36 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
37 import org.springframework.util.ResourceUtils;
38 import org.springframework.web.client.ResponseErrorHandler;
39 import org.springframework.web.client.RestTemplate;
41 import javax.net.ssl.SSLContext;
43 import java.io.FileInputStream;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.security.KeyStore;
49 public class TraversalTestConfiguration {
51 private static final EELFLogger logger = EELFManager.getInstance().getLogger(TraversalTestConfiguration.class);
54 private Environment env;
57 * Create a RestTemplate bean, using the RestTemplateBuilder provided
58 * by the auto-configuration.
61 RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {
63 char[] trustStorePassword = env.getProperty("server.ssl.trust-store-password").toCharArray();
64 char[] keyStorePassword = env.getProperty("server.ssl.key-store-password").toCharArray();
66 String keyStore = env.getProperty("server.ssl.key-store");
67 String trustStore = env.getProperty("server.ssl.trust-store");
69 SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();
71 if(env.acceptsProfiles("two-way-ssl")){
72 sslContextBuilder = sslContextBuilder.loadKeyMaterial(loadPfx(keyStore, keyStorePassword), keyStorePassword);
75 SSLContext sslContext = sslContextBuilder
76 .loadTrustMaterial(ResourceUtils.getFile(trustStore), trustStorePassword)
79 HttpClient client = HttpClients.custom()
80 .setSSLContext(sslContext)
81 .setSSLHostnameVerifier((s, sslSession) -> true)
84 RestTemplate restTemplate = builder
85 .requestFactory(new HttpComponentsClientHttpRequestFactory(client))
88 restTemplate.setErrorHandler(new ResponseErrorHandler() {
90 public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
91 if (clientHttpResponse.getStatusCode() != HttpStatus.OK) {
93 logger.debug("Status code: " + clientHttpResponse.getStatusCode());
95 if (clientHttpResponse.getStatusCode() == HttpStatus.FORBIDDEN) {
96 logger.debug("Call returned a error 403 forbidden resposne ");
100 if(clientHttpResponse.getRawStatusCode() % 100 == 5){
101 logger.debug("Call returned a error " + clientHttpResponse.getStatusText());
110 public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
117 private KeyStore loadPfx(String file, char[] password) throws Exception {
118 KeyStore keyStore = KeyStore.getInstance("PKCS12");
119 File key = ResourceUtils.getFile(file);
120 try (InputStream in = new FileInputStream(key)) {
121 keyStore.load(in, password);