2 * ===============LICENSE_START=======================================================
4 * ===================================================================================
5 * Copyright (C) 2017-2019 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);
89 * Get the normalized URL for the nexus content server.
90 * @return the URL, as a String, with exactly one trailing slash.
93 public String getUrl() {
94 return this.url.toString().replaceAll("/*$", "") + "/";
97 public void setUsername(String theUsername) {
98 this.username = theUsername;
101 public void setPassword(String thePassword) {
102 this.password = thePassword;
106 public void setProxy(String theProxy) {
107 this.proxy = theProxy;
110 public void setGroupId(String theGroupId) {
111 this.groupId = theGroupId;
114 public String getGroupId() {
118 public void setNameSperator(String theNameSeparator) {
119 this.nameSeparator = theNameSeparator;
122 public String getNameSeparator() {
123 return this.nameSeparator;
127 * Prepare a RestTemplate fitted for Nexus interactions, in particular ready to perform preemptive basic authentication.
128 * @return RestTemplate
130 public RestTemplate getNexusClient() {
132 RestTemplateBuilder builder =
133 new RestTemplateBuilder()
135 () -> new HttpComponentsClientHttpRequestFactory(this.localIfConfig.buildClient()) {
137 protected HttpContext createHttpContext(HttpMethod theMethod, URI theUri) {
138 HttpHost nexusHost = new HttpHost(NexusConfiguration.this.url.getHost(), NexusConfiguration.this.url.getPort());
140 CredentialsProvider nexusCreds = new BasicCredentialsProvider();
141 nexusCreds.setCredentials(
142 new AuthScope(nexusHost.getHostName(), nexusHost.getPort()),
143 new UsernamePasswordCredentials(NexusConfiguration.this.username, NexusConfiguration.this.password));
145 AuthCache authCache = new BasicAuthCache();
146 BasicScheme basicAuth = new BasicScheme();
147 authCache.put(nexusHost, basicAuth);
149 HttpClientContext nexusContext = HttpClientContext.create();
150 nexusContext.setAuthCache(authCache);
151 nexusContext.setCredentialsProvider(nexusCreds);
156 return builder.build();