use of io.github.microcks.domain.Header in project microcks by microcks.
the class PostmanCollectionImporter method buildHeaders.
private Set<Header> buildHeaders(JsonNode headerNode) {
if (headerNode == null || headerNode.size() == 0) {
return null;
}
// Prepare and map the set of headers.
Set<Header> headers = new HashSet<>();
Iterator<JsonNode> items = headerNode.elements();
while (items.hasNext()) {
JsonNode item = items.next();
Header header = new Header();
header.setName(item.path("key").asText());
Set<String> values = new HashSet<>();
values.add(item.path("value").asText());
header.setValues(values);
headers.add(header);
}
return headers;
}
use of io.github.microcks.domain.Header in project microcks by microcks.
the class AsyncAPIImporter method extractFromAsyncAPIExample.
/**
* Extract example using the AsyncAPI master branch restrictions.
*/
private EventMessage extractFromAsyncAPIExample(String contentType, JsonNode exampleNode, String exampleName) {
// Retrieve payload value.
String exampleValue = getExamplePayload(exampleNode);
// Build and store a request object.
EventMessage eventMessage = new EventMessage();
eventMessage.setName(exampleName);
eventMessage.setContent(exampleValue);
eventMessage.setMediaType(contentType);
// Now complete with specified headers.
List<Header> headers = getExampleHeaders(exampleNode);
for (Header header : headers) {
eventMessage.addHeader(header);
}
return eventMessage;
}
use of io.github.microcks.domain.Header in project microcks by microcks.
the class AsyncAPIImporter method getExampleHeaders.
/**
* Extract the list of Header from an example node.
*/
private List<Header> getExampleHeaders(JsonNode example) {
List<Header> results = new ArrayList<>();
if (example.has("headers")) {
Iterator<Entry<String, JsonNode>> headers = null;
if (example.path("headers").getNodeType() == JsonNodeType.OBJECT) {
headers = example.path("headers").fields();
} else if (example.path("headers").getNodeType() == JsonNodeType.STRING) {
// Try to parse string as a JSON Object...
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode headersNode = mapper.readTree(example.path("headers").asText());
headers = headersNode.fields();
} catch (Exception e) {
log.warn("Headers value {} is a string but not JSON, skipping it", example.path("headers").asText());
}
}
if (headers != null) {
while (headers.hasNext()) {
Entry<String, JsonNode> property = headers.next();
String propertyName = property.getKey();
Header header = new Header();
header.setName(property.getKey());
// Values may be multiple and CSV.
Set<String> headerValues = Arrays.stream(property.getValue().asText().split(",")).map(value -> value.trim()).collect(Collectors.toSet());
header.setValues(headerValues);
results.add(header);
}
}
}
return results;
}
use of io.github.microcks.domain.Header in project microcks by microcks.
the class AsyncAPIImporter method extractFromMicrocksExample.
/**
* Extract example using the Microcks (and Apicurio) extended notation.
*/
private EventMessage extractFromMicrocksExample(String contentType, JsonNode exampleNode) {
EventMessage eventMessage = null;
Iterator<String> exampleNames = exampleNode.fieldNames();
while (exampleNames.hasNext()) {
String exampleName = exampleNames.next();
JsonNode example = exampleNode.path(exampleName);
// No need to go further if no payload.
if (example.has("payload")) {
String exampleValue = getExamplePayload(example);
// Build and store a request object.
eventMessage = new EventMessage();
eventMessage.setName(exampleName);
eventMessage.setContent(exampleValue);
eventMessage.setMediaType(contentType);
// Now complete with specified headers.
List<Header> headers = getExampleHeaders(example);
for (Header header : headers) {
eventMessage.addHeader(header);
}
}
}
return eventMessage;
}
Aggregations