Skip to content

Commit

Permalink
Finish GH-5
Browse files Browse the repository at this point in the history
  • Loading branch information
lex-em committed Mar 26, 2019
2 parents 6adf23a + 911880f commit 1a97014
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ public class ZuulSpringfoxSwaggerConfiguration {
@EnableSwagger2
public static final class EnableSwagger2Config {}

@ConditionalOnMissingBean(RestTemplate.class)
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
public RestTemplate loadBalancedRestTemplate() {
return new RestTemplate();
}

@Bean
public RestTemplate pureRestTemplate() {
return new RestTemplate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@
@ResponseStatus(HttpStatus.NOT_FOUND)
public class NotFoundException extends RuntimeException {

public NotFoundException() {
}

public NotFoundException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class ServiceInfo {

private String swaggerUri;

private String directSwaggerBaseUrl;

private String directSwaggerPath;

private String swaggerResourcesUri;

private String protocol = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,30 @@ public Optional<String> getServiceUrl(String route) {
}

public Optional<String> getServicePath(String route) {
Optional<String> directPath = getDirectSwaggerBaseUrl(route)
.flatMap(x -> getDirectSwaggerPath(route));
if (directPath.isPresent()) {
return directPath;
}
return Optional.ofNullable(routes.get(route))
.map(ServiceInfo::getPath)
.map(path -> path.replaceAll("^/", "").replaceAll("/\\*\\*", ""));
}

public Optional<String> getDirectSwaggerBaseUrl(String route) {
return Optional.ofNullable(routes.get(route))
.map(ServiceInfo::getDirectSwaggerBaseUrl);
}

public Optional<String> getDirectSwaggerPath(String route) {
return Optional.ofNullable(routes.get(route))
.map(ServiceInfo::getDirectSwaggerPath);
}

public boolean groupAllowed(String route, String group) {
return Optional.ofNullable(routes.get(route))
.map(serviceInfo -> serviceInfo.groupAllowed(group))
.orElse(true);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
Expand All @@ -15,17 +17,24 @@

import java.util.List;

import java.util.Optional;

/**
* General implementation
*
* @author Alexandr Emelyanov <mr.lex91@gmail.com>
* on 27.11.2017.
*/
@Component
@Slf4j
public class GenericSwaggerService implements SwaggerService {

@Autowired
private RestTemplate restTemplate;
@Qualifier("pureRestTemplate")
private RestTemplate pureRestTemplate;
@Autowired
@Qualifier("loadBalancedRestTemplate")
private RestTemplate loadBalancedRestTemplate;
@Autowired
private ServicesSwaggerInfo servicesSwaggerInfo;
@Autowired
Expand All @@ -46,27 +55,29 @@ public ObjectNode getSwaggerDoc(String route, String group) {

@Override
public List<SwaggerResource> getSwaggerResources(String route) {
String serviceUrl = servicesSwaggerInfo.getServiceUrl(route)
.orElseGet(() -> servicesSwaggerInfo.getDefaultProtocol() + route);
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(serviceUrl)
.path(servicesSwaggerInfo.getSwaggerResourcesUrl(route));
String url = uriBuilder.build().toUriString();
Optional<String> serviceUrlOpt = servicesSwaggerInfo.getServiceUrl(route);
RestTemplate restTemplate = getRestTemplate(serviceUrlOpt.isPresent());
String url = getServiceUrlBuilder(route, serviceUrlOpt)
.path(servicesSwaggerInfo.getSwaggerResourcesUrl(route))
.build()
.toUriString();
try {
return restTemplate.exchange(url, HttpMethod.GET, HttpEntity.EMPTY, new ParameterizedTypeReference<List<SwaggerResource>>(){})
.getBody();
} catch (IllegalStateException e) {
log.error("Some unexpected error while requesting swagger resources from: {}", url);
if (e.getMessage() == null || !e.getMessage().startsWith("No instances available for")) {
throw e;
}
throw new NotFoundException();
throw new NotFoundException(e);
}
}

@Override
public ObjectNode getOriginalSwaggerDoc(String route, String group) {
String serviceUrl = servicesSwaggerInfo.getServiceUrl(route)
.orElseGet(() -> servicesSwaggerInfo.getDefaultProtocol() + route);
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(serviceUrl)
Optional<String> serviceUrlOpt = servicesSwaggerInfo.getServiceUrl(route);
RestTemplate restTemplate = getRestTemplate(serviceUrlOpt.isPresent());
UriComponentsBuilder uriBuilder = getServiceUrlBuilder(route, serviceUrlOpt)
.path(servicesSwaggerInfo.getSwaggerUrl(route));
if (group != null) {
uriBuilder.queryParam("group", group);
Expand All @@ -75,10 +86,24 @@ public ObjectNode getOriginalSwaggerDoc(String route, String group) {
try {
return restTemplate.getForObject(url, ObjectNode.class);
} catch (IllegalStateException e) {
log.error("Some unexpected error while requesting swagger docs from: {}", url);
if (e.getMessage() == null || !e.getMessage().startsWith("No instances available for")) {
throw e;
}
throw new NotFoundException();
throw new NotFoundException(e);
}
}

private RestTemplate getRestTemplate(boolean isUrlBased) {
if (isUrlBased) {
return pureRestTemplate;
}
return loadBalancedRestTemplate;
}

private UriComponentsBuilder getServiceUrlBuilder(String route, Optional<String> serviceUrlOpt) {
String serviceUrl = servicesSwaggerInfo.getDirectSwaggerBaseUrl(route)
.orElseGet(() -> serviceUrlOpt.orElseGet(() -> servicesSwaggerInfo.getDefaultProtocol() + route));
return UriComponentsBuilder.fromHttpUrl(serviceUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private Stream<SwaggerResource> generateSwaggerDocumentationResource(String rout
})
.filter(Objects::nonNull);
} catch (Exception e) {
log.error(String.format("Some error during obtain swagger documentation for route %s", route), e);
log.error(String.format("Some error during obtain swagger documentation for route '%s'", route), e);
return Stream.empty();
}
}
Expand Down

0 comments on commit 1a97014

Please sign in to comment.