Skip to content
This repository was archived by the owner on Oct 30, 2023. It is now read-only.

Commit 17b27f5

Browse files
committed
OpenApiServlet standalone sample with external resources lib
1 parent d4a7939 commit 17b27f5

30 files changed

Lines changed: 1824 additions & 0 deletions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Swagger Sample App
2+
3+
## Overview
4+
This is a java project to build a stand-alone server which implements the OpenAPI Spec. You can find out
5+
more about both the spec and the framework at http://swagger.io.
6+
7+
This sample demonstrate resolving OpenAPI definition out of JAX-RS annotated resources, as a different application
8+
than the one serving the actual APIs
9+
10+
### To run (with Maven)
11+
To run the server, run this task:
12+
13+
```
14+
mvn package -Dlog4j.configuration=file:./conf/log4j.properties jetty:run
15+
```
16+
17+
This will start Jetty embedded on port 8002.
18+
19+
### Testing the server
20+
Once started, you can navigate to http://localhost:8002/openapi/openapi.json to view the Swagger Resource Listing.
21+
This tells you that the server is up and ready to demonstrate Swagger.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<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">
2+
<parent>
3+
<groupId>io.swagger.samples.v3</groupId>
4+
<artifactId>swagger-samples-project</artifactId>
5+
<version>2.0.0</version>
6+
<relativePath>../..</relativePath>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
<groupId>io.swagger.samples.v3</groupId>
10+
<artifactId>swagger-jaxrs2-openapiservlet-resourceslib-sample-app</artifactId>
11+
<packaging>war</packaging>
12+
<name>swagger-jaxrs2-openapiservlet-resourceslib-sample-app</name>
13+
<version>2.0.0</version>
14+
<build>
15+
<sourceDirectory>src/main/java</sourceDirectory>
16+
<plugins>
17+
<plugin>
18+
<groupId>org.apache.maven.plugins</groupId>
19+
<artifactId>maven-war-plugin</artifactId>
20+
<version>2.1.1</version>
21+
</plugin>
22+
<plugin>
23+
<artifactId>maven-failsafe-plugin</artifactId>
24+
<version>2.6</version>
25+
<executions>
26+
<execution>
27+
<goals>
28+
<goal>integration-test</goal>
29+
<goal>verify</goal>
30+
</goals>
31+
</execution>
32+
</executions>
33+
</plugin>
34+
<plugin>
35+
<groupId>org.eclipse.jetty</groupId>
36+
<artifactId>jetty-maven-plugin</artifactId>
37+
<version>${jetty-version}</version>
38+
<configuration>
39+
<webApp>
40+
<contextPath>/</contextPath>
41+
</webApp>
42+
<webAppSourceDirectory>target/${project.artifactId}-${project.version}</webAppSourceDirectory>
43+
<stopPort>8079</stopPort>
44+
<stopKey>stopit</stopKey>
45+
<httpConnector>
46+
<port>8002</port>
47+
<idleTimeout>60000</idleTimeout>
48+
</httpConnector>
49+
<systemProperties>
50+
<systemProperty>
51+
<name>org.eclipse.jetty.util.log.Log</name>
52+
<value>org.eclipse.jetty.util.log.Slf4jLog</value>
53+
</systemProperty>
54+
<systemProperty>
55+
<name>logback.configurationFile</name>
56+
<value>src/main/resources/logback.xml</value>
57+
</systemProperty>
58+
</systemProperties>
59+
</configuration>
60+
<executions>
61+
<execution>
62+
<id>start-jetty</id>
63+
<phase>pre-integration-test</phase>
64+
<goals>
65+
<goal>stop</goal>
66+
<goal>start</goal>
67+
</goals>
68+
<configuration>
69+
<scanIntervalSeconds>0</scanIntervalSeconds>
70+
</configuration>
71+
</execution>
72+
<execution>
73+
<id>stop-jetty</id>
74+
<phase>post-integration-test</phase>
75+
<goals>
76+
<goal>stop</goal>
77+
</goals>
78+
</execution>
79+
</executions>
80+
</plugin>
81+
<plugin>
82+
<groupId>org.apache.maven.plugins</groupId>
83+
<artifactId>maven-compiler-plugin</artifactId>
84+
<configuration>
85+
<source>1.8</source>
86+
<target>1.8</target>
87+
</configuration>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
<dependencies>
92+
<dependency>
93+
<groupId>io.swagger.core.v3</groupId>
94+
<artifactId>swagger-jaxrs2</artifactId>
95+
<scope>compile</scope>
96+
<version>${swagger-version}</version>
97+
</dependency>
98+
<dependency>
99+
<groupId>io.swagger.samples.v3</groupId>
100+
<artifactId>swagger-jaxrs2-resources</artifactId>
101+
<scope>compile</scope>
102+
<version>2.0.0</version>
103+
</dependency>
104+
<dependency>
105+
<groupId>com.fasterxml.jackson.jaxrs</groupId>
106+
<artifactId>jackson-jaxrs-json-provider</artifactId>
107+
<version>${jackson-version}</version>
108+
</dependency>
109+
<dependency>
110+
<groupId>javax.ws.rs</groupId>
111+
<artifactId>javax.ws.rs-api</artifactId>
112+
<version>2.1</version>
113+
</dependency>
114+
<dependency>
115+
<groupId>ch.qos.logback</groupId>
116+
<artifactId>logback-classic</artifactId>
117+
<version>${logback-version}</version>
118+
<scope>compile</scope>
119+
</dependency>
120+
<dependency>
121+
<groupId>ch.qos.logback</groupId>
122+
<artifactId>logback-core</artifactId>
123+
<version>${logback-version}</version>
124+
<scope>compile</scope>
125+
</dependency>
126+
127+
<dependency>
128+
<groupId>junit</groupId>
129+
<artifactId>junit</artifactId>
130+
<scope>test</scope>
131+
</dependency>
132+
<dependency>
133+
<groupId>javax.servlet</groupId>
134+
<artifactId>javax.servlet-api</artifactId>
135+
<scope>provided</scope>
136+
</dependency>
137+
</dependencies>
138+
</project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Copyright 2016 SmartBear Software
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.swagger.sample.util;
18+
19+
import io.swagger.v3.core.filter.AbstractSpecFilter;
20+
import io.swagger.v3.core.model.ApiDescription;
21+
import io.swagger.v3.oas.models.Operation;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.Optional;
28+
29+
/**
30+
*
31+
* The rules are maintained in simple map with key as path and a boolean value
32+
* indicating given path is secure or not. For method level security the key is
33+
* combination of http method and path .
34+
*
35+
* If the resource or method is secure then it can only be viewed using a
36+
* secured api key
37+
*
38+
* Note: Objective of this class is not to provide fully functional
39+
* implementation of authorization filter. This is only a sample demonstration
40+
* how API authorization filter works.
41+
*
42+
*/
43+
44+
public class ApiAuthorizationFilterImpl extends AbstractSpecFilter {
45+
static Logger logger = LoggerFactory.getLogger(ApiAuthorizationFilterImpl.class);
46+
47+
public boolean checkKey(Map<String, List<String>> params, Map<String, List<String>> headers) {
48+
String keyValue = null;
49+
if(params.containsKey("api_key"))
50+
keyValue = params.get("api_key").get(0);
51+
else {
52+
if(headers.containsKey("api_key"))
53+
keyValue = headers.get("api_key").get(0);
54+
}
55+
if("special-key".equals(keyValue))
56+
return true;
57+
else
58+
return false;
59+
}
60+
61+
@Override
62+
public Optional<Operation> filterOperation(Operation operation, ApiDescription api, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
63+
if(!api.getMethod().equals("get") || api.getPath().startsWith("/store"))
64+
return checkKey(params, headers) ? Optional.of(operation) : Optional.empty();
65+
return Optional.empty();
66+
}
67+
68+
public boolean isRemovingUnreferencedDefinitions() {
69+
return true;
70+
}
71+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright 2016 SmartBear Software
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.swagger.sample.util;
18+
19+
import java.io.IOException;
20+
21+
import javax.servlet.*;
22+
import javax.servlet.http.HttpServletResponse;
23+
24+
public class ApiOriginFilter implements javax.servlet.Filter {
25+
@Override
26+
public void doFilter(ServletRequest request, ServletResponse response,
27+
FilterChain chain) throws IOException, ServletException {
28+
HttpServletResponse res = (HttpServletResponse) response;
29+
res.addHeader("Access-Control-Allow-Origin", "*");
30+
res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
31+
res.addHeader("Access-Control-Allow-Headers", "Content-Type");
32+
chain.doFilter(request, response);
33+
}
34+
35+
@Override
36+
public void destroy() {
37+
}
38+
39+
@Override
40+
public void init(FilterConfig filterConfig) throws ServletException {
41+
}
42+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.swagger.sample.util;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import io.swagger.v3.core.util.Json;
5+
6+
import javax.ws.rs.ext.ContextResolver;
7+
import javax.ws.rs.ext.Provider;
8+
9+
@Provider
10+
public class JsonProvider implements ContextResolver<ObjectMapper> {
11+
private final ObjectMapper objectMapper;
12+
13+
public JsonProvider() {
14+
objectMapper = Json.mapper();
15+
};
16+
17+
@Override
18+
public ObjectMapper getContext(Class<?> type) {
19+
return objectMapper;
20+
}
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<layout class="ch.qos.logback.classic.PatternLayout">
5+
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
6+
</layout>
7+
</appender>
8+
<logger name="io.swagger" level="error"/>
9+
<root level="error">
10+
<appender-ref ref="STDOUT" />
11+
</root>
12+
</configuration>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="ISO-8859-1"?>
2+
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
5+
6+
<servlet>
7+
<!-- use OpenApi servlet to serve spec -->
8+
<servlet-name>OpenApi</servlet-name>
9+
<servlet-class>io.swagger.v3.jaxrs2.integration.OpenApiServlet</servlet-class>
10+
11+
<init-param>
12+
<param-name>openApi.configuration.resourcePackages</param-name>
13+
<param-value>io.swagger.sample.resource</param-value>
14+
</init-param>
15+
16+
<!-- alternatively include a file openapi-configuration.json or openapi-configuration.yaml in classpath -->
17+
18+
<!-- alternatively include a configuration file in the location specified below -->
19+
<!--
20+
<init-param>
21+
<param-name>openApi.configuration.location</param-name>
22+
<param-value>/openapi-configuration.json</param-value>
23+
</init-param>
24+
-->
25+
26+
27+
<load-on-startup>2</load-on-startup>
28+
</servlet>
29+
30+
<servlet-mapping>
31+
<servlet-name>OpenApi</servlet-name>
32+
<url-pattern>/openapi/*</url-pattern>
33+
</servlet-mapping>
34+
35+
<filter>
36+
<filter-name>ApiOriginFilter</filter-name>
37+
<filter-class>io.swagger.sample.util.ApiOriginFilter</filter-class>
38+
</filter>
39+
40+
<filter-mapping>
41+
<filter-name>ApiOriginFilter</filter-name>
42+
<url-pattern>/*</url-pattern>
43+
</filter-mapping>
44+
</web-app>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Swagger Sample JAX-RS resources library
2+
3+
## Overview
4+
This is a java project including a set of JAX-RS resources, used in other sample projects.

0 commit comments

Comments
 (0)