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");
}
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");
}
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");
}
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());
}
}
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;
}
Aggregations