Code Improvements-Vnfsdk-refrepo sonar issue fixes
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / main / java / org / onap / vnfsdk / marketplace / rest / RestfulClient.java
1 /**
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vnfsdk.marketplace.rest;
17
18 import java.io.IOException;
19
20 import org.apache.http.HttpEntity;
21 import org.apache.http.HttpHost;
22 import org.apache.http.HttpRequest;
23 import org.apache.http.HttpResponse;
24 import org.apache.http.client.methods.HttpDelete;
25 import org.apache.http.client.methods.HttpGet;
26 import org.apache.http.client.methods.HttpPost;
27 import org.apache.http.client.methods.HttpPut;
28 import org.apache.http.entity.StringEntity;
29 import org.apache.http.impl.client.CloseableHttpClient;
30 import org.apache.http.impl.client.HttpClientBuilder;
31 import org.apache.http.impl.client.HttpClients;
32 import org.apache.http.util.EntityUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class RestfulClient {
37     private static final String HTTP = "http";
38     private static final Logger logger = LoggerFactory.getLogger(RestfulClient.class);
39
40     enum HttpMethod {
41         GET, POST, PUT, DELETE
42     }
43
44     /**
45      * execute http.
46      * @param method http method
47      * @param ip ip
48      * @param port port
49      * @param url url
50      * @param body http body
51      * @return RestResponse
52      */
53     public static RestResponse executeHttp(HttpMethod method, String ip, int port, String url,
54             HttpEntity body) {
55         RestResponse result = new RestResponse();
56         try (
57             CloseableHttpClient httpclient = HttpClients.createDefault();
58         ){
59             // specify the host, protocol, and port
60             HttpHost target = new HttpHost(ip, port, HTTP);
61             // specify the get request
62             HttpRequest request = getRequest(method, url, body);
63             HttpResponse httpResponse = httpclient.execute(target, request);
64             HttpEntity entity = httpResponse.getEntity();
65             if (entity != null) {
66                 result.setStatusCode(httpResponse.getStatusLine().getStatusCode());
67                 result.setResult(EntityUtils.toString(entity));
68             }
69         } catch (Exception e1) {
70             logger.error("send get rest request error:", e1);
71         }
72         return result;
73     }
74
75     private static HttpRequest getRequest(HttpMethod method, String url, HttpEntity body) {
76         HttpRequest request = null;
77         switch (method) {
78             case GET:
79                 request = new HttpGet(url);
80                 break;
81             case POST:
82                 request = new HttpPost(url);
83                 ((HttpPost) request).setEntity(body);
84                 break;
85             case PUT:
86                 request = new HttpPut(url);
87                 ((HttpPut) request).setEntity(body);
88                 break;
89             case DELETE:
90                 request = new HttpDelete(url);
91                 break;
92             default:
93                 break;
94         }
95         return request;
96     }
97
98     public static RestResponse get(String ip, int port, String url) {
99         return executeHttp(HttpMethod.GET, ip, port, url, null);
100     }
101
102     public static RestResponse delete(String ip, int port, String url) {
103         return executeHttp(HttpMethod.DELETE, ip, port, url, null);
104     }
105
106     public static RestResponse post(String ip, int port, String url, HttpEntity requestBody) {
107         return executeHttp(HttpMethod.POST, ip, port, url, requestBody);
108     }
109
110     public static RestResponse sendPostRequest(String ip, String  port, String url, String strJson)
111     {
112         RestResponse result = new RestResponse();
113         CloseableHttpClient httpClient = HttpClientBuilder.create().build();
114         HttpResponse httpResponse = null;
115         try
116         {
117             String urlPost =  "http://" + ip + ":" + port + url;
118             logger.info("URL formed for Post, URL :{}" , urlPost);
119             logger.info("URL formed for Post, JSON :{}" , strJson);
120
121             HttpPost request = new HttpPost(urlPost);
122
123             StringEntity params = new StringEntity(strJson);
124             request.addHeader("content-type", "application/json");
125             request.setEntity(params);
126
127             httpResponse = httpClient.execute(request);
128             HttpEntity entity = httpResponse.getEntity();
129             if (entity != null)
130             {
131                 result.setStatusCode(httpResponse.getStatusLine().getStatusCode());
132                 result.setResult(EntityUtils.toString(entity));
133             }
134         }
135         catch (Exception ex)
136         {
137             logger.error("Send Post request error:", ex);
138         }
139         finally
140         {
141             try {
142                 if(null != httpClient) {
143                     httpClient.close();
144                 }
145             }
146             catch(IOException e){
147                 logger.error("IOException :Send Post request error:", e);
148             }
149         }
150         return result;
151     }
152 }
153