Normalize to one trailing slash on Nexus URL
[federation.git] / gateway / src / main / java / org / acumos / federation / gateway / config / NexusConfiguration.java
1 /*-
2  * ===============LICENSE_START=======================================================
3  * Acumos
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
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
88         /**
89          * Get the normalized URL for the nexus content server.
90          * @return the URL, as a String, with exactly one trailing slash.
91          */
92
93         public String getUrl() {
94                 return this.url.toString().replaceAll("/*$", "") + "/";
95         }
96
97         public void setUsername(String theUsername) {
98                 this.username = theUsername;
99         }
100
101         public void setPassword(String thePassword) {
102                 this.password = thePassword;
103         }
104
105         @Deprecated
106         public void setProxy(String theProxy) {
107                 this.proxy = theProxy;
108         }
109
110         public void setGroupId(String theGroupId) {
111                 this.groupId = theGroupId;
112         }
113
114         public String getGroupId() {
115                 return this.groupId;
116         }
117
118         public void setNameSperator(String theNameSeparator) {
119                 this.nameSeparator = theNameSeparator;
120         }
121
122         public String getNameSeparator() {
123                 return this.nameSeparator;
124         }
125
126         /**
127          * Prepare a RestTemplate fitted for Nexus interactions, in particular ready to perform preemptive basic authentication.
128          * @return RestTemplate
129          */
130         public RestTemplate getNexusClient() {
131
132                 RestTemplateBuilder builder =
133                         new RestTemplateBuilder()
134                                 .requestFactory(
135                                         () -> new HttpComponentsClientHttpRequestFactory(this.localIfConfig.buildClient()) {
136
137                                                 protected HttpContext createHttpContext(HttpMethod theMethod, URI theUri) {
138                                                         HttpHost nexusHost = new HttpHost(NexusConfiguration.this.url.getHost(), NexusConfiguration.this.url.getPort());
139
140                                                         CredentialsProvider nexusCreds = new BasicCredentialsProvider();
141                                                         nexusCreds.setCredentials(
142                                         new AuthScope(nexusHost.getHostName(), nexusHost.getPort()),
143                                         new UsernamePasswordCredentials(NexusConfiguration.this.username, NexusConfiguration.this.password));
144
145                                                         AuthCache authCache = new BasicAuthCache();
146                                                         BasicScheme basicAuth = new BasicScheme();
147                                                         authCache.put(nexusHost, basicAuth);
148  
149                                                         HttpClientContext nexusContext = HttpClientContext.create();
150                                                         nexusContext.setAuthCache(authCache);
151                                                         nexusContext.setCredentialsProvider(nexusCreds);
152                                                         return nexusContext;
153                                                 }
154                                         });
155
156                 return builder.build();
157         }
158
159         
160
161 }