summaryrefslogtreecommitdiffstats
path: root/wso2/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/org/opentosca/bpel4restlight/rest/HighLevelRestApi.java
blob: ec109593fd38058f4cd7a816920fc52ce7336a94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*******************************************************************************
 * Copyright (c) 2012-2013 University of Stuttgart.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *******************************************************************************/
/*
 * Modifications Copyright 2016-2017 ZTE Corporation.
 */
/**
 * This class wraps HTTP-Method functionality and thereby abstracts from low
 * level code to simplify the usage.
 */
package org.opentosca.bpel4restlight.rest;

import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpHeaders;


public class HighLevelRestApi {
  protected static final Log log = LogFactory.getLog(HighLevelRestApi.class);
	/**
	 * This method implements the HTTP Put Method
	 * 
	 * @param uri
	 *            Resource URI
	 * @param requestPayload
	 *            Content which has to be put into the Resource
	 * @return ResponseCode of HTTP Interaction
	 */
	@SuppressWarnings("deprecation")
	public static HttpResponseMessage Put(String uri, String requestPayload, String acceptHeaderValue, String contentTypeHeader) {

		PutMethod method = new PutMethod(uri);
		// requestPayload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
		// requestPayload;

		HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
		method.setRequestBody(requestPayload);

		HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);

		// kill <?xml... in front of response
		HighLevelRestApi.cleanResponseBody(responseMessage);

		return responseMessage;
	}

	/**
	 * This method implements the HTTP Post Method
	 * 
	 * @param uri
	 *            Resource URI
	 * @param requestPayload
	 *            Content which has to be posted into the Resource
	 * @return ResponseCode of HTTP Interaction
	 */
	@SuppressWarnings("deprecation")
	public static HttpResponseMessage Post(String uri, String requestPayload, String acceptHeaderValue, String contentTypeHeader) {

		PostMethod method = null;
		if (uri.contains("?")) {
			log.debug("Found query trying to split");
			String[] split = uri.split("\\?");
			log.debug("Raw URI part: " + split[0]);
			log.debug("Raw Query part: " + split[1]);
			method = new PostMethod(split[0]);
			method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
		} else {
			method = new PostMethod(uri);
			;
		}
		method.setRequestBody(requestPayload);
		HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
		HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
		HighLevelRestApi.cleanResponseBody(responseMessage);
		return responseMessage;
	}

	/**
	 * This method implements the HTTP Get Method
	 * 
	 * @param uri
	 *            Resource URI
	 * @return Content represented by the Resource URI
	 */
	public static HttpResponseMessage Get(String uri, String acceptHeaderValue, String contentTypeHeader) {
		log.debug("Setting URI to: \n");
		log.debug(uri);
		GetMethod method = null;
		if (uri.contains("?")) {
		  log.debug("Found query trying to split");
			String[] split = uri.split("\\?");
			log.debug("Raw URI part: " + split[0]);
			log.debug("Raw Query part: " + split[1]);
			
			method = new GetMethod(split[0]);
			method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
		} else {
			method = new GetMethod(uri);
		}
		HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
		HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
		HighLevelRestApi.cleanResponseBody(responseMessage);
		return responseMessage;
	}

	private static NameValuePair[] createNameValuePairArrayFromQuery(String query) {
		// example:
		// csarID=Moodle.csar&serviceTemplateID={http://www.example.com/tosca/ServiceTemplates/Moodle}Moodle&nodeTemplateID={http://www.example.com/tosca/ServiceTemplates/Moodle}VmApache
		log.debug("Splitting query: " + query);
		String[] pairs = query.trim().split("&");
		NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length];
		int count = 0;
		for (String pair : pairs) {
		  log.debug("Splitting query pair: " + pair);
			String[] keyValue = pair.split("=");
			NameValuePair nameValuePair = new NameValuePair();
			log.debug("Key: " + keyValue[0] + " Value: " + keyValue[1]);
			nameValuePair.setName(keyValue[0]);
			nameValuePair.setValue(keyValue[1]);
			nameValuePairArray[count] = nameValuePair;
			count++;
		}
		return nameValuePairArray;
	}

	/**
	 * This method implements the HTTP Delete Method
	 * 
	 * @param uri
	 *            Resource URI
	 * @return ResponseCode of HTTP Interaction
	 */
	public static HttpResponseMessage Delete(String uri, String acceptHeaderValue, String contentTypeHeader) {

		DeleteMethod method = new DeleteMethod(uri);
		HighLevelRestApi.setHeader(method, acceptHeaderValue, contentTypeHeader);
		HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
		HighLevelRestApi.cleanResponseBody(responseMessage);
		return responseMessage;
	}

	private static void setHeader(HttpMethodBase method, String accept, String contentType) {
		if (!"".equals(accept)) {
			method.setRequestHeader(HttpHeaders.ACCEPT, accept);
		} else {
			method.setRequestHeader(HttpHeaders.ACCEPT, "application/xml");
		}
		
		if (contentType != null && !"".equals(contentType)) {
		  method.setRequestHeader(HttpHeaders.CONTENT_TYPE, contentType);
        } else {
//          method.setRequestHeader("Accept", accept);
        }
		
	}

	private static void cleanResponseBody(HttpResponseMessage responseMessage) {
	  log.debug("ResponseBody: \n");
	  if (responseMessage != null && responseMessage.getResponseBody() != null) {  
	    log.debug(responseMessage.getResponseBody());
	    String temp = responseMessage.getResponseBody()
	        .replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>", "");
	    responseMessage.setResponseBody(temp);
	  }
	}
	
}