9171c315d20325ce8bf044a839be0c46e27056bf
[vfc/nfvo/wfengine.git] / activiti-extension / src / main / java / org / onap / workflow / activitiext / restservicetask / LowLevelRestApi.java
1 /**
2  * Copyright 2017 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.onap.workflow.activitiext.restservicetask;
17
18 import org.apache.commons.httpclient.HttpClient;
19 import org.apache.commons.httpclient.HttpMethod;
20
21 /**
22  * This static-class eases HTTP-method execution by self-managed fault-handling and automated
23  * Response-information processing
24  */
25 public class LowLevelRestApi {
26
27   // Local HttpClient used for every communication (Singleton implementation)
28   private static HttpClient httpClient = new HttpClient();
29
30
31   /**
32    * Executes a passed HttpMethod (Method type is either PUT, POST, GET or DELETE) and returns a
33    * HttpResponseMessage
34    * 
35    * @param method Method to execute
36    * @return HttpResponseMessage which contains all information about the execution
37    */
38   public static HttpResponseMessage executeHttpMethod(HttpMethod method) {
39
40     HttpResponseMessage responseMessage = null;
41
42     try {
43
44       // Execute Request
45       LowLevelRestApi.httpClient.executeMethod(method);
46       responseMessage = LowLevelRestApi.extractResponseInformation(method);
47
48     } catch (Exception e) {
49       e.printStackTrace();
50     } finally {
51
52       // Release Connection anyway
53       method.releaseConnection();
54     }
55
56     // Extract response information and return
57     return responseMessage;
58   }
59
60   /**
61    * Extracts the response information from an executed HttpMethod
62    * 
63    * @param method Executed Method
64    * @return Packaged response information
65    */
66   private static HttpResponseMessage extractResponseInformation(HttpMethod method) {
67     // Create and return HttpResponseMethod
68     HttpResponseMessage responseMessage = new HttpResponseMessage();
69     responseMessage.setStatusCode(method.getStatusCode());
70     try {
71       responseMessage.setResponseBody(method.getResponseBodyAsString());
72     } catch (Exception e) {
73       e.printStackTrace();
74     }
75     return responseMessage;
76
77   }
78
79 }