Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.highlevelrestapi / src / main / java / org / eclipse / winery / highlevelrestapi / LowLevelRestApi.java
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
8  *
9  * Contributors:
10  *     Uwe Breitenbücher - initial API and implementation
11  *     Kálmán Képes - improvements
12  *******************************************************************************/
13 package org.eclipse.winery.highlevelrestapi;
14
15 import org.apache.commons.httpclient.HttpClient;
16 import org.apache.commons.httpclient.HttpMethod;
17
18 /**
19  * This static-class eases HTTP-method execution by self-managed fault-handling
20  * and automated Response-information processing
21  */
22 public class LowLevelRestApi {
23         
24         // Local HttpClient used for every communication (Singleton implementation)
25         private static HttpClient httpClient = new HttpClient();
26         
27         
28         /**
29          * Executes a passed HttpMethod (Method type is either PUT, POST, GET or
30          * DELETE) and returns a HttpResponseMessage
31          * 
32          * @param method Method to execute
33          * @return HttpResponseMessage which contains all information about the
34          *         execution
35          */
36         public static HttpResponseMessage executeHttpMethod(HttpMethod method) {
37                 
38                 HttpResponseMessage responseMessage = null;
39                 
40                 try {
41                         System.out.println("Method invocation on URI: \n");
42                         System.out.println(method.getURI().toString());
43                         // Execute Request
44                         LowLevelRestApi.httpClient.executeMethod(method);
45                         responseMessage = LowLevelRestApi.extractResponseInformation(method);
46                         
47                 } catch (Exception e) {
48                         e.printStackTrace();
49                 } finally {
50                         
51                         // Release Connection anyway
52                         method.releaseConnection();
53                 }
54                 
55                 // Extract response information and return
56                 return responseMessage;
57         }
58         
59         /**
60          * Extracts the response information from an executed HttpMethod
61          * 
62          * @param method Executed Method
63          * @return Packaged response information
64          */
65         private static HttpResponseMessage extractResponseInformation(HttpMethod method) {
66                 // Create and return HttpResponseMethod
67                 HttpResponseMessage responseMessage = new HttpResponseMessage();
68                 responseMessage.setStatusCode(method.getStatusCode());
69                 try {
70                         responseMessage.setResponseBody(method.getResponseBodyAsString());
71                 } catch (Exception e) {
72                         e.printStackTrace();
73                 }
74                 return responseMessage;
75                 
76         }
77         
78 }