summaryrefslogtreecommitdiffstats
path: root/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApi.java
blob: 5dd207105f1cfcad3ba0e29f64438df3e6822031 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
 * Copyright 2016-2017 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.workflow.activitiext.restservicetask;

import org.activiti.engine.ActivitiException;
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.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 10222158
 *
 */
public class HighLevelRestApi {

	private static final Logger logger = LoggerFactory.getLogger(HighLevelRestApi.class);

	/**
	 * invoke http service
	 * 
	 * @param methodValue
	 * @param uri
	 * @param requestPayload
	 * @param acceptValue
	 * @param contentTypeValue
	 * @return
	 */
	public static HttpResponseMessage invoke(String methodValue, String uri, String requestPayload, String acceptValue,
			String contentTypeValue) throws Exception {

		// complete uri
		uri = completeUri(uri);

		logger.info("uri: " + uri);
		logger.info("method: " + methodValue);
		logger.info("requestbody: " + requestPayload);
		logger.info("accept: " + acceptValue);
		logger.info("content-type: " + contentTypeValue);

		HttpResponseMessage msg = new HttpResponseMessage();
		switch (methodValue.toUpperCase()) {
		case ConstString.HTTP_GET:
			msg = HighLevelRestApi.Get(uri, acceptValue, contentTypeValue);
			break;
		case ConstString.HTTP_POST:
			msg = HighLevelRestApi.Post(uri, requestPayload, acceptValue, contentTypeValue);
			break;
		case ConstString.HTTP_DELETE:
			msg = HighLevelRestApi.Delete(uri, acceptValue, contentTypeValue);
			break;
		case ConstString.HTTP_PUT:
			msg = HighLevelRestApi.Put(uri, requestPayload, acceptValue, contentTypeValue);
			break;
		default:
			throw new ActivitiException("can not find the http method type '" + methodValue + "'!");
		}
		return msg;
	}

	/**
	 * 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 acceptValue,
			String contentTypeValue) {

		PutMethod method = new PutMethod(uri);

		HighLevelRestApi.setAcceptHeader(method, acceptValue);
		HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
		method.setRequestBody(requestPayload);

		HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);

		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 acceptValue,
			String contentTypeValue) {

		PostMethod method = null;
		if (uri.contains("?")) {
			String[] split = uri.split("\\?");
			method = new PostMethod(split[0]);
			method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
		} else {
			method = new PostMethod(uri);
		}
		method.setRequestBody(requestPayload);
		HighLevelRestApi.setAcceptHeader(method, acceptValue);
		HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
		HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
		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 acceptValue, String contentTypeValue) {
		GetMethod method = null;
		if (uri.contains("?")) {
			String[] split = uri.split("\\?");
			method = new GetMethod(split[0]);
			method.setQueryString(HighLevelRestApi.createNameValuePairArrayFromQuery(split[1]));
		} else {
			method = new GetMethod(uri);
		}

		HighLevelRestApi.setAcceptHeader(method, acceptValue);
		HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
		HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
		return responseMessage;
	}

	private static NameValuePair[] createNameValuePairArrayFromQuery(String query) {
		String[] pairs = query.trim().split("&");
		NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length];
		int count = 0;
		for (String pair : pairs) {
			String[] keyValue = pair.split("=");
			NameValuePair nameValuePair = new NameValuePair();
			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 acceptValue, String contentTypeValue) {

		DeleteMethod method = new DeleteMethod(uri);
		HighLevelRestApi.setAcceptHeader(method, acceptValue);
		HighLevelRestApi.setContentTypeHeader(method, contentTypeValue);
		HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
		return responseMessage;
	}

	private static void setAcceptHeader(HttpMethodBase method, String value) {
		if (!StringUtils.isEmpty(value)) {
			method.setRequestHeader("Accept", value);
		} else {
			method.setRequestHeader("Accept", "application/xml");
		}
	}

	private static void setContentTypeHeader(HttpMethodBase method, String value) {
		if (!StringUtils.isEmpty(value)) {
			method.setRequestHeader("content-type", value);
		} else {
			method.setRequestHeader("content-type", "application/json");
		}
	}

	private static String completeUri(String uri) {

		String urlReg = "^http[\\s\\S]*";
		if (uri != null && !uri.matches(urlReg)) {
			uri = PropertyUtil.getBasePath() + uri;
		}

		return uri;
	}
}