Skip to content

Commit

Permalink
RestConfiguration - Make it simpler and only have one
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Mar 17, 2020
1 parent c583be3 commit 6d65984
Show file tree
Hide file tree
Showing 40 changed files with 322 additions and 495 deletions.
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 @@ -28,6 +28,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.support.RestComponentHelper;
import org.apache.camel.util.FileUtil;
Expand Down Expand Up @@ -103,7 +104,7 @@ private Consumer doCreateConsumer(CamelContext camelContext, Processor processor
// if no explicit port/host configured, then use port from rest configuration
RestConfiguration config = configuration;
if (config == null) {
config = camelContext.getRestConfiguration(PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME, true);
config = CamelContextHelper.getRestConfiguration(getCamelContext(), PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME);
}

Map<String, Object> map = RestComponentHelper.initRestEndpointProperties(PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME, config);
Expand All @@ -113,11 +114,11 @@ private Consumer doCreateConsumer(CamelContext camelContext, Processor processor
// 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);

// do not append with context-path as the servlet path should be without context-path
Expand Down

0 comments on commit 6d65984

Please sign in to comment.