1 /*******************************************************************************
2 * Copyright (c) 2012-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 *******************************************************************************/
10 * Modifications Copyright 2016-2017 ZTE Corporation.
13 * This class wraps HTTP-Method functionality and thereby abstracts from low
14 * level code to simplify the usage.
16 package org.opentosca.bpel4restlight.rest;
18 import org.apache.commons.httpclient.HttpMethodBase;
19 import org.apache.commons.httpclient.NameValuePair;
20 import org.apache.commons.httpclient.methods.DeleteMethod;
21 import org.apache.commons.httpclient.methods.GetMethod;
22 import org.apache.commons.httpclient.methods.PostMethod;
23 import org.apache.commons.httpclient.methods.PutMethod;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.http.HttpHeaders;
29 public class HighLevelRestApi {
30 protected static final Log log = LogFactory.getLog(HighLevelRestApi.class);
32 * This method implements the HTTP Put Method
36 * @param requestPayload
37 * Content which has to be put into the Resource
38 * @return ResponseCode of HTTP Interaction
40 @SuppressWarnings("deprecation")
41 public static HttpResponseMessage Put(String uri, String requestPayload, String acceptHeaderValue, String contentTypeHeader) {
43 PutMethod method = new PutMethod(uri);
44 // requestPayload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
47 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
48 method.setRequestBody(requestPayload);
50 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
52 // kill <?xml... in front of response
53 HighLevelRestApi.cleanResponseBody(responseMessage);
55 return responseMessage;
59 * This method implements the HTTP Post Method
63 * @param requestPayload
64 * Content which has to be posted into the Resource
65 * @return ResponseCode of HTTP Interaction
67 @SuppressWarnings("deprecation")
68 public static HttpResponseMessage Post(String uri, String requestPayload, String acceptHeaderValue, String contentTypeHeader) {
70 PostMethod method = null;
71 if (uri.contains("?")) {
72 log.debug("Found query trying to split");
73 String[] split = uri.split("\\?");
74 log.debug("Raw URI part: " + split[0]);
75 log.debug("Raw Query part: " + split[1]);
76 method = new PostMethod(split[0]);
77 method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
79 method = new PostMethod(uri);
82 method.setRequestBody(requestPayload);
83 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
84 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
85 HighLevelRestApi.cleanResponseBody(responseMessage);
86 return responseMessage;
90 * This method implements the HTTP Get Method
94 * @return Content represented by the Resource URI
96 public static HttpResponseMessage Get(String uri, String acceptHeaderValue, String contentTypeHeader) {
97 log.debug("Setting URI to: \n");
99 GetMethod method = null;
100 if (uri.contains("?")) {
101 log.debug("Found query trying to split");
102 String[] split = uri.split("\\?");
103 log.debug("Raw URI part: " + split[0]);
104 log.debug("Raw Query part: " + split[1]);
106 method = new GetMethod(split[0]);
107 method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
109 method = new GetMethod(uri);
111 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
112 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
113 HighLevelRestApi.cleanResponseBody(responseMessage);
114 return responseMessage;
117 private static NameValuePair[] createNameValuePairArrayFromQuery(String query) {
119 // csarID=Moodle.csar&serviceTemplateID={http://www.example.com/tosca/ServiceTemplates/Moodle}Moodle&nodeTemplateID={http://www.example.com/tosca/ServiceTemplates/Moodle}VmApache
120 log.debug("Splitting query: " + query);
121 String[] pairs = query.trim().split("&");
122 NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length];
124 for (String pair : pairs) {
125 log.debug("Splitting query pair: " + pair);
126 String[] keyValue = pair.split("=");
127 NameValuePair nameValuePair = new NameValuePair();
128 log.debug("Key: " + keyValue[0] + " Value: " + keyValue[1]);
129 nameValuePair.setName(keyValue[0]);
130 nameValuePair.setValue(keyValue[1]);
131 nameValuePairArray[count] = nameValuePair;
134 return nameValuePairArray;
138 * This method implements the HTTP Delete Method
142 * @return ResponseCode of HTTP Interaction
144 public static HttpResponseMessage Delete(String uri, String acceptHeaderValue, String contentTypeHeader) {
146 DeleteMethod method = new DeleteMethod(uri);
147 HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
148 HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
149 HighLevelRestApi.cleanResponseBody(responseMessage);
150 return responseMessage;
153 private static void setHeader(HttpMethodBase method, String accept, String contentType) {
154 if (!"".equals(accept)) {
155 method.setRequestHeader(HttpHeaders.ACCEPT, accept);
157 method.setRequestHeader(HttpHeaders.ACCEPT, "application/xml");
160 if (contentType != null && !"".equals(contentType)) {
161 method.setRequestHeader(HttpHeaders.CONTENT_TYPE, contentType);
163 // method.setRequestHeader("Accept", accept);
168 private static void cleanResponseBody(HttpResponseMessage responseMessage) {
169 log.debug("ResponseBody: \n");
170 if (responseMessage != null && responseMessage.getResponseBody() != null) {
171 log.debug(responseMessage.getResponseBody());
172 String temp = responseMessage.getResponseBody()
173 .replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>", "");
174 responseMessage.setResponseBody(temp);