Skip to content

Commit

Permalink
Remove webjars-locator-core #2173
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Mar 28, 2023
1 parent 1849bc7 commit 57fb0f6
Show file tree
Hide file tree
Showing 22 changed files with 339 additions and 227 deletions.
18 changes: 0 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@
<swagger-api.version>2.2.9</swagger-api.version>
<swagger-ui.version>4.18.1</swagger-ui.version>
<spring-security-oauth2.version>2.5.2.RELEASE</spring-security-oauth2.version>
<classgraph.version>4.8.149</classgraph.version>
<webjars-locator-core.version>0.52</webjars-locator-core.version>
<gmavenplus-plugin.version>1.8.1</gmavenplus-plugin.version>
<jaxb-impl.version>2.1</jaxb-impl.version>
<javax.jws-api.version>1.1</javax.jws-api.version>
Expand All @@ -96,17 +94,6 @@
<artifactId>swagger-ui</artifactId>
<version>${swagger-ui.version}</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</artifactId>
<version>${webjars-locator-core.version}</version>
<exclusions>
<exclusion>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
Expand All @@ -117,11 +104,6 @@
<artifactId>spring-security-oauth2-authorization-server</artifactId>
<version>${spring-security-oauth2-authorization-server.version}</version>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>${classgraph.version}</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxb-impl</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions springdoc-openapi-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
*
* *
* * *
* * * *
* * * * * Copyright 2019-2022 the original author or authors.
* * * * *
* * * * * 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
* * * * *
* * * * * https://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.springdoc.core;

import java.io.IOException;
import java.util.Optional;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.AntPathMatcher;

/**
* The type Spring doc Native Configuration.
* @author bnasslahsen
*/
@Lazy(false)
@ConditionalOnExpression("${springdoc.api-docs.enabled:true}")
@ConditionalOnWebApplication
@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(SpringDocConfiguration.class)
public class SpringDocUIConfiguration implements InitializingBean {

/**
* The constant SPRINGDOC_CONFIG_PROPERTIES.
*/
public static final String SPRINGDOC_CONFIG_PROPERTIES = "springdoc.config.properties";

/**
* The constant SPRINGDOC_SWAGGERUI_VERSION.
*/
private static final String SPRINGDOC_SWAGGER_UI_VERSION = "springdoc.swagger-ui.version";

/**
* The Swagger ui config properties.
*/
private final Optional<SwaggerUiConfigProperties> optionalSwaggerUiConfigProperties;

/**
* Instantiates a new Spring doc hints.
*
* @param optionalSwaggerUiConfigProperties the swagger ui config properties
*/
public SpringDocUIConfiguration(Optional<SwaggerUiConfigProperties> optionalSwaggerUiConfigProperties) {
this.optionalSwaggerUiConfigProperties = optionalSwaggerUiConfigProperties;
}

@Override
public void afterPropertiesSet() {
optionalSwaggerUiConfigProperties.ifPresent(swaggerUiConfigProperties -> {
if (StringUtils.isEmpty(swaggerUiConfigProperties.getVersion())) {
try {
Resource resource = new ClassPathResource(AntPathMatcher.DEFAULT_PATH_SEPARATOR + SPRINGDOC_CONFIG_PROPERTIES);
Properties props = PropertiesLoaderUtils.loadProperties(resource);
swaggerUiConfigProperties.setVersion(props.getProperty(SPRINGDOC_SWAGGER_UI_VERSION));
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.springdoc.ui;

import java.io.File;

import org.springdoc.core.SwaggerUiConfigProperties;

import org.springframework.lang.Nullable;

/**
* The type Web jars version resource resolver.
*
* @author bnasslahsen
*/
public class AbstractSwaggerResourceResolver {

/**
* The Swagger ui config properties.
*/
private final SwaggerUiConfigProperties swaggerUiConfigProperties;

/**
* Instantiates a new Web jars version resource resolver.
*
* @param swaggerUiConfigProperties the swagger ui config properties
*/
public AbstractSwaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfigProperties) {
this.swaggerUiConfigProperties = swaggerUiConfigProperties;
}

/**
* Find web jar resource path string.
*
* @param path the path
* @return the string
*/
@Nullable
protected String findWebJarResourcePath(String path) {
String webjar = webjar(path);
if (webjar.length() > 0) {
String version = swaggerUiConfigProperties.getVersion();
if (version != null) {
String partialPath = path(webjar, path);
return webjar + File.separator + version + File.separator + partialPath;
}
}
return null;
}

/**
* Webjar string.
*
* @param path the path
* @return the string
*/
private String webjar(String path) {
int startOffset = (path.startsWith("/") ? 1 : 0);
int endOffset = path.indexOf('/', 1);
return endOffset != -1 ? path.substring(startOffset, endOffset) : path;
}


/**
* Path string.
*
* @param webjar the webjar
* @param path the path
* @return the string
*/
private String path(String webjar, String path) {
if (path.startsWith(webjar)) {
path = path.substring(webjar.length() + 1);
}
return path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@
import org.springframework.util.CollectionUtils;
import org.springframework.web.util.UriComponentsBuilder;

import static org.springdoc.core.Constants.INDEX_PAGE;
import static org.springdoc.core.Constants.OAUTH_REDIRECT_PAGE;
import static org.springdoc.core.Constants.SWAGGER_UI_OAUTH_REDIRECT_URL;
import static org.springdoc.core.Constants.SWAGGER_UI_PREFIX;
import static org.springdoc.core.Constants.SWAGGER_UI_URL;
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;


/**
* The type Abstract swagger welcome.
*
* @author bnasslashen
*/
public abstract class AbstractSwaggerWelcome {
Expand Down Expand Up @@ -222,12 +220,7 @@ protected void calculateUiRootCommon(StringBuilder sbUrl, StringBuilder[] sbUrls
* @return the oauth2 redirect url
*/
protected String getOauth2RedirectUrl() {
if (StringUtils.isNotEmpty(swaggerUiConfig.getVersion())) {
return StringUtils.defaultIfBlank(swaggerUiConfig.getOauth2RedirectUrl(), SWAGGER_UI_PREFIX + DEFAULT_PATH_SEPARATOR + swaggerUiConfig.getVersion() + OAUTH_REDIRECT_PAGE);
}
else {
return StringUtils.defaultIfBlank(swaggerUiConfig.getOauth2RedirectUrl(), SWAGGER_UI_OAUTH_REDIRECT_URL);
}
return StringUtils.defaultIfBlank(swaggerUiConfig.getOauth2RedirectUrl(), SWAGGER_UI_OAUTH_REDIRECT_URL);
}

/**
Expand All @@ -236,9 +229,6 @@ protected String getOauth2RedirectUrl() {
* @return the swagger ui url
*/
protected String getSwaggerUiUrl() {
if (StringUtils.isNotEmpty(swaggerUiConfig.getVersion()))
return SWAGGER_UI_PREFIX + DEFAULT_PATH_SEPARATOR + swaggerUiConfig.getVersion() + INDEX_PAGE;
else
return SWAGGER_UI_URL;
return SWAGGER_UI_URL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
springdoc.swagger-ui.version=@swagger-ui.version@
8 changes: 0 additions & 8 deletions springdoc-openapi-ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</artifactId>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

/**
* The type Swagger config.
*
* @author bnasslahsen
*/
@Lazy(false)
Expand Down Expand Up @@ -149,8 +150,9 @@ SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUi
@Bean
@ConditionalOnMissingBean
@Lazy(false)
SwaggerWebMvcConfigurer swaggerWebMvcConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerIndexTransformer swaggerIndexTransformer, Optional<ActuatorProvider> actuatorProvider) {
return new SwaggerWebMvcConfigurer(swaggerUiConfigParameters, swaggerIndexTransformer, actuatorProvider);
SwaggerWebMvcConfigurer swaggerWebMvcConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerIndexTransformer swaggerIndexTransformer,
Optional<ActuatorProvider> actuatorProvider, SwaggerResourceResolver swaggerResourceResolver) {
return new SwaggerWebMvcConfigurer(swaggerUiConfigParameters, swaggerIndexTransformer, actuatorProvider, swaggerResourceResolver);
}

/**
Expand All @@ -166,6 +168,12 @@ SwaggerUiConfigParameters swaggerUiConfigParameters(SwaggerUiConfigProperties sw
return new SwaggerUiConfigParameters(swaggerUiConfig);
}

@Bean
@ConditionalOnMissingBean
@Lazy(false)
SwaggerResourceResolver swaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfigProperties) {
return new SwaggerResourceResolver(swaggerUiConfigProperties);
}
/**
* The type Swagger actuator welcome configuration.
*/
Expand All @@ -190,4 +198,5 @@ SwaggerWelcomeActuator swaggerActuatorWelcome(SwaggerUiConfigProperties swaggerU
return new SwaggerWelcomeActuator(swaggerUiConfig, springDocConfigProperties, swaggerUiConfigParameters, webEndpointProperties);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.springdoc.webmvc.ui;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springdoc.core.SwaggerUiConfigProperties;
import org.springdoc.ui.AbstractSwaggerResourceResolver;

import org.springframework.core.io.Resource;
import org.springframework.web.servlet.resource.ResourceResolver;
import org.springframework.web.servlet.resource.ResourceResolverChain;

/**
* The type Web jars version resource resolver.
*
* @author bnasslahsen
*/
public class SwaggerResourceResolver extends AbstractSwaggerResourceResolver implements ResourceResolver {

/**
* Instantiates a new Web jars version resource resolver.
*
* @param swaggerUiConfigProperties the swagger ui config properties
*/
public SwaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfigProperties) {
super(swaggerUiConfigProperties);
}

@Override
public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
Resource resolved = chain.resolveResource(request, requestPath, locations);
if (resolved == null) {
String webJarResourcePath = findWebJarResourcePath(requestPath);
if (webJarResourcePath != null)
return chain.resolveResource(request, webJarResourcePath, locations);
}
return resolved; }

@Override
public String resolveUrlPath(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain) {
String path = chain.resolveUrlPath(resourcePath, locations);
if (path == null) {
String webJarResourcePath = findWebJarResourcePath(resourcePath);
if (webJarResourcePath != null)
return chain.resolveUrlPath(webJarResourcePath, locations);
}
return path;
}
}
Loading

0 comments on commit 57fb0f6

Please sign in to comment.