2 * Copyright 2017 ZTE Corporation.
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.
16 package org.onap.workflow.activitiext.restservicetask;
18 import org.apache.commons.httpclient.HttpClient;
19 import org.apache.commons.httpclient.HttpMethod;
22 * This static-class eases HTTP-method execution by self-managed fault-handling and automated
23 * Response-information processing
25 public class LowLevelRestApi {
27 // Local HttpClient used for every communication (Singleton implementation)
28 private static HttpClient httpClient = new HttpClient();
32 * Executes a passed HttpMethod (Method type is either PUT, POST, GET or DELETE) and returns a
35 * @param method Method to execute
36 * @return HttpResponseMessage which contains all information about the execution
38 public static HttpResponseMessage executeHttpMethod(HttpMethod method) {
40 HttpResponseMessage responseMessage = null;
45 LowLevelRestApi.httpClient.executeMethod(method);
46 responseMessage = LowLevelRestApi.extractResponseInformation(method);
48 } catch (Exception e) {
52 // Release Connection anyway
53 method.releaseConnection();
56 // Extract response information and return
57 return responseMessage;
61 * Extracts the response information from an executed HttpMethod
63 * @param method Executed Method
64 * @return Packaged response information
66 private static HttpResponseMessage extractResponseInformation(HttpMethod method) {
67 // Create and return HttpResponseMethod
68 HttpResponseMessage responseMessage = new HttpResponseMessage();
69 responseMessage.setStatusCode(method.getStatusCode());
71 responseMessage.setResponseBody(method.getResponseBodyAsString());
72 } catch (Exception e) {
75 return responseMessage;