Commit f087c3fcedcbf329190eae5439840410ea47a26f

Authored by VoBa
Committed by Igor Kulikov
1 parent 6afef792

Moved Swagger configuration to TB config file (#1982)

... ... @@ -23,9 +23,6 @@ import org.springframework.scheduling.annotation.SchedulingConfigurer;
23 23 import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
24 24 import org.springframework.scheduling.config.ScheduledTaskRegistrar;
25 25
26   -import java.util.concurrent.Executor;
27   -import java.util.concurrent.Executors;
28   -
29 26 @Configuration
30 27 @EnableScheduling
31 28 public class SchedulingConfiguration implements SchedulingConfigurer {
... ...
... ... @@ -19,6 +19,7 @@ import com.fasterxml.classmate.ResolvedType;
19 19 import com.fasterxml.classmate.TypeResolver;
20 20 import com.fasterxml.jackson.databind.JsonNode;
21 21 import com.google.common.base.Predicate;
  22 +import org.springframework.beans.factory.annotation.Value;
22 23 import org.springframework.context.annotation.Bean;
23 24 import org.springframework.context.annotation.Configuration;
24 25 import org.thingsboard.server.common.data.security.Authority;
... ... @@ -43,71 +44,94 @@ import static springfox.documentation.builders.PathSelectors.regex;
43 44 @Configuration
44 45 public class SwaggerConfiguration {
45 46
46   - @Bean
47   - public Docket thingsboardApi() {
48   - TypeResolver typeResolver = new TypeResolver();
49   - final ResolvedType jsonNodeType =
50   - typeResolver.resolve(
51   - JsonNode.class);
52   - final ResolvedType stringType =
53   - typeResolver.resolve(
54   - String.class);
  47 + @Value("${swagger.api_path_regex}")
  48 + private String apiPathRegex;
  49 + @Value("${swagger.security_path_regex}")
  50 + private String securityPathRegex;
  51 + @Value("${swagger.non_security_path_regex}")
  52 + private String nonSecurityPathRegex;
  53 + @Value("${swagger.title}")
  54 + private String title;
  55 + @Value("${swagger.description}")
  56 + private String description;
  57 + @Value("${swagger.contact.name}")
  58 + private String contactName;
  59 + @Value("${swagger.contact.url}")
  60 + private String contactUrl;
  61 + @Value("${swagger.contact.email}")
  62 + private String contactEmail;
  63 + @Value("${swagger.license.title}")
  64 + private String licenseTitle;
  65 + @Value("${swagger.license.url}")
  66 + private String licenseUrl;
  67 + @Value("${swagger.version}")
  68 + private String version;
55 69
56   - return new Docket(DocumentationType.SWAGGER_2)
57   - .groupName("thingsboard")
58   - .apiInfo(apiInfo())
59   - .alternateTypeRules(
  70 + @Bean
  71 + public Docket thingsboardApi() {
  72 + TypeResolver typeResolver = new TypeResolver();
  73 + final ResolvedType jsonNodeType =
  74 + typeResolver.resolve(
  75 + JsonNode.class);
  76 + final ResolvedType stringType =
  77 + typeResolver.resolve(
  78 + String.class);
  79 +
  80 + return new Docket(DocumentationType.SWAGGER_2)
  81 + .groupName("thingsboard")
  82 + .apiInfo(apiInfo())
  83 + .alternateTypeRules(
60 84 new AlternateTypeRule(
61 85 jsonNodeType,
62 86 stringType))
63   - .select()
64   - .paths(apiPaths())
65   - .build()
66   - .securitySchemes(newArrayList(jwtTokenKey()))
67   - .securityContexts(newArrayList(securityContext()))
68   - .enableUrlTemplating(true);
69   - }
  87 + .select()
  88 + .paths(apiPaths())
  89 + .build()
  90 + .securitySchemes(newArrayList(jwtTokenKey()))
  91 + .securityContexts(newArrayList(securityContext()))
  92 + .enableUrlTemplating(true);
  93 + }
70 94
71   - private ApiKey jwtTokenKey() {
72   - return new ApiKey("X-Authorization", "JWT token", "header");
73   - }
  95 + private ApiKey jwtTokenKey() {
  96 + return new ApiKey("X-Authorization", "JWT token", "header");
  97 + }
74 98
75   - private SecurityContext securityContext() {
76   - return SecurityContext.builder()
77   - .securityReferences(defaultAuth())
78   - .forPaths(securityPaths())
79   - .build();
80   - }
  99 + private SecurityContext securityContext() {
  100 + return SecurityContext.builder()
  101 + .securityReferences(defaultAuth())
  102 + .forPaths(securityPaths())
  103 + .build();
  104 + }
81 105
82   - private Predicate<String> apiPaths() {
83   - return regex("/api.*");
84   - }
  106 + private Predicate<String> apiPaths() {
  107 + return regex(apiPathRegex);
  108 + }
85 109
86   - private Predicate<String> securityPaths() {
87   - return and(
88   - regex("/api.*"),
89   - not(regex("/api/noauth.*"))
90   - );
91   - }
  110 + private Predicate<String> securityPaths() {
  111 + return and(
  112 + regex(securityPathRegex),
  113 + not(regex(nonSecurityPathRegex))
  114 + );
  115 + }
92 116
93   - List<SecurityReference> defaultAuth() {
94   - AuthorizationScope[] authorizationScopes = new AuthorizationScope[3];
95   - authorizationScopes[0] = new AuthorizationScope(Authority.SYS_ADMIN.name(), "System administrator");
96   - authorizationScopes[1] = new AuthorizationScope(Authority.TENANT_ADMIN.name(), "Tenant administrator");
97   - authorizationScopes[2] = new AuthorizationScope(Authority.CUSTOMER_USER.name(), "Customer");
98   - return newArrayList(
99   - new SecurityReference("X-Authorization", authorizationScopes));
100   - }
  117 + List<SecurityReference> defaultAuth() {
  118 + AuthorizationScope[] authorizationScopes = new AuthorizationScope[3];
  119 + authorizationScopes[0] = new AuthorizationScope(Authority.SYS_ADMIN.name(), "System administrator");
  120 + authorizationScopes[1] = new AuthorizationScope(Authority.TENANT_ADMIN.name(), "Tenant administrator");
  121 + authorizationScopes[2] = new AuthorizationScope(Authority.CUSTOMER_USER.name(), "Customer");
  122 + return newArrayList(
  123 + new SecurityReference("X-Authorization", authorizationScopes));
  124 + }
101 125
102   - private ApiInfo apiInfo() {
103   - return new ApiInfoBuilder()
104   - .title("Thingsboard REST API")
105   - .description("For instructions how to authorize requests please visit <a href='http://thingsboard.io/docs/reference/rest-api/'>REST API documentation page</a>.")
106   - .contact(new Contact("Thingsboard team", "http://thingsboard.io", "info@thingsboard.io"))
107   - .license("Apache License Version 2.0")
108   - .licenseUrl("https://github.com/thingsboard/thingsboard/blob/master/LICENSE")
109   - .version("2.0")
  126 + private ApiInfo apiInfo() {
  127 + return new ApiInfoBuilder()
  128 + .title(title)
  129 + .description(description)
  130 + .contact(new Contact(contactName, contactUrl, contactEmail))
  131 + .license(licenseTitle)
  132 + .licenseUrl(licenseUrl)
  133 + .version(version)
110 134 .build();
111   - }
  135 + }
112 136
113 137 }
... ...
... ... @@ -488,3 +488,18 @@ transport:
488 488 bind_address: "${COAP_BIND_ADDRESS:0.0.0.0}"
489 489 bind_port: "${COAP_BIND_PORT:5683}"
490 490 timeout: "${COAP_TIMEOUT:10000}"
  491 +
  492 +swagger:
  493 + api_path_regex: "${SWAGGER_API_PATH_REGEX:/api.*}"
  494 + security_path_regex: "${SWAGGER_SECURITY_PATH_REGEX:/api.*}"
  495 + non_security_path_regex: "${SWAGGER_NON_SECURITY_PATH_REGEX:/api/noauth.*}"
  496 + title: "${SWAGGER_TITLE:Thingsboard REST API}"
  497 + description: "${SWAGGER_DESCRIPTION:For instructions how to authorize requests please visit <a href='http://thingsboard.io/docs/reference/rest-api/'>REST API documentation page</a>.}"
  498 + contact:
  499 + name: "${SWAGGER_CONTACT_NAME:Thingsboard team}"
  500 + url: "${SWAGGER_CONTACT_URL:http://thingsboard.io}"
  501 + email: "${SWAGGER_CONTACT_EMAIL:info@thingsboard.io}"
  502 + license:
  503 + title: "${SWAGGER_LICENSE_TITLE:Apache License Version 2.0}"
  504 + url: "${SWAGGER_LICENSE_URL:https://github.com/thingsboard/thingsboard/blob/master/LICENSE}"
  505 + version: "${SWAGGER_VERSION:2.0}"
\ No newline at end of file
... ...
... ... @@ -19,37 +19,18 @@ import lombok.Data;
19 19 import org.springframework.beans.factory.annotation.Value;
20 20 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
21 21 import org.springframework.cache.CacheManager;
22   -import org.springframework.cache.interceptor.SimpleKey;
23   -import org.springframework.core.convert.ConversionService;
24   -import org.springframework.core.convert.TypeDescriptor;
25   -import org.springframework.core.convert.converter.ConditionalGenericConverter;
26   -import org.springframework.core.convert.converter.Converter;
27 22 import org.springframework.cache.annotation.EnableCaching;
28 23 import org.springframework.cache.interceptor.KeyGenerator;
29 24 import org.springframework.context.annotation.Bean;
30 25 import org.springframework.context.annotation.Configuration;
31 26 import org.springframework.core.convert.converter.ConverterRegistry;
32   -import org.springframework.data.convert.ReadingConverter;
33   -import org.springframework.data.convert.WritingConverter;
34 27 import org.springframework.data.redis.cache.RedisCacheConfiguration;
35 28 import org.springframework.data.redis.cache.RedisCacheManager;
36 29 import org.springframework.data.redis.connection.RedisConnectionFactory;
37 30 import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
38   -import org.springframework.data.redis.core.RedisTemplate;
39   -import org.springframework.data.redis.core.convert.RedisCustomConversions;
40 31 import org.springframework.format.support.DefaultFormattingConversionService;
41   -import org.springframework.lang.Nullable;
42 32 import org.springframework.util.Assert;
43   -import org.springframework.util.StringUtils;
44   -import org.thingsboard.server.common.data.EntityType;
45 33 import org.thingsboard.server.common.data.id.EntityId;
46   -import org.thingsboard.server.common.data.id.EntityIdFactory;
47   -
48   -import java.nio.charset.StandardCharsets;
49   -import java.util.Arrays;
50   -import java.util.Collections;
51   -import java.util.Set;
52   -import java.util.UUID;
53 34
54 35 @Configuration
55 36 @ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis", matchIfMissing = false)
... ...