Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAMEL-13844: RestConfiguration - Make it simpler and only have one #3643

Merged
merged 3 commits into from Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions components/camel-coap/pom.xml
Expand Up @@ -79,11 +79,6 @@
<artifactId>camel-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jetty</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Expand Up @@ -33,6 +33,7 @@
import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spi.RestConsumerFactory;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.DefaultComponent;
import org.apache.camel.util.FileUtil;
import org.apache.camel.util.HostUtils;
Expand Down Expand Up @@ -136,7 +137,7 @@ public Consumer createConsumer(CamelContext camelContext, Processor processor, S

RestConfiguration config = configuration;
if (config == null) {
config = camelContext.getRestConfiguration("coap", true);
config = CamelContextHelper.getRestConfiguration(getCamelContext(), "coap");
}

if (config.isEnableCORS()) {
Expand Down
Expand Up @@ -123,7 +123,7 @@ protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
RestConfigurationDefinition restConfig = restConfiguration("coap").scheme(getProtocol()).host("localhost").port(coapport);
RestConfigurationDefinition restConfig = restConfiguration().scheme(getProtocol()).host("localhost").port(coapport);
decorateRestConfiguration(restConfig);

rest("/TestParams").get().to("direct:get1").post().to("direct:post1");
Expand Down
Expand Up @@ -37,7 +37,7 @@ protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
restConfiguration("coap").host("localhost").port(PORT).contextPath("/rest/services");
restConfiguration().host("localhost").port(PORT).contextPath("/rest/services");

rest("/test").get("/a").route().setBody(constant("GET: /test/a"));
}
Expand Down
Expand Up @@ -43,6 +43,7 @@
import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spi.RestProducerFactory;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.support.RestProducerFactoryHelper;
import org.apache.camel.support.jsse.SSLContextParameters;
Expand Down Expand Up @@ -428,13 +429,7 @@ public Producer createProducer(CamelContext camelContext, String host,

RestConfiguration config = configuration;
if (config == null) {
config = camelContext.getRestConfiguration("http", false);
if (config == null) {
camelContext.getRestConfiguration();
}
if (config == null) {
config = camelContext.getRestConfiguration("http", true);
}
config = CamelContextHelper.getRestConfiguration(getCamelContext(), "http");
}

Map<String, Object> map = new HashMap<>();
Expand Down
Expand Up @@ -57,6 +57,7 @@
import org.apache.camel.spi.RestApiConsumerFactory;
import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spi.RestConsumerFactory;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.RestComponentHelper;
import org.apache.camel.support.jsse.SSLContextParameters;
import org.apache.camel.support.service.ServiceHelper;
Expand Down Expand Up @@ -1048,8 +1049,9 @@ Consumer doCreateConsumer(CamelContext camelContext, Processor processor, String
// if no explicit port/host configured, then use port from rest configuration
RestConfiguration config = configuration;
if (config == null) {
config = camelContext.getRestConfiguration("jetty", true);
config = CamelContextHelper.getRestConfiguration(getCamelContext(), "jetty");
}

if (config.getScheme() != null) {
scheme = config.getScheme();
}
Expand Down Expand Up @@ -1083,11 +1085,11 @@ Consumer doCreateConsumer(CamelContext camelContext, Processor processor, String
// allow HTTP Options as we want to handle CORS in rest-dsl
map.put("optionsEnabled", "true");
}

if (api) {
map.put("matchOnUriPrefix", "true");
}

RestComponentHelper.addHttpRestrictParam(map, verb, cors);

String url = RestComponentHelper.createRestConsumerUrl("jetty", scheme, host, port, path, map);
Expand Down Expand Up @@ -1126,16 +1128,16 @@ protected CamelServlet createServletForConnector(Server server, Connector connec
holder.setAsyncSupported(true);
holder.setInitParameter(CamelServlet.ASYNC_PARAM, Boolean.toString(endpoint.isAsync()));
context.addServlet(holder, "/*");

File file = File.createTempFile("camel", "");
file.delete();

//must register the MultipartConfig to make jetty server multipart aware
holder.getRegistration().setMultipartConfig(new MultipartConfigElement(file.getParentFile().getAbsolutePath(), -1, -1, 0));

// use rest enabled resolver in case we use rest
camelServlet.setServletResolveConsumerStrategy(new HttpRestServletResolveConsumerStrategy());

//must make RFC7578 as default to avoid using the deprecated MultiPartInputStreamParser
connector.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration()
.setMultiPartFormDataCompliance(MultiPartFormDataCompliance.RFC7578);
Expand Down Expand Up @@ -1245,7 +1247,8 @@ protected void writeErrorPage(HttpServletRequest request, Writer writer, int cod
protected void doStart() throws Exception {
super.doStart();

RestConfiguration config = getCamelContext().getRestConfiguration("jetty", true);
RestConfiguration config = CamelContextHelper.getRestConfiguration(getCamelContext(), "jetty");

// configure additional options on jetty configuration
if (config.getComponentProperties() != null && !config.getComponentProperties().isEmpty()) {
setProperties(this, config.getComponentProperties());
Expand Down
Expand Up @@ -94,7 +94,7 @@ protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
RestConfigurationDefinition restConfig = restConfiguration("jetty").scheme("https").host("localhost").port(port);
RestConfigurationDefinition restConfig = restConfiguration().scheme("https").host("localhost").port(port);
decorateRestConfiguration(restConfig);

rest("/TestParams").get().to("direct:get1").post().to("direct:post1");
Expand Down
Expand Up @@ -70,7 +70,7 @@ protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
restConfiguration("jetty").host("localhost").port(getPort());
restConfiguration().host("localhost").port(getPort());

rest("/").get("/issues/{isin}").route().id("issues").process(e -> e.getOut().setBody("Here's your issue " + e.getIn().getHeader("isin"))).endRest().get("/listings")
.route().id("listings").process(e -> e.getOut().setBody("some listings"));
Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.apache.camel.spi.RestConsumerFactory;
import org.apache.camel.spi.RestProducerFactory;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.support.RestComponentHelper;
import org.apache.camel.support.RestProducerFactoryHelper;
Expand Down Expand Up @@ -351,7 +352,7 @@ Consumer doCreateConsumer(CamelContext camelContext, Processor processor, String
// if no explicit port/host configured, then use port from rest configuration
RestConfiguration config = configuration;
if (config == null) {
config = camelContext.getRestConfiguration("netty-http", true);
config = CamelContextHelper.getRestConfiguration(getCamelContext(), "netty-http");
}
if (config.getScheme() != null) {
scheme = config.getScheme();
Expand Down Expand Up @@ -383,11 +384,11 @@ Consumer doCreateConsumer(CamelContext camelContext, Processor processor, String

// allow HTTP Options as we want to handle CORS in rest-dsl
boolean cors = config.isEnableCORS();

if (api) {
map.put("matchOnUriPrefix", "true");
}

RestComponentHelper.addHttpRestrictParam(map, verb, cors);

String url = RestComponentHelper.createRestConsumerUrl("netty-http", scheme, host, port, path, map);
Expand All @@ -404,6 +405,7 @@ Consumer doCreateConsumer(CamelContext camelContext, Processor processor, String
return consumer;
}

@SuppressWarnings("unchecked")
@Override
public Producer createProducer(CamelContext camelContext, String host,
String verb, String basePath, String uriTemplate, String queryParameters,
Expand All @@ -422,13 +424,7 @@ public Producer createProducer(CamelContext camelContext, String host,
url += "/" + uriTemplate;
}

RestConfiguration config = getCamelContext().getRestConfiguration("netty-http", false);
if (config == null) {
config = getCamelContext().getRestConfiguration();
}
if (config == null) {
config = getCamelContext().getRestConfiguration("netty-http", true);
}
RestConfiguration config = CamelContextHelper.getRestConfiguration(getCamelContext(), "netty-http");

Map<String, Object> map = new HashMap<>();
// build query string, and append any endpoint configuration properties
Expand Down Expand Up @@ -471,7 +467,8 @@ public Producer createProducer(CamelContext camelContext, String host,
protected void doStart() throws Exception {
super.doStart();

RestConfiguration config = getCamelContext().getRestConfiguration("netty-http", true);
RestConfiguration config = CamelContextHelper.getRestConfiguration(getCamelContext(), "netty-http");

// configure additional options on netty-http configuration
if (config.getComponentProperties() != null && !config.getComponentProperties().isEmpty()) {
setProperties(this, config.getComponentProperties());
Expand Down
Expand Up @@ -37,6 +37,7 @@
import org.apache.camel.Producer;
import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spi.RestProducerFactory;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.util.CollectionStringBuffer;
import org.apache.camel.util.IOHelper;
import org.slf4j.Logger;
Expand Down Expand Up @@ -100,13 +101,13 @@ OasDocument loadOpenApiModel(CamelContext camelContext, String apiDoc) throws Ex
final JsonNode node = mapper.readTree(is);
LOG.debug("Loaded openApi api-doc:\n{}", node.toPrettyString());
return (OasDocument)Library.readDocument(node);


} finally {
IOHelper.close(is);
}


}

private OasOperation getOpenApiOperation(OasDocument openApi, String verb, String path) {
Expand Down Expand Up @@ -166,7 +167,7 @@ private Producer createHttpProducer(CamelContext camelContext, OasDocument openA
list.add(ct);
}
}

}
if (list == null || list.isEmpty()) {
if (openApi instanceof Oas20Document) {
Expand All @@ -187,13 +188,13 @@ private Producer createHttpProducer(CamelContext camelContext, OasDocument openA
list = ((Oas20Operation)operation).consumes;
} else if (operation instanceof Oas30Operation) {
Oas30Operation oas30Operation = (Oas30Operation)operation;
if (oas30Operation.requestBody != null
&& oas30Operation.requestBody.content != null) {
if (oas30Operation.requestBody != null
&& oas30Operation.requestBody.content != null) {
for (String ct : oas30Operation.requestBody.content.keySet()) {
list.add(ct);
}
}

}
if (list == null || list.isEmpty()) {
if (openApi instanceof Oas20Document) {
Expand All @@ -211,18 +212,19 @@ private Producer createHttpProducer(CamelContext camelContext, OasDocument openA
String basePath = null;
String uriTemplate = null;
if (host == null) {

//if no explicit host has been configured then use host and base path from the openApi api-doc
host = RestOpenApiSupport.getHostFromOasDocument(openApi);
basePath = RestOpenApiSupport.getBasePathFromOasDocument(openApi);
uriTemplate = path;

} else {
// path includes also uri template
basePath = path;
uriTemplate = null;
}
RestConfiguration config = camelContext.getRestConfiguration(componentName, true);

RestConfiguration config = CamelContextHelper.getRestConfiguration(camelContext, componentName);
return factory.createProducer(camelContext, host, verb, basePath, uriTemplate, queryParameters, consumes, produces, config, parameters);

} else {
Expand Down
Expand Up @@ -37,7 +37,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.verifyNoInteractions;

public class RestOpenApiSupportTest {

Expand All @@ -53,14 +53,14 @@ public void shouldAdaptFromXForwardHeaders() {
headers.put(RestOpenApiSupport.HEADER_X_FORWARDED_PROTO, "http, HTTPS ");
RestOpenApiSupport.setupXForwardedHeaders(openApi, headers);


assertEquals(openApi.basePath, "/prefix/base");
assertEquals(openApi.host, "host");
assertTrue(openApi.schemes.contains("http"));
assertTrue(openApi.schemes.contains("https"));

}

@Test
public void shouldAdaptFromXForwardHeadersV3() {
Oas30Document doc = new Oas30Document();
Expand All @@ -73,11 +73,11 @@ public void shouldAdaptFromXForwardHeadersV3() {
headers.put(RestOpenApiSupport.HEADER_X_FORWARDED_PROTO, "http, HTTPS ");
RestOpenApiSupport.setupXForwardedHeaders(openApi, headers);


assertEquals(openApi.getServers().get(0).url, "http://host/prefix/base");
assertEquals(openApi.getServers().get(1).url, "https://host/prefix/base");


}

@ParameterizedTest
Expand All @@ -94,7 +94,7 @@ public void shouldAdaptWithVaryingBasePathsAndPrefixes(final String prefix, fina

assertEquals(openApi.basePath, expected);
}

@ParameterizedTest
@MethodSource("basePathAndPrefixVariations")
public void shouldAdaptWithVaryingBasePathsAndPrefixesV3(final String prefix, final String basePath,
Expand Down Expand Up @@ -127,15 +127,15 @@ public void shouldAdaptWithVaryingSchemes(final String xForwardedScheme, final S
}

}

@ParameterizedTest
@MethodSource("schemeVariations")
public void shouldAdaptWithVaryingSchemesV3(final String xForwardedScheme, final String[] expected) {
final Oas30Document openApi = spy(new Oas30Document());

RestOpenApiSupport.setupXForwardedHeaders(openApi,
Collections.singletonMap(RestOpenApiSupport.HEADER_X_FORWARDED_PROTO, xForwardedScheme));

List<String> schemas = new ArrayList<String>();
if (openApi.servers != null) {
for (Server server : openApi.servers) {
Expand All @@ -144,7 +144,7 @@ public void shouldAdaptWithVaryingSchemesV3(final String xForwardedScheme, final
schemas.add(url.getProtocol());
} catch (MalformedURLException e) {


}
}
}
Expand All @@ -160,7 +160,7 @@ public void shouldNotAdaptFromXForwardHeadersWhenNoHeadersSpecified() {

RestOpenApiSupport.setupXForwardedHeaders(openApi, Collections.emptyMap());

verifyZeroInteractions(openApi);
verifyNoInteractions(openApi);
}

static Stream<Arguments> basePathAndPrefixVariations() {
Expand Down