This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 89f61b5 CAMEL-16934: camel-swagger-java and camel-openapi-java - Removed support for rendering API DOCs from other CamelContext discovered in the same JVM via JMX. 89f61b5 is described below commit 89f61b59c61280c83c46c23750826db5b005c8c9 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Oct 21 09:57:52 2021 +0200 CAMEL-16934: camel-swagger-java and camel-openapi-java - Removed support for rendering API DOCs from other CamelContext discovered in the same JVM via JMX. --- .../src/main/docs/openapi-java.adoc | 26 ---- .../openapi/OpenApiRestApiProcessorFactory.java | 4 +- .../apache/camel/openapi/RestOpenApiProcessor.java | 53 +------- .../apache/camel/openapi/RestOpenApiSupport.java | 86 +------------ .../camel/openapi/RestOpenApiLicenseInfoTest.java | 2 +- .../camel/openapi/RestOpenApiProcessorTest.java | 139 ++------------------- .../src/main/docs/swagger-java.adoc | 26 ---- .../apache/camel/swagger/RestSwaggerProcessor.java | 50 +------- .../apache/camel/swagger/RestSwaggerSupport.java | 131 +------------------ .../swagger/SwaggerRestApiProcessorFactory.java | 4 +- .../apache/camel/spi/RestApiProcessorFactory.java | 15 ++- .../component/rest/DummyRestProcessorFactory.java | 2 +- .../ROOT/pages/camel-3x-upgrade-guide-3_13.adoc | 9 ++ 13 files changed, 46 insertions(+), 501 deletions(-) diff --git a/components/camel-openapi-java/src/main/docs/openapi-java.adoc b/components/camel-openapi-java/src/main/docs/openapi-java.adoc index 4fc5179..c0c4e6e 100644 --- a/components/camel-openapi-java/src/main/docs/openapi-java.adoc +++ b/components/camel-openapi-java/src/main/docs/openapi-java.adoc @@ -121,23 +121,10 @@ So using relative paths is much easier. See above for an example. |api.license.name |String |The license name used for the API. |api.license.url |String |A URL to the license used for the API. - -|apiContextIdListing |boolean |Whether to allow listing all the CamelContext names in the JVM that has -REST services. When enabled then the root path of the api-doc will list -all the contexts. When disabled then no context ids is listed and the -root path of the api-doc lists the current CamelContext. Is default -false. - -|apiContextIdPattern |String |A pattern that allows to filter which CamelContext names is shown in the -context listing. The pattern is using regular expression and * as -wildcard. Its the same pattern matching as used by -Intercept |=== == Adding Security Definitions in API doc -*Available as of Camel 3.1.0* - The Rest DSL now supports declaring OpenApi `securityDefinitions` in the generated API document. For example as shown below: @@ -174,21 +161,8 @@ their key (petstore_auth or api_key). Here the get operation is using the Api Key security and the put operation is using OAuth security with permitted scopes of read and write pets. - - -== ContextIdListing enabled - -When contextIdListing is enabled then its detecting all the running -CamelContexts in the same JVM. These contexts are listed in the root -path, eg `/api-docs` as a simple list of names in json format. To access -the OpenApi documentation then the context-path must be appended with -the Camel context id, such as `api-docs/myCamel`. The -option apiContextIdPattern can be used to filter the names in this list. - == JSon or Yaml -*Available as of Camel 3.1.0 - The camel-openapi-java module supports both JSon and Yaml out of the box. You can specify in the request url what you want returned by using /openapi.json or /openapi.yaml for either one. If none is specified then diff --git a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/OpenApiRestApiProcessorFactory.java b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/OpenApiRestApiProcessorFactory.java index f6a0fb3..cb046a0 100644 --- a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/OpenApiRestApiProcessorFactory.java +++ b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/OpenApiRestApiProcessorFactory.java @@ -28,7 +28,7 @@ public class OpenApiRestApiProcessorFactory implements RestApiProcessorFactory { @Override public Processor createApiProcessor( - CamelContext camelContext, String contextPath, String contextIdPattern, boolean contextIdListing, + CamelContext camelContext, String contextPath, RestConfiguration configuration, Map<String, Object> parameters) throws Exception { @@ -78,6 +78,6 @@ public class OpenApiRestApiProcessorFactory implements RestApiProcessorFactory { options.put("cors", "true"); } - return new RestOpenApiProcessor(contextIdPattern, contextIdListing, options, configuration); + return new RestOpenApiProcessor(options, configuration); } } diff --git a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiProcessor.java b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiProcessor.java index 94ae818..480b9b8 100644 --- a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiProcessor.java +++ b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiProcessor.java @@ -23,8 +23,6 @@ import java.util.Map; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.spi.RestConfiguration; -import org.apache.camel.support.PatternHelper; -import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,15 +31,11 @@ public class RestOpenApiProcessor implements Processor { private static final Logger LOG = LoggerFactory.getLogger(RestOpenApiProcessor.class); private final BeanConfig openApiConfig; private final RestOpenApiSupport support; - private final String contextIdPattern; - private final boolean contextIdListing; private final RestConfiguration configuration; @SuppressWarnings("unchecked") - public RestOpenApiProcessor(String contextIdPattern, boolean contextIdListing, Map<String, Object> parameters, + public RestOpenApiProcessor(Map<String, Object> parameters, RestConfiguration configuration) { - this.contextIdPattern = contextIdPattern; - this.contextIdListing = contextIdListing; this.configuration = configuration; this.support = new RestOpenApiSupport(); this.openApiConfig = new BeanConfig(); @@ -55,7 +49,6 @@ public class RestOpenApiProcessor implements Processor { @Override public void process(Exchange exchange) throws Exception { - String contextId = exchange.getContext().getName(); String route = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class); String accept = exchange.getIn().getHeader("Accept", String.class); @@ -81,48 +74,8 @@ public class RestOpenApiProcessor implements Processor { } try { - // render list of camel contexts as root - if (contextIdListing && (ObjectHelper.isEmpty(route) || route.equals("/"))) { - support.renderCamelContexts(exchange.getContext(), adapter, contextId, contextIdPattern, json, yaml, - configuration); - } else { - String name; - if (contextIdListing && ObjectHelper.isNotEmpty(route)) { - // first part is the camel context - if (route.startsWith("/")) { - route = route.substring(1); - } - // the remainder is the route part - name = route.split("/")[0]; - if (route.startsWith(contextId)) { - route = route.substring(name.length()); - } - } else { - // listing not enabled then get current camel context as the name - name = exchange.getContext().getName(); - // prevent route filtering - route = ""; - } - - boolean match = true; - if (contextIdPattern != null) { - if ("#name#".equals(contextIdPattern)) { - match = name.equals(contextId); - } else { - match = PatternHelper.matchPattern(name, contextIdPattern); - } - if (LOG.isDebugEnabled()) { - LOG.debug("Match contextId: {} with pattern: {} -> {}", name, contextIdPattern, match); - } - } - - if (!match) { - adapter.noContent(); - } else { - support.renderResourceListing(exchange.getContext(), adapter, openApiConfig, name, route, json, yaml, - exchange.getIn().getHeaders(), exchange.getContext().getClassResolver(), configuration); - } - } + support.renderResourceListing(exchange.getContext(), adapter, openApiConfig, route, json, yaml, + exchange.getIn().getHeaders(), exchange.getContext().getClassResolver(), configuration); } catch (Exception e) { LOG.warn("Error rendering OpenApi API due {}", e.getMessage(), e); } diff --git a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiSupport.java b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiSupport.java index b95cd35..92a1f82 100644 --- a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiSupport.java +++ b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiSupport.java @@ -20,7 +20,6 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.regex.Matcher; @@ -45,7 +44,6 @@ import org.apache.camel.ExtendedCamelContext; import org.apache.camel.model.rest.RestDefinition; import org.apache.camel.spi.ClassResolver; import org.apache.camel.spi.RestConfiguration; -import org.apache.camel.support.PatternHelper; import org.apache.camel.support.ResolverHelper; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.URISupport; @@ -374,7 +372,7 @@ public class RestOpenApiSupport { public void renderResourceListing( CamelContext camelContext, RestApiResponseAdapter response, - BeanConfig openApiConfig, String contextId, String route, boolean json, + BeanConfig openApiConfig, String route, boolean json, boolean yaml, Map<String, Object> headers, ClassResolver classResolver, RestConfiguration configuration) throws Exception { @@ -388,12 +386,7 @@ public class RestOpenApiSupport { setupCorsHeaders(response, configuration.getCorsHeaders()); } - List<RestDefinition> rests; - if (camelContext.getName().equals(contextId)) { - rests = getRestDefinitions(camelContext); - } else { - rests = getRestDefinitions(camelContext, contextId); - } + List<RestDefinition> rests = getRestDefinitions(camelContext); if (rests != null) { final Map<String, Object> apiProperties = configuration.getApiProperties() != null @@ -403,7 +396,8 @@ public class RestOpenApiSupport { .getOrDefault("api.specification.contentType.json", "application/json")); // read the rest-dsl into openApi model - OasDocument openApi = reader.read(camelContext, rests, route, openApiConfig, contextId, classResolver); + OasDocument openApi + = reader.read(camelContext, rests, route, openApiConfig, camelContext.getName(), classResolver); if (configuration.isUseXForwardHeaders()) { setupXForwardedHeaders(openApi, headers); } @@ -423,7 +417,8 @@ public class RestOpenApiSupport { .getOrDefault("api.specification.contentType.yaml", "text/yaml")); // read the rest-dsl into openApi model - OasDocument openApi = reader.read(camelContext, rests, route, openApiConfig, contextId, classResolver); + OasDocument openApi + = reader.read(camelContext, rests, route, openApiConfig, camelContext.getName(), classResolver); if (configuration.isUseXForwardHeaders()) { setupXForwardedHeaders(openApi, headers); } @@ -449,73 +444,4 @@ public class RestOpenApiSupport { } } - /** - * Renders a list of available CamelContexts in the JVM - */ - public void renderCamelContexts( - CamelContext camelContext, RestApiResponseAdapter response, String contextId, - String contextIdPattern, boolean json, boolean yaml, - RestConfiguration configuration) - throws Exception { - LOG.trace("renderCamelContexts"); - - if (cors) { - setupCorsHeaders(response, configuration.getCorsHeaders()); - } - - List<String> contexts = findCamelContexts(camelContext); - - // filter not matched CamelContext's - if (contextIdPattern != null) { - Iterator<String> it = contexts.iterator(); - while (it.hasNext()) { - String name = it.next(); - - boolean match; - if ("#name#".equals(contextIdPattern)) { - match = name.equals(contextId); - } else { - match = PatternHelper.matchPattern(name, contextIdPattern); - } - if (!match) { - it.remove(); - } - } - } - - StringBuffer sb = new StringBuffer(); - - if (json) { - response.setHeader(Exchange.CONTENT_TYPE, "application/json"); - - sb.append("[\n"); - for (int i = 0; i < contexts.size(); i++) { - String name = contexts.get(i); - sb.append("{\"name\": \"").append(name).append("\"}"); - if (i < contexts.size() - 1) { - sb.append(",\n"); - } - } - sb.append("\n]"); - } else { - response.setHeader(Exchange.CONTENT_TYPE, "text/yaml"); - - for (int i = 0; i < contexts.size(); i++) { - String name = contexts.get(i); - sb.append("- \"").append(name).append("\"\n"); - } - } - - int len = sb.length(); - response.setHeader(Exchange.CONTENT_LENGTH, "" + len); - - response.writeBytes(sb.toString().getBytes()); - } - - private List<String> findCamelContexts(CamelContext camelContext) throws Exception { - if (jmxRestDefinitionResolver == null) { - jmxRestDefinitionResolver = createJmxRestDefinitionsResolver(camelContext); - } - return jmxRestDefinitionResolver.findCamelContexts(); - } } diff --git a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiLicenseInfoTest.java b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiLicenseInfoTest.java index b9ef5ad..be9817b 100644 --- a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiLicenseInfoTest.java +++ b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiLicenseInfoTest.java @@ -55,7 +55,7 @@ public class RestOpenApiLicenseInfoTest { RestConfiguration restConfiguration = context.getRestConfiguration(); RestOpenApiProcessor processor - = new RestOpenApiProcessor(null, false, restConfiguration.getApiProperties(), restConfiguration); + = new RestOpenApiProcessor(restConfiguration.getApiProperties(), restConfiguration); Exchange exchange = new DefaultExchange(context); processor.process(exchange); diff --git a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiProcessorTest.java b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiProcessorTest.java index b200264..1805812 100644 --- a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiProcessorTest.java +++ b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiProcessorTest.java @@ -44,7 +44,7 @@ public class RestOpenApiProcessorTest { } }); - RestOpenApiProcessor processor = new RestOpenApiProcessor(null, false, null, context.getRestConfiguration()); + RestOpenApiProcessor processor = new RestOpenApiProcessor(null, context.getRestConfiguration()); Exchange exchange = new DefaultExchange(context); processor.process(exchange); @@ -67,7 +67,7 @@ public class RestOpenApiProcessorTest { } }); - RestOpenApiProcessor processor = new RestOpenApiProcessor(null, false, null, context.getRestConfiguration()); + RestOpenApiProcessor processor = new RestOpenApiProcessor(null, context.getRestConfiguration()); Exchange exchange = new DefaultExchange(context); exchange.getMessage().setHeader(Exchange.HTTP_PATH, "/openapi.json"); processor.process(exchange); @@ -92,7 +92,7 @@ public class RestOpenApiProcessorTest { } }); - RestOpenApiProcessor processor = new RestOpenApiProcessor(null, false, null, context.getRestConfiguration()); + RestOpenApiProcessor processor = new RestOpenApiProcessor(null, context.getRestConfiguration()); Exchange exchange = new DefaultExchange(context); exchange.getMessage().setHeader(Exchange.HTTP_PATH, "/openapi.yaml"); processor.process(exchange); @@ -121,7 +121,7 @@ public class RestOpenApiProcessorTest { } }); - RestOpenApiProcessor processor = new RestOpenApiProcessor(null, false, null, context.getRestConfiguration()); + RestOpenApiProcessor processor = new RestOpenApiProcessor(null, context.getRestConfiguration()); Exchange exchange = new DefaultExchange(context); exchange.getMessage().setHeader(Exchange.HTTP_PATH, "/openapi.yaml"); processor.process(exchange); @@ -136,31 +136,6 @@ public class RestOpenApiProcessorTest { } @Test - public void testRestOpenApiProcessorCustomPath() throws Exception { - CamelContext context = new DefaultCamelContext(); - context.addRoutes(new RouteBuilder() { - @Override - public void configure() throws Exception { - rest().get("/foo").description("Foo endpoint").route().log("Hello /foo").endRest() - .post("/bar").description("Bar endpoint").route().log("Hello /foo").endRest(); - } - }); - - RestOpenApiProcessor processor = new RestOpenApiProcessor(null, false, null, context.getRestConfiguration()); - Exchange exchange = new DefaultExchange(context); - exchange.getMessage().setHeader(Exchange.HTTP_PATH, "/some/custom/path/api.json"); - processor.process(exchange); - - String json = exchange.getMessage().getBody(String.class); - assertNotNull(json); - assertEquals("application/json", exchange.getMessage().getHeader(Exchange.CONTENT_TYPE)); - assertTrue(json.contains("\"/foo\"")); - assertTrue(json.contains("\"/bar\"")); - assertTrue(json.contains("\"summary\" : \"Foo endpoint\"")); - assertTrue(json.contains("\"summary\" : \"Bar endpoint\"")); - } - - @Test public void testRestOpenApiProcessorAcceptHeaderJson() throws Exception { CamelContext context = new DefaultCamelContext(); context.addRoutes(new RouteBuilder() { @@ -171,9 +146,9 @@ public class RestOpenApiProcessorTest { } }); - RestOpenApiProcessor processor = new RestOpenApiProcessor(null, false, null, context.getRestConfiguration()); + RestOpenApiProcessor processor = new RestOpenApiProcessor(null, context.getRestConfiguration()); Exchange exchange = new DefaultExchange(context); - exchange.getMessage().setHeader(Exchange.HTTP_PATH, "/some/custom/path/api"); + exchange.getMessage().setHeader(Exchange.HTTP_PATH, "/"); exchange.getMessage().setHeader("Accept", "application/json"); processor.process(exchange); @@ -197,9 +172,9 @@ public class RestOpenApiProcessorTest { } }); - RestOpenApiProcessor processor = new RestOpenApiProcessor(null, false, null, context.getRestConfiguration()); + RestOpenApiProcessor processor = new RestOpenApiProcessor(null, context.getRestConfiguration()); Exchange exchange = new DefaultExchange(context); - exchange.getMessage().setHeader(Exchange.HTTP_PATH, "/some/custom/path/api"); + exchange.getMessage().setHeader(Exchange.HTTP_PATH, "/"); exchange.getMessage().setHeader("Accept", "application/yaml"); processor.process(exchange); @@ -212,102 +187,4 @@ public class RestOpenApiProcessorTest { assertTrue(yaml.contains("summary: \"Bar endpoint\"")); } - @Test - public void testRestOpenApiProcessorContextIdListingEnabledForDefaultPath() throws Exception { - CamelContext context = new DefaultCamelContext(); - context.addRoutes(new RouteBuilder() { - @Override - public void configure() throws Exception { - rest().get("/foo").description("Foo endpoint").route().log("Hello /foo").endRest() - .post("/bar").description("Bar endpoint").route().log("Hello /foo").endRest(); - } - }); - - context.getRegistry().bind("dummy", new DummyRestConsumerFactory()); - - RestOpenApiProcessor processor = new RestOpenApiProcessor(".*camel.*", true, null, context.getRestConfiguration()); - Exchange exchange = new DefaultExchange(context); - exchange.getMessage().setHeader(Exchange.HTTP_PATH, "/openapi.json"); - - context.start(); - try { - processor.process(exchange); - - String json = exchange.getMessage().getBody(String.class); - assertNotNull(json); - - assertEquals("[{\"name\":\"" + context.getName() + "\"}]", json.replaceAll("\\s+", "")); - } finally { - context.stop(); - } - } - - @Test - public void testRestOpenApiProcessorContextIdListingForNamePlaceholder() throws Exception { - CamelContext context = new DefaultCamelContext(); - context.addRoutes(new RouteBuilder() { - @Override - public void configure() throws Exception { - rest().get("/foo").description("Foo endpoint").route().log("Hello /foo").endRest() - .post("/bar").description("Bar endpoint").route().log("Hello /foo").endRest(); - } - }); - - RestOpenApiProcessor processor = new RestOpenApiProcessor("#name#", false, null, context.getRestConfiguration()); - Exchange exchange = new DefaultExchange(context); - exchange.getMessage().setHeader(Exchange.HTTP_PATH, "/openapi.json"); - processor.process(exchange); - - String json = exchange.getMessage().getBody(String.class); - assertNotNull(json); - assertTrue(json.contains("\"/foo\"")); - assertTrue(json.contains("\"/bar\"")); - assertTrue(json.contains("\"summary\" : \"Foo endpoint\"")); - assertTrue(json.contains("\"summary\" : \"Bar endpoint\"")); - } - - @Test - public void testRestOpenApiProcessorContextIdListingEnabledForCustomPath() throws Exception { - CamelContext context = new DefaultCamelContext(); - context.addRoutes(new RouteBuilder() { - @Override - public void configure() throws Exception { - rest("/rest").get("/foo").description("Foo endpoint").route().id("foo-route").log("Hello /foo").endRest() - .post("/bar").description("Bar endpoint").route().id("bar-route").log("Hello /foo").endRest(); - } - }); - - RestOpenApiProcessor processor = new RestOpenApiProcessor(".*camel.*", true, null, context.getRestConfiguration()); - Exchange exchange = new DefaultExchange(context); - exchange.getMessage().setHeader(Exchange.HTTP_PATH, "/" + context.getName() + "/rest"); - processor.process(exchange); - - String json = exchange.getMessage().getBody(String.class); - assertNotNull(json); - assertTrue(json.contains("\"/rest/foo\"")); - assertTrue(json.contains("\"/rest/bar\"")); - assertTrue(json.contains("\"summary\" : \"Foo endpoint\"")); - assertTrue(json.contains("\"summary\" : \"Bar endpoint\"")); - } - - @Test - public void testRestOpenApiProcessorContextIdPatternNoMatches() throws Exception { - CamelContext context = new DefaultCamelContext(); - context.addRoutes(new RouteBuilder() { - @Override - public void configure() throws Exception { - rest("/").get("/foo").description("Foo endpoint").route().log("Hello /foo").endRest().post("/bar") - .description("Bar endpoint").route().log("Hello /foo").endRest(); - } - }); - - RestOpenApiProcessor processor - = new RestOpenApiProcessor("an-invalid-pattern", false, null, context.getRestConfiguration()); - Exchange exchange = new DefaultExchange(context); - exchange.getMessage().setHeader("/some/rest/api/document.json", Exchange.HTTP_PATH); - processor.process(exchange); - - assertEquals(204, exchange.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE)); - assertNull(exchange.getMessage().getBody()); - } } diff --git a/components/camel-swagger-java/src/main/docs/swagger-java.adoc b/components/camel-swagger-java/src/main/docs/swagger-java.adoc index d6fef61..50e182e 100644 --- a/components/camel-swagger-java/src/main/docs/swagger-java.adoc +++ b/components/camel-swagger-java/src/main/docs/swagger-java.adoc @@ -121,23 +121,10 @@ So using relative paths is much easier. See above for an example. |api.license.name |String |The license name used for the API. |api.license.url |String |A URL to the license used for the API. - -|apiContextIdListing |boolean |Whether to allow listing all the CamelContext names in the JVM that has -REST services. When enabled then the root path of the api-doc will list -all the contexts. When disabled then no context ids is listed and the -root path of the api-doc lists the current CamelContext. Is default -false. - -|apiContextIdPattern |String |A pattern that allows to filter which CamelContext names is shown in the -context listing. The pattern is using regular expression and * as -wildcard. Its the same pattern matching as used by -Intercept |=== == Adding Security Definitions in API doc -*Since Camel 2.22.0* - The Rest DSL now supports declaring Swagger `securityDefinitions` in the generated API document. For example as shown below: @@ -174,21 +161,8 @@ their key (petstore_auth or api_key). Here the get operation is using the Api Key security and the put operation is using OAuth security with permitted scopes of read and write pets. - - -== ContextIdListing enabled - -When contextIdListing is enabled then its detecting all the running -CamelContexts in the same JVM. These contexts are listed in the root -path, eg `/api-docs` as a simple list of names in json format. To access -the swagger documentation then the context-path must be appended with -the Camel context id, such as `api-docs/myCamel`. The -option apiContextIdPattern can be used to filter the names in this list. - == JSon or Yaml -*Since Camel 2.17* - The camel-swagger-java module supports both JSon and Yaml out of the box. You can specify in the request url what you want returned by using /swagger.json or /swagger.yaml for either one. If none is specified then diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerProcessor.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerProcessor.java index 1763c9a..6c59677 100644 --- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerProcessor.java +++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerProcessor.java @@ -24,8 +24,6 @@ import io.swagger.jaxrs.config.BeanConfig; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.spi.RestConfiguration; -import org.apache.camel.support.PatternHelper; -import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,15 +32,11 @@ public class RestSwaggerProcessor implements Processor { private static final Logger LOG = LoggerFactory.getLogger(RestSwaggerProcessor.class); private final BeanConfig swaggerConfig; private final RestSwaggerSupport support; - private final String contextIdPattern; - private final boolean contextIdListing; private final RestConfiguration configuration; @SuppressWarnings("unchecked") - public RestSwaggerProcessor(String contextIdPattern, boolean contextIdListing, Map<String, Object> parameters, + public RestSwaggerProcessor(Map<String, Object> parameters, RestConfiguration configuration) { - this.contextIdPattern = contextIdPattern; - this.contextIdListing = contextIdListing; this.configuration = configuration; this.support = new RestSwaggerSupport(); this.swaggerConfig = new BeanConfig(); @@ -56,7 +50,6 @@ public class RestSwaggerProcessor implements Processor { @Override public void process(Exchange exchange) throws Exception { - String contextId = exchange.getContext().getName(); String route = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class); String accept = exchange.getIn().getHeader("Accept", String.class); @@ -82,45 +75,8 @@ public class RestSwaggerProcessor implements Processor { } try { - // render list of camel contexts as root - if (contextIdListing && (ObjectHelper.isEmpty(route) || route.equals("/"))) { - support.renderCamelContexts(adapter, contextId, contextIdPattern, json, yaml, configuration); - } else { - String name; - if (ObjectHelper.isNotEmpty(route)) { - // first part is the camel context - if (route.startsWith("/")) { - route = route.substring(1); - } - // the remainder is the route part - name = route.split("/")[0]; - if (route.startsWith(contextId)) { - route = route.substring(name.length()); - } - } else { - // listing not enabled then get current camel context as the name - name = exchange.getContext().getName(); - } - - boolean match = true; - if (contextIdPattern != null) { - if ("#name#".equals(contextIdPattern)) { - match = name.equals(contextId); - } else { - match = PatternHelper.matchPattern(name, contextIdPattern); - } - if (LOG.isDebugEnabled()) { - LOG.debug("Match contextId: {} with pattern: {} -> {}", name, contextIdPattern, match); - } - } - - if (!match) { - adapter.noContent(); - } else { - support.renderResourceListing(exchange.getContext(), adapter, swaggerConfig, name, route, json, yaml, - exchange.getIn().getHeaders(), exchange.getContext().getClassResolver(), configuration); - } - } + support.renderResourceListing(exchange.getContext(), adapter, swaggerConfig, route, json, yaml, + exchange.getIn().getHeaders(), exchange.getContext().getClassResolver(), configuration); } catch (Exception e) { LOG.warn("Error rendering Swagger API due {}", e.getMessage(), e); } diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerSupport.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerSupport.java index 18cefe2..6b59565 100644 --- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerSupport.java +++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerSupport.java @@ -17,17 +17,10 @@ package org.apache.camel.swagger; import java.io.InputStream; -import java.lang.management.ManagementFactory; -import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Set; - -import javax.management.MBeanServer; -import javax.management.ObjectName; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.JsonNode; @@ -49,7 +42,6 @@ import org.apache.camel.model.rest.RestDefinition; import org.apache.camel.model.rest.RestsDefinition; import org.apache.camel.spi.ClassResolver; import org.apache.camel.spi.RestConfiguration; -import org.apache.camel.support.PatternHelper; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.URISupport; import org.slf4j.Logger; @@ -164,56 +156,8 @@ public class RestSwaggerSupport { return rests; } - public List<RestDefinition> getRestDefinitions(CamelContext camelContext, String camelId) throws Exception { - ObjectName found = null; - - MBeanServer server = ManagementFactory.getPlatformMBeanServer(); - Set<ObjectName> names = server.queryNames(new ObjectName("org.apache.camel:type=context,*"), null); - for (ObjectName on : names) { - String id = on.getKeyProperty("name"); - if (id.startsWith("\"") && id.endsWith("\"")) { - id = id.substring(1, id.length() - 1); - } - if (camelId == null || camelId.equals(id)) { - found = on; - } - } - - if (found != null) { - String xml = (String) server.invoke(found, "dumpRestsAsXml", new Object[] { true }, new String[] { "boolean" }); - if (xml != null) { - LOG.debug("DumpRestAsXml:\n{}", xml); - InputStream isxml = camelContext.getTypeConverter().convertTo(InputStream.class, xml); - ExtendedCamelContext ecc = camelContext.adapt(ExtendedCamelContext.class); - RestsDefinition rests - = (RestsDefinition) ecc.getXMLRoutesDefinitionLoader().loadRestsDefinition(camelContext, isxml); - if (rests != null) { - return rests.getRests(); - } - } - } - - return null; - } - - public List<String> findCamelContexts() throws Exception { - List<String> answer = new ArrayList<>(); - - MBeanServer server = ManagementFactory.getPlatformMBeanServer(); - Set<ObjectName> names = server.queryNames(new ObjectName("*:type=context,*"), null); - for (ObjectName on : names) { - - String id = on.getKeyProperty("name"); - if (id.startsWith("\"") && id.endsWith("\"")) { - id = id.substring(1, id.length() - 1); - } - answer.add(id); - } - return answer; - } - public void renderResourceListing( - CamelContext camelContext, RestApiResponseAdapter response, BeanConfig swaggerConfig, String contextId, + CamelContext camelContext, RestApiResponseAdapter response, BeanConfig swaggerConfig, String route, boolean json, boolean yaml, Map<String, Object> headers, ClassResolver classResolver, RestConfiguration configuration) throws Exception { @@ -227,12 +171,7 @@ public class RestSwaggerSupport { setupCorsHeaders(response, configuration.getCorsHeaders()); } - List<RestDefinition> rests; - if (camelContext.getName().equals(contextId)) { - rests = getRestDefinitions(camelContext); - } else { - rests = getRestDefinitions(camelContext, contextId); - } + List<RestDefinition> rests = getRestDefinitions(camelContext); if (rests != null) { final Map<String, Object> apiProperties @@ -242,7 +181,7 @@ public class RestSwaggerSupport { (String) apiProperties.getOrDefault("api.specification.contentType.json", "application/json")); // read the rest-dsl into swagger model - Swagger swagger = reader.read(rests, route, swaggerConfig, contextId, classResolver); + Swagger swagger = reader.read(rests, route, swaggerConfig, camelContext.getName(), classResolver); if (configuration.isUseXForwardHeaders()) { setupXForwardedHeaders(swagger, headers); } @@ -262,7 +201,7 @@ public class RestSwaggerSupport { (String) apiProperties.getOrDefault("api.specification.contentType.yaml", "text/yaml")); // read the rest-dsl into swagger model - Swagger swagger = reader.read(rests, route, swaggerConfig, contextId, classResolver); + Swagger swagger = reader.read(rests, route, swaggerConfig, camelContext.getName(), classResolver); if (configuration.isUseXForwardHeaders()) { setupXForwardedHeaders(swagger, headers); } @@ -287,68 +226,6 @@ public class RestSwaggerSupport { } } - /** - * Renders a list of available CamelContexts in the JVM - */ - public void renderCamelContexts( - RestApiResponseAdapter response, String contextId, String contextIdPattern, boolean json, boolean yaml, - RestConfiguration configuration) - throws Exception { - LOG.trace("renderCamelContexts"); - - if (cors) { - setupCorsHeaders(response, configuration.getCorsHeaders()); - } - - List<String> contexts = findCamelContexts(); - - // filter non matched CamelContext's - if (contextIdPattern != null) { - Iterator<String> it = contexts.iterator(); - while (it.hasNext()) { - String name = it.next(); - - boolean match; - if ("#name#".equals(contextIdPattern)) { - match = name.equals(contextId); - } else { - match = PatternHelper.matchPattern(name, contextIdPattern); - } - if (!match) { - it.remove(); - } - } - } - - StringBuffer sb = new StringBuffer(); - - if (json) { - response.setHeader(Exchange.CONTENT_TYPE, "application/json"); - - sb.append("[\n"); - for (int i = 0; i < contexts.size(); i++) { - String name = contexts.get(i); - sb.append("{\"name\": \"").append(name).append("\"}"); - if (i < contexts.size() - 1) { - sb.append(",\n"); - } - } - sb.append("\n]"); - } else { - response.setHeader(Exchange.CONTENT_TYPE, "text/yaml"); - - for (int i = 0; i < contexts.size(); i++) { - String name = contexts.get(i); - sb.append("- \"").append(name).append("\"\n"); - } - } - - int len = sb.length(); - response.setHeader(Exchange.CONTENT_LENGTH, "" + len); - - response.writeBytes(sb.toString().getBytes()); - } - private static void setupCorsHeaders(RestApiResponseAdapter response, Map<String, String> corsHeaders) { // use default value if none has been configured String allowOrigin = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Origin") : null; diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java index 0162a83..5712521 100644 --- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java +++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java @@ -28,7 +28,7 @@ public class SwaggerRestApiProcessorFactory implements RestApiProcessorFactory { @Override public Processor createApiProcessor( - CamelContext camelContext, String contextPath, String contextIdPattern, boolean contextIdListing, + CamelContext camelContext, String contextPath, RestConfiguration configuration, Map<String, Object> parameters) throws Exception { @@ -78,6 +78,6 @@ public class SwaggerRestApiProcessorFactory implements RestApiProcessorFactory { options.put("cors", "true"); } - return new RestSwaggerProcessor(contextIdPattern, contextIdListing, options, configuration); + return new RestSwaggerProcessor(options, configuration); } } diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/RestApiProcessorFactory.java b/core/camel-api/src/main/java/org/apache/camel/spi/RestApiProcessorFactory.java index 94ee785..a66554a 100644 --- a/core/camel-api/src/main/java/org/apache/camel/spi/RestApiProcessorFactory.java +++ b/core/camel-api/src/main/java/org/apache/camel/spi/RestApiProcessorFactory.java @@ -34,16 +34,15 @@ public interface RestApiProcessorFactory { * Creates a new REST API <a href="http://camel.apache.org/processor.html">Processor </a>, which provides API * listing of the REST services * - * @param camelContext the camel context - * @param contextPath the context-path - * @param contextIdPattern id pattern to only allow Rest APIs from rest services within CamelContext's which name - * matches the pattern. - * @param parameters additional parameters - * @return a newly created REST API provider - * @throws Exception can be thrown + * @param camelContext the camel context + * @param contextPath the context-path + * @param configuration the rest configuration + * @param parameters additional parameters + * @return a newly created REST API provider + * @throws Exception can be thrown */ Processor createApiProcessor( - CamelContext camelContext, String contextPath, String contextIdPattern, boolean contextIdListing, + CamelContext camelContext, String contextPath, RestConfiguration configuration, Map<String, Object> parameters) throws Exception; diff --git a/core/camel-core/src/test/java/org/apache/camel/component/rest/DummyRestProcessorFactory.java b/core/camel-core/src/test/java/org/apache/camel/component/rest/DummyRestProcessorFactory.java index 6d42bd1..d24145d 100644 --- a/core/camel-core/src/test/java/org/apache/camel/component/rest/DummyRestProcessorFactory.java +++ b/core/camel-core/src/test/java/org/apache/camel/component/rest/DummyRestProcessorFactory.java @@ -28,7 +28,7 @@ public class DummyRestProcessorFactory implements RestApiProcessorFactory { @Override public Processor createApiProcessor( - CamelContext camelContext, String contextPath, String contextIdPattern, boolean contextIdListing, + CamelContext camelContext, String contextPath, RestConfiguration configuration, Map<String, Object> parameters) throws Exception { diff --git a/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide-3_13.adoc b/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide-3_13.adoc index b3c5721..731e400 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide-3_13.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide-3_13.adoc @@ -15,6 +15,10 @@ Added method `scheduleQueue` to `org.apache.camel.spi.ReactiveExecutor`. The `org.apache.camel.support.ObjectHelper#createIterable` now supports `java.util.Map` returning an `Iterable<Map.Entry>` iterating each row in the map. +The `org.apache.camel.spi.RestApiProcessorFactory` has removed +the following two parameters (`String contextIdPattern, boolean contextIdListing`) +from the `createApiProcessor` method. + === Split EIP The Split EIP now supports splitting message bodies that are of type `java.util.Map`, which @@ -93,3 +97,8 @@ The `ScpOperations` class were modified to comply with the API changes on the `c === camel-kafka The `KafkaManualCommit` class were modified in order to support async manual commit. Please use the new function `commit()` instead of the old deprecated one `commitSync()`. + +=== camel-openapi-java / camel-swagger-java + +Support for rendering api docs by discovering other CamelContext via JMX in the same JVM has been removed. +Rendering of api docs is now only supported for the same CamelContext.