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