blob: 56f4961115bf1c8382f57f3ed22e7a1d240e0a26 [file] [log] [blame]
Sahil Lele372d1f32015-07-31 15:01:41 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sahil Lele372d1f32015-07-31 15:01:41 -07003 *
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 */
16package org.onosproject.maven;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
HIGUCHI Yuta547c2052016-02-23 00:08:13 -080021import com.google.common.base.Throwables;
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070022import com.google.common.io.ByteStreams;
23import com.google.common.io.Files;
Andrea Campanella82baf6b2015-12-14 10:23:37 -080024import com.google.gson.JsonParser;
Sahil Lele372d1f32015-07-31 15:01:41 -070025import com.thoughtworks.qdox.JavaProjectBuilder;
26import com.thoughtworks.qdox.model.DocletTag;
27import com.thoughtworks.qdox.model.JavaAnnotation;
28import com.thoughtworks.qdox.model.JavaClass;
29import com.thoughtworks.qdox.model.JavaMethod;
30import com.thoughtworks.qdox.model.JavaParameter;
31import com.thoughtworks.qdox.model.JavaType;
32import org.apache.maven.plugin.AbstractMojo;
33import org.apache.maven.plugin.MojoExecutionException;
34import org.apache.maven.plugins.annotations.LifecyclePhase;
35import org.apache.maven.plugins.annotations.Mojo;
36import org.apache.maven.plugins.annotations.Parameter;
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070037import org.apache.maven.project.MavenProject;
Sahil Lele372d1f32015-07-31 15:01:41 -070038
39import java.io.File;
Andrea Campanella82baf6b2015-12-14 10:23:37 -080040import java.io.FileReader;
Sahil Lele372d1f32015-07-31 15:01:41 -070041import java.io.FileWriter;
42import java.io.IOException;
43import java.io.PrintWriter;
44import java.util.HashMap;
45import java.util.Map;
46import java.util.Optional;
47
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070048import static com.google.common.base.Strings.isNullOrEmpty;
49
Sahil Lele372d1f32015-07-31 15:01:41 -070050/**
51 * Produces ONOS Swagger api-doc.
52 */
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070053@Mojo(name = "swagger", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
Sahil Lele372d1f32015-07-31 15:01:41 -070054public class OnosSwaggerMojo extends AbstractMojo {
55 private final ObjectMapper mapper = new ObjectMapper();
Andrea Campanella82baf6b2015-12-14 10:23:37 -080056 private final JsonParser jsonParser = new JsonParser();
Sahil Lele372d1f32015-07-31 15:01:41 -070057
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070058 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 Lele372d1f32015-07-31 15:01:41 -070062 private static final String PATH = "javax.ws.rs.Path";
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070063 private static final String PATH_PARAM = "javax.ws.rs.PathParam";
64 private static final String QUERY_PARAM = "javax.ws.rs.QueryParam";
Sahil Lele372d1f32015-07-31 15:01:41 -070065 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";
andrea863201a2015-11-24 13:18:30 -080072 private static final String OCTET_STREAM = "MediaType.APPLICATION_OCTET_STREAM";
Sahil Lele372d1f32015-07-31 15:01:41 -070073
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 Vachuska0fa2aa12015-08-18 12:53:04 -070083 @Parameter(defaultValue = "${project.build.directory}")
Sahil Lele372d1f32015-07-31 15:01:41 -070084 protected File dstDirectory;
85
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070086 /**
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 Lele372d1f32015-07-31 15:01:41 -0700123 @Override
124 public void execute() throws MojoExecutionException {
Sahil Lele372d1f32015-07-31 15:01:41 -0700125 try {
126 JavaProjectBuilder builder = new JavaProjectBuilder();
127 builder.addSourceTree(new File(srcDirectory, "src/main/java"));
128
129 ObjectNode root = initializeRoot();
Sahil Lele372d1f32015-07-31 15:01:41 -0700130 ArrayNode tags = mapper.createArrayNode();
Sahil Lele372d1f32015-07-31 15:01:41 -0700131 ObjectNode paths = mapper.createObjectNode();
andreafaa2c4b2015-11-16 13:48:39 -0800132 ObjectNode definitions = mapper.createObjectNode();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700133
134 root.set("tags", tags);
Sahil Lele372d1f32015-07-31 15:01:41 -0700135 root.set("paths", paths);
andreafaa2c4b2015-11-16 13:48:39 -0800136 root.set("definitions", definitions);
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700137
andreafaa2c4b2015-11-16 13:48:39 -0800138 builder.getClasses().forEach(jc -> processClass(jc, paths, tags, definitions));
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700139
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 Lele372d1f32015-07-31 15:01:41 -0700151 } catch (Exception e) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700152 getLog().warn("Unable to generate ONOS REST API documentation", e);
Sahil Lele372d1f32015-07-31 15:01:41 -0700153 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 Lele372d1f32015-07-31 15:01:41 -0700163
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700164 root.put("basePath", webContext);
165 info.put("version", apiVersion);
166 info.put("title", apiTitle);
167 info.put("description", apiDescription);
Sahil Lele372d1f32015-07-31 15:01:41 -0700168
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
andreafaa2c4b2015-11-16 13:48:39 -0800182 void processClass(JavaClass javaClass, ObjectNode paths, ArrayNode tags, ObjectNode definitions) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700183 // If the class does not have a Path tag then ignore it
184 JavaAnnotation annotation = getPathAnnotation(javaClass);
Sahil Lele372d1f32015-07-31 15:01:41 -0700185 if (annotation == null) {
186 return;
187 }
188
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700189 String path = getPath(annotation);
190 if (path == null) {
191 return;
Sahil Lele372d1f32015-07-31 15:01:41 -0700192 }
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700193
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 Lele372d1f32015-07-31 15:01:41 -0700200 if (javaClass.getComment() != null) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700201 tagObject.put("description", shortText(javaClass.getComment()));
Sahil Lele372d1f32015-07-31 15:01:41 -0700202 }
203 tags.add(tagObject);
204
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700205 // Create an array node add to all methods from this class.
Sahil Lele372d1f32015-07-31 15:01:41 -0700206 ArrayNode tagArray = mapper.createArrayNode();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700207 tagArray.add(tagPath);
Sahil Lele372d1f32015-07-31 15:01:41 -0700208
andreafaa2c4b2015-11-16 13:48:39 -0800209 processAllMethods(javaClass, resourcePath, paths, tagArray, definitions);
Sahil Lele372d1f32015-07-31 15:01:41 -0700210 }
211
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700212 private JavaAnnotation getPathAnnotation(JavaClass javaClass) {
213 Optional<JavaAnnotation> optional = javaClass.getAnnotations()
214 .stream().filter(a -> a.getType().getName().equals(PATH)).findAny();
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800215 return optional.orElse(null);
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700216 }
217
Sahil Lele372d1f32015-07-31 15:01:41 -0700218 // 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,
andreafaa2c4b2015-11-16 13:48:39 -0800221 ObjectNode paths, ArrayNode tagArray, ObjectNode definitions) {
Sahil Lele372d1f32015-07-31 15:01:41 -0700222 // 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();
andreafaa2c4b2015-11-16 13:48:39 -0800231 processRestMethod(javaMethod, method, pathMap, resourcePath, tagArray, definitions);
Sahil Lele372d1f32015-07-31 15:01:41 -0700232 }
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 Vachuska0fa2aa12015-08-18 12:53:04 -0700245 Map<String, ObjectNode> pathMap,
andreafaa2c4b2015-11-16 13:48:39 -0800246 String resourcePath, ArrayNode tagArray, ObjectNode definitions) {
Sahil Lele372d1f32015-07-31 15:01:41 -0700247 String fullPath = resourcePath, consumes = "", produces = "",
248 comment = javaMethod.getComment();
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800249 DocletTag tag = javaMethod.getTagByName("onos.rsModel");
Sahil Lele372d1f32015-07-31 15:01:41 -0700250 for (JavaAnnotation annotation : javaMethod.getAnnotations()) {
251 String name = annotation.getType().getName();
252 if (name.equals(PATH)) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700253 fullPath = resourcePath + "/" + getPath(annotation);
254 fullPath = fullPath.replaceFirst("^//", "/");
Sahil Lele372d1f32015-07-31 15:01:41 -0700255 }
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);
andreafaa2c4b2015-11-16 13:48:39 -0800267 addJsonSchemaDefinition(definitions, tag);
andreafaa2c4b2015-11-16 13:48:39 -0800268
269 processParameters(javaMethod, methodNode, method, tag);
Sahil Lele372d1f32015-07-31 15:01:41 -0700270
271 processConsumesProduces(methodNode, "consumes", consumes);
272 processConsumesProduces(methodNode, "produces", produces);
andreafaa2c4b2015-11-16 13:48:39 -0800273 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 Lele372d1f32015-07-31 15:01:41 -0700279
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
andreafaa2c4b2015-11-16 13:48:39 -0800290 private void addJsonSchemaDefinition(ObjectNode definitions, DocletTag tag) {
291 File definitionsDirectory = new File(srcDirectory + "/src/main/resources/definitions");
292 if (tag != null) {
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700293 tag.getParameters().forEach(param -> {
andreafaa2c4b2015-11-16 13:48:39 -0800294 try {
295 File config = new File(definitionsDirectory.getAbsolutePath() + "/"
296 + param + ".json");
Andrea Campanella82baf6b2015-12-14 10:23:37 -0800297 definitions.putPOJO(param, jsonParser.parse(new FileReader(config)));
andreafaa2c4b2015-11-16 13:48:39 -0800298 } catch (IOException e) {
HIGUCHI Yuta547c2052016-02-23 00:08:13 -0800299 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);
andreafaa2c4b2015-11-16 13:48:39 -0800303 }
304 });
305
306 }
307 }
308
Sahil Lele372d1f32015-07-31 15:01:41 -0700309 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 Vachuska0fa2aa12015-08-18 12:53:04 -0700333 // Temporary solution to add responses to a method
andreafaa2c4b2015-11-16 13:48:39 -0800334 private void addResponses(ObjectNode methodNode, DocletTag tag, boolean responseJson) {
Sahil Lele372d1f32015-07-31 15:01:41 -0700335 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);
andreafaa2c4b2015-11-16 13:48:39 -0800341 if (tag != null && responseJson) {
342 ObjectNode schema = mapper.createObjectNode();
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700343 tag.getParameters().forEach(
andreafaa2c4b2015-11-16 13:48:39 -0800344 param -> schema.put("$ref", "#/definitions/" + param));
345 success.set("schema", schema);
346 }
Sahil Lele372d1f32015-07-31 15:01:41 -0700347
348 ObjectNode defaultObj = mapper.createObjectNode();
349 defaultObj.put("description", "Unexpected error");
350 responses.set("default", defaultObj);
351 }
352
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700353 // Checks if the annotations has a value of JSON and returns the string
354 // that Swagger requires
Sahil Lele372d1f32015-07-31 15:01:41 -0700355 private String getIOType(JavaAnnotation annotation) {
356 if (annotation.getNamedParameter("value").toString().equals(JSON)) {
357 return "application/json";
Andrea Campanella260645b2015-12-06 10:24:18 -0800358 } else if (annotation.getNamedParameter("value").toString().equals(OCTET_STREAM)) {
andrea863201a2015-11-24 13:18:30 -0800359 return "application/octet_stream";
Sahil Lele372d1f32015-07-31 15:01:41 -0700360 }
361 return "";
362 }
363
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700364 // If the annotation has a Path tag, returns the value with leading and
365 // trailing double quotes and slash removed.
Sahil Lele372d1f32015-07-31 15:01:41 -0700366 private String getPath(JavaAnnotation annotation) {
367 String path = annotation.getNamedParameter("value").toString();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700368 return path == null ? null : path.replaceAll("(^[\\\"/]*|[/\\\"]*$)", "");
Sahil Lele372d1f32015-07-31 15:01:41 -0700369 }
370
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700371 // Processes parameters of javaMethod and enters the proper key-values into the methodNode
andreafaa2c4b2015-11-16 13:48:39 -0800372 private void processParameters(JavaMethod javaMethod, ObjectNode methodNode, String method, DocletTag tag) {
Sahil Lele372d1f32015-07-31 15:01:41 -0700373 ArrayNode parameters = mapper.createArrayNode();
374 methodNode.set("parameters", parameters);
375 boolean required = true;
376
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700377 for (JavaParameter javaParameter : javaMethod.getParameters()) {
Sahil Lele372d1f32015-07-31 15:01:41 -0700378 ObjectNode individualParameterNode = mapper.createObjectNode();
379 Optional<JavaAnnotation> optional = javaParameter.getAnnotations().stream().filter(
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700380 annotation -> annotation.getType().getName().equals(PATH_PARAM) ||
381 annotation.getType().getName().equals(QUERY_PARAM)).findAny();
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800382 JavaAnnotation pathType = optional.orElse(null);
Sahil Lele372d1f32015-07-31 15:01:41 -0700383
384 String annotationName = javaParameter.getName();
385
386
387 if (pathType != null) { //the parameter is a path or query parameter
388 individualParameterNode.put("name",
andreafaa2c4b2015-11-16 13:48:39 -0800389 pathType.getNamedParameter("value")
390 .toString().replace("\"", ""));
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700391 if (pathType.getType().getName().equals(PATH_PARAM)) {
Sahil Lele372d1f32015-07-31 15:01:41 -0700392 individualParameterNode.put("in", "path");
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700393 } else if (pathType.getType().getName().equals(QUERY_PARAM)) {
Sahil Lele372d1f32015-07-31 15:01:41 -0700394 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
andreafaa2c4b2015-11-16 13:48:39 -0800401 // 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 SHIMIZUa09e1bb2016-08-01 14:25:25 -0700406 tag.getParameters().forEach(param -> {
andreafaa2c4b2015-11-16 13:48:39 -0800407 schema.put("$ref", "#/definitions/" + param);
408 });
409 individualParameterNode.set("schema", schema);
410 }
Sahil Lele372d1f32015-07-31 15:01:41 -0700411 }
412 for (DocletTag p : javaMethod.getTagsByName("param")) {
413 if (p.getValue().contains(annotationName)) {
Andrea Campanella260645b2015-12-06 10:24:18 -0800414 String description = "";
415 if (p.getValue().split(" ", 2).length >= 2) {
416 description = p.getValue().split(" ", 2)[1].trim();
Sahil Lele372d1f32015-07-31 15:01:41 -0700417 if (description.contains("optional")) {
418 required = false;
419 }
Andrea Campanella260645b2015-12-06 10:24:18 -0800420 } 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 Lele372d1f32015-07-31 15:01:41 -0700427 }
Andrea Campanella260645b2015-12-06 10:24:18 -0800428 individualParameterNode.put("description", description);
Sahil Lele372d1f32015-07-31 15:01:41 -0700429 }
430 }
431 individualParameterNode.put("required", required);
432 parameters.add(individualParameterNode);
433 }
434 }
435
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700436 // Returns the Swagger specified strings for the type of a parameter
Sahil Lele372d1f32015-07-31 15:01:41 -0700437 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 Vachuska0fa2aa12015-08-18 12:53:04 -0700454 // 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 Lele372d1f32015-07-31 15:01:41 -0700466 }
467 }
468
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700469 // 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 Lele372d1f32015-07-31 15:01:41 -0700491 private String setNickname(String method, String path) {
492 if (!path.equals("")) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700493 return (method + path.replace('/', '_').replace("{", "").replace("}", "")).toLowerCase();
Sahil Lele372d1f32015-07-31 15:01:41 -0700494 } else {
495 return method.toLowerCase();
496 }
497 }
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700498
499 private String shortText(String comment) {
500 int i = comment.indexOf('.');
501 return i > 0 ? comment.substring(0, i) : comment;
502 }
503
Sahil Lele372d1f32015-07-31 15:01:41 -0700504}