5fe1f48627d3e0b81cd35181a90f3c3b6b811f59
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 [ZTE] and others.
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
17 package org.openo.commontosca.catalog.model.plan.wso2;
18
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;
32
33 import java.io.IOException;
34
35 public class RestfulClient {
36   private static final String HTTP = "http";
37   private static final Logger logger = LoggerFactory.getLogger(RestfulClient.class);
38
39   enum HttpMethod {
40     GET, POST, PUT, DELETE
41   }
42
43   /**
44    * execute http.
45    * @param method http method
46    * @param ip ip
47    * @param port port
48    * @param url url
49    * @param body http body
50    * @return RestResponse
51    */
52   public static RestResponse executeHttp(HttpMethod method, String ip, int port, String url,
53       HttpEntity body) {
54     CloseableHttpClient httpclient = HttpClients.createDefault();
55     HttpResponse httpResponse = null;
56     RestResponse result = new RestResponse();
57     try {
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();
64       if (entity != null) {
65         result.setStatusCode(httpResponse.getStatusLine().getStatusCode());
66         result.setResult(EntityUtils.toString(entity));
67       }
68     } catch (Exception e1) {
69       logger.error("send get rest request error:", e1.getMessage());
70     } finally {
71       if (httpclient != null) {
72         try {
73           httpclient.close();
74         } catch (IOException e2) {
75           logger.error("close httpclient error:", e2.getMessage());
76         }
77       }
78     }
79     return result;
80   }
81
82   private static HttpRequest getRequest(HttpMethod method, String url, HttpEntity body) {
83     HttpRequest request = null;
84     switch (method) {
85       case GET:
86         request = new HttpGet(url);
87         break;
88       case POST:
89         request = new HttpPost(url);
90         ((HttpPost) request).setEntity(body);
91         break;
92       case PUT:
93         request = new HttpPut(url);
94         ((HttpPut) request).setEntity(body);
95         break;
96       case DELETE:
97         request = new HttpDelete(url);
98         break;
99       default:
100         break;
101     }
102     return request;
103   }
104
105   public static String get(String ip, int port, String url) {
106     return executeHttp(HttpMethod.GET, ip, port, url, null).getResult();
107   }
108
109   public static RestResponse delete(String ip, int port, String url) {
110     return executeHttp(HttpMethod.GET, ip, port, url, null);
111   }
112
113   public static RestResponse post(String ip, int port, String url, HttpEntity requestBody) {
114     return executeHttp(HttpMethod.POST, ip, port, url, requestBody);
115   }
116
117 }