Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.maven; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 21 | import com.google.common.base.Charsets; |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 22 | import com.google.common.io.ByteStreams; |
| 23 | import com.google.common.io.Files; |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 24 | import com.thoughtworks.qdox.JavaProjectBuilder; |
| 25 | import com.thoughtworks.qdox.model.DocletTag; |
| 26 | import com.thoughtworks.qdox.model.JavaAnnotation; |
| 27 | import com.thoughtworks.qdox.model.JavaClass; |
| 28 | import com.thoughtworks.qdox.model.JavaMethod; |
| 29 | import com.thoughtworks.qdox.model.JavaParameter; |
| 30 | import com.thoughtworks.qdox.model.JavaType; |
| 31 | import org.apache.maven.plugin.AbstractMojo; |
| 32 | import org.apache.maven.plugin.MojoExecutionException; |
| 33 | import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 34 | import org.apache.maven.plugins.annotations.Mojo; |
| 35 | import org.apache.maven.plugins.annotations.Parameter; |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 36 | import org.apache.maven.project.MavenProject; |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 37 | |
| 38 | import java.io.File; |
| 39 | import java.io.FileWriter; |
| 40 | import java.io.IOException; |
| 41 | import java.io.PrintWriter; |
| 42 | import java.util.HashMap; |
| 43 | import java.util.Map; |
| 44 | import java.util.Optional; |
| 45 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 46 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 47 | |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Produces ONOS Swagger api-doc. |
| 50 | */ |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 51 | @Mojo(name = "swagger", defaultPhase = LifecyclePhase.GENERATE_SOURCES) |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 52 | public class OnosSwaggerMojo extends AbstractMojo { |
| 53 | private final ObjectMapper mapper = new ObjectMapper(); |
| 54 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 55 | private static final String JSON_FILE = "swagger.json"; |
| 56 | private static final String GEN_SRC = "generated-sources"; |
| 57 | private static final String REG_SRC = "registrator.javat"; |
| 58 | |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 59 | private static final String PATH = "javax.ws.rs.Path"; |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 60 | private static final String PATH_PARAM = "javax.ws.rs.PathParam"; |
| 61 | private static final String QUERY_PARAM = "javax.ws.rs.QueryParam"; |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 62 | private static final String POST = "javax.ws.rs.POST"; |
| 63 | private static final String GET = "javax.ws.rs.GET"; |
| 64 | private static final String PUT = "javax.ws.rs.PUT"; |
| 65 | private static final String DELETE = "javax.ws.rs.DELETE"; |
| 66 | private static final String PRODUCES = "javax.ws.rs.Produces"; |
| 67 | private static final String CONSUMES = "javax.ws.rs.Consumes"; |
| 68 | private static final String JSON = "MediaType.APPLICATION_JSON"; |
| 69 | |
| 70 | /** |
| 71 | * The directory where the generated catalogue file will be put. |
| 72 | */ |
| 73 | @Parameter(defaultValue = "${basedir}") |
| 74 | protected File srcDirectory; |
| 75 | |
| 76 | /** |
| 77 | * The directory where the generated catalogue file will be put. |
| 78 | */ |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 79 | @Parameter(defaultValue = "${project.build.directory}") |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 80 | protected File dstDirectory; |
| 81 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 82 | /** |
| 83 | * REST API web-context |
| 84 | */ |
| 85 | @Parameter(defaultValue = "${web.context}") |
| 86 | protected String webContext; |
| 87 | |
| 88 | /** |
| 89 | * REST API version |
| 90 | */ |
| 91 | @Parameter(defaultValue = "${api.version}") |
| 92 | protected String apiVersion; |
| 93 | |
| 94 | /** |
| 95 | * REST API description |
| 96 | */ |
| 97 | @Parameter(defaultValue = "${api.description}") |
| 98 | protected String apiDescription; |
| 99 | |
| 100 | /** |
| 101 | * REST API title |
| 102 | */ |
| 103 | @Parameter(defaultValue = "${api.title}") |
| 104 | protected String apiTitle; |
| 105 | |
| 106 | /** |
| 107 | * REST API title |
| 108 | */ |
| 109 | @Parameter(defaultValue = "${api.package}") |
| 110 | protected String apiPackage; |
| 111 | |
| 112 | /** |
| 113 | * Maven project |
| 114 | */ |
| 115 | @Parameter(defaultValue = "${project}") |
| 116 | protected MavenProject project; |
| 117 | |
| 118 | |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 119 | @Override |
| 120 | public void execute() throws MojoExecutionException { |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 121 | try { |
| 122 | JavaProjectBuilder builder = new JavaProjectBuilder(); |
| 123 | builder.addSourceTree(new File(srcDirectory, "src/main/java")); |
| 124 | |
| 125 | ObjectNode root = initializeRoot(); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 126 | ArrayNode tags = mapper.createArrayNode(); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 127 | ObjectNode paths = mapper.createObjectNode(); |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 128 | ObjectNode definitions = mapper.createObjectNode(); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 129 | |
| 130 | root.set("tags", tags); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 131 | root.set("paths", paths); |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 132 | root.set("definitions", definitions); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 133 | |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 134 | builder.getClasses().forEach(jc -> processClass(jc, paths, tags, definitions)); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 135 | |
| 136 | if (paths.size() > 0) { |
| 137 | getLog().info("Generating ONOS REST API documentation..."); |
| 138 | genCatalog(root); |
| 139 | |
| 140 | if (!isNullOrEmpty(apiPackage)) { |
| 141 | genRegistrator(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | project.addCompileSourceRoot(new File(dstDirectory, GEN_SRC).getPath()); |
| 146 | |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 147 | } catch (Exception e) { |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 148 | getLog().warn("Unable to generate ONOS REST API documentation", e); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 149 | throw e; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // initializes top level root with Swagger required specifications |
| 154 | private ObjectNode initializeRoot() { |
| 155 | ObjectNode root = mapper.createObjectNode(); |
| 156 | root.put("swagger", "2.0"); |
| 157 | ObjectNode info = mapper.createObjectNode(); |
| 158 | root.set("info", info); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 159 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 160 | root.put("basePath", webContext); |
| 161 | info.put("version", apiVersion); |
| 162 | info.put("title", apiTitle); |
| 163 | info.put("description", apiDescription); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 164 | |
| 165 | ArrayNode produces = mapper.createArrayNode(); |
| 166 | produces.add("application/json"); |
| 167 | root.set("produces", produces); |
| 168 | |
| 169 | ArrayNode consumes = mapper.createArrayNode(); |
| 170 | consumes.add("application/json"); |
| 171 | root.set("consumes", consumes); |
| 172 | |
| 173 | return root; |
| 174 | } |
| 175 | |
| 176 | // Checks whether javaClass has a path tag associated with it and if it does |
| 177 | // processes its methods and creates a tag for the class on the root |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 178 | void processClass(JavaClass javaClass, ObjectNode paths, ArrayNode tags, ObjectNode definitions) { |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 179 | // If the class does not have a Path tag then ignore it |
| 180 | JavaAnnotation annotation = getPathAnnotation(javaClass); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 181 | if (annotation == null) { |
| 182 | return; |
| 183 | } |
| 184 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 185 | String path = getPath(annotation); |
| 186 | if (path == null) { |
| 187 | return; |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 188 | } |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 189 | |
| 190 | String resourcePath = "/" + path; |
| 191 | String tagPath = path.isEmpty() ? "/" : path; |
| 192 | |
| 193 | // Create tag node for this class. |
| 194 | ObjectNode tagObject = mapper.createObjectNode(); |
| 195 | tagObject.put("name", tagPath); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 196 | if (javaClass.getComment() != null) { |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 197 | tagObject.put("description", shortText(javaClass.getComment())); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 198 | } |
| 199 | tags.add(tagObject); |
| 200 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 201 | // Create an array node add to all methods from this class. |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 202 | ArrayNode tagArray = mapper.createArrayNode(); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 203 | tagArray.add(tagPath); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 204 | |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 205 | processAllMethods(javaClass, resourcePath, paths, tagArray, definitions); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 208 | private JavaAnnotation getPathAnnotation(JavaClass javaClass) { |
| 209 | Optional<JavaAnnotation> optional = javaClass.getAnnotations() |
| 210 | .stream().filter(a -> a.getType().getName().equals(PATH)).findAny(); |
| 211 | return optional.isPresent() ? optional.get() : null; |
| 212 | } |
| 213 | |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 214 | // Checks whether a class's methods are REST methods and then places all the |
| 215 | // methods under a specific path into the paths node |
| 216 | private void processAllMethods(JavaClass javaClass, String resourcePath, |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 217 | ObjectNode paths, ArrayNode tagArray, ObjectNode definitions) { |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 218 | // map of the path to its methods represented by an ObjectNode |
| 219 | Map<String, ObjectNode> pathMap = new HashMap<>(); |
| 220 | |
| 221 | javaClass.getMethods().forEach(javaMethod -> { |
| 222 | javaMethod.getAnnotations().forEach(annotation -> { |
| 223 | String name = annotation.getType().getName(); |
| 224 | if (name.equals(POST) || name.equals(GET) || name.equals(DELETE) || name.equals(PUT)) { |
| 225 | // substring(12) removes "javax.ws.rs." |
| 226 | String method = annotation.getType().toString().substring(12).toLowerCase(); |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 227 | processRestMethod(javaMethod, method, pathMap, resourcePath, tagArray, definitions); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 228 | } |
| 229 | }); |
| 230 | }); |
| 231 | |
| 232 | // for each path add its methods to the path node |
| 233 | for (Map.Entry<String, ObjectNode> entry : pathMap.entrySet()) { |
| 234 | paths.set(entry.getKey(), entry.getValue()); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | } |
| 239 | |
| 240 | private void processRestMethod(JavaMethod javaMethod, String method, |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 241 | Map<String, ObjectNode> pathMap, |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 242 | String resourcePath, ArrayNode tagArray, ObjectNode definitions) { |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 243 | String fullPath = resourcePath, consumes = "", produces = "", |
| 244 | comment = javaMethod.getComment(); |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 245 | DocletTag tag = javaMethod.getTagByName("rsModel"); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 246 | for (JavaAnnotation annotation : javaMethod.getAnnotations()) { |
| 247 | String name = annotation.getType().getName(); |
| 248 | if (name.equals(PATH)) { |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 249 | fullPath = resourcePath + "/" + getPath(annotation); |
| 250 | fullPath = fullPath.replaceFirst("^//", "/"); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 251 | } |
| 252 | if (name.equals(CONSUMES)) { |
| 253 | consumes = getIOType(annotation); |
| 254 | } |
| 255 | if (name.equals(PRODUCES)) { |
| 256 | produces = getIOType(annotation); |
| 257 | } |
| 258 | } |
| 259 | ObjectNode methodNode = mapper.createObjectNode(); |
| 260 | methodNode.set("tags", tagArray); |
| 261 | |
| 262 | addSummaryDescriptions(methodNode, comment); |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 263 | addJsonSchemaDefinition(definitions, tag); |
| 264 | addJsonSchemaDefinition(definitions, tag); |
| 265 | |
| 266 | processParameters(javaMethod, methodNode, method, tag); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 267 | |
| 268 | processConsumesProduces(methodNode, "consumes", consumes); |
| 269 | processConsumesProduces(methodNode, "produces", produces); |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 270 | if (tag == null || ((method.toLowerCase().equals("post") || method.toLowerCase().equals("put")) |
| 271 | && !(tag.getParameters().size() > 1))) { |
| 272 | addResponses(methodNode, tag, false); |
| 273 | } else { |
| 274 | addResponses(methodNode, tag, true); |
| 275 | } |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 276 | |
| 277 | ObjectNode operations = pathMap.get(fullPath); |
| 278 | if (operations == null) { |
| 279 | operations = mapper.createObjectNode(); |
| 280 | operations.set(method, methodNode); |
| 281 | pathMap.put(fullPath, operations); |
| 282 | } else { |
| 283 | operations.set(method, methodNode); |
| 284 | } |
| 285 | } |
| 286 | |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 287 | private void addJsonSchemaDefinition(ObjectNode definitions, DocletTag tag) { |
| 288 | File definitionsDirectory = new File(srcDirectory + "/src/main/resources/definitions"); |
| 289 | if (tag != null) { |
| 290 | tag.getParameters().stream().forEach(param -> { |
| 291 | try { |
| 292 | File config = new File(definitionsDirectory.getAbsolutePath() + "/" |
| 293 | + param + ".json"); |
| 294 | String lines = Files.readLines(config, Charsets.UTF_8).stream().reduce((t, u) -> t + u). |
| 295 | get(); |
| 296 | definitions.putPOJO(param, lines); |
| 297 | } catch (IOException e) { |
| 298 | e.printStackTrace(); |
| 299 | } |
| 300 | }); |
| 301 | |
| 302 | } |
| 303 | } |
| 304 | |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 305 | private void processConsumesProduces(ObjectNode methodNode, String type, String io) { |
| 306 | if (!io.equals("")) { |
| 307 | ArrayNode array = mapper.createArrayNode(); |
| 308 | methodNode.set(type, array); |
| 309 | array.add(io); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | private void addSummaryDescriptions(ObjectNode methodNode, String comment) { |
| 314 | String summary = "", description; |
| 315 | if (comment != null) { |
| 316 | if (comment.contains(".")) { |
| 317 | int periodIndex = comment.indexOf("."); |
| 318 | summary = comment.substring(0, periodIndex); |
| 319 | description = comment.length() > periodIndex + 1 ? |
| 320 | comment.substring(periodIndex + 1).trim() : ""; |
| 321 | } else { |
| 322 | description = comment; |
| 323 | } |
| 324 | methodNode.put("summary", summary); |
| 325 | methodNode.put("description", description); |
| 326 | } |
| 327 | } |
| 328 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 329 | // Temporary solution to add responses to a method |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 330 | // TODO Provide annotations in the web resources for responses and parse them |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 331 | private void addResponses(ObjectNode methodNode, DocletTag tag, boolean responseJson) { |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 332 | ObjectNode responses = mapper.createObjectNode(); |
| 333 | methodNode.set("responses", responses); |
| 334 | |
| 335 | ObjectNode success = mapper.createObjectNode(); |
| 336 | success.put("description", "successful operation"); |
| 337 | responses.set("200", success); |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 338 | if (tag != null && responseJson) { |
| 339 | ObjectNode schema = mapper.createObjectNode(); |
| 340 | tag.getParameters().stream().forEach( |
| 341 | param -> schema.put("$ref", "#/definitions/" + param)); |
| 342 | success.set("schema", schema); |
| 343 | } |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 344 | |
| 345 | ObjectNode defaultObj = mapper.createObjectNode(); |
| 346 | defaultObj.put("description", "Unexpected error"); |
| 347 | responses.set("default", defaultObj); |
| 348 | } |
| 349 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 350 | // Checks if the annotations has a value of JSON and returns the string |
| 351 | // that Swagger requires |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 352 | private String getIOType(JavaAnnotation annotation) { |
| 353 | if (annotation.getNamedParameter("value").toString().equals(JSON)) { |
| 354 | return "application/json"; |
| 355 | } |
| 356 | return ""; |
| 357 | } |
| 358 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 359 | // If the annotation has a Path tag, returns the value with leading and |
| 360 | // trailing double quotes and slash removed. |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 361 | private String getPath(JavaAnnotation annotation) { |
| 362 | String path = annotation.getNamedParameter("value").toString(); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 363 | return path == null ? null : path.replaceAll("(^[\\\"/]*|[/\\\"]*$)", ""); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 366 | // Processes parameters of javaMethod and enters the proper key-values into the methodNode |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 367 | private void processParameters(JavaMethod javaMethod, ObjectNode methodNode, String method, DocletTag tag) { |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 368 | ArrayNode parameters = mapper.createArrayNode(); |
| 369 | methodNode.set("parameters", parameters); |
| 370 | boolean required = true; |
| 371 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 372 | for (JavaParameter javaParameter : javaMethod.getParameters()) { |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 373 | ObjectNode individualParameterNode = mapper.createObjectNode(); |
| 374 | Optional<JavaAnnotation> optional = javaParameter.getAnnotations().stream().filter( |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 375 | annotation -> annotation.getType().getName().equals(PATH_PARAM) || |
| 376 | annotation.getType().getName().equals(QUERY_PARAM)).findAny(); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 377 | JavaAnnotation pathType = optional.isPresent() ? optional.get() : null; |
| 378 | |
| 379 | String annotationName = javaParameter.getName(); |
| 380 | |
| 381 | |
| 382 | if (pathType != null) { //the parameter is a path or query parameter |
| 383 | individualParameterNode.put("name", |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 384 | pathType.getNamedParameter("value") |
| 385 | .toString().replace("\"", "")); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 386 | if (pathType.getType().getName().equals(PATH_PARAM)) { |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 387 | individualParameterNode.put("in", "path"); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 388 | } else if (pathType.getType().getName().equals(QUERY_PARAM)) { |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 389 | individualParameterNode.put("in", "query"); |
| 390 | } |
| 391 | individualParameterNode.put("type", getType(javaParameter.getType())); |
| 392 | } else { // the parameter is a body parameter |
| 393 | individualParameterNode.put("name", annotationName); |
| 394 | individualParameterNode.put("in", "body"); |
| 395 | |
andrea | faa2c4b | 2015-11-16 13:48:39 -0800 | [diff] [blame] | 396 | // Adds the reference to the Json model for the input |
| 397 | // that goes in the post or put operation |
| 398 | if (tag != null && (method.toLowerCase().equals("post") || |
| 399 | method.toLowerCase().equals("put"))) { |
| 400 | ObjectNode schema = mapper.createObjectNode(); |
| 401 | tag.getParameters().stream().forEach(param -> { |
| 402 | schema.put("$ref", "#/definitions/" + param); |
| 403 | }); |
| 404 | individualParameterNode.set("schema", schema); |
| 405 | } |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 406 | } |
| 407 | for (DocletTag p : javaMethod.getTagsByName("param")) { |
| 408 | if (p.getValue().contains(annotationName)) { |
| 409 | try { |
| 410 | String description = p.getValue().split(" ", 2)[1].trim(); |
| 411 | if (description.contains("optional")) { |
| 412 | required = false; |
| 413 | } |
| 414 | individualParameterNode.put("description", description); |
| 415 | } catch (Exception e) { |
| 416 | e.printStackTrace(); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | individualParameterNode.put("required", required); |
| 421 | parameters.add(individualParameterNode); |
| 422 | } |
| 423 | } |
| 424 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 425 | // Returns the Swagger specified strings for the type of a parameter |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 426 | private String getType(JavaType javaType) { |
| 427 | String type = javaType.getFullyQualifiedName(); |
| 428 | String value; |
| 429 | if (type.equals(String.class.getName())) { |
| 430 | value = "string"; |
| 431 | } else if (type.equals("int")) { |
| 432 | value = "integer"; |
| 433 | } else if (type.equals(boolean.class.getName())) { |
| 434 | value = "boolean"; |
| 435 | } else if (type.equals(long.class.getName())) { |
| 436 | value = "number"; |
| 437 | } else { |
| 438 | value = ""; |
| 439 | } |
| 440 | return value; |
| 441 | } |
| 442 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 443 | // Writes the swagger.json file using the supplied JSON root. |
| 444 | private void genCatalog(ObjectNode root) { |
| 445 | File swaggerCfg = new File(dstDirectory, JSON_FILE); |
| 446 | if (dstDirectory.exists() || dstDirectory.mkdirs()) { |
| 447 | try (FileWriter fw = new FileWriter(swaggerCfg); |
| 448 | PrintWriter pw = new PrintWriter(fw)) { |
| 449 | pw.println(root.toString()); |
| 450 | } catch (IOException e) { |
| 451 | getLog().warn("Unable to write " + JSON_FILE); |
| 452 | } |
| 453 | } else { |
| 454 | getLog().warn("Unable to create " + dstDirectory); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 458 | // Generates the registrator Java component. |
| 459 | private void genRegistrator() { |
| 460 | File dir = new File(dstDirectory, GEN_SRC); |
| 461 | File reg = new File(dir, apiPackage.replaceAll("\\.", "/") + "/ApiDocRegistrator.java"); |
| 462 | File pkg = reg.getParentFile(); |
| 463 | if (pkg.exists() || pkg.mkdirs()) { |
| 464 | try { |
| 465 | String src = new String(ByteStreams.toByteArray(getClass().getResourceAsStream(REG_SRC))); |
| 466 | src = src.replace("${api.package}", apiPackage) |
| 467 | .replace("${web.context}", webContext) |
| 468 | .replace("${api.title}", apiTitle) |
| 469 | .replace("${api.description}", apiTitle); |
| 470 | Files.write(src.getBytes(), reg); |
| 471 | } catch (IOException e) { |
| 472 | getLog().warn("Unable to write " + reg); |
| 473 | } |
| 474 | } else { |
| 475 | getLog().warn("Unable to create " + reg); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | // Returns "nickname" based on method and path for a REST method |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 480 | private String setNickname(String method, String path) { |
| 481 | if (!path.equals("")) { |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 482 | return (method + path.replace('/', '_').replace("{", "").replace("}", "")).toLowerCase(); |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 483 | } else { |
| 484 | return method.toLowerCase(); |
| 485 | } |
| 486 | } |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 487 | |
| 488 | private String shortText(String comment) { |
| 489 | int i = comment.indexOf('.'); |
| 490 | return i > 0 ? comment.substring(0, i) : comment; |
| 491 | } |
| 492 | |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 493 | } |