Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to Swagger and Modify Swagger UI API Key #56

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
dropwizard-swagger
==================

a Dropwizard bundle that serves Swagger UI static content and loads Swagger endpoints. Swagger UI static content is taken from https://github.com/wordnik/swagger-ui
a Dropwizard bundle that serves Swagger UI static content and loads Swagger endpoints. Swagger UI static content is taken from https://github.com/swagger-api/swagger-ui

Current version has been tested with Dropwizard 0.8.0 and Swagger 1.5.1-M2 which supports Swagger 2 spec!
Current version has been tested with Dropwizard 0.8.0 and Swagger 1.5.4 which supports Swagger 2 spec!

Note: if you come from previous versions there have been some changes in the way the bundle is configured, see details below.

Expand All @@ -21,6 +21,7 @@ dropwizard-swagger|Dropwizard|Swagger API|Swagger UI
0.5.x | 0.7.x | 1.3.12 | v2.1.4-M1
0.6.x | 0.8.0 | 1.3.12 | v2.1.4-M1
0.7.x | 0.8.0 | 1.5.1-M2| v2.1.4-M1
0.8.x | 0.8.4 | 1.5.4 | v2.1.4-M1

How to use it
-------------
Expand Down
22 changes: 16 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.federecio</groupId>
<artifactId>dropwizard-swagger</artifactId>
<version>0.7.1-SNAPSHOT</version>
<version>0.8.2-wikia</version>

<name>Dropwizard Swagger support</name>
<description>A simple way to document your REST APIs in DropWizard using Swagger</description>
Expand Down Expand Up @@ -47,12 +47,12 @@
</scm>

<properties>
<dropwizard.version>0.8.0</dropwizard.version>
<dropwizard.version>0.8.1</dropwizard.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<file.encoding>UTF-8</file.encoding>
<jdk.version>1.7</jdk.version>
<swagger.version>1.5.1-M2</swagger.version>
<swagger.version>1.5.3</swagger.version>
</properties>

<dependencies>
Expand All @@ -77,7 +77,7 @@
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>${swagger.version}</version>
<exclusions>
Expand Down Expand Up @@ -127,13 +127,17 @@
</exclusion>

<exclusion>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</exclusion>
<exclusion>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>javax.inject</artifactId>
</exclusion>
</exclusions>
</dependency>

Expand Down Expand Up @@ -178,6 +182,12 @@
<artifactId>dropwizard-junit</artifactId>
<version>0.6</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium.client-drivers</groupId>
Expand Down
51 changes: 39 additions & 12 deletions src/main/java/io/federecio/dropwizard/swagger/SwaggerBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@
package io.federecio.dropwizard.swagger;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.common.collect.ImmutableMap;
import com.wordnik.swagger.jaxrs.config.BeanConfig;
import com.wordnik.swagger.jaxrs.listing.ApiListingResource;
import io.dropwizard.Configuration;
import io.dropwizard.ConfiguredBundle;
import io.dropwizard.assets.AssetsBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.views.ViewBundle;
import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.listing.ApiListingResource;
import io.swagger.models.Swagger;
import org.eclipse.jetty.servlets.CrossOriginFilter;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;

/**
* A {@link io.dropwizard.ConfiguredBundle} that provides hassle-free configuration of Swagger and Swagger UI
Expand All @@ -38,12 +44,7 @@ public abstract class SwaggerBundle<T extends Configuration> implements Configur

@Override
public void initialize(Bootstrap<?> bootstrap) {
bootstrap.addBundle(new ViewBundle<Configuration>() {
@Override
public ImmutableMap<String, ImmutableMap<String, String>> getViewConfiguration(final Configuration configuration) {
return ImmutableMap.of();
}
});
bootstrap.addBundle(new ViewBundle<>());
}

@Override
Expand All @@ -56,17 +57,40 @@ public void run(T configuration, Environment environment) throws Exception {
ConfigurationHelper configurationHelper = new ConfigurationHelper(configuration, swaggerBundleConfiguration);
new AssetsBundle(Constants.SWAGGER_RESOURCES_PATH, configurationHelper.getSwaggerUriPath(), null, Constants.SWAGGER_ASSETS_NAME).run(environment);

environment.jersey().register(new SwaggerResource(configurationHelper.getUrlPattern()));
environment.jersey().register(
new SwaggerResource(
configurationHelper.getUrlPattern(),
swaggerBundleConfiguration.getUiConfiguration()));
environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);

setUpSwagger(swaggerBundleConfiguration, configurationHelper.getUrlPattern());
BeanConfig beanConfig = setUpSwagger(swaggerBundleConfiguration,
configurationHelper.getUrlPattern());

configureCors(environment, "/swagger.json", "/swagger.yaml");

environment.getApplicationContext().setAttribute("swagger", beanConfig.getSwagger());
environment.jersey().register(new ApiListingResource());
}

@SuppressWarnings("unused")
protected abstract SwaggerBundleConfiguration getSwaggerBundleConfiguration(T configuration);

private void setUpSwagger(SwaggerBundleConfiguration swaggerBundleConfiguration, String urlPattern) {
@SuppressWarnings("unused")
protected void setUpSwagger(Swagger swagger) {}

protected void configureCors(Environment environment, String... urlPatterns) {
FilterRegistration.Dynamic
filter = environment.servlets().addFilter("CORS", CrossOriginFilter.class);
filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, urlPatterns);
filter.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,PUT,POST,DELETE,OPTIONS");
filter.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
filter.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
filter.setInitParameter("allowedHeaders", "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin");
filter.setInitParameter("allowCredentials", "true");
}

private BeanConfig setUpSwagger(SwaggerBundleConfiguration swaggerBundleConfiguration,
String urlPattern) {
BeanConfig config = new BeanConfig();

if (swaggerBundleConfiguration.getTitle() != null) {
Expand Down Expand Up @@ -107,5 +131,8 @@ private void setUpSwagger(SwaggerBundleConfiguration swaggerBundleConfiguration,


config.setScan(true);
setUpSwagger(config.getSwagger());

return config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* For the meaning of all these properties please refer to Swagger documentation or {@link com.wordnik.swagger.jaxrs.config.BeanConfig}
* For the meaning of all these properties please refer to Swagger documentation or {@link io.swagger.jaxrs.config.BeanConfig}
*
* @author Tristan Burch
* @author Federico Recio
Expand All @@ -30,7 +30,7 @@ public class SwaggerBundleConfiguration {
/**
* This is the only property that is required for Swagger to work correctly.
* <p/>
* It is a comma separated list of the all the packages that contain the {@link com.wordnik.swagger.annotations.Api}
* It is a comma separated list of the all the packages that contain the {@link io.swagger.annotations.Api}
* annoted resources
*/
@JsonProperty
Expand Down Expand Up @@ -69,6 +69,9 @@ public class SwaggerBundleConfiguration {
@JsonProperty
private String uriPrefix;

@JsonProperty("ui")
private SwaggerUIConfiguration uiConfiguration = new SwaggerUIConfiguration();

public String getResourcePackage() {
return resourcePackage;
}
Expand Down Expand Up @@ -141,6 +144,16 @@ public void setUriPrefix(String uriPrefix) {
this.uriPrefix = uriPrefix;
}

@JsonProperty("ui")
public SwaggerUIConfiguration getUiConfiguration() {
return uiConfiguration;
}

@JsonProperty("ui")
public void setUiConfiguration(SwaggerUIConfiguration ui) {
this.uiConfiguration = ui;
}

@Override
public String toString() {
return "SwaggerBundleConfiguration{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
@Produces(MediaType.TEXT_HTML)
public class SwaggerResource {
private final String urlPattern;
private final SwaggerUIConfiguration config;

public SwaggerResource(String urlPattern) {
public SwaggerResource(String urlPattern, SwaggerUIConfiguration config) {
this.urlPattern = urlPattern;
this.config = config;
}

@GET
public SwaggerView get() {
return new SwaggerView(urlPattern);
return new SwaggerView(urlPattern, config);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.federecio.dropwizard.swagger;

import com.fasterxml.jackson.annotation.JsonProperty;

public class SwaggerUIConfiguration {
@JsonProperty
String authName = "api_key";

@JsonProperty
String authKey = "api_key";

@JsonProperty
String authKeyLocation = "query";

public String getAuthName() {
return authName;
}

public void setAuthName(String authName) {
this.authName = authName;
}

public String getAuthKey() {
return authKey;
}

public void setAuthKey(String authKey) {
this.authKey = authKey;
}

public String getAuthKeyLocation() {
return authKeyLocation;
}

public void setAuthKeyLocation(String authKeyLocation) {
this.authKeyLocation = authKeyLocation;
}
}
19 changes: 18 additions & 1 deletion src/main/java/io/federecio/dropwizard/swagger/SwaggerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ public class SwaggerView extends View {

private final String swaggerAssetsPath;
private final String contextPath;
private final SwaggerUIConfiguration config;

protected SwaggerView(String urlPattern) {
protected SwaggerView(String urlPattern, SwaggerUIConfiguration config) {
super("index.ftl", Charsets.UTF_8);
this.config = config;

if (urlPattern.equals("/")) {
swaggerAssetsPath = Constants.SWAGGER_URI_PATH;
Expand Down Expand Up @@ -61,4 +63,19 @@ public String getSwaggerAssetsPath() {
public String getContextPath() {
return contextPath;
}

@SuppressWarnings("unused")
public String getAuthName() {
return config.getAuthName();
}

@SuppressWarnings("unused")
public String getAuthKey() {
return config.getAuthKey();
}

@SuppressWarnings("unused")
public String getAuthKeyLocation() {
return config.getAuthKeyLocation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
log("key: " + key);
if(key && key.trim() != "") {
log("added key " + key);
window.authorizations.add("api_key", new ApiKeyAuthorization("api_key", key, "query"));
window.authorizations.add("${authName}", new ApiKeyAuthorization("${authKey}", key, "${authKeyLocation}"));
}
}

Expand Down
10 changes: 0 additions & 10 deletions src/test/java/io/federecio/dropwizard/swagger/DropwizardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.jayway.restassured.RestAssured;
import com.wordnik.swagger.jaxrs.listing.ApiListingResource;
import org.eclipse.jetty.http.HttpStatus;
import org.hamcrest.core.StringContains;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -47,13 +44,6 @@ public void setPort() {
RestAssured.port = port;
}

@BeforeClass
public static void crap() throws Exception {
Field initialized = ApiListingResource.class.getDeclaredField("initialized");
initialized.setAccessible(true);
initialized.set(null, false);
}

@Test
public void resourceIsAvailable() throws Exception {
RestAssured.expect().statusCode(HttpStatus.OK_200).when().get(Path.from(basePath, "test.json"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package io.federecio.dropwizard.swagger;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
Expand Down