9b7801711fd27d7ea515998afcece8de117a601c
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.apihandler.common;
24
25 import java.io.IOException;
26 import java.security.GeneralSecurityException;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.client.ClientProtocolException;
29 import org.apache.http.client.HttpClient;
30 import org.onap.so.utils.CryptoUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.core.env.Environment;
34
35 public abstract class RequestClient {
36     private static Logger logger = LoggerFactory.getLogger(RequestClient.class);
37     protected Environment props;
38     protected String url;
39     protected HttpClient client;
40     private int type;
41
42     public RequestClient(int type) {
43         this.type = type;
44     }
45
46     public void setProps(Environment env) {
47         this.props = env;
48     }
49
50     public void setUrl(String url) {
51         this.url = url;
52     }
53
54     public String getUrl() {
55         return url;
56     }
57
58     public int getType() {
59         return type;
60     }
61
62     public HttpClient getClient() {
63         return client;
64     }
65
66     public void setClient(HttpClient client) {
67         this.client = client;
68     }
69
70     public abstract HttpResponse post(String request, String requestId, String requestTimeout, String schemaVersion,
71             String serviceInstanceId, String action) throws IOException;
72
73     public abstract HttpResponse post(String request) throws ClientProtocolException, IOException;
74
75     public abstract HttpResponse post(RequestClientParameter parameterObject)
76             throws ClientProtocolException, IOException;
77
78     public abstract HttpResponse get() throws IOException;
79
80     protected String decryptPropValue(String prop, String defaultValue, String encryptionKey) {
81         try {
82             return CryptoUtils.decrypt(prop, encryptionKey);
83         } catch (GeneralSecurityException e) {
84             logger.debug("Security exception", e);
85         }
86         return defaultValue;
87     }
88
89     protected String getEncryptedPropValue(String prop, String defaultValue, String encryptionKey) {
90         try {
91             return CryptoUtils.decrypt(prop, encryptionKey);
92         } catch (GeneralSecurityException e) {
93             logger.debug("Security exception", e);
94         }
95         return defaultValue;
96     }
97 }