9f5c8460a112447cacca16cf9267f9da558de597
[so.git] / mso-api-handlers / mso-api-handler-common / src / main / java / org / onap / so / apihandler / common / RequestClient.java
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
28 import org.apache.http.HttpResponse;
29 import org.apache.http.client.ClientProtocolException;
30 import org.apache.http.client.HttpClient;
31 import org.onap.so.utils.CryptoUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.core.env.Environment;
35
36 public abstract class RequestClient {
37         private static Logger logger = LoggerFactory.getLogger(RequestClient.class);
38         protected Environment props;
39         protected String url;
40         protected HttpClient client;
41         private int type;
42         
43         public RequestClient(int type){
44                 this.type = type;
45         }
46         
47         public void setProps(Environment env) {
48                 this.props = env;
49         }
50
51         public void setUrl(String url) {
52                 this.url = url;
53         }
54         
55         public String getUrl() {
56                 return url;
57         }
58
59         public int getType(){
60                 return type;
61         }
62
63         public HttpClient getClient() {
64                 return client;
65         }
66
67         public void setClient(HttpClient client) {
68                 this.client = client;
69         }
70
71         public abstract HttpResponse post(String request, String requestId, String requestTimeout, String schemaVersion, 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) throws ClientProtocolException, IOException;
76
77         public abstract HttpResponse get() 
78                                         throws IOException;
79         
80         protected String decryptPropValue(String prop, String defaultValue, String encryptionKey) {
81                  try {
82                          String result = CryptoUtils.decrypt(prop, encryptionKey);
83                          return result;
84                  }      
85                  catch (GeneralSecurityException e) {
86                          logger.debug("Security exception", e);
87                  }
88                  return defaultValue;
89          }
90         
91         protected String getEncryptedPropValue (String prop, String defaultValue, String encryptionKey) {
92                  try {
93                          String result = CryptoUtils.decrypt(prop, encryptionKey);
94                          return result;
95                  }      
96                  catch (GeneralSecurityException e) {
97                          logger.debug("Security exception", e);
98                  }
99                  return defaultValue;
100          }
101
102         
103
104
105 }