376d0da2507c7e6da3eb69365206efa31108d0a1
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.highlevelrestapi / src / main / java / org / eclipse / winery / highlevelrestapi / HighLevelRestApi.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.HttpMethodBase;
16 import org.apache.commons.httpclient.NameValuePair;
17 import org.apache.commons.httpclient.methods.DeleteMethod;
18 import org.apache.commons.httpclient.methods.GetMethod;
19 import org.apache.commons.httpclient.methods.PostMethod;
20 import org.apache.commons.httpclient.methods.PutMethod;
21
22 /**
23  * This class wraps HTTP-Method functionality and thereby abstracts from low
24  * level code to simplify the usage.
25  */
26 public class HighLevelRestApi {
27         
28         /**
29          * This method implements the HTTP Put Method
30          * 
31          * @param uri Resource URI
32          * @param requestPayload Content which has to be put into the Resource
33          * @return ResponseCode of HTTP Interaction
34          */
35         @SuppressWarnings("deprecation")
36         public static HttpResponseMessage Put(String uri, String requestPayload, String acceptHeaderValue) {
37                 
38                 PutMethod method = new PutMethod(uri);
39                 // requestPayload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
40                 // requestPayload;
41                 
42                 HighLevelRestApi.setAcceptHeader(method, acceptHeaderValue);
43                 method.setRequestBody(requestPayload);
44                 
45                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
46                 
47                 // kill <?xml... in front of response
48                 HighLevelRestApi.cleanResponseBody(responseMessage);
49                 
50                 return responseMessage;
51         }
52         
53         /**
54          * This method implements the HTTP Post Method
55          * 
56          * @param uri Resource URI
57          * @param requestPayload Content which has to be posted into the Resource
58          * @return ResponseCode of HTTP Interaction
59          */
60         @SuppressWarnings("deprecation")
61         public static HttpResponseMessage Post(String uri, String requestPayload, String acceptHeaderValue) {
62                 
63                 PostMethod method = null;
64                 if (uri.contains("?")) {
65                         System.out.println("Found query trying to split");
66                         String[] split = uri.split("\\?");
67                         System.out.println("Raw URI part: " + split[0]);
68                         System.out.println("Raw Query part: " + split[1]);
69                         method = new PostMethod(split[0]);
70                         method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
71                 } else {
72                         method = new PostMethod(uri);
73                         ;
74                 }
75                 method.setRequestBody(requestPayload);
76                 HighLevelRestApi.setAcceptHeader(method, acceptHeaderValue);
77                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
78                 HighLevelRestApi.cleanResponseBody(responseMessage);
79                 return responseMessage;
80         }
81         
82         /**
83          * This method implements the HTTP Get Method
84          * 
85          * @param uri Resource URI
86          * @return Content represented by the Resource URI
87          */
88         public static HttpResponseMessage Get(String uri, String acceptHeaderValue) {
89                 System.out.println("Setting URI to: \n");
90                 System.out.println(uri);
91                 GetMethod method = null;
92                 if (uri.contains("?")) {
93                         System.out.println("Found query trying to split");
94                         String[] split = uri.split("\\?");
95                         System.out.println("Raw URI part: " + split[0]);
96                         System.out.println("Raw Query part: " + split[1]);
97                         method = new GetMethod(split[0]);
98                         method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
99                 } else {
100                         method = new GetMethod(uri);
101                 }
102                 HighLevelRestApi.setAcceptHeader(method, acceptHeaderValue);
103                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
104                 HighLevelRestApi.cleanResponseBody(responseMessage);
105                 return responseMessage;
106         }
107         
108         private static NameValuePair[] createNameValuePairArrayFromQuery(String query) {
109                 // example:
110                 // csarID=Moodle.csar&serviceTemplateID={http://www.example.com/tosca/ServiceTemplates/Moodle}Moodle&nodeTemplateID={http://www.example.com/tosca/ServiceTemplates/Moodle}VmApache
111                 System.out.println("Splitting query: " + query);
112                 String[] pairs = query.trim().split("&");
113                 NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length];
114                 int count = 0;
115                 for (String pair : pairs) {
116                         System.out.println("Splitting query pair: " + pair);
117                         String[] keyValue = pair.split("=");
118                         NameValuePair nameValuePair = new NameValuePair();
119                         System.out.println("Key: " + keyValue[0] + " Value: " + keyValue[1]);
120                         nameValuePair.setName(keyValue[0]);
121                         nameValuePair.setValue(keyValue[1]);
122                         nameValuePairArray[count] = nameValuePair;
123                         count++;
124                 }
125                 return nameValuePairArray;
126         }
127         
128         /**
129          * This method implements the HTTP Delete Method
130          * 
131          * @param uri Resource URI
132          * @return ResponseCode of HTTP Interaction
133          */
134         public static HttpResponseMessage Delete(String uri, String acceptHeaderValue) {
135                 
136                 DeleteMethod method = new DeleteMethod(uri);
137                 HighLevelRestApi.setAcceptHeader(method, acceptHeaderValue);
138                 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
139                 HighLevelRestApi.cleanResponseBody(responseMessage);
140                 return responseMessage;
141         }
142         
143         private static void setAcceptHeader(HttpMethodBase method, String value) {
144                 if (!value.equals("")) {
145                         method.setRequestHeader("Accept", value);
146                 } else {
147                         method.setRequestHeader("Accept", "application/xml");
148                 }
149         }
150         
151         private static void cleanResponseBody(HttpResponseMessage responseMessage) {
152                 System.out.println("ResponseBody: \n");
153                 System.out.println(responseMessage.getResponseBody());
154                 // @formatter:off
155                 String temp = responseMessage
156                                 .getResponseBody()
157                                 .replace(
158                                                 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>",
159                                                 "");
160                 // @formatter:on
161                 responseMessage.setResponseBody(temp);
162         }
163 }