You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/09/17 17:15:41 UTC
[2/8] camel git commit: CAMEL-7800: camel-swagger-java - work in
progress
CAMEL-7800: camel-swagger-java - work in progress
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1364c958
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1364c958
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1364c958
Branch: refs/heads/swagger
Commit: 1364c9588d462a3be980d9b2040b305afdb5cfec
Parents: d40c009
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Sep 17 15:02:33 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Sep 17 15:02:33 2015 +0200
----------------------------------------------------------------------
.../apache/camel/swagger/RestSwaggerReader.java | 116 +++++++++++++++++++
.../camel/swagger/DummyRestConsumerFactory.java | 48 ++++++++
.../camel/swagger/RestSwaggerReaderTest.java | 79 +++++++++++++
.../src/test/resources/log4j.properties | 2 +-
4 files changed, 244 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/1364c958/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
new file mode 100644
index 0000000..4a896a3
--- /dev/null
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
@@ -0,0 +1,116 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.swagger;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Locale;
+
+import io.swagger.config.SwaggerConfig;
+import io.swagger.models.Operation;
+import io.swagger.models.Path;
+import io.swagger.models.Swagger;
+import org.apache.camel.model.rest.RestDefinition;
+import org.apache.camel.model.rest.VerbDefinition;
+
+public class RestSwaggerReader {
+
+ public Swagger read(RestDefinition rest) {
+ Swagger swagger = new Swagger();
+
+ List<VerbDefinition> verbs = new ArrayList<>(rest.getVerbs());
+ // must sort the verbs by uri so we group them together when an uri has multiple operations
+ Collections.sort(verbs, new VerbOrdering());
+
+ // used during gathering of apis
+ List<Path> paths = new ArrayList<>();
+
+ String basePath = rest.getPath();
+
+ for (VerbDefinition verb : verbs) {
+
+ // TODO: should be base path and a lot more
+
+
+ // the method must be in lower case
+ String method = verb.asVerb().toLowerCase(Locale.US);
+
+ String opPath = getPath(basePath, verb.getUri());
+
+ Operation op = new Operation();
+
+ Path path = swagger.getPath(opPath);
+ if (path == null) {
+ path = new Path();
+ paths.add(path);
+ }
+ path = path.set(method, op);
+
+ if (verb.getConsumes() != null) {
+ op.consumes(verb.getConsumes());
+ }
+ if (verb.getProduces() != null) {
+ op.produces(verb.getProduces());
+ }
+ if (verb.getDescriptionText() != null) {
+ op.summary(verb.getDescriptionText());
+ }
+
+ // add path
+ swagger.path(opPath, path);
+ }
+
+ return swagger;
+ }
+
+ private String getPath(String basePath, String uri) {
+ // TODO: slash check and avoid double slash and all that
+ return basePath + "/" + uri;
+ }
+
+ /**
+ * To sort the rest operations
+ */
+ private static class VerbOrdering implements Comparator<VerbDefinition> {
+
+ @Override
+ public int compare(VerbDefinition a, VerbDefinition b) {
+
+ String u1 = "";
+ if (a.getUri() != null) {
+ // replace { with _ which comes before a when soring by char
+ u1 = a.getUri().replace("{", "_");
+ }
+ String u2 = "";
+ if (b.getUri() != null) {
+ // replace { with _ which comes before a when soring by char
+ u2 = b.getUri().replace("{", "_");
+ }
+
+ int num = u1.compareTo(u2);
+ if (num == 0) {
+ // same uri, so use http method as sorting
+ num = a.asVerb().compareTo(b.asVerb());
+ }
+ return num;
+ }
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/1364c958/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/DummyRestConsumerFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/DummyRestConsumerFactory.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/DummyRestConsumerFactory.java
new file mode 100644
index 0000000..d0d6efe
--- /dev/null
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/DummyRestConsumerFactory.java
@@ -0,0 +1,48 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.swagger;
+
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.component.seda.SedaEndpoint;
+import org.apache.camel.impl.ActiveMQUuidGenerator;
+import org.apache.camel.spi.RestConsumerFactory;
+
+public class DummyRestConsumerFactory implements RestConsumerFactory {
+
+ @Override
+ public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate,
+ String consumes, String produces, Map<String, Object> parameters) throws Exception {
+ // just use a seda endpoint for testing purpose
+ String id;
+ if (uriTemplate != null) {
+ id = ActiveMQUuidGenerator.generateSanitizedId(basePath + uriTemplate);
+ } else {
+ id = ActiveMQUuidGenerator.generateSanitizedId(basePath);
+ }
+ // remove leading dash as we add that ourselves
+ if (id.startsWith("-")) {
+ id = id.substring(1);
+ }
+ SedaEndpoint seda = camelContext.getEndpoint("seda:" + verb + "-" + id, SedaEndpoint.class);
+ return seda.createConsumer(processor);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/1364c958/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderTest.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderTest.java
new file mode 100644
index 0000000..2252479
--- /dev/null
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderTest.java
@@ -0,0 +1,79 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.swagger;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import io.swagger.jaxrs.config.BeanConfig;
+import io.swagger.models.Swagger;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.model.rest.RestDefinition;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class RestSwaggerReaderTest extends CamelTestSupport {
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("dummy-rest", new DummyRestConsumerFactory());
+ return jndi;
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ rest("/hello")
+ .get("/hi").to("log:hi")
+ .get("/bye").to("log:bye")
+ .post("/bye").to("log:bye");
+ }
+ };
+ }
+
+ @Test
+ public void testReaderRead() throws Exception {
+ RestDefinition rest = context.getRestDefinitions().get(0);
+ assertNotNull(rest);
+
+ BeanConfig config = new BeanConfig();
+ config.setBasePath("http://localhost:8080/api");
+ RestSwaggerReader reader = new RestSwaggerReader();
+
+ Swagger swagger = reader.read(rest);
+ assertNotNull(swagger);
+
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.enable(SerializationFeature.INDENT_OUTPUT);
+ mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+ String json = mapper.writeValueAsString(swagger);
+
+ log.info(json);
+
+ assertTrue(json.contains("\"basePath\":\"http://localhost:8080/api\""));
+ assertTrue(json.contains("\"resourcePath\":\"/hello\""));
+ assertTrue(json.contains("\"method\":\"GET\""));
+ assertTrue(json.contains("\"nickname\":\"getHelloHi\""));
+
+ context.stop();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/1364c958/components/camel-swagger-java/src/test/resources/log4j.properties
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/test/resources/log4j.properties b/components/camel-swagger-java/src/test/resources/log4j.properties
index 3e0349e..36983fa 100644
--- a/components/camel-swagger-java/src/test/resources/log4j.properties
+++ b/components/camel-swagger-java/src/test/resources/log4j.properties
@@ -18,7 +18,7 @@
#
# The logging properties used for testing.
#
-log4j.rootLogger=INFO, file
+log4j.rootLogger=INFO, out
# uncomment the following to enable camel debugging
#log4j.logger.org.apache.camel.component.swagger=TRACE