Search in sources :

Example 16 with Header

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;
}
Also used : Header(io.github.microcks.domain.Header) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 17 with Header

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;
}
Also used : EventMessage(io.github.microcks.domain.EventMessage) Header(io.github.microcks.domain.Header)

Example 18 with Header

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;
}
Also used : ResourceType(io.github.microcks.domain.ResourceType) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) UnidirectionalEvent(io.github.microcks.domain.UnidirectionalEvent) DispatchCriteriaHelper(io.github.microcks.util.DispatchCriteriaHelper) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) Exchange(io.github.microcks.domain.Exchange) Map(java.util.Map) MockRepositoryImporter(io.github.microcks.util.MockRepositoryImporter) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) Binding(io.github.microcks.domain.Binding) Resource(io.github.microcks.domain.Resource) IdBuilder(io.github.microcks.util.IdBuilder) ReferenceResolver(io.github.microcks.util.ReferenceResolver) JsonNode(com.fasterxml.jackson.databind.JsonNode) DispatchStyles(io.github.microcks.util.DispatchStyles) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) MetadataExtensions(io.github.microcks.util.metadata.MetadataExtensions) JsonNodeType(com.fasterxml.jackson.databind.node.JsonNodeType) URIBuilder(io.github.microcks.util.URIBuilder) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Metadata(io.github.microcks.domain.Metadata) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) EventMessage(io.github.microcks.domain.EventMessage) Service(io.github.microcks.domain.Service) List(java.util.List) Header(io.github.microcks.domain.Header) Operation(io.github.microcks.domain.Operation) ServiceType(io.github.microcks.domain.ServiceType) Paths(java.nio.file.Paths) MetadataExtractor(io.github.microcks.util.metadata.MetadataExtractor) BindingType(io.github.microcks.domain.BindingType) Entry(java.util.Map.Entry) BufferedReader(java.io.BufferedReader) Entry(java.util.Map.Entry) Header(io.github.microcks.domain.Header) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) IOException(java.io.IOException)

Example 19 with Header

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;
}
Also used : EventMessage(io.github.microcks.domain.EventMessage) Header(io.github.microcks.domain.Header) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

Header (io.github.microcks.domain.Header)19 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ArrayList (java.util.ArrayList)5 EventMessage (io.github.microcks.domain.EventMessage)4 Response (io.github.microcks.domain.Response)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Operation (io.github.microcks.domain.Operation)3 Service (io.github.microcks.domain.Service)3 HttpHeaders (org.springframework.http.HttpHeaders)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Request (io.github.microcks.domain.Request)2 Resource (io.github.microcks.domain.Resource)2 FallbackSpecification (io.github.microcks.util.dispatcher.FallbackSpecification)2 URI (java.net.URI)2 ResponseEntity (org.springframework.http.ResponseEntity)2 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)2