25dc873a1e4730e534b80a9b1a3667a6f802e891
[vfc/nfvo/wfengine.git] / activiti-extension / src / main / java / org / onap / workflow / activitiext / restservicetask / LowLevelRestApi.java
1 /**
2  * Copyright 2017 [ZTE] and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.onap.workflow.activitiext.restservicetask;
15
16 import org.apache.commons.httpclient.HttpClient;
17 import org.apache.commons.httpclient.HttpMethod;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import com.alibaba.fastjson.JSON;
22
23 /**
24  * This static-class eases HTTP-method execution by self-managed fault-handling
25  * and automated Response-information processing
26  */
27 public class LowLevelRestApi {
28
29         private static final Logger logger = LoggerFactory.getLogger(LowLevelRestApi.class);
30
31         // Local HttpClient used for every communication (Singleton implementation)
32         private static HttpClient httpClient = new HttpClient();
33
34         /**
35          * Executes a passed HttpMethod (Method type is either PUT, POST, GET or
36          * DELETE) and returns a HttpResponseMessage
37          * 
38          * @param method
39          *            Method to execute
40          * @return HttpResponseMessage which contains all information about the
41          *         execution
42          */
43         public static HttpResponseMessage executeHttpMethod(HttpMethod method) throws Exception {
44
45                 HttpResponseMessage responseMessage = null;
46
47                 try {
48
49                         // Execute Request
50                         LowLevelRestApi.httpClient.executeMethod(method);
51                         responseMessage = LowLevelRestApi.extractResponseInformation(method);
52                         
53                         logger.info("statusCode: {}", method.getStatusCode());
54                         logger.info("responseBody: {}", method.getResponseBodyAsString());
55                 } catch (Exception e) {
56                         logger.error("visit interface failed!", e);
57                         throw e;
58                 } finally {
59
60                         // Release Connection anyway
61                         method.releaseConnection();
62                 }
63
64                 // Extract response information and return
65                 return responseMessage;
66         }
67
68         /**
69          * Extracts the response information from an executed HttpMethod
70          * 
71          * @param method
72          *            Executed Method
73          * @return Packaged response information
74          */
75         private static HttpResponseMessage extractResponseInformation(HttpMethod method) {
76                 // Create and return HttpResponseMethod
77                 HttpResponseMessage responseMessage = new HttpResponseMessage();
78                 responseMessage.setStatusCode(method.getStatusCode());
79                 try {
80                         JSON json = (JSON) JSON.parse(method.getResponseBodyAsString());
81                         responseMessage.setResponseBody(json);
82                         json.toJSONString();
83                 } catch (Exception e) {
84                         logger.info("response is not a json!");
85                 }
86                 return responseMessage;
87         }
88
89 }