2 * Copyright 2016 [ZTE] and others.
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.
17 package org.openo.commontosca.catalog.model.plan.wso2;
19 import org.apache.http.HttpEntity;
20 import org.apache.http.HttpHost;
21 import org.apache.http.HttpRequest;
22 import org.apache.http.HttpResponse;
23 import org.apache.http.client.methods.HttpDelete;
24 import org.apache.http.client.methods.HttpGet;
25 import org.apache.http.client.methods.HttpPost;
26 import org.apache.http.client.methods.HttpPut;
27 import org.apache.http.impl.client.CloseableHttpClient;
28 import org.apache.http.impl.client.HttpClients;
29 import org.apache.http.util.EntityUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import java.io.IOException;
35 public class RestfulClient {
36 private static final String HTTP = "http";
37 private static final Logger logger = LoggerFactory.getLogger(RestfulClient.class);
40 GET, POST, PUT, DELETE
45 * @param method http method
49 * @param body http body
50 * @return RestResponse
52 public static RestResponse executeHttp(HttpMethod method, String ip, int port, String url,
54 CloseableHttpClient httpclient = HttpClients.createDefault();
55 HttpResponse httpResponse = null;
56 RestResponse result = new RestResponse();
58 // specify the host, protocol, and port
59 HttpHost target = new HttpHost(ip, port, HTTP);
60 // specify the get request
61 HttpRequest request = getRequest(method, url, body);
62 httpResponse = httpclient.execute(target, request);
63 HttpEntity entity = httpResponse.getEntity();
65 result.setStatusCode(httpResponse.getStatusLine().getStatusCode());
66 result.setResult(EntityUtils.toString(entity));
68 } catch (Exception e1) {
69 logger.error("send get rest request error:", e1.getMessage());
71 if (httpclient != null) {
74 } catch (IOException e2) {
75 logger.error("close httpclient error:", e2.getMessage());
82 private static HttpRequest getRequest(HttpMethod method, String url, HttpEntity body) {
83 HttpRequest request = null;
86 request = new HttpGet(url);
89 request = new HttpPost(url);
90 ((HttpPost) request).setEntity(body);
93 request = new HttpPut(url);
94 ((HttpPut) request).setEntity(body);
97 request = new HttpDelete(url);
105 public static String get(String ip, int port, String url) {
106 return executeHttp(HttpMethod.GET, ip, port, url, null).getResult();
109 public static RestResponse delete(String ip, int port, String url) {
110 return executeHttp(HttpMethod.GET, ip, port, url, null);
113 public static RestResponse post(String ip, int port, String url, HttpEntity requestBody) {
114 return executeHttp(HttpMethod.POST, ip, port, url, requestBody);