1 /*******************************************************************************
2 * Copyright (c) 2013 University of Stuttgart.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * Uwe Breitenbücher - initial API and implementation
11 * Kálmán Képes - improvements
12 *******************************************************************************/
13 package org.eclipse.winery.highlevelrestapi;
15 import org.apache.commons.httpclient.HttpClient;
16 import org.apache.commons.httpclient.HttpMethod;
19 * This static-class eases HTTP-method execution by self-managed fault-handling
20 * and automated Response-information processing
22 public class LowLevelRestApi {
24 // Local HttpClient used for every communication (Singleton implementation)
25 private static HttpClient httpClient = new HttpClient();
29 * Executes a passed HttpMethod (Method type is either PUT, POST, GET or
30 * DELETE) and returns a HttpResponseMessage
32 * @param method Method to execute
33 * @return HttpResponseMessage which contains all information about the
36 public static HttpResponseMessage executeHttpMethod(HttpMethod method) {
38 HttpResponseMessage responseMessage = null;
41 System.out.println("Method invocation on URI: \n");
42 System.out.println(method.getURI().toString());
44 LowLevelRestApi.httpClient.executeMethod(method);
45 responseMessage = LowLevelRestApi.extractResponseInformation(method);
47 } catch (Exception e) {
51 // Release Connection anyway
52 method.releaseConnection();
55 // Extract response information and return
56 return responseMessage;
60 * Extracts the response information from an executed HttpMethod
62 * @param method Executed Method
63 * @return Packaged response information
65 private static HttpResponseMessage extractResponseInformation(HttpMethod method) {
66 // Create and return HttpResponseMethod
67 HttpResponseMessage responseMessage = new HttpResponseMessage();
68 responseMessage.setStatusCode(method.getStatusCode());
70 responseMessage.setResponseBody(method.getResponseBodyAsString());
71 } catch (Exception e) {
74 return responseMessage;