|
| 1 | +package com.intuit.platform.swagger |
| 2 | + |
| 3 | +import com.wordnik.swagger.core.util.ReaderUtil |
| 4 | +import com.wordnik.swagger.annotations._ |
| 5 | +import com.wordnik.swagger.jaxrs.config.BeanConfig |
| 6 | +import com.wordnik.swagger.core.filter._ |
| 7 | +import com.wordnik.swagger.config._ |
| 8 | +import com.wordnik.swagger.model._ |
| 9 | +import com.wordnik.swagger.reader._ |
| 10 | +import com.wordnik.swagger.core.util.JsonSerializer |
| 11 | + |
| 12 | +import org.slf4j.LoggerFactory |
| 13 | + |
| 14 | +import javax.ws.rs._ |
| 15 | +import javax.ws.rs.core.{ Response, MediaType } |
| 16 | + |
| 17 | +@Path("/api-docs") |
| 18 | +@Produces(Array(MediaType.APPLICATION_JSON)) |
| 19 | +class ApiListingJSON { |
| 20 | + private val LOGGER = LoggerFactory.getLogger(classOf[ApiListingJSON]) |
| 21 | + |
| 22 | + @GET |
| 23 | + def getResource(): Response = { |
| 24 | + val docRoot = "" |
| 25 | + val queryParams = Map[String, List[String]]() |
| 26 | + val cookies = Map[String, String]() |
| 27 | + val headers = Map[String, List[String]]() |
| 28 | + |
| 29 | + val f = new SpecFilter |
| 30 | + val listings = ApiListingCache.listing(docRoot).map(specs => { |
| 31 | + (for(spec <- specs.values) |
| 32 | + yield f.filter(spec, FilterFactory.filter, queryParams, cookies, headers) |
| 33 | + ).filter(m => m.apis.size > 0) |
| 34 | + }) |
| 35 | + val references = (for(listing <- listings.getOrElse(List())) yield { |
| 36 | + ApiListingReference(listing.resourcePath, listing.description) |
| 37 | + }).toList |
| 38 | + val config = ConfigFactory.config |
| 39 | + val resourceListing = ResourceListing(config.apiVersion, |
| 40 | + config.swaggerVersion, |
| 41 | + references |
| 42 | + ) |
| 43 | + |
| 44 | + Response.ok(JsonSerializer.asJson(resourceListing)).build(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * individual api listing |
| 49 | + **/ |
| 50 | + @GET |
| 51 | + @Path("/{route: .+}") |
| 52 | + def apiDeclaration ( |
| 53 | + @PathParam("route") route: String |
| 54 | + ): Response = { |
| 55 | + LOGGER.debug("requested apiDeclaration for " + route) |
| 56 | + val docRoot = this.getClass.getAnnotation(classOf[Path]).value |
| 57 | + val queryParams = Map[String, List[String]]() |
| 58 | + val cookies = Map[String, String]() |
| 59 | + val headers = Map[String, List[String]]() |
| 60 | + val f = new SpecFilter |
| 61 | + val pathPart = cleanRoute(route) |
| 62 | + LOGGER.debug("requested route " + pathPart) |
| 63 | + val listings = ApiListingCache.listing(docRoot).map(specs => { |
| 64 | + (for(spec <- specs.values) yield { |
| 65 | + LOGGER.debug("inspecting path " + spec.resourcePath) |
| 66 | + f.filter(spec, FilterFactory.filter, queryParams, cookies, headers) |
| 67 | + }).filter(m => { |
| 68 | + val resourcePath = m.resourcePath match { |
| 69 | + case e: String if(e.startsWith("/")) => e |
| 70 | + case e: String => "/" + e |
| 71 | + } |
| 72 | + resourcePath == pathPart |
| 73 | + }) |
| 74 | + }).toList.flatten |
| 75 | + |
| 76 | + listings.size match { |
| 77 | + case 1 => Response.ok(listings(0)).build |
| 78 | + case _ => Response.status(404).build |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // ensure leading slash, remove trailing |
| 83 | + def cleanRoute(route: String) = { |
| 84 | + val cleanStart = { |
| 85 | + if(route.startsWith("/")) route |
| 86 | + else "/" + route |
| 87 | + } |
| 88 | + if(cleanStart.endsWith("/")) cleanStart.substring(0, cleanStart.length - 1) |
| 89 | + else cleanStart |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +object ApiListingCache extends ReaderUtil { |
| 94 | + var cache: Option[Map[String, com.wordnik.swagger.model.ApiListing]] = None |
| 95 | + |
| 96 | + def listing(docRoot: String): Option[Map[String, com.wordnik.swagger.model.ApiListing]] = { |
| 97 | + cache.orElse{ |
| 98 | + ClassReaders.reader.map{reader => |
| 99 | + ScannerFactory.scanner.map(scanner => { |
| 100 | + val classes = scanner match { |
| 101 | + case scanner: Scanner => { |
| 102 | + val beanConfig = scanner.asInstanceOf[BeanConfig] |
| 103 | + beanConfig.classesFromContext(null, null) |
| 104 | + } |
| 105 | + case _ =>List() |
| 106 | + } |
| 107 | + val listings = (for(cls <- classes) yield reader.read(docRoot, cls, ConfigFactory.config)).flatten |
| 108 | + val mergedListings = groupByResourcePath(listings) |
| 109 | + cache = Some(mergedListings.map(m => (m.resourcePath, m)).toMap) |
| 110 | + }) |
| 111 | + } |
| 112 | + cache |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments