summaryrefslogtreecommitdiffstats
path: root/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApi.java
blob: cd8517b4566cf5372de39c6faa13a02398b13a80 (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
/**
 * Copyright 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.onap.workflow.activitiext.common.ConstString;
import org.onap.workflow.activitiext.common.RestInfo;
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 method
	 * @param uri
	 * @param requestBody
	 * @param accept
	 * @param contentType
	 * @return
	 */
	public static HttpResponseMessage invoke(RestInfo restInfo) throws Exception {

		String realUri = restInfo.getRealUri();
		String method = restInfo.getMethod();
		String requestBody = restInfo.getRequestBody();
		String accept = restInfo.getAccept();
		String contentType = restInfo.getContentType();

		logger.info("uri: " + realUri);
		logger.info("method: " + method);
		logger.info("requestbody: " + requestBody);
		logger.info("accept: " + accept);
		logger.info("content-type: " + contentType);
		
		HttpResponseMessage msg = new HttpResponseMessage();
		switch (method.toUpperCase()) {
		case ConstString.HTTP_GET:
			msg = HighLevelRestApi.Get(realUri, accept, contentType);
			break;
		case ConstString.HTTP_POST:
			msg = HighLevelRestApi.Post(realUri, requestBody, accept, contentType);
			break;
		case ConstString.HTTP_DELETE:
			msg = HighLevelRestApi.Delete(realUri, accept, contentType);
			break;
		case ConstString.HTTP_PUT:
			msg = HighLevelRestApi.Put(realUri, requestBody, accept, contentType);
			break;
		default:
			throw new ActivitiException("can not find the http method type '" + method + "'!");
		}
		return msg;
	}

	/**
	 * This method implements the HTTP Put Method
	 * 
	 * @param uri
	 *            Resource URI
	 * @param requestBody
	 *            Content which has to be put into the Resource
	 * @return ResponseCode of HTTP Interaction
	 */
	@SuppressWarnings("deprecation")
	public static HttpResponseMessage Put(String uri, String requestBody, String accept,
			String contentType) throws Exception {

		PutMethod method = new PutMethod(uri);

		HighLevelRestApi.setAcceptHeader(method, accept);
		HighLevelRestApi.setContentTypeHeader(method, contentType);
		method.setRequestBody(requestBody);

		HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);

		return responseMessage;
	}

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

		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(requestBody);
		HighLevelRestApi.setAcceptHeader(method, accept);
		HighLevelRestApi.setContentTypeHeader(method, contentType);
		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 accept, String contentType) throws Exception {
		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, accept);
		HighLevelRestApi.setContentTypeHeader(method, contentType);
		HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method);
		return responseMessage;
	}

	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 accept, String contentType)  throws Exception {

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

	private static void setAcceptHeader(HttpMethodBase method, String value) {
		if (StringUtils.isNotEmpty(value)) {
			if(value.startsWith("[")&&value.endsWith("]")){
				value = value.substring(1, value.length()-1);
			}
			
			method.setRequestHeader("Accept", value);
		}
	}

	private static void setContentTypeHeader(HttpMethodBase method, String value) {
		if (StringUtils.isNotEmpty(value)) {
			if(value.startsWith("[")&&value.endsWith("]")){
				value = value.substring(1, value.length()-1);
			}
			
			method.setRequestHeader("content-type", value);
		}
	}
}