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