use of io.swagger.models.Swagger in project camel by apache.
the class RestSwaggerEndpoint method createProducer.
@Override
public Producer createProducer() throws Exception {
final CamelContext camelContext = getCamelContext();
final Swagger swagger = loadSpecificationFrom(camelContext, specificationUri);
final Map<String, Path> paths = swagger.getPaths();
for (final Entry<String, Path> pathEntry : paths.entrySet()) {
final Path path = pathEntry.getValue();
final Optional<Entry<HttpMethod, Operation>> maybeOperationEntry = path.getOperationMap().entrySet().stream().filter(operationEntry -> operationId.equals(operationEntry.getValue().getOperationId())).findAny();
if (maybeOperationEntry.isPresent()) {
final Entry<HttpMethod, Operation> operationEntry = maybeOperationEntry.get();
final String uriTemplate = pathEntry.getKey();
final HttpMethod httpMethod = operationEntry.getKey();
final String method = httpMethod.name();
final Operation operation = operationEntry.getValue();
return createProducerFor(swagger, operation, method, uriTemplate);
}
}
final String supportedOperations = paths.values().stream().flatMap(p -> p.getOperations().stream()).map(Operation::getOperationId).collect(Collectors.joining(", "));
throw new IllegalArgumentException("The specified operation with ID: `" + operationId + "` cannot be found in the Swagger specification loaded from `" + specificationUri + "`. Operations defined in the specification are: " + supportedOperations);
}
use of io.swagger.models.Swagger in project camel by apache.
the class RestSwaggerEndpoint method loadSpecificationFrom.
/**
* Loads the Swagger definition model from the given path. Tries to resolve
* the resource using Camel's resource loading support, if it fails uses
* Swagger's resource loading support instead.
*
* @param uri URI of the specification
* @param camelContext context to use
* @return the specification
* @throws IOException
*/
static Swagger loadSpecificationFrom(final CamelContext camelContext, final URI uri) throws IOException {
final ObjectMapper mapper = Json.mapper();
final SwaggerParser swaggerParser = new SwaggerParser();
final String uriAsString = uri.toString();
try (InputStream stream = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, uriAsString)) {
final JsonNode node = mapper.readTree(stream);
return swaggerParser.read(node);
} catch (final IOException e) {
// try Swaggers loader
final Swagger swagger = swaggerParser.read(uriAsString);
if (swagger != null) {
return swagger;
}
throw new IllegalArgumentException("The given Swagger specification could not be loaded from `" + uri + "`. Tried loading using Camel's resource resolution and using Swagger's own resource resolution." + " Swagger tends to swallow exceptions while parsing, try specifying Java system property `debugParser`" + " (e.g. `-DdebugParser=true`), the exception that occured when loading using Camel's resource" + " loader follows", e);
}
}
use of io.swagger.models.Swagger in project camel by apache.
the class RestSwaggerEndpointTest method shouldHonourHostPrecedence.
@Test
public void shouldHonourHostPrecedence() {
final RestConfiguration globalRestConfiguration = new RestConfiguration();
final RestConfiguration componentRestConfiguration = new RestConfiguration();
final RestConfiguration specificRestConfiguration = new RestConfiguration();
final CamelContext camelContext = mock(CamelContext.class);
when(camelContext.getRestConfiguration()).thenReturn(globalRestConfiguration);
when(camelContext.getRestConfiguration("rest-swagger", false)).thenReturn(componentRestConfiguration);
when(camelContext.getRestConfiguration("petstore", false)).thenReturn(specificRestConfiguration);
final RestSwaggerComponent component = new RestSwaggerComponent();
component.setCamelContext(camelContext);
final RestSwaggerEndpoint endpoint = new RestSwaggerEndpoint("petstore:http://specification-uri#getPetById", "http://specification-uri#getPetById", component);
final Swagger swagger = new Swagger();
assertThat(endpoint.determineHost(swagger)).isEqualTo("http://specification-uri");
globalRestConfiguration.setHost("global-rest");
globalRestConfiguration.setScheme("http");
assertThat(endpoint.determineHost(swagger)).isEqualTo("http://global-rest");
globalRestConfiguration.setHost("component-rest");
globalRestConfiguration.setScheme("http");
assertThat(endpoint.determineHost(swagger)).isEqualTo("http://component-rest");
specificRestConfiguration.setHost("specific-rest");
specificRestConfiguration.setScheme("http");
assertThat(endpoint.determineHost(swagger)).isEqualTo("http://specific-rest");
swagger.host("specification").scheme(Scheme.HTTP);
assertThat(endpoint.determineHost(swagger)).isEqualTo("http://specification");
component.setHost("http://component");
assertThat(endpoint.determineHost(swagger)).isEqualTo("http://component");
endpoint.setHost("http://endpoint");
assertThat(endpoint.determineHost(swagger)).isEqualTo("http://endpoint");
}
use of io.swagger.models.Swagger in project camel by apache.
the class RestSwaggerEndpointTest method shouldDetermineHostFromSpecification.
@Test
public void shouldDetermineHostFromSpecification() {
final RestSwaggerComponent component = new RestSwaggerComponent();
final RestSwaggerEndpoint endpoint = new RestSwaggerEndpoint("rest-swagger:http://some-uri#getPetById", "http://some-uri#getPetById", component);
final Swagger swagger = new Swagger();
swagger.host("petstore.swagger.io");
assertThat(endpoint.determineHost(swagger)).isEqualTo("http://petstore.swagger.io");
swagger.schemes(Arrays.asList(Scheme.HTTPS));
assertThat(endpoint.determineHost(swagger)).isEqualTo("https://petstore.swagger.io");
}
use of io.swagger.models.Swagger in project camel by apache.
the class RestSwaggerEndpointTest method shouldDetermineEndpointParameters.
@Test
public void shouldDetermineEndpointParameters() {
final CamelContext camelContext = mock(CamelContext.class);
final RestSwaggerComponent component = new RestSwaggerComponent();
component.setCamelContext(camelContext);
final RestSwaggerEndpoint endpoint = new RestSwaggerEndpoint("uri", "remaining", component);
endpoint.setHost("http://petstore.swagger.io");
final Swagger swagger = new Swagger();
final Operation operation = new Operation();
assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(entry("host", "http://petstore.swagger.io"));
component.setComponentName("xyz");
assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(entry("host", "http://petstore.swagger.io"), entry("componentName", "xyz"));
swagger.consumes("application/json").produces("application/xml");
assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(entry("host", "http://petstore.swagger.io"), entry("componentName", "xyz"), entry("consumes", "application/xml"), entry("produces", "application/json"));
component.setProduces("application/json");
component.setConsumes("application/atom+xml");
assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(entry("host", "http://petstore.swagger.io"), entry("componentName", "xyz"), entry("consumes", "application/atom+xml"), entry("produces", "application/json"));
endpoint.setProduces("application/atom+xml");
endpoint.setConsumes("application/json");
assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(entry("host", "http://petstore.swagger.io"), entry("componentName", "xyz"), entry("consumes", "application/json"), entry("produces", "application/atom+xml"));
endpoint.setComponentName("zyx");
assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(entry("host", "http://petstore.swagger.io"), entry("componentName", "zyx"), entry("consumes", "application/json"), entry("produces", "application/atom+xml"));
}
Aggregations