You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2017/07/31 07:45:33 UTC

[3/7] james-project git commit: JAMES-2099 Add James REST API to expose the swagger API

JAMES-2099 Add James REST API to expose the swagger API


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/4363f0a2
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/4363f0a2
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/4363f0a2

Branch: refs/heads/master
Commit: 4363f0a24e426abe76d55ae009eb9db3e752c3f8
Parents: b4f432a
Author: quynhn <qn...@linagora.com>
Authored: Tue Jul 25 10:49:35 2017 +0700
Committer: quynhn <qn...@linagora.com>
Committed: Mon Jul 31 10:35:46 2017 +0700

----------------------------------------------------------------------
 .../protocols/webadmin/webadmin-swagger/pom.xml | 212 +++++++++++++++++++
 .../james/webadmin/swagger/SwaggerParser.java   |  84 ++++++++
 .../webadmin/swagger/routes/SwaggerRoutes.java  |  48 +++++
 3 files changed, 344 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/4363f0a2/server/protocols/webadmin/webadmin-swagger/pom.xml
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-swagger/pom.xml b/server/protocols/webadmin/webadmin-swagger/pom.xml
new file mode 100644
index 0000000..81e654c
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-swagger/pom.xml
@@ -0,0 +1,212 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <artifactId>james-server</artifactId>
+        <groupId>org.apache.james</groupId>
+        <version>3.1.0-SNAPSHOT</version>
+        <relativePath>../../../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>james-server-webadmin-swagger</artifactId>
+    <packaging>jar</packaging>
+
+    <name>Apache James :: Server :: Web Admin :: Swagger</name>
+
+    <profiles>
+        <profile>
+            <id>noTest</id>
+            <activation>
+                <os>
+                    <family>windows</family>
+                </os>
+            </activation>
+            <build>
+                <plugins>
+                    <plugin>
+                        <groupId>org.apache.maven.plugins</groupId>
+                        <artifactId>maven-surefire-plugin</artifactId>
+                        <configuration>
+                            <skipTests>true</skipTests>
+                        </configuration>
+                    </plugin>
+                </plugins>
+            </build>
+        </profile>
+        <profile>
+            <id>disable-build-for-older-jdk</id>
+            <activation>
+                <jdk>(,1.8)</jdk>
+            </activation>
+            <build>
+                <plugins>
+                    <plugin>
+                        <artifactId>maven-jar-plugin</artifactId>
+                        <executions>
+                            <execution>
+                                <id>default-jar</id>
+                                <phase>none</phase>
+                            </execution>
+                            <execution>
+                                <id>jar</id>
+                                <phase>none</phase>
+                            </execution>
+                            <execution>
+                                <id>test-jar</id>
+                                <phase>none</phase>
+                            </execution>
+                        </executions>
+                    </plugin>
+                    <plugin>
+                        <artifactId>maven-compiler-plugin</artifactId>
+                        <executions>
+                            <execution>
+                                <id>default-compile</id>
+                                <phase>none</phase>
+                            </execution>
+                            <execution>
+                                <id>default-testCompile</id>
+                                <phase>none</phase>
+                            </execution>
+                        </executions>
+                    </plugin>
+                    <plugin>
+                        <artifactId>maven-surefire-plugin</artifactId>
+                        <executions>
+                            <execution>
+                                <id>default-test</id>
+                                <phase>none</phase>
+                            </execution>
+                        </executions>
+                    </plugin>
+                    <plugin>
+                        <artifactId>maven-source-plugin</artifactId>
+                        <executions>
+                            <execution>
+                                <id>attach-sources</id>
+                                <phase>none</phase>
+                            </execution>
+                        </executions>
+                    </plugin>
+                    <plugin>
+                        <artifactId>maven-install-plugin</artifactId>
+                        <executions>
+                            <execution>
+                                <id>default-install</id>
+                                <phase>none</phase>
+                            </execution>
+                        </executions>
+                    </plugin>
+                    <plugin>
+                        <artifactId>maven-resources-plugin</artifactId>
+                        <executions>
+                            <execution>
+                                <id>default-resources</id>
+                                <phase>none</phase>
+                            </execution>
+                            <execution>
+                                <id>default-testResources</id>
+                                <phase>none</phase>
+                            </execution>
+                        </executions>
+                    </plugin>
+                    <plugin>
+                        <artifactId>maven-site-plugin</artifactId>
+                        <executions>
+                            <execution>
+                                <id>attach-descriptor</id>
+                                <phase>none</phase>
+                            </execution>
+                        </executions>
+                    </plugin>
+                </plugins>
+            </build>
+        </profile>
+        <profile>
+            <id>build-for-jdk-8</id>
+            <activation>
+                <jdk>[1.8,)</jdk>
+            </activation>
+            <dependencies>
+                <dependency>
+                    <groupId>org.apache.james</groupId>
+                    <artifactId>james-server-webadmin-core</artifactId>
+                </dependency>
+            </dependencies>
+            <build>
+                <plugins>
+                    <plugin>
+                        <artifactId>maven-assembly-plugin</artifactId>
+                        <configuration>
+                            <archive>
+                                <manifest>
+                                    <mainClass>fully.qualified.MainClass</mainClass>
+                                </manifest>
+                            </archive>
+                            <descriptorRefs>
+                                <descriptorRef>jar-with-dependencies</descriptorRef>
+                            </descriptorRefs>
+                        </configuration>
+                    </plugin>
+                    <plugin>
+                        <groupId>org.apache.maven.plugins</groupId>
+                        <artifactId>maven-compiler-plugin</artifactId>
+                        <configuration>
+                            <source>1.8</source>
+                            <target>1.8</target>
+                        </configuration>
+                    </plugin>
+                </plugins>
+            </build>
+        </profile>
+        <profile>
+            <id>animal-sniffer-java-8</id>
+            <activation>
+                <jdk>[1.8,)</jdk>
+            </activation>
+            <build>
+                <plugins>
+                    <plugin>
+                        <groupId>org.codehaus.mojo</groupId>
+                        <artifactId>animal-sniffer-maven-plugin</artifactId>
+                        <configuration>
+                            <signature>
+                                <groupId>org.codehaus.mojo.signature</groupId>
+                                <artifactId>java18</artifactId>
+                                <version>1.0</version>
+                            </signature>
+                        </configuration>
+                        <executions>
+                            <execution>
+                                <id>check_java_8</id>
+                                <phase>test</phase>
+                                <goals>
+                                    <goal>check</goal>
+                                </goals>
+                            </execution>
+                        </executions>
+                    </plugin>
+                </plugins>
+            </build>
+        </profile>
+    </profiles>
+</project>

http://git-wip-us.apache.org/repos/asf/james-project/blob/4363f0a2/server/protocols/webadmin/webadmin-swagger/src/main/java/org/apache/james/webadmin/swagger/SwaggerParser.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-swagger/src/main/java/org/apache/james/webadmin/swagger/SwaggerParser.java b/server/protocols/webadmin/webadmin-swagger/src/main/java/org/apache/james/webadmin/swagger/SwaggerParser.java
new file mode 100644
index 0000000..8ebd303
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-swagger/src/main/java/org/apache/james/webadmin/swagger/SwaggerParser.java
@@ -0,0 +1,84 @@
+/****************************************************************
+ * 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.james.webadmin.swagger;
+
+import javax.inject.Inject;
+
+import org.apache.james.webadmin.WebAdminConfiguration;
+
+import com.fasterxml.jackson.annotation.JsonInclude.Include;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import io.swagger.annotations.Api;
+import io.swagger.annotations.SwaggerDefinition;
+import io.swagger.jaxrs.Reader;
+import io.swagger.jaxrs.config.BeanConfig;
+import io.swagger.models.Swagger;
+import org.reflections.Reflections;
+
+@SwaggerDefinition
+public class SwaggerParser {
+	private static final String[] SCHEMES = new String[]{SwaggerDefinition.Scheme.HTTP.name(), SwaggerDefinition.Scheme.HTTPS.name()};
+	private static final String JSON_TYPE = "application/json";
+	private static final String API_DOC_VERSION = "V1.0";
+	private static final String API_DOC_TITLE = "JAMES Web Admin API";
+	private static final String API_DOC_DESCRIPTION = "All the web administration API for JAMES";
+	private static final String JAMES_WEBADMIN_HOST = "localhost:";
+
+	@Inject
+	public static String getSwaggerJson(String packageName, WebAdminConfiguration configuration) throws JsonProcessingException {
+		return swaggerToJson(getSwagger(packageName, configuration));
+	}
+
+	private static Swagger getSwagger(String packageName, WebAdminConfiguration configuration) {
+		return new Reader(getSwagger(getBeanConfig(packageName, configuration)))
+				.read(new Reflections(packageName)
+				.getTypesAnnotatedWith(Api.class));
+	}
+
+	private static Swagger getSwagger(BeanConfig beanConfig) {
+		Swagger swagger = beanConfig.getSwagger();
+
+		swagger.addConsumes(JSON_TYPE);
+		swagger.addProduces(JSON_TYPE);
+		return swagger;
+	}
+
+	private static BeanConfig getBeanConfig(String packageName, WebAdminConfiguration configuration) {
+		BeanConfig beanConfig = new BeanConfig();
+		beanConfig.setResourcePackage(packageName);
+		beanConfig.setVersion(API_DOC_VERSION);
+		beanConfig.setTitle(API_DOC_TITLE);
+		beanConfig.setDescription(API_DOC_DESCRIPTION);
+		beanConfig.setHost(JAMES_WEBADMIN_HOST + configuration.getPort().toInt());
+		beanConfig.setSchemes(SCHEMES);
+		beanConfig.setScan(true);
+		beanConfig.scanAndRead();
+		return beanConfig;
+	}
+
+	public static String swaggerToJson(Swagger swagger) throws JsonProcessingException {
+		ObjectMapper objectMapper = new ObjectMapper();
+		objectMapper.setSerializationInclusion(Include.NON_EMPTY);
+		return objectMapper.writeValueAsString(swagger);
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/4363f0a2/server/protocols/webadmin/webadmin-swagger/src/main/java/org/apache/james/webadmin/swagger/routes/SwaggerRoutes.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-swagger/src/main/java/org/apache/james/webadmin/swagger/routes/SwaggerRoutes.java b/server/protocols/webadmin/webadmin-swagger/src/main/java/org/apache/james/webadmin/swagger/routes/SwaggerRoutes.java
new file mode 100644
index 0000000..d6d364e
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-swagger/src/main/java/org/apache/james/webadmin/swagger/routes/SwaggerRoutes.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.james.webadmin.swagger.routes;
+
+
+import javax.inject.Inject;
+
+import org.apache.james.webadmin.Routes;
+import org.apache.james.webadmin.WebAdminConfiguration;
+import org.apache.james.webadmin.swagger.SwaggerParser;
+
+import spark.Service;
+
+public class SwaggerRoutes implements Routes {
+    public static final String SWAGGER_ENDPOINT = "/james-swagger";
+    private static final String APP_PACKAGE = "org.apache.james.webadmin.routes";
+    private final WebAdminConfiguration webAdminConfiguration;
+
+    @Inject
+    public SwaggerRoutes(WebAdminConfiguration webAdminConfiguration) {
+        this.webAdminConfiguration = webAdminConfiguration;
+    }
+
+    @Override
+    public void define(Service service) {
+        service.get(SWAGGER_ENDPOINT, (request, response) -> {
+            response.status(200);
+            return SwaggerParser.getSwaggerJson(APP_PACKAGE, webAdminConfiguration);
+        });
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org