2 * Copyright 2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vnfsdk.marketplace.rest;
18 import java.io.IOException;
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;
36 public class RestfulClient {
37 private static final String HTTP = "http";
38 private static final Logger logger = LoggerFactory.getLogger(RestfulClient.class);
41 GET, POST, PUT, DELETE
46 * @param method http method
50 * @param body http body
51 * @return RestResponse
53 public static RestResponse executeHttp(HttpMethod method, String ip, int port, String url,
55 RestResponse result = new RestResponse();
57 CloseableHttpClient httpclient = HttpClients.createDefault();
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();
66 result.setStatusCode(httpResponse.getStatusLine().getStatusCode());
67 result.setResult(EntityUtils.toString(entity));
69 } catch (Exception e1) {
70 logger.error("send get rest request error:", e1);
75 private static HttpRequest getRequest(HttpMethod method, String url, HttpEntity body) {
76 HttpRequest request = null;
79 request = new HttpGet(url);
82 request = new HttpPost(url);
83 ((HttpPost) request).setEntity(body);
86 request = new HttpPut(url);
87 ((HttpPut) request).setEntity(body);
90 request = new HttpDelete(url);
98 public static RestResponse get(String ip, int port, String url) {
99 return executeHttp(HttpMethod.GET, ip, port, url, null);
102 public static RestResponse delete(String ip, int port, String url) {
103 return executeHttp(HttpMethod.DELETE, ip, port, url, null);
106 public static RestResponse post(String ip, int port, String url, HttpEntity requestBody) {
107 return executeHttp(HttpMethod.POST, ip, port, url, requestBody);
110 public static RestResponse sendPostRequest(String ip, String port, String url, String strJson)
112 RestResponse result = new RestResponse();
113 CloseableHttpClient httpClient = HttpClientBuilder.create().build();
114 HttpResponse httpResponse = null;
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);
121 HttpPost request = new HttpPost(urlPost);
123 StringEntity params = new StringEntity(strJson);
124 request.addHeader("content-type", "application/json");
125 request.setEntity(params);
127 httpResponse = httpClient.execute(request);
128 HttpEntity entity = httpResponse.getEntity();
131 result.setStatusCode(httpResponse.getStatusLine().getStatusCode());
132 result.setResult(EntityUtils.toString(entity));
137 logger.error("Send Post request error:", ex);
142 if(null != httpClient) {
146 catch(IOException e){
147 logger.error("IOException :Send Post request error:", e);