Search in sources :

Example 16 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration in project camel by apache.

the class RestSwaggerEndpoint method determineHost.

String determineHost(final Swagger swagger) {
    if (isNotEmpty(host)) {
        return host;
    }
    final String componentHost = component().getHost();
    if (isNotEmpty(componentHost)) {
        return componentHost;
    }
    final String swaggerScheme = pickBestScheme(specificationUri.getScheme(), swagger.getSchemes());
    final String swaggerHost = swagger.getHost();
    if (isNotEmpty(swaggerScheme) && isNotEmpty(swaggerHost)) {
        return swaggerScheme + "://" + swaggerHost;
    }
    final CamelContext camelContext = getCamelContext();
    final RestConfiguration specificRestConfiguration = camelContext.getRestConfiguration(assignedComponentName, false);
    final String specificConfigurationHost = hostFrom(specificRestConfiguration);
    if (specificConfigurationHost != null) {
        return specificConfigurationHost;
    }
    final RestConfiguration componentRestConfiguration = camelContext.getRestConfiguration("rest-swagger", false);
    final String componentConfigurationHost = hostFrom(componentRestConfiguration);
    if (componentConfigurationHost != null) {
        return componentConfigurationHost;
    }
    final RestConfiguration globalRestConfiguration = camelContext.getRestConfiguration();
    final String globalConfigurationHost = hostFrom(globalRestConfiguration);
    if (globalConfigurationHost != null) {
        return globalConfigurationHost;
    }
    final String specificationScheme = specificationUri.getScheme();
    if (specificationUri.isAbsolute() && specificationScheme.toLowerCase().startsWith("http")) {
        try {
            return new URI(specificationUri.getScheme(), specificationUri.getUserInfo(), specificationUri.getHost(), specificationUri.getPort(), null, null, null).toString();
        } catch (final URISyntaxException e) {
            throw new IllegalStateException("Unable to create a new URI from: " + specificationUri, e);
        }
    }
    final boolean areTheSame = "rest-swagger".equals(assignedComponentName);
    throw new IllegalStateException("Unable to determine destionation host for requests. The Swagger specification" + " does not specify `scheme` and `host` parameters, the specification URI is not absolute with `http` or" + " `https` scheme, and no RestConfigurations configured with `scheme`, `host` and `port` were found for `" + (areTheSame ? "rest-swagger` component" : assignedComponentName + "` or `rest-swagger` components") + " and there is no global RestConfiguration with those properties");
}
Also used : CamelContext(org.apache.camel.CamelContext) RestConfiguration(org.apache.camel.spi.RestConfiguration) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 17 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration 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");
}
Also used : CamelContext(org.apache.camel.CamelContext) Swagger(io.swagger.models.Swagger) RestConfiguration(org.apache.camel.spi.RestConfiguration) Test(org.junit.Test)

Example 18 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration in project camel by apache.

the class RestSwaggerEndpointTest method shouldDetermineBasePath.

@Test
public void shouldDetermineBasePath() {
    final RestConfiguration restConfiguration = new RestConfiguration();
    final CamelContext camelContext = mock(CamelContext.class);
    when(camelContext.getRestConfiguration("rest-swagger", true)).thenReturn(restConfiguration);
    final Swagger swagger = new Swagger();
    final RestSwaggerComponent component = new RestSwaggerComponent();
    component.setCamelContext(camelContext);
    final RestSwaggerEndpoint endpoint = new RestSwaggerEndpoint("rest-swagger:getPetById", "getPetById", component);
    assertThat(endpoint.determineBasePath(swagger)).as("When no base path is specified on component, endpoint or rest configuration it should default to `/`").isEqualTo("/");
    restConfiguration.setContextPath("/rest");
    assertThat(endpoint.determineBasePath(swagger)).as("When base path is specified in REST configuration and not specified in component the base path should be from the REST configuration").isEqualTo("/rest");
    swagger.basePath("/specification");
    assertThat(endpoint.determineBasePath(swagger)).as("When base path is specified in the specification it should take precedence the one specified in the REST configuration").isEqualTo("/specification");
    component.setBasePath("/component");
    assertThat(endpoint.determineBasePath(swagger)).as("When base path is specified on the component it should take precedence over Swagger specification and REST configuration").isEqualTo("/component");
    endpoint.setBasePath("/endpoint");
    assertThat(endpoint.determineBasePath(swagger)).as("When base path is specified on the endpoint it should take precedence over any other").isEqualTo("/endpoint");
}
Also used : CamelContext(org.apache.camel.CamelContext) Swagger(io.swagger.models.Swagger) RestConfiguration(org.apache.camel.spi.RestConfiguration) Test(org.junit.Test)

Example 19 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration in project camel by apache.

the class UndertowComponent method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    RestConfiguration config = getCamelContext().getRestConfiguration("undertow", true);
    // configure additional options on undertow configuration
    if (config.getComponentProperties() != null && !config.getComponentProperties().isEmpty()) {
        setProperties(this, config.getComponentProperties());
    }
}
Also used : RestConfiguration(org.apache.camel.spi.RestConfiguration)

Example 20 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration in project camel by apache.

the class NettyHttpComponent method doCreateConsumer.

Consumer doCreateConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters, boolean api) throws Exception {
    String path = basePath;
    if (uriTemplate != null) {
        // make sure to avoid double slashes
        if (uriTemplate.startsWith("/")) {
            path = path + uriTemplate;
        } else {
            path = path + "/" + uriTemplate;
        }
    }
    path = FileUtil.stripLeadingSeparator(path);
    String scheme = "http";
    String host = "";
    int port = 0;
    // if no explicit port/host configured, then use port from rest configuration
    RestConfiguration config = configuration;
    if (config == null) {
        config = camelContext.getRestConfiguration("netty4-http", true);
    }
    if (config.getScheme() != null) {
        scheme = config.getScheme();
    }
    if (config.getHost() != null) {
        host = config.getHost();
    }
    int num = config.getPort();
    if (num > 0) {
        port = num;
    }
    // prefix path with context-path if configured in rest-dsl configuration
    String contextPath = config.getContextPath();
    if (ObjectHelper.isNotEmpty(contextPath)) {
        contextPath = FileUtil.stripTrailingSeparator(contextPath);
        contextPath = FileUtil.stripLeadingSeparator(contextPath);
        if (ObjectHelper.isNotEmpty(contextPath)) {
            path = contextPath + "/" + path;
        }
    }
    // if no explicit hostname set then resolve the hostname
    if (ObjectHelper.isEmpty(host)) {
        if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
            host = "0.0.0.0";
        } else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) {
            host = HostUtils.getLocalHostName();
        } else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
            host = HostUtils.getLocalIp();
        }
    }
    Map<String, Object> map = new HashMap<String, Object>();
    // build query string, and append any endpoint configuration properties
    if (config.getComponent() == null || config.getComponent().equals("netty4-http")) {
        // setup endpoint options
        if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
            map.putAll(config.getEndpointProperties());
        }
    }
    // allow HTTP Options as we want to handle CORS in rest-dsl
    boolean cors = config.isEnableCORS();
    String query = URISupport.createQueryString(map);
    String url;
    if (api) {
        url = "netty4-http:%s://%s:%s/%s?matchOnUriPrefix=true&httpMethodRestrict=%s";
    } else {
        url = "netty4-http:%s://%s:%s/%s?httpMethodRestrict=%s";
    }
    // must use upper case for restrict
    String restrict = verb.toUpperCase(Locale.US);
    if (cors) {
        restrict += ",OPTIONS";
    }
    // get the endpoint
    url = String.format(url, scheme, host, port, path, restrict);
    if (!query.isEmpty()) {
        url = url + "&" + query;
    }
    NettyHttpEndpoint endpoint = camelContext.getEndpoint(url, NettyHttpEndpoint.class);
    setProperties(camelContext, endpoint, parameters);
    // configure consumer properties
    Consumer consumer = endpoint.createConsumer(processor);
    if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) {
        setProperties(camelContext, consumer, config.getConsumerProperties());
    }
    return consumer;
}
Also used : Consumer(org.apache.camel.Consumer) HashMap(java.util.HashMap) RestConfiguration(org.apache.camel.spi.RestConfiguration) Endpoint(org.apache.camel.Endpoint)

Aggregations

RestConfiguration (org.apache.camel.spi.RestConfiguration)36 HashMap (java.util.HashMap)10 Consumer (org.apache.camel.Consumer)10 Endpoint (org.apache.camel.Endpoint)8 CamelContext (org.apache.camel.CamelContext)6 Component (org.apache.camel.Component)3 NoSuchBeanException (org.apache.camel.NoSuchBeanException)3 RouteDefinition (org.apache.camel.model.RouteDefinition)3 Test (org.junit.Test)3 Swagger (io.swagger.models.Swagger)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)2 HttpConsumer (org.apache.camel.http.common.HttpConsumer)2 DefaultEndpoint (org.apache.camel.impl.DefaultEndpoint)2 FromDefinition (org.apache.camel.model.FromDefinition)2 InterceptFromDefinition (org.apache.camel.model.InterceptFromDefinition)2 RestDefinition (org.apache.camel.model.rest.RestDefinition)2 FactoryFinder (org.apache.camel.spi.FactoryFinder)2