Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor ApiListingResource to not use greedy @path("/") annotation
- Loading branch information
Showing
4 changed files
with
238 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
.../swagger-jaxrs/src/main/java/io/swagger/jaxrs/listing/AcceptHeaderApiListingResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.swagger.jaxrs.listing; | ||
|
||
import io.swagger.annotations.ApiOperation; | ||
|
||
import javax.servlet.ServletConfig; | ||
import javax.servlet.ServletContext; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.*; | ||
|
||
/** | ||
* Created by rbolles on 2/15/16. | ||
*/ | ||
@Path("/swagger") | ||
public class AcceptHeaderApiListingResource extends BaseApiListingResource { | ||
|
||
@Context | ||
ServletContext context; | ||
|
||
@GET | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
@ApiOperation(value = "The swagger definition in JSON", hidden = true) | ||
public Response getListingJson( | ||
@Context Application app, | ||
@Context ServletConfig sc, | ||
@Context HttpHeaders headers, | ||
@Context UriInfo uriInfo) { | ||
return getListingJsonResponse(app, context, sc, headers, uriInfo); | ||
} | ||
|
||
@GET | ||
@Produces("application/yaml") | ||
@ApiOperation(value = "The swagger definition in YAML", hidden = true) | ||
public Response getListingYaml( | ||
@Context Application app, | ||
@Context ServletConfig sc, | ||
@Context HttpHeaders headers, | ||
@Context UriInfo uriInfo) { | ||
return getListingYamlResponse(app, context, sc, headers, uriInfo); | ||
} | ||
} |
196 changes: 5 additions & 191 deletions
196
modules/swagger-jaxrs/src/main/java/io/swagger/jaxrs/listing/ApiListingResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,222 +1,36 @@ | ||
package io.swagger.jaxrs.listing; | ||
|
||
import io.swagger.annotations.ApiOperation; | ||
import io.swagger.config.FilterFactory; | ||
import io.swagger.config.Scanner; | ||
import io.swagger.config.SwaggerConfig; | ||
import io.swagger.core.filter.SpecFilter; | ||
import io.swagger.core.filter.SwaggerSpecFilter; | ||
import io.swagger.jaxrs.Reader; | ||
import io.swagger.jaxrs.config.JaxrsScanner; | ||
import io.swagger.jaxrs.config.ReaderConfigUtils; | ||
import io.swagger.jaxrs.config.SwaggerContextService; | ||
import io.swagger.models.Swagger; | ||
import io.swagger.util.Yaml; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.servlet.ServletConfig; | ||
import javax.servlet.ServletContext; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Application; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.Cookie; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.core.UriInfo; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import javax.ws.rs.core.*; | ||
|
||
@Path("/") | ||
public class ApiListingResource { | ||
private static volatile boolean initialized = false; | ||
|
||
private static volatile ConcurrentMap<String, Boolean> initializedScanner = new ConcurrentHashMap<String, Boolean>(); | ||
private static volatile ConcurrentMap<String, Boolean> initializedConfig = new ConcurrentHashMap<String, Boolean>(); | ||
|
||
Logger LOGGER = LoggerFactory.getLogger(ApiListingResource.class); | ||
@Path("/swagger.{type:json|yaml}") | ||
public class ApiListingResource extends BaseApiListingResource { | ||
|
||
@Context | ||
ServletContext context; | ||
|
||
protected synchronized Swagger scan(Application app, ServletConfig sc) { | ||
Swagger swagger = null; | ||
SwaggerContextService ctxService = new SwaggerContextService().withServletConfig(sc); | ||
Scanner scanner = ctxService.getScanner(); | ||
if (scanner != null) { | ||
SwaggerSerializers.setPrettyPrint(scanner.getPrettyPrint()); | ||
swagger = new SwaggerContextService().withServletConfig(sc).getSwagger(); | ||
Set<Class<?>> classes; | ||
if (scanner instanceof JaxrsScanner) { | ||
JaxrsScanner jaxrsScanner = (JaxrsScanner) scanner; | ||
classes = jaxrsScanner.classesFromContext(app, sc); | ||
} else { | ||
classes = scanner.classes(); | ||
} | ||
if (classes != null) { | ||
Reader reader = new Reader(swagger, ReaderConfigUtils.getReaderConfig(context)); | ||
swagger = reader.read(classes); | ||
if (scanner instanceof SwaggerConfig) { | ||
swagger = ((SwaggerConfig) scanner).configure(swagger); | ||
} else { | ||
SwaggerConfig swaggerConfig = ctxService.getConfig(); | ||
if (swaggerConfig != null) { | ||
LOGGER.debug("configuring swagger with " + swaggerConfig); | ||
swaggerConfig.configure(swagger); | ||
} else { | ||
LOGGER.debug("no configurator"); | ||
} | ||
} | ||
new SwaggerContextService().withServletConfig(sc).updateSwagger(swagger); | ||
} | ||
} | ||
if (SwaggerContextService.isScannerIdInitParamDefined(sc)) { | ||
initializedScanner.put(sc.getServletName() + "_" + SwaggerContextService.getScannerIdFromInitParam(sc), true); | ||
} else if (SwaggerContextService.isConfigIdInitParamDefined(sc)) { | ||
initializedConfig.put(sc.getServletName() + "_" + SwaggerContextService.getConfigIdFromInitParam(sc), true); | ||
} else { | ||
initialized = true; | ||
} | ||
|
||
return swagger; | ||
} | ||
|
||
private Swagger process( | ||
Application app, | ||
ServletConfig sc, | ||
HttpHeaders headers, | ||
UriInfo uriInfo) { | ||
Swagger swagger = new SwaggerContextService().withServletConfig(sc).getSwagger(); | ||
synchronized (ApiListingResource.class) { | ||
if (SwaggerContextService.isScannerIdInitParamDefined(sc)) { | ||
if (!initializedScanner.containsKey(sc.getServletName() + "_" + SwaggerContextService.getScannerIdFromInitParam(sc))) { | ||
swagger = scan(app, sc); | ||
} | ||
} else { | ||
if (SwaggerContextService.isConfigIdInitParamDefined(sc)) { | ||
if (!initializedConfig.containsKey(sc.getServletName() + "_" + SwaggerContextService.getConfigIdFromInitParam(sc))) { | ||
swagger = scan(app, sc); | ||
} | ||
} else { | ||
if (!initialized) { | ||
swagger = scan(app, sc); | ||
} | ||
} | ||
} | ||
} | ||
if (swagger != null) { | ||
SwaggerSpecFilter filterImpl = FilterFactory.getFilter(); | ||
if (filterImpl != null) { | ||
SpecFilter f = new SpecFilter(); | ||
swagger = f.filter(swagger, filterImpl, getQueryParams(uriInfo.getQueryParameters()), getCookies(headers), | ||
getHeaders(headers)); | ||
} | ||
} | ||
return swagger; | ||
} | ||
|
||
@GET | ||
@Produces({MediaType.APPLICATION_JSON, "application/yaml"}) | ||
@ApiOperation(value = "The swagger definition in either JSON or YAML", hidden = true) | ||
@Path("/swagger.{type:json|yaml}") | ||
public Response getListing( | ||
@Context Application app, | ||
@Context ServletConfig sc, | ||
@Context HttpHeaders headers, | ||
@Context UriInfo uriInfo, | ||
@PathParam("type") String type) { | ||
if (StringUtils.isNotBlank(type) && type.trim().equalsIgnoreCase("yaml")) { | ||
return getListingYaml(app, sc, headers, uriInfo); | ||
} else { | ||
return getListingJson(app, sc, headers, uriInfo); | ||
} | ||
} | ||
|
||
@GET | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
@Path("/swagger") | ||
@ApiOperation(value = "The swagger definition in JSON", hidden = true) | ||
public Response getListingJson( | ||
@Context Application app, | ||
@Context ServletConfig sc, | ||
@Context HttpHeaders headers, | ||
@Context UriInfo uriInfo) { | ||
Swagger swagger = process(app, sc, headers, uriInfo); | ||
|
||
if (swagger != null) { | ||
return Response.ok().entity(swagger).build(); | ||
return getListingYamlResponse(app, context, sc, headers, uriInfo); | ||
} else { | ||
return Response.status(404).build(); | ||
} | ||
} | ||
|
||
@GET | ||
@Produces("application/yaml") | ||
@Path("/swagger") | ||
@ApiOperation(value = "The swagger definition in YAML", hidden = true) | ||
public Response getListingYaml( | ||
@Context Application app, | ||
@Context ServletConfig sc, | ||
@Context HttpHeaders headers, | ||
@Context UriInfo uriInfo) { | ||
Swagger swagger = process(app, sc, headers, uriInfo); | ||
try { | ||
if (swagger != null) { | ||
String yaml = Yaml.mapper().writeValueAsString(swagger); | ||
StringBuilder b = new StringBuilder(); | ||
String[] parts = yaml.split("\n"); | ||
for (String part : parts) { | ||
b.append(part); | ||
b.append("\n"); | ||
} | ||
return Response.ok().entity(b.toString()).type("application/yaml").build(); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return getListingJsonResponse(app, context, sc, headers, uriInfo); | ||
} | ||
return Response.status(404).build(); | ||
} | ||
|
||
protected Map<String, List<String>> getQueryParams(MultivaluedMap<String, String> params) { | ||
Map<String, List<String>> output = new HashMap<String, List<String>>(); | ||
if (params != null) { | ||
for (String key : params.keySet()) { | ||
List<String> values = params.get(key); | ||
output.put(key, values); | ||
} | ||
} | ||
return output; | ||
} | ||
|
||
protected Map<String, String> getCookies(HttpHeaders headers) { | ||
Map<String, String> output = new HashMap<String, String>(); | ||
if (headers != null) { | ||
for (String key : headers.getCookies().keySet()) { | ||
Cookie cookie = headers.getCookies().get(key); | ||
output.put(key, cookie.getValue()); | ||
} | ||
} | ||
return output; | ||
} | ||
|
||
protected Map<String, List<String>> getHeaders(HttpHeaders headers) { | ||
Map<String, List<String>> output = new HashMap<String, List<String>>(); | ||
if (headers != null) { | ||
for (String key : headers.getRequestHeaders().keySet()) { | ||
List<String> values = headers.getRequestHeaders().get(key); | ||
output.put(key, values); | ||
} | ||
} | ||
return output; | ||
} | ||
} |
Oops, something went wrong.