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.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;
23 * This class wraps HTTP-Method functionality and thereby abstracts from low
24 * level code to simplify the usage.
26 public class HighLevelRestApi {
29 * This method implements the HTTP Put Method
31 * @param uri Resource URI
32 * @param requestPayload Content which has to be put into the Resource
33 * @return ResponseCode of HTTP Interaction
35 @SuppressWarnings("deprecation")
36 public static HttpResponseMessage Put(String uri, String requestPayload, String acceptHeaderValue) {
38 PutMethod method = new PutMethod(uri);
39 // requestPayload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
42 HighLevelRestApi.setAcceptHeader(method, acceptHeaderValue);
43 method.setRequestBody(requestPayload);
45 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
47 // kill <?xml... in front of response
48 HighLevelRestApi.cleanResponseBody(responseMessage);
50 return responseMessage;
54 * This method implements the HTTP Post Method
56 * @param uri Resource URI
57 * @param requestPayload Content which has to be posted into the Resource
58 * @return ResponseCode of HTTP Interaction
60 @SuppressWarnings("deprecation")
61 public static HttpResponseMessage Post(String uri, String requestPayload, String acceptHeaderValue) {
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]));
72 method = new PostMethod(uri);
75 method.setRequestBody(requestPayload);
76 HighLevelRestApi.setAcceptHeader(method, acceptHeaderValue);
77 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
78 HighLevelRestApi.cleanResponseBody(responseMessage);
79 return responseMessage;
83 * This method implements the HTTP Get Method
85 * @param uri Resource URI
86 * @return Content represented by the Resource URI
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]));
100 method = new GetMethod(uri);
102 HighLevelRestApi.setAcceptHeader(method, acceptHeaderValue);
103 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
104 HighLevelRestApi.cleanResponseBody(responseMessage);
105 return responseMessage;
108 private static NameValuePair[] createNameValuePairArrayFromQuery(String query) {
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];
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;
125 return nameValuePairArray;
129 * This method implements the HTTP Delete Method
131 * @param uri Resource URI
132 * @return ResponseCode of HTTP Interaction
134 public static HttpResponseMessage Delete(String uri, String acceptHeaderValue) {
136 DeleteMethod method = new DeleteMethod(uri);
137 HighLevelRestApi.setAcceptHeader(method, acceptHeaderValue);
138 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
139 HighLevelRestApi.cleanResponseBody(responseMessage);
140 return responseMessage;
143 private static void setAcceptHeader(HttpMethodBase method, String value) {
144 if (!value.equals("")) {
145 method.setRequestHeader("Accept", value);
147 method.setRequestHeader("Accept", "application/xml");
151 private static void cleanResponseBody(HttpResponseMessage responseMessage) {
152 System.out.println("ResponseBody: \n");
153 System.out.println(responseMessage.getResponseBody());
155 String temp = responseMessage
158 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>",
161 responseMessage.setResponseBody(temp);