2779df5563b1b9903401c36311333ac1b2fe4f0f
[ccsdk/dashboard.git] /
1 /*******************************************************************************
2  * =============LICENSE_START=========================================================
3  *
4  * =================================================================================
5  *  Copyright (c) 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
10  *  
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *  
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=========================================================
19  *
20  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  *******************************************************************************/
22 package org.onap.oom.dashboard.rest;
23
24 import java.net.URI;
25
26 import org.apache.http.HttpHost;
27 import org.apache.http.client.AuthCache;
28 import org.apache.http.client.protocol.HttpClientContext;
29 import org.apache.http.impl.auth.BasicScheme;
30 import org.apache.http.impl.client.BasicAuthCache;
31 import org.apache.http.protocol.BasicHttpContext;
32 import org.apache.http.protocol.HttpContext;
33 import org.springframework.http.HttpMethod;
34 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
35
36 /**
37  * Utility class to enable Basic HTTP Authentication with Spring REST templates.
38  *   
39  * From:
40  * http://www.baeldung.com/2012/04/16/how-to-use-resttemplate-with-basic-authentication-in-spring-3-1/
41  */
42 public class HttpComponentsClientHttpRequestFactoryBasicAuth extends HttpComponentsClientHttpRequestFactory {
43
44         private HttpHost host;
45
46         /**
47          * @param host
48          * HttpHost
49          */
50         public HttpComponentsClientHttpRequestFactoryBasicAuth(HttpHost host) {
51                 super();
52                 this.host = host;
53         }
54
55     @Override
56     protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
57         return createHttpContext();
58     }
59
60         private HttpContext createHttpContext() {
61                 // Create AuthCache instance
62                 AuthCache authCache = new BasicAuthCache();
63                 // Generate BASIC scheme object and add it to the local auth cache
64                 BasicScheme basicAuth = new BasicScheme();
65                 authCache.put(host, basicAuth);
66
67                 // Add AuthCache to the execution context
68                 BasicHttpContext localcontext = new BasicHttpContext();
69                 localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
70                 return localcontext;
71         }
72 }