From 013229627b77db9b89b334c82f686dcf6ef9a123 Mon Sep 17 00:00:00 2001 From: Ravi Desai Date: Sun, 25 Jan 2026 16:02:03 +0530 Subject: [PATCH 1/3] [#14] Added copyright content to the ftl files --- .../in/vectorpro/dropwizard/swagger/index.ftl | 17 +++++++++++++++++ .../src/test/resources/myassets/customIndex.ftl | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/dropwizard-swagger/src/main/resources/in/vectorpro/dropwizard/swagger/index.ftl b/dropwizard-swagger/src/main/resources/in/vectorpro/dropwizard/swagger/index.ftl index 3957991..557afa7 100644 --- a/dropwizard-swagger/src/main/resources/in/vectorpro/dropwizard/swagger/index.ftl +++ b/dropwizard-swagger/src/main/resources/in/vectorpro/dropwizard/swagger/index.ftl @@ -1,3 +1,20 @@ +<#-- + + Copyright © 2021 Vector Pro (teamvectorpro@googlegroups.com) + + Licensed 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. + +--> <#-- @ftlvariable name="" type="SwaggerView" --> diff --git a/dropwizard-swagger/src/test/resources/myassets/customIndex.ftl b/dropwizard-swagger/src/test/resources/myassets/customIndex.ftl index 33827e7..7735dd0 100644 --- a/dropwizard-swagger/src/test/resources/myassets/customIndex.ftl +++ b/dropwizard-swagger/src/test/resources/myassets/customIndex.ftl @@ -1,3 +1,20 @@ +<#-- + + Copyright © 2021 Vector Pro (teamvectorpro@googlegroups.com) + + Licensed 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. + +--> <#-- @ftlvariable name="" type="SwaggerView" --> From 2ae757901608ff36e724ddc8913b182ac3c8f6fb Mon Sep 17 00:00:00 2001 From: Ravi Desai Date: Sun, 25 Jan 2026 16:03:52 +0530 Subject: [PATCH 2/3] [#14] Refactor Swagger integration to support dynamic context path and asset loading - Updated SwaggerBundle to remove unnecessary parameters and improve configuration handling. - Simplified SwaggerResource by eliminating contextRoot and urlPattern parameters. - Enhanced SwaggerView to dynamically calculate contextPath and swaggerAssetsPath based on the browser URL. - Modified index.ftl and customIndex.ftl to use dynamic asset loading for improved flexibility in different deployment environments. --- .../dropwizard/swagger/SwaggerBundle.java | 10 ++- .../dropwizard/swagger/SwaggerResource.java | 20 +---- .../dropwizard/swagger/SwaggerView.java | 56 +----------- .../in/vectorpro/dropwizard/swagger/index.ftl | 81 ++++++++++++++--- .../test/resources/myassets/customIndex.ftl | 90 +++++++++++++------ 5 files changed, 144 insertions(+), 113 deletions(-) diff --git a/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerBundle.java b/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerBundle.java index 358132f..836cb36 100644 --- a/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerBundle.java +++ b/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerBundle.java @@ -42,8 +42,10 @@ public void initialize(Bootstrap bootstrap) { @Override public void run(T configuration, Environment environment) throws Exception { + final SwaggerBundleConfiguration swaggerBundleConfiguration = getSwaggerBundleConfiguration(configuration); + if (swaggerBundleConfiguration == null) { throw new IllegalStateException( "You need to provide an instance of SwaggerBundleConfiguration"); @@ -55,6 +57,7 @@ public void run(T configuration, Environment environment) throws Exception { final ConfigurationHelper configurationHelper = new ConfigurationHelper(configuration, swaggerBundleConfiguration); + new AssetsBundle( "/swagger-static", configurationHelper.getSwaggerUriPath(), null, "swagger-assets") .run(configuration, environment); @@ -67,20 +70,19 @@ public void run(T configuration, Environment environment) throws Exception { .run(configuration, environment); final SwaggerConfiguration oasConfiguration = swaggerBundleConfiguration.build(); - new JaxrsOpenApiContextBuilder().openApiConfiguration(oasConfiguration).buildContext(true); + new JaxrsOpenApiContextBuilder<>().openApiConfiguration(oasConfiguration).buildContext(true); environment.jersey().register(new OpenApiResource()); environment.jersey().register(new BackwardsCompatibleSwaggerResource()); environment.jersey().register(new SwaggerSerializers()); + if (swaggerBundleConfiguration.isIncludeSwaggerResource()) { environment .jersey() .register( new SwaggerResource( - configurationHelper.getUrlPattern(), swaggerBundleConfiguration.getSwaggerViewConfiguration(), - swaggerBundleConfiguration.getSwaggerOAuth2Configuration(), - swaggerBundleConfiguration.getContextRoot())); + swaggerBundleConfiguration.getSwaggerOAuth2Configuration())); } } diff --git a/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerResource.java b/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerResource.java index 3c98a60..bbdb19b 100644 --- a/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerResource.java +++ b/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerResource.java @@ -25,35 +25,21 @@ @Path(PATH) @Produces(MediaType.TEXT_HTML) public class SwaggerResource { + static final String PATH = "/swagger"; + private final SwaggerViewConfiguration viewConfiguration; private final SwaggerOAuth2Configuration oAuth2Configuration; - private final String contextRoot; - private final String urlPattern; public SwaggerResource( - String urlPattern, SwaggerViewConfiguration viewConfiguration, SwaggerOAuth2Configuration oAuth2Configuration) { - this.urlPattern = urlPattern; - this.viewConfiguration = viewConfiguration; - this.oAuth2Configuration = oAuth2Configuration; - this.contextRoot = "/"; - } - - public SwaggerResource( - String urlPattern, - SwaggerViewConfiguration viewConfiguration, - SwaggerOAuth2Configuration oAuth2Configuration, - String contextRoot) { this.viewConfiguration = viewConfiguration; this.oAuth2Configuration = oAuth2Configuration; - this.urlPattern = urlPattern; - this.contextRoot = contextRoot; } @GET public SwaggerView get() { - return new SwaggerView(contextRoot, urlPattern, viewConfiguration, oAuth2Configuration); + return new SwaggerView(viewConfiguration, oAuth2Configuration); } } diff --git a/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerView.java b/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerView.java index 77eb566..cd895e1 100644 --- a/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerView.java +++ b/dropwizard-swagger/src/main/java/in/vectorpro/dropwizard/swagger/SwaggerView.java @@ -17,75 +17,25 @@ import io.dropwizard.views.View; import java.nio.charset.StandardCharsets; -import javax.annotation.Nullable; /** - * Serves the content of Swagger's index page which has been "templatized" to support replacing the - * directory in which Swagger's static content is located (i.e. JS files) and the path with which - * requests to resources need to be prefixed. + * Serves the content of Swagger's index page which has been "templatized" to support dynamic + * configuration of Swagger UI. The template calculates paths dynamically from the browser URL, + * enabling the application to work behind proxies with different path prefixes. */ public class SwaggerView extends View { - private static final String SWAGGER_URI_PATH = "/swagger-static"; - - private final String swaggerAssetsPath; - private final String contextPath; - private final SwaggerViewConfiguration viewConfiguration; private final SwaggerOAuth2Configuration oauth2Configuration; public SwaggerView( - final String contextRoot, - final String urlPattern, final SwaggerViewConfiguration viewConfiguration, final SwaggerOAuth2Configuration oauth2Configuration) { super(viewConfiguration.getTemplateUrl(), StandardCharsets.UTF_8); - - String contextRootPrefix = "/".equals(contextRoot) ? "" : contextRoot; - - // swagger-static should be found on the root context - if (!contextRootPrefix.isEmpty()) { - swaggerAssetsPath = contextRootPrefix + SWAGGER_URI_PATH; - } else { - swaggerAssetsPath = - (urlPattern.equals("/") ? SWAGGER_URI_PATH : (urlPattern + SWAGGER_URI_PATH)); - } - - contextPath = urlPattern.equals("/") ? contextRootPrefix : (contextRootPrefix + urlPattern); - this.viewConfiguration = viewConfiguration; this.oauth2Configuration = oauth2Configuration; } - /** - * Returns the title for the browser header - * - * @return String - */ - @Nullable - public String getTitle() { - return viewConfiguration.getPageTitle(); - } - - /** - * Returns the path with which all requests for Swagger's static content need to be prefixed - * - * @return String - */ - public String getSwaggerAssetsPath() { - return swaggerAssetsPath; - } - - /** - * Returns the path with with which all requests made by Swagger's UI to Resources need to be - * prefixed - * - * @return String - */ - public String getContextPath() { - return contextPath; - } - /** @return {@link SwaggerViewConfiguration} containing every properties to customize swagger */ public SwaggerViewConfiguration getViewConfiguration() { return viewConfiguration; diff --git a/dropwizard-swagger/src/main/resources/in/vectorpro/dropwizard/swagger/index.ftl b/dropwizard-swagger/src/main/resources/in/vectorpro/dropwizard/swagger/index.ftl index 557afa7..842839e 100644 --- a/dropwizard-swagger/src/main/resources/in/vectorpro/dropwizard/swagger/index.ftl +++ b/dropwizard-swagger/src/main/resources/in/vectorpro/dropwizard/swagger/index.ftl @@ -22,20 +22,51 @@ ${viewConfiguration.pageTitle} - - - - +
- - - + - - - - - - - - - - - - - + ${viewConfiguration.pageTitle} + - - - + + + +