2 * ===============LICENSE_START=======================================================
4 * ===================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
6 * ===================================================================================
7 * This Acumos software file is distributed by AT&T and Tech Mahindra
8 * under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * This file 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.acumos.federation.gateway.config;
23 import java.lang.invoke.MethodHandles;
24 import java.net.MalformedURLException;
28 import org.apache.http.HttpHost;
29 import org.apache.http.auth.AuthScope;
30 import org.apache.http.auth.UsernamePasswordCredentials;
31 import org.apache.http.client.AuthCache;
32 import org.apache.http.client.CredentialsProvider;
33 import org.apache.http.client.protocol.HttpClientContext;
34 import org.apache.http.impl.auth.BasicScheme;
35 import org.apache.http.impl.client.BasicAuthCache;
36 import org.apache.http.impl.client.BasicCredentialsProvider;
37 import org.apache.http.protocol.HttpContext;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.context.properties.ConfigurationProperties;
42 import org.springframework.boot.web.client.RestTemplateBuilder;
43 import org.springframework.http.HttpMethod;
44 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
45 import org.springframework.stereotype.Component;
46 import org.springframework.web.client.RestTemplate;
53 @ConfigurationProperties(prefix = "nexus")
54 public class NexusConfiguration {
56 private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59 private String groupId;
62 private String username;
63 private String password;
64 private String nameSeparator;
66 private LocalInterfaceConfiguration localIfConfig = null;
68 public NexusConfiguration() {
72 private void reset() {
76 this.nameSeparator = ".";
79 public void setId(String theId) {
83 public void setUrl(String theSpec) throws MalformedURLException {
84 this.url = new URL(theSpec);
87 public String getUrl() {
88 return this.url.toString();
91 public void setUsername(String theUsername) {
92 this.username = theUsername;
95 public void setPassword(String thePassword) {
96 this.password = thePassword;
100 public void setProxy(String theProxy) {
101 this.proxy = theProxy;
104 public void setGroupId(String theGroupId) {
105 this.groupId = theGroupId;
108 public String getGroupId() {
112 public void setNameSperator(String theNameSeparator) {
113 this.nameSeparator = theNameSeparator;
116 public String getNameSeparator() {
117 return this.nameSeparator;
121 * Prepare a RestTemplate fitted for Nexus interactions, in particular ready to perform preemptive basic authentication.
122 * @return RestTemplate
124 public RestTemplate getNexusClient() {
126 RestTemplateBuilder builder =
127 new RestTemplateBuilder()
129 () -> new HttpComponentsClientHttpRequestFactory(this.localIfConfig.buildClient()) {
131 protected HttpContext createHttpContext(HttpMethod theMethod, URI theUri) {
132 HttpHost nexusHost = new HttpHost(NexusConfiguration.this.url.getHost(), NexusConfiguration.this.url.getPort());
134 CredentialsProvider nexusCreds = new BasicCredentialsProvider();
135 nexusCreds.setCredentials(
136 new AuthScope(nexusHost.getHostName(), nexusHost.getPort()),
137 new UsernamePasswordCredentials(NexusConfiguration.this.username, NexusConfiguration.this.password));
139 AuthCache authCache = new BasicAuthCache();
140 BasicScheme basicAuth = new BasicScheme();
141 authCache.put(nexusHost, basicAuth);
143 HttpClientContext nexusContext = HttpClientContext.create();
144 nexusContext.setAuthCache(authCache);
145 nexusContext.setCredentialsProvider(nexusCreds);
150 return builder.build();