2 * Copyright 2016 ZTE Corporation.
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.openo.commontosca.catalog.model.plan.wso2;
18 import org.apache.http.HttpEntity;
19 import org.apache.http.HttpHost;
20 import org.apache.http.HttpRequest;
21 import org.apache.http.HttpResponse;
22 import org.apache.http.client.methods.HttpDelete;
23 import org.apache.http.client.methods.HttpGet;
24 import org.apache.http.client.methods.HttpPost;
25 import org.apache.http.client.methods.HttpPut;
26 import org.apache.http.impl.client.CloseableHttpClient;
27 import org.apache.http.impl.client.HttpClients;
28 import org.apache.http.util.EntityUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import java.io.IOException;
34 public class RestfulClient {
35 private static final String HTTP = "http";
36 private static final Logger logger = LoggerFactory.getLogger(RestfulClient.class);
39 GET, POST, PUT, DELETE
44 * @param method http method
48 * @param body http body
49 * @return RestResponse
51 public static RestResponse executeHttp(HttpMethod method, String ip, int port, String url,
53 CloseableHttpClient httpclient = HttpClients.createDefault();
54 HttpResponse httpResponse = null;
55 RestResponse result = new RestResponse();
57 // specify the host, protocol, and port
58 HttpHost target = new HttpHost(ip, port, HTTP);
59 // specify the get request
60 HttpRequest request = getRequest(method, url, body);
61 httpResponse = httpclient.execute(target, request);
62 HttpEntity entity = httpResponse.getEntity();
64 result.setStatusCode(httpResponse.getStatusLine().getStatusCode());
65 result.setResult(EntityUtils.toString(entity));
67 } catch (Exception e1) {
68 logger.error("send get rest request error:", e1.getMessage());
70 if (httpclient != null) {
73 } catch (IOException e2) {
74 logger.error("close httpclient error:", e2.getMessage());
81 private static HttpRequest getRequest(HttpMethod method, String url, HttpEntity body) {
82 HttpRequest request = null;
85 request = new HttpGet(url);
88 request = new HttpPost(url);
89 ((HttpPost) request).setEntity(body);
92 request = new HttpPut(url);
93 ((HttpPut) request).setEntity(body);
96 request = new HttpDelete(url);
104 public static String get(String ip, int port, String url) {
105 return executeHttp(HttpMethod.GET, ip, port, url, null).getResult();
108 public static RestResponse delete(String ip, int port, String url) {
109 return executeHttp(HttpMethod.GET, ip, port, url, null);
112 public static RestResponse post(String ip, int port, String url, HttpEntity requestBody) {
113 return executeHttp(HttpMethod.POST, ip, port, url, requestBody);