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

Commit ec69a73

Browse files
authored
Merge pull request #164 from swagger-api/swagger-petstore-v2
adds swagger-petstore-v2 (from docker branch)
2 parents d321d39 + c07203c commit ec69a73

30 files changed

Lines changed: 2065 additions & 0 deletions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM openjdk:8-jre
2+
3+
WORKDIR /petstore
4+
COPY ./jetty-runner* /petstore/jetty-runner.jar
5+
COPY ./run.sh /petstore/
6+
ADD target/swagger-petstore-v2-1.0.0 /petstore/webapp
7+
8+
RUN apt-get update
9+
10+
EXPOSE 8080
11+
CMD ["bash", "/petstore/run.sh"]

java/swagger-petstore-v2/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Swagger Sample App
2+
3+
This is the pet store sample hosted at https://petstore.swagger.io!
4+
5+
## Overview
6+
This is a java project to build a stand-alone server which implements the OpenAPI Spec. You can find out
7+
more about both the spec and the framework at http://swagger.io.
8+
9+
### To run in docker
10+
11+
```
12+
mvn package
13+
docker build -t petstore:dev .
14+
docker run -d -e SWAGGER_HOST=http://localhost -e SWAGGER_BASE_PATH=/v2 -p 8080:8080 petstore:dev
15+
```
16+
17+
### Testing the server
18+
Once started, you can navigate to http://localhost:8080/v2/swagger.json to view the Swagger Resource Listing.
19+
This tells you that the server is up and ready to demonstrate Swagger.
20+
21+
### Using the UI
22+
There is an HTML5-based API tool bundled in this sample--you can view it it at [http://localhost:8080](http://localhost:8080). This lets you inspect the API using an interactive UI. You can access the source of this code from [here](https://github.com/swagger-api/swagger-ui)
23+
24+
### Applying an API key
25+
The sample app has an implementation of the Swagger ApiAuthorizationFilter. This restricts access to resources
26+
based on api-key. There are two keys defined in the sample app:
27+
28+
<li>- default-key</li>
29+
30+
<li>- special-key</li>
31+
32+
When no key is applied, the "default-key" is applied to all operations. If the "special-key" is entered, a
33+
number of other resources are shown in the UI, including sample CRUD operations.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
log4j.rootCategory=ERROR, CONSOLE, LOGFILE
2+
3+
log4j.logger.io.swagger=ERROR
4+
log4j.logger.org.atmosphere=ERROR
5+
6+
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
7+
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
8+
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
9+
log4j.appender.CONSOLE.layout.ConversionPattern=%p %d{yyyy-MM-dd HH:mm:ss.SSS Z} %c{1} - %m%n
10+
11+
# LOGFILE is set to be a File appender using a PatternLayout.
12+
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
13+
log4j.appender.LOGFILE.File=logs/swagger.log
14+
log4j.appender.LOGFILE.Append=true
15+
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
16+
log4j.appender.LOGFILE.layout.ConversionPattern=%p %d{yyyy-MM-dd HH:mm:ss.SSS Z} %c{1} - %m%n
17+
log4j.appender.LOGFILE.MaxFileSize=10MB
18+
log4j.appender.LOGFILE.MaxBackupIndex=10
5.03 MB
Binary file not shown.

java/swagger-petstore-v2/pom.xml

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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</groupId>
4+
<artifactId>swagger-samples-project</artifactId>
5+
<version>1.0.0</version>
6+
<relativePath>../..</relativePath>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
<groupId>io.swagger</groupId>
10+
<artifactId>swagger-petstore-v2</artifactId>
11+
<packaging>war</packaging>
12+
<name>swagger-petstore-v2</name>
13+
<version>1.0.0</version>
14+
<properties>
15+
<swagger-version>1.5.7</swagger-version>
16+
</properties>
17+
<build>
18+
<sourceDirectory>src/main/java</sourceDirectory>
19+
<plugins>
20+
<plugin>
21+
<groupId>org.apache.maven.plugins</groupId>
22+
<artifactId>maven-war-plugin</artifactId>
23+
<version>2.1.1</version>
24+
<configuration>
25+
<webResources>
26+
<resource>
27+
<!-- this is relative to the pom.xml directory -->
28+
<directory>${project.build.directory}/swagger-ui-master/dist</directory>
29+
<excludes>
30+
<exclude>index.html</exclude>
31+
</excludes>
32+
</resource>
33+
</webResources>
34+
</configuration>
35+
</plugin>
36+
<plugin>
37+
<artifactId>maven-failsafe-plugin</artifactId>
38+
<version>2.6</version>
39+
<executions>
40+
<execution>
41+
<goals>
42+
<goal>integration-test</goal>
43+
<goal>verify</goal>
44+
</goals>
45+
</execution>
46+
</executions>
47+
</plugin>
48+
<plugin>
49+
<groupId>org.mortbay.jetty</groupId>
50+
<artifactId>jetty-maven-plugin</artifactId>
51+
<version>${jetty-version}</version>
52+
<configuration>
53+
<webAppConfig>
54+
<contextPath>/</contextPath>
55+
</webAppConfig>
56+
<webAppSourceDirectory>target/${project.artifactId}-${project.version}</webAppSourceDirectory>
57+
<webDefaultXml>${project.basedir}/conf/jetty/webdefault.xml</webDefaultXml>
58+
<stopPort>8079</stopPort>
59+
<stopKey>stopit</stopKey>
60+
<connectors>
61+
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
62+
<port>8002</port>
63+
<maxIdleTime>60000</maxIdleTime>
64+
<confidentialPort>8443</confidentialPort>
65+
</connector>
66+
</connectors>
67+
</configuration>
68+
<executions>
69+
<execution>
70+
<id>start-jetty</id>
71+
<phase>pre-integration-test</phase>
72+
<goals>
73+
<goal>run</goal>
74+
</goals>
75+
<configuration>
76+
<scanIntervalSeconds>0</scanIntervalSeconds>
77+
<daemon>true</daemon>
78+
</configuration>
79+
</execution>
80+
<execution>
81+
<id>stop-jetty</id>
82+
<phase>post-integration-test</phase>
83+
<goals>
84+
<goal>stop</goal>
85+
</goals>
86+
</execution>
87+
</executions>
88+
</plugin>
89+
<plugin>
90+
<groupId>com.googlecode.maven-download-plugin</groupId>
91+
<artifactId>download-maven-plugin</artifactId>
92+
<version>1.2.1</version>
93+
<executions>
94+
<execution>
95+
<id>swagger-ui</id>
96+
<goals>
97+
<goal>wget</goal>
98+
</goals>
99+
<configuration>
100+
<url>https://github.com/swagger-api/swagger-ui/archive/master.tar.gz</url>
101+
<unpack>true</unpack>
102+
<outputDirectory>${project.build.directory}</outputDirectory>
103+
<skipCache>true</skipCache>
104+
</configuration>
105+
</execution>
106+
</executions>
107+
</plugin>
108+
<plugin>
109+
<artifactId>maven-resources-plugin</artifactId>
110+
<version>2.6</version>
111+
<executions>
112+
<execution>
113+
<id>copy-resources</id>
114+
<phase>validate</phase>
115+
<goals>
116+
<goal>copy-resources</goal>
117+
</goals>
118+
<configuration>
119+
<outputDirectory>target/${project.artifactId}-${project.version}</outputDirectory>
120+
<resources>
121+
<resource>
122+
<directory>${project.build.directory}/swagger-ui-master/dist</directory>
123+
<filtering>true</filtering>
124+
<excludes>
125+
<exclude>index.html</exclude>
126+
</excludes>
127+
</resource>
128+
</resources>
129+
</configuration>
130+
</execution>
131+
</executions>
132+
</plugin>
133+
</plugins>
134+
</build>
135+
<dependencies>
136+
<dependency>
137+
<groupId>io.swagger</groupId>
138+
<artifactId>swagger-jersey-jaxrs</artifactId>
139+
<version>${swagger-version}</version>
140+
</dependency>
141+
<dependency>
142+
<groupId>ch.qos.logback</groupId>
143+
<artifactId>logback-classic</artifactId>
144+
<version>${logback-version}</version>
145+
</dependency>
146+
<dependency>
147+
<groupId>ch.qos.logback</groupId>
148+
<artifactId>logback-core</artifactId>
149+
<version>${logback-version}</version>
150+
</dependency>
151+
<dependency>
152+
<groupId>commons-io</groupId>
153+
<artifactId>commons-io</artifactId>
154+
<version>1.3.2</version>
155+
</dependency>
156+
<dependency>
157+
<groupId>javax.servlet</groupId>
158+
<artifactId>servlet-api</artifactId>
159+
</dependency>
160+
<dependency>
161+
<groupId>com.sun.jersey</groupId>
162+
<artifactId>jersey-core</artifactId>
163+
</dependency>
164+
<dependency>
165+
<groupId>com.sun.jersey</groupId>
166+
<artifactId>jersey-json</artifactId>
167+
</dependency>
168+
<dependency>
169+
<groupId>com.sun.jersey</groupId>
170+
<artifactId>jersey-servlet</artifactId>
171+
</dependency>
172+
<dependency>
173+
<groupId>org.scalatest</groupId>
174+
<artifactId>scalatest_2.10</artifactId>
175+
<scope>test</scope>
176+
</dependency>
177+
<dependency>
178+
<groupId>junit</groupId>
179+
<artifactId>junit</artifactId>
180+
<scope>test</scope>
181+
</dependency>
182+
</dependencies>
183+
</project>

java/swagger-petstore-v2/run.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
# use default URL if none supplied
6+
if [ -z $SWAGGER_URL ]; then SWAGGER_URL=http://localhost:8080; fi
7+
8+
# make it safe for sed
9+
URL=$(sed -e 's,/,\\/,g' <<< $SWAGGER_URL)
10+
sed -i "s/SWAGGER_HOST/$URL/g" /petstore/webapp/WEB-INF/web.xml
11+
sed -i "s/SWAGGER_HOST/$URL/g" /petstore/webapp/index.html
12+
13+
# use default basePath if none supplied
14+
if [ -z $SWAGGER_BASE_PATH ]; then SWAGGER_BASE_PATH=/api; fi
15+
16+
# make it safe for sed
17+
BASE_PATH=$(sed -e 's,/,\\/,g' <<< $SWAGGER_BASE_PATH)
18+
sed -i "s/BASE_PATH/$BASE_PATH/g" /petstore/webapp/WEB-INF/web.xml
19+
20+
# full path
21+
FULL_PATH=$SWAGGER_URL$SWAGGER_BASE_PATH
22+
FULL_SWAGGER_PATH=$(sed -e 's,/,\\/,g' <<< $FULL_PATH)
23+
sed -i "s/FULL_SWAGGER_PATH/$FULL_SWAGGER_PATH/g" /petstore/webapp/index.html
24+
sed -i "s/FULL_SWAGGER_PATH/$FULL_SWAGGER_PATH/g" /petstore/webapp/WEB-INF/web.xml
25+
26+
java -jar /petstore/jetty-runner.jar /petstore/webapp
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.swagger.sample;
2+
3+
import io.swagger.jaxrs.config.SwaggerContextService;
4+
import io.swagger.models.*;
5+
6+
import io.swagger.models.auth.*;
7+
8+
import javax.servlet.http.HttpServlet;
9+
import javax.servlet.ServletContext;
10+
import javax.servlet.ServletConfig;
11+
import javax.servlet.ServletException;
12+
13+
public class Bootstrap extends HttpServlet {
14+
@Override
15+
public void init(ServletConfig config) throws ServletException {
16+
Info info = new Info()
17+
.title("Swagger Petstore")
18+
.description("This is a sample server Petstore server. You can find out more about Swagger " +
19+
"at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, " +
20+
"you can use the api key `special-key` to test the authorization filters.")
21+
.termsOfService("http://swagger.io/terms/")
22+
.contact(new Contact()
23+
.email("apiteam@swagger.io"))
24+
.license(new License()
25+
.name("Apache 2.0")
26+
.url("http://www.apache.org/licenses/LICENSE-2.0.html"));
27+
28+
ServletContext context = config.getServletContext();
29+
Swagger swagger = new Swagger().info(info);
30+
swagger.externalDocs(new ExternalDocs("Find out more about Swagger", "http://swagger.io"));
31+
swagger.securityDefinition("api_key", new ApiKeyAuthDefinition("api_key", In.HEADER));
32+
swagger.securityDefinition("petstore_auth",
33+
new OAuth2Definition()
34+
.implicit("https://petstore.swagger.io/oauth/authorize")
35+
.scope("read:pets", "read your pets")
36+
.scope("write:pets", "modify pets in your account"));
37+
swagger.tag(new Tag()
38+
.name("pet")
39+
.description("Everything about your Pets")
40+
.externalDocs(new ExternalDocs("Find out more", "http://swagger.io")));
41+
swagger.tag(new Tag()
42+
.name("store")
43+
.description("Access to Petstore orders"));
44+
swagger.tag(new Tag()
45+
.name("user")
46+
.description("Operations about user")
47+
.externalDocs(new ExternalDocs("Find out more about our store", "http://swagger.io")));
48+
49+
swagger.scheme(Scheme.HTTPS).scheme(Scheme.HTTP);
50+
51+
new SwaggerContextService().withServletConfig(config).updateSwagger(swagger);
52+
}
53+
}

0 commit comments

Comments
 (0)