blob: 4618cd06e3bb2d6e041869a238375eecf9272d55 [file] [log] [blame]
Sahil Lele372d1f32015-07-31 15:01:41 -07001/*
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 */
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;
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070021import com.google.common.io.ByteStreams;
22import com.google.common.io.Files;
Andrea Campanella82baf6b2015-12-14 10:23:37 -080023import com.google.gson.JsonParser;
Sahil Lele372d1f32015-07-31 15:01:41 -070024import com.thoughtworks.qdox.JavaProjectBuilder;
25import com.thoughtworks.qdox.model.DocletTag;
26import com.thoughtworks.qdox.model.JavaAnnotation;
27import com.thoughtworks.qdox.model.JavaClass;
28import com.thoughtworks.qdox.model.JavaMethod;
29import com.thoughtworks.qdox.model.JavaParameter;
30import com.thoughtworks.qdox.model.JavaType;
31import org.apache.maven.plugin.AbstractMojo;
32import org.apache.maven.plugin.MojoExecutionException;
33import org.apache.maven.plugins.annotations.LifecyclePhase;
34import org.apache.maven.plugins.annotations.Mojo;
35import org.apache.maven.plugins.annotations.Parameter;
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070036import org.apache.maven.project.MavenProject;
Sahil Lele372d1f32015-07-31 15:01:41 -070037
38import java.io.File;
Andrea Campanella82baf6b2015-12-14 10:23:37 -080039import java.io.FileReader;
Sahil Lele372d1f32015-07-31 15:01:41 -070040import java.io.FileWriter;
41import java.io.IOException;
42import java.io.PrintWriter;
43import java.util.HashMap;
44import java.util.Map;
45import java.util.Optional;
46
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070047import static com.google.common.base.Strings.isNullOrEmpty;
48
Sahil Lele372d1f32015-07-31 15:01:41 -070049/**
50 * Produces ONOS Swagger api-doc.
51 */
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070052@Mojo(name = "swagger", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
Sahil Lele372d1f32015-07-31 15:01:41 -070053public class OnosSwaggerMojo extends AbstractMojo {
54 private final ObjectMapper mapper = new ObjectMapper();
Andrea Campanella82baf6b2015-12-14 10:23:37 -080055 private final JsonParser jsonParser = new JsonParser();
Sahil Lele372d1f32015-07-31 15:01:41 -070056
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070057 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 Lele372d1f32015-07-31 15:01:41 -070061 private static final String PATH = "javax.ws.rs.Path";
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070062 private static final String PATH_PARAM = "javax.ws.rs.PathParam";
63 private static final String QUERY_PARAM = "javax.ws.rs.QueryParam";
Sahil Lele372d1f32015-07-31 15:01:41 -070064 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";
andrea863201a2015-11-24 13:18:30 -080071 private static final String OCTET_STREAM = "MediaType.APPLICATION_OCTET_STREAM";
Sahil Lele372d1f32015-07-31 15:01:41 -070072
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 Vachuska0fa2aa12015-08-18 12:53:04 -070082 @Parameter(defaultValue = "${project.build.directory}")
Sahil Lele372d1f32015-07-31 15:01:41 -070083 protected File dstDirectory;
84
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070085 /**
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 Lele372d1f32015-07-31 15:01:41 -0700122 @Override
123 public void execute() throws MojoExecutionException {
Sahil Lele372d1f32015-07-31 15:01:41 -0700124 try {
125 JavaProjectBuilder builder = new JavaProjectBuilder();
126 builder.addSourceTree(new File(srcDirectory, "src/main/java"));
127
128 ObjectNode root = initializeRoot();
Sahil Lele372d1f32015-07-31 15:01:41 -0700129 ArrayNode tags = mapper.createArrayNode();
Sahil Lele372d1f32015-07-31 15:01:41 -0700130 ObjectNode paths = mapper.createObjectNode();
andreafaa2c4b2015-11-16 13:48:39 -0800131 ObjectNode definitions = mapper.createObjectNode();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700132
133 root.set("tags", tags);
Sahil Lele372d1f32015-07-31 15:01:41 -0700134 root.set("paths", paths);
andreafaa2c4b2015-11-16 13:48:39 -0800135 root.set("definitions", definitions);
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700136
andreafaa2c4b2015-11-16 13:48:39 -0800137 builder.getClasses().forEach(jc -> processClass(jc, paths, tags, definitions));
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700138
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 Lele372d1f32015-07-31 15:01:41 -0700150 } catch (Exception e) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700151 getLog().warn("Unable to generate ONOS REST API documentation", e);
Sahil Lele372d1f32015-07-31 15:01:41 -0700152 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 Lele372d1f32015-07-31 15:01:41 -0700162
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700163 root.put("basePath", webContext);
164 info.put("version", apiVersion);
165 info.put("title", apiTitle);
166 info.put("description", apiDescription);
Sahil Lele372d1f32015-07-31 15:01:41 -0700167
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
andreafaa2c4b2015-11-16 13:48:39 -0800181 void processClass(JavaClass javaClass, ObjectNode paths, ArrayNode tags, ObjectNode definitions) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700182 // If the class does not have a Path tag then ignore it
183 JavaAnnotation annotation = getPathAnnotation(javaClass);
Sahil Lele372d1f32015-07-31 15:01:41 -0700184 if (annotation == null) {
185 return;
186 }
187
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700188 String path = getPath(annotation);
189 if (path == null) {
190 return;
Sahil Lele372d1f32015-07-31 15:01:41 -0700191 }
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700192
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 Lele372d1f32015-07-31 15:01:41 -0700199 if (javaClass.getComment() != null) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700200 tagObject.put("description", shortText(javaClass.getComment()));
Sahil Lele372d1f32015-07-31 15:01:41 -0700201 }
202 tags.add(tagObject);
203
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700204 // Create an array node add to all methods from this class.
Sahil Lele372d1f32015-07-31 15:01:41 -0700205 ArrayNode tagArray = mapper.createArrayNode();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700206 tagArray.add(tagPath);
Sahil Lele372d1f32015-07-31 15:01:41 -0700207
andreafaa2c4b2015-11-16 13:48:39 -0800208 processAllMethods(javaClass, resourcePath, paths, tagArray, definitions);
Sahil Lele372d1f32015-07-31 15:01:41 -0700209 }
210
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700211 private JavaAnnotation getPathAnnotation(JavaClass javaClass) {
212 Optional<JavaAnnotation> optional = javaClass.getAnnotations()
213 .stream().filter(a -> a.getType().getName().equals(PATH)).findAny();
214 return optional.isPresent() ? optional.get() : null;
215 }
216
Sahil Lele372d1f32015-07-31 15:01:41 -0700217 // 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,
andreafaa2c4b2015-11-16 13:48:39 -0800220 ObjectNode paths, ArrayNode tagArray, ObjectNode definitions) {
Sahil Lele372d1f32015-07-31 15:01:41 -0700221 // 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();
andreafaa2c4b2015-11-16 13:48:39 -0800230 processRestMethod(javaMethod, method, pathMap, resourcePath, tagArray, definitions);
Sahil Lele372d1f32015-07-31 15:01:41 -0700231 }
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 Vachuska0fa2aa12015-08-18 12:53:04 -0700244 Map<String, ObjectNode> pathMap,
andreafaa2c4b2015-11-16 13:48:39 -0800245 String resourcePath, ArrayNode tagArray, ObjectNode definitions) {
Sahil Lele372d1f32015-07-31 15:01:41 -0700246 String fullPath = resourcePath, consumes = "", produces = "",
247 comment = javaMethod.getComment();
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800248 DocletTag tag = javaMethod.getTagByName("onos.rsModel");
Sahil Lele372d1f32015-07-31 15:01:41 -0700249 for (JavaAnnotation annotation : javaMethod.getAnnotations()) {
250 String name = annotation.getType().getName();
251 if (name.equals(PATH)) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700252 fullPath = resourcePath + "/" + getPath(annotation);
253 fullPath = fullPath.replaceFirst("^//", "/");
Sahil Lele372d1f32015-07-31 15:01:41 -0700254 }
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);
andreafaa2c4b2015-11-16 13:48:39 -0800266 addJsonSchemaDefinition(definitions, tag);
andreafaa2c4b2015-11-16 13:48:39 -0800267
268 processParameters(javaMethod, methodNode, method, tag);
Sahil Lele372d1f32015-07-31 15:01:41 -0700269
270 processConsumesProduces(methodNode, "consumes", consumes);
271 processConsumesProduces(methodNode, "produces", produces);
andreafaa2c4b2015-11-16 13:48:39 -0800272 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 Lele372d1f32015-07-31 15:01:41 -0700278
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
andreafaa2c4b2015-11-16 13:48:39 -0800289 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 Campanella82baf6b2015-12-14 10:23:37 -0800296 definitions.putPOJO(param, jsonParser.parse(new FileReader(config)));
andreafaa2c4b2015-11-16 13:48:39 -0800297 } catch (IOException e) {
298 e.printStackTrace();
299 }
300 });
301
302 }
303 }
304
Sahil Lele372d1f32015-07-31 15:01:41 -0700305 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 Vachuska0fa2aa12015-08-18 12:53:04 -0700329 // Temporary solution to add responses to a method
andreafaa2c4b2015-11-16 13:48:39 -0800330 private void addResponses(ObjectNode methodNode, DocletTag tag, boolean responseJson) {
Sahil Lele372d1f32015-07-31 15:01:41 -0700331 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);
andreafaa2c4b2015-11-16 13:48:39 -0800337 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 Lele372d1f32015-07-31 15:01:41 -0700343
344 ObjectNode defaultObj = mapper.createObjectNode();
345 defaultObj.put("description", "Unexpected error");
346 responses.set("default", defaultObj);
347 }
348
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700349 // Checks if the annotations has a value of JSON and returns the string
350 // that Swagger requires
Sahil Lele372d1f32015-07-31 15:01:41 -0700351 private String getIOType(JavaAnnotation annotation) {
352 if (annotation.getNamedParameter("value").toString().equals(JSON)) {
353 return "application/json";
Andrea Campanella260645b2015-12-06 10:24:18 -0800354 } else if (annotation.getNamedParameter("value").toString().equals(OCTET_STREAM)) {
andrea863201a2015-11-24 13:18:30 -0800355 return "application/octet_stream";
Sahil Lele372d1f32015-07-31 15:01:41 -0700356 }
357 return "";
358 }
359
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700360 // If the annotation has a Path tag, returns the value with leading and
361 // trailing double quotes and slash removed.
Sahil Lele372d1f32015-07-31 15:01:41 -0700362 private String getPath(JavaAnnotation annotation) {
363 String path = annotation.getNamedParameter("value").toString();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700364 return path == null ? null : path.replaceAll("(^[\\\"/]*|[/\\\"]*$)", "");
Sahil Lele372d1f32015-07-31 15:01:41 -0700365 }
366
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700367 // Processes parameters of javaMethod and enters the proper key-values into the methodNode
andreafaa2c4b2015-11-16 13:48:39 -0800368 private void processParameters(JavaMethod javaMethod, ObjectNode methodNode, String method, DocletTag tag) {
Sahil Lele372d1f32015-07-31 15:01:41 -0700369 ArrayNode parameters = mapper.createArrayNode();
370 methodNode.set("parameters", parameters);
371 boolean required = true;
372
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700373 for (JavaParameter javaParameter : javaMethod.getParameters()) {
Sahil Lele372d1f32015-07-31 15:01:41 -0700374 ObjectNode individualParameterNode = mapper.createObjectNode();
375 Optional<JavaAnnotation> optional = javaParameter.getAnnotations().stream().filter(
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700376 annotation -> annotation.getType().getName().equals(PATH_PARAM) ||
377 annotation.getType().getName().equals(QUERY_PARAM)).findAny();
Sahil Lele372d1f32015-07-31 15:01:41 -0700378 JavaAnnotation pathType = optional.isPresent() ? optional.get() : null;
379
380 String annotationName = javaParameter.getName();
381
382
383 if (pathType != null) { //the parameter is a path or query parameter
384 individualParameterNode.put("name",
andreafaa2c4b2015-11-16 13:48:39 -0800385 pathType.getNamedParameter("value")
386 .toString().replace("\"", ""));
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700387 if (pathType.getType().getName().equals(PATH_PARAM)) {
Sahil Lele372d1f32015-07-31 15:01:41 -0700388 individualParameterNode.put("in", "path");
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700389 } else if (pathType.getType().getName().equals(QUERY_PARAM)) {
Sahil Lele372d1f32015-07-31 15:01:41 -0700390 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
andreafaa2c4b2015-11-16 13:48:39 -0800397 // 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 Lele372d1f32015-07-31 15:01:41 -0700407 }
408 for (DocletTag p : javaMethod.getTagsByName("param")) {
409 if (p.getValue().contains(annotationName)) {
Andrea Campanella260645b2015-12-06 10:24:18 -0800410 String description = "";
411 if (p.getValue().split(" ", 2).length >= 2) {
412 description = p.getValue().split(" ", 2)[1].trim();
Sahil Lele372d1f32015-07-31 15:01:41 -0700413 if (description.contains("optional")) {
414 required = false;
415 }
Andrea Campanella260645b2015-12-06 10:24:18 -0800416 } 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 Lele372d1f32015-07-31 15:01:41 -0700423 }
Andrea Campanella260645b2015-12-06 10:24:18 -0800424 individualParameterNode.put("description", description);
Sahil Lele372d1f32015-07-31 15:01:41 -0700425 }
426 }
427 individualParameterNode.put("required", required);
428 parameters.add(individualParameterNode);
429 }
430 }
431
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700432 // Returns the Swagger specified strings for the type of a parameter
Sahil Lele372d1f32015-07-31 15:01:41 -0700433 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 Vachuska0fa2aa12015-08-18 12:53:04 -0700450 // 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 Lele372d1f32015-07-31 15:01:41 -0700462 }
463 }
464
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700465 // 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 Lele372d1f32015-07-31 15:01:41 -0700487 private String setNickname(String method, String path) {
488 if (!path.equals("")) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700489 return (method + path.replace('/', '_').replace("{", "").replace("}", "")).toLowerCase();
Sahil Lele372d1f32015-07-31 15:01:41 -0700490 } else {
491 return method.toLowerCase();
492 }
493 }
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700494
495 private String shortText(String comment) {
496 int i = comment.indexOf('.');
497 return i > 0 ? comment.substring(0, i) : comment;
498 }
499
Sahil Lele372d1f32015-07-31 15:01:41 -0700500}