Merge "Enable long-running processes in ControllerExecutionBB"
[so.git] / common / src / main / java / org / onap / so / client / HttpClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
21 package org.onap.so.client;
22
23 import static org.apache.commons.lang3.StringUtils.isNotBlank;
24 import java.net.URL;
25 import java.util.Optional;
26 import javax.ws.rs.core.MultivaluedMap;
27 import org.javatuples.Pair;
28 import org.onap.logging.filter.base.ONAPComponentsList;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class HttpClient extends RestClient {
33
34     protected final Logger log = LoggerFactory.getLogger(HttpClient.class);
35     private ONAPComponentsList targetEntity;
36
37     HttpClient(URL host, String contentType, ONAPComponentsList targetEntity) {
38         super(host, contentType);
39         this.targetEntity = targetEntity;
40     }
41
42     HttpClient(URL host, String acceptType, String contentType, ONAPComponentsList targetEntity) {
43         super(host, acceptType, contentType);
44         this.targetEntity = targetEntity;
45     }
46
47     @Override
48     public ONAPComponentsList getTargetEntity() {
49         return targetEntity;
50     }
51
52     @Override
53     protected void initializeHeaderMap(MultivaluedMap<String, Pair<String, String>> headerMap) {}
54
55     @Override
56     protected Optional<ResponseExceptionMapper> addResponseExceptionMapper() {
57         return Optional.empty();
58     }
59
60     /**
61      * Adds a basic authentication header to the request.
62      * 
63      * @param auth the encrypted credentials
64      * @param key the key for decrypting the credentials
65      */
66     @Override
67     public void addBasicAuthHeader(String auth, String key) {
68         if (isNotBlank(auth) && isNotBlank(key)) {
69             super.addBasicAuthHeader(auth, key);
70         } else {
71             log.warn("Not adding basic auth to headers.");
72         }
73     }
74
75     /**
76      * Adds an additional header to the header map
77      * 
78      * @param encoded basic auth value
79      */
80     public void addAdditionalHeader(String name, String value) {
81         try {
82             if (isNotBlank(name) && isNotBlank(value)) {
83                 headerMap.add("ALL", Pair.with(name, value));
84             } else {
85                 log.warn("Not adding " + name + " to headers.");
86             }
87         } catch (Exception e) {
88             logger.error(e.getMessage(), e);
89         }
90     }
91
92     public void setAcceptType(String value) {
93         try {
94             if (isNotBlank(value)) {
95                 super.accept = value;
96             } else {
97                 log.warn("Not adding accept to headers.");
98             }
99         } catch (Exception e) {
100             logger.error(e.getMessage(), e);
101         }
102     }
103
104 }