-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenAPIWorkflowParser.java
More file actions
118 lines (91 loc) · 3.96 KB
/
Copy pathOpenAPIWorkflowParser.java
File metadata and controls
118 lines (91 loc) · 3.96 KB
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
package com.apiflows.parser;
import com.apiflows.model.OpenAPIWorkflow;
import com.apiflows.parser.source.OperationBinder;
import com.apiflows.parser.source.WorkflowBinder;
import com.apiflows.parser.util.HttpUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.apiflows.parser.util.PathUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OpenAPIWorkflowParser {
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAPIWorkflowParser.class);
/**
* Parse an OpenAPI Workflow file
* @param input url, filepath or content (as string)
* @return instance of OpenAPIWorkflowParserResult
*/
public OpenAPIWorkflowParserResult parse(String input) {
return parse(input, new ParseOptions());
}
/**
* Parse an OpenAPI Workflow file
* @param input url, filepath or content (as string)
* @param options Options
* @return instance of OpenAPIWorkflowParserResult
*/
public OpenAPIWorkflowParserResult parse(String input, ParseOptions options) {
OpenAPIWorkflowParserResult result = new OpenAPIWorkflowParserResult();
try {
PathUtil pathUtil = new PathUtil();
HttpUtil httpUtil = new HttpUtil();
String content;
if (httpUtil.isUrl(input)) {
content = httpUtil.call(input);
result.setLocation(input);
} else if (pathUtil.isFile(input)) {
content = pathUtil.getFromFile(input);
result.setLocation(input);
} else {
// content as string
content = input;
result.setLocation(null);
}
result.setContent(content);
result.setFormat(getFormat(content));
try {
final ObjectMapper mapper = getObjectMapper(content);
OpenAPIWorkflow openAPIWorkflow = mapper.readValue(content, OpenAPIWorkflow.class);
result.setOpenAPIWorkflow(openAPIWorkflow);
if(options != null && options.isApplyValidation()) {
OpenAPIWorkflowValidatorResult validatorResult = new OpenAPIWorkflowValidator(openAPIWorkflow).validate();
result.setValid(validatorResult.isValid());
result.setErrors(validatorResult.getErrors());
}
new OperationBinder().bind(openAPIWorkflow, result.getLocation());
new WorkflowBinder().bind(openAPIWorkflow);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
result.setValid(false);
result.addError(e.getMessage());
}
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
result.setValid(false);
}
return result;
}
OpenAPIWorkflowParserResult.Format getFormat(String content) {
if (content.trim().startsWith("{")) {
return OpenAPIWorkflowParserResult.Format.JSON;
} else {
return OpenAPIWorkflowParserResult.Format.YAML;
}
}
private ObjectMapper getObjectMapper(String content) {
ObjectMapper objectMapper = null;
if (content.trim().startsWith("{")) {
objectMapper = new ObjectMapper();
} else {
objectMapper = new ObjectMapper(new YAMLFactory());
}
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return objectMapper;
}
}