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