66e988ced19fdce42a39561dc457e09e335657c7
[federation.git] / gateway / src / main / java / org / acumos / federation / gateway / config / NexusConfiguration.java
1 /*-
2  * ===============LICENSE_START=======================================================
3  * Acumos
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
11  *  
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *  
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=========================================================
19  */
20
21 package org.acumos.federation.gateway.config;
22
23 import java.lang.invoke.MethodHandles;
24 import java.net.MalformedURLException;
25 import java.net.URI;
26 import java.net.URL;
27
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;
47
48
49 /**
50  * 
51  */
52 @Component
53 @ConfigurationProperties(prefix = "nexus")
54 public class NexusConfiguration {
55
56         private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
57
58         private String          proxy;
59         private String    groupId;
60         private String    id;
61         private URL                             url;
62         private String          username;
63         private String          password;
64         private String          nameSeparator;
65         @Autowired
66         private LocalInterfaceConfiguration localIfConfig = null;
67
68         public NexusConfiguration() {
69                 reset();
70         }
71
72         private void reset() {
73                 //defaults
74                 this.id = "1";
75                 this.groupId = null;
76                 this.nameSeparator = ".";
77         }
78
79         public void setId(String theId) {
80                 this.id = theId;
81         }
82
83         public void setUrl(String theSpec) throws MalformedURLException {
84                 this.url = new URL(theSpec);
85   }
86         
87         public String getUrl() {
88                 return this.url.toString();
89   }
90
91         public void setUsername(String theUsername) {
92                 this.username = theUsername;
93         }
94
95         public void setPassword(String thePassword) {
96                 this.password = thePassword;
97         }
98
99         @Deprecated
100         public void setProxy(String theProxy) {
101                 this.proxy = theProxy;
102         }
103
104         public void setGroupId(String theGroupId) {
105                 this.groupId = theGroupId;
106         }
107
108         public String getGroupId() {
109                 return this.groupId;
110         }
111
112         public void setNameSperator(String theNameSeparator) {
113                 this.nameSeparator = theNameSeparator;
114         }
115
116         public String getNameSeparator() {
117                 return this.nameSeparator;
118         }
119
120         /**
121          * Prepare a RestTemplate fitted for Nexus interactions, in particular ready to perform preemptive basic authentication.
122          * @return RestTemplate
123          */
124         public RestTemplate getNexusClient() {
125
126                 RestTemplateBuilder builder =
127                         new RestTemplateBuilder()
128                                 .requestFactory(
129                                         () -> new HttpComponentsClientHttpRequestFactory(this.localIfConfig.buildClient()) {
130
131                                                 protected HttpContext createHttpContext(HttpMethod theMethod, URI theUri) {
132                                                         HttpHost nexusHost = new HttpHost(NexusConfiguration.this.url.getHost(), NexusConfiguration.this.url.getPort());
133
134                                                         CredentialsProvider nexusCreds = new BasicCredentialsProvider();
135                                                         nexusCreds.setCredentials(
136                                         new AuthScope(nexusHost.getHostName(), nexusHost.getPort()),
137                                         new UsernamePasswordCredentials(NexusConfiguration.this.username, NexusConfiguration.this.password));
138
139                                                         AuthCache authCache = new BasicAuthCache();
140                                                         BasicScheme basicAuth = new BasicScheme();
141                                                         authCache.put(nexusHost, basicAuth);
142  
143                                                         HttpClientContext nexusContext = HttpClientContext.create();
144                                                         nexusContext.setAuthCache(authCache);
145                                                         nexusContext.setCredentialsProvider(nexusCreds);
146                                                         return nexusContext;
147                                                 }
148                                         });
149
150                 return builder.build();
151         }
152
153         
154
155 }