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