aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCoreBPMN/src/main/java/org/onap/so/bpmn/core/ResponseBuilder.java
blob: fb794e251d2b2e33dab87c3b3c65db27f3c874cd (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.bpmn.core;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Used in the output variable mapping configuration of subflow call activity
 * tasks to normalize subflow responses.  The output mapping is normally set up
 * as follows.  Note that the order of these mappings is important!
 * <p>
 * OUTPUT MAPPING
 * <pre>
 *   SOURCE EXPRESSION                                      TARGET
 *   ${ResponseBuilder.buildWorkflowException(execution)}   WorkflowException
 *   ${ResponseBuilder.buildWorkflowResponse(execution)}    SomeResponseVariable
 * </pre>
 */
public class ResponseBuilder implements java.io.Serializable {
	private static final long serialVersionUID = 1L;
	private static final Logger logger = LoggerFactory.getLogger(ResponseBuilder.class);

	/**
	 * Creates a WorkflowException using data from the execution variables.
	 * If the variables do not indicate that there was an error, null
	 * is returned.
	 * @param execution the execution
	 */
	public WorkflowException buildWorkflowException(DelegateExecution execution) {

		String method = getClass().getSimpleName() + ".buildWorkflowException(" +
			"execution=" + execution.getId() +
			")";

		logger.debug("Entered " + method);

		String prefix = (String) execution.getVariable("prefix");
		String processKey = getProcessKey(execution);

		logger.debug("processKey=" + processKey);

		// See if there"s already a WorkflowException object in the execution.
		WorkflowException theException = (WorkflowException) execution.getVariable("WorkflowException");

		if (theException != null) {
			logger.debug("Exited " + method + " - propagated " + theException);
			return theException;
		}

		// Look in the legacy variables: ErrorResponse and ResponseCode

		String errorResponse = trimString(execution.getVariable(prefix + "ErrorResponse"), null);
		String responseCode = trimString(execution.getVariable(prefix + "ResponseCode"), null);
		logger.debug("errorResponse=" + errorResponse);
		logger.debug("responseCode=" + responseCode);
		if (errorResponse != null || !isOneOf(responseCode, null, "0", "200", "201", "202", "204")) {
			// This is an error condition.  We need to return a WorkflowExcpetion

			if (errorResponse == null) {
				// No errorResponse string.  See if there"s something in the Response variable
				String response = trimString(execution.getVariable(processKey + "Response"), null);
				if (response == null) {
					errorResponse = "Received response code " + responseCode + " from " + processKey;
				} else {
					errorResponse = response;
				}
			}

			// Some subflows may try to return a WorkflowException as XML in the
			// errorResponse. If provided, use the errorCode and errorMessage
			// from the XML

			String maybeXML = removeXMLNamespaces(errorResponse);

			String xmlErrorMessage = trimString(getXMLTextElement(maybeXML, "ErrorMessage"), null);
			String xmlErrorCode = trimString(getXMLTextElement(maybeXML, "ErrorCode"), null);

			if (xmlErrorMessage != null || xmlErrorCode != null) {
				logger.debug("xmlErrorMessage=" + xmlErrorMessage);
				logger.debug("xmlErrorCode=" + xmlErrorCode);

				if (xmlErrorMessage == null) {
					errorResponse = "Received error code " + xmlErrorCode + " from " + processKey;
				} else {
					errorResponse = xmlErrorMessage;
				}

				if (xmlErrorCode != null) {
					responseCode = xmlErrorCode;
				}
			}

			// Convert the responseCode to an integer

			int intResponseCode;

			try {
				intResponseCode = Integer.valueOf(responseCode);
			} catch (NumberFormatException e) {
				// Internal Error
				intResponseCode = 2000;
			}

			// Convert 3-digit HTTP response codes (we should not be using them here)
			// to appropriate 4-digit response codes

			if (intResponseCode < 1000) {
				if (intResponseCode >= 400 && intResponseCode <= 499) {
					// Invalid Message
					intResponseCode = 1002;
				} else {
					// Internal Error
					intResponseCode = 2000;
				}
			}

			// Create a new WorkflowException object

			theException = new WorkflowException(processKey, intResponseCode, errorResponse);
			execution.setVariable("WorkflowException", theException);
			logger.debug("Exited " + method + " - created " + theException);
			return theException;
		}

		logger.debug("Exited " + method + " - no WorkflowException");
		return null;
	}

	/**
	 * Returns the "Response" variable, unless the execution variables
	 * indicate there was an error. In that case, null is returned.
	 * @param execution the execution
	 */
	public Object buildWorkflowResponse(DelegateExecution execution) {

		String method = getClass().getSimpleName() + ".buildWorkflowResponse(" +
			"execution=" + execution.getId() +
			")";
		logger.debug("Entered " + method);

		String prefix = (String) execution.getVariable("prefix");
		String processKey = getProcessKey(execution);

		Object theResponse = null;

		WorkflowException theException = (WorkflowException) execution.getVariable("WorkflowException");
		String errorResponse = trimString(execution.getVariable(prefix + "ErrorResponse"), null);
		String responseCode = trimString(execution.getVariable(prefix + "ResponseCode"), null);

		if (theException == null && errorResponse == null &&
				isOneOf(responseCode, null, "0", "200", "201", "202", "204")) {

			theResponse = execution.getVariable("WorkflowResponse");

			if (theResponse == null) {
				theResponse = execution.getVariable(processKey + "Response");
			}
		}

		logger.debug("Exited " + method);
		return theResponse;
	}

	/**
	 * Checks if the specified item is one of the specified values.
	 * @param item the item
	 * @param values the list of values
	 * @return true if the item is in the list of values
	 */
	private boolean isOneOf(Object item, Object ... values) {
		if (values == null) {
			return item == null;
		}

		for (Object value : values) {
			if (value == null) {
				if (item == null) {
					return true;
				}
			} else {
				if (value.equals(item)) {
					return true;
				}
			}
		}

		return false;
	}

	/**
	 * Creates a string value of the specified object, trimming whitespace in
	 * the process.  If the result is null or empty, the specified empty string
	 * value is returned.  Otherwise the trimmed value is returned.  This method
	 * helps ensure consistent treatment of empty and null strings.
	 * @param object the object to convert (possibly null)
	 * @param emptyStringValue the desired value for empty results
	 */
	private String trimString(Object object, String emptyStringValue) {
		if (object == null) {
			return emptyStringValue;
		}

		String s = String.valueOf(object).trim();
		return s.equals("") ? emptyStringValue : s;
	}

	/**
	 * Returns the process definition key (i.e. the process name) from the
	 * execution.
	 * @param execution the execution
	 */
	private String getProcessKey(DelegateExecution execution) {
		Object testKey = execution.getVariable("testProcessKey");

		if (testKey instanceof String) {
			return (String) testKey;
		}

		return execution.getProcessEngineServices().getRepositoryService()
			.getProcessDefinition(execution.getProcessDefinitionId()).getKey();
	}

	/**
	 * Removes namespace definitions and prefixes from XML, if any.
	 */
	private String removeXMLNamespaces(String xml) {
		// remove xmlns declaration
		xml = xml.replaceAll("xmlns.*?(\"|\').*?(\"|\')", "");

		// remove opening tag prefix
		xml = xml.replaceAll("(<)(\\w+:)(.*?>)", "$1$3");

		// remove closing tags prefix
		xml = xml.replaceAll("(</)(\\w+:)(.*?>)", "$1$3");

		// remove extra spaces left when xmlns declarations are removed
		xml = xml.replaceAll("\\s+>", ">");

		return xml;
	}

	/**
	 * Extracts text from an XML element. This method is not namespace aware
	 * (namespaces are ignored).  The first matching element is selected.
	 * @param xml the XML document or fragment
	 * @param tag the desired element, e.g. "<name>"
	 * @return the element text, or null if the element was not found
	 */
	private String getXMLTextElement(String xml, String tag) {
		xml = removeXMLNamespaces(xml);

		if (!tag.startsWith("<")) {
			tag = "<" + tag + ">";
		}

		int start = xml.indexOf(tag);

		if (start == -1) {
			return null;
		}

		int end = xml.indexOf('<', start + tag.length());

		if (end == -1) {
			return null;
		}

		return xml.substring(start + tag.length(), end);
	}
}