forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiManager.java
More file actions
241 lines (216 loc) · 9.03 KB
/
ApiManager.java
File metadata and controls
241 lines (216 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package act.apidoc;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import static act.controller.Controller.Util.renderJson;
import act.Act;
import act.apidoc.Endpoint.ParamInfo;
import act.apidoc.javadoc.Javadoc;
import act.apidoc.javadoc.JavadocBlockTag;
import act.apidoc.javadoc.JavadocParser;
import act.app.*;
import act.app.event.SysEventId;
import act.app.util.NamedPort;
import act.conf.AppConfig;
import act.handler.RequestHandler;
import act.handler.RequestHandlerBase;
import act.handler.builtin.ResourceGetter;
import act.handler.builtin.controller.RequestHandlerProxy;
import act.route.Router;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.expr.AnnotationExpr;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.logging.LogManager;
import org.osgl.logging.Logger;
import org.osgl.util.C;
import org.osgl.util.IO;
import org.osgl.util.S;
import java.util.*;
/**
* Keep track endpoints defined in the system
*/
public class ApiManager extends AppServiceBase<ApiManager> {
static final Logger LOGGER = LogManager.get(ApiManager.class);
/**
* The {@link Endpoint} defined in the system
*/
SortedSet<Endpoint> endpoints = new TreeSet<>();
public ApiManager(final App app) {
super(app);
if (!app.config().apiDocEnabled()) {
return;
}
app.jobManager().alongWith(SysEventId.POST_START, "compile-api-book", new Runnable() {
@Override
public void run() {
load(app);
}
});
app.router().addMapping(H.Method.GET, "/~/apibook/endpoint", new GetEndpointsHandler(this));
ResourceGetter apidocHandler = new ResourceGetter("asset/~act/apibook/index.html");
app.router().addMapping(H.Method.GET, "/~/apibook", apidocHandler);
app.router().addMapping(H.Method.GET, "/~/apidoc", apidocHandler);
}
@Override
protected void releaseResources() {
endpoints.clear();
}
public void load(App app) {
LOGGER.info("start compiling API book");
Router router = app.router();
AppConfig config = app.config();
Set<Class> controllerClasses = new HashSet<>();
load(router, null, config, controllerClasses);
for (NamedPort port : app.config().namedPorts()) {
router = app.router(port);
load(router, port, config, controllerClasses);
}
if (Act.isDev()) {
exploreDescriptions(controllerClasses);
}
LOGGER.info("API book compiled");
}
private void load(Router router, NamedPort port, AppConfig config, final Set<Class> controllerClasses) {
final int portNumber = null == port ? config.httpExternalPort() : port.port();
final boolean isDev = Act.isDev();
final boolean hideBuiltIn = app().config().isHideBuiltInEndpointsInApiDoc();
router.accept(new Router.Visitor() {
@Override
public void visit(H.Method method, String path, RequestHandler handler) {
if (showEndpoint(path, handler)) {
Endpoint endpoint = new Endpoint(portNumber, method, path, handler);
endpoints.add(endpoint);
if (isDev) {
controllerClasses.add(endpoint.controllerClass());
}
}
}
private boolean showEndpoint(String path, RequestHandler handler) {
return (handler instanceof RequestHandlerProxy)
&& !(hideBuiltIn && path.startsWith("/~/"));
}
});
}
private void exploreDescriptions(Set<Class> controllerClasses) {
DevModeClassLoader cl = $.cast(Act.app().classLoader());
Map<String, Javadoc> methodJavaDocs = new HashMap<>();
for (Class controllerClass: controllerClasses) {
Source src = cl.source(controllerClass);
if (null == src) {
continue;
}
try {
CompilationUnit compilationUnit = JavaParser.parse(IO.reader(src.code()), true);
List<TypeDeclaration> types = compilationUnit.getTypes();
for (TypeDeclaration type : types) {
if (type instanceof ClassOrInterfaceDeclaration) {
exploreDeclaration((ClassOrInterfaceDeclaration) type, methodJavaDocs, "");
}
}
} catch (Exception e) {
LOGGER.warn(e, "error parsing source for " + controllerClass);
}
}
for (Endpoint endpoint : endpoints) {
Javadoc javadoc = methodJavaDocs.get(endpoint.getId());
if (null != javadoc) {
String desc = javadoc.getDescription().toText();
if (S.notBlank(desc)) {
endpoint.setDescription(desc);
}
List<ParamInfo> params = endpoint.getParams();
if (params.isEmpty()) {
continue;
}
Map<String, ParamInfo> paramLookup = new HashMap<>();
for (ParamInfo param : params) {
paramLookup.put(param.getName(), param);
}
List<JavadocBlockTag> blockTags = javadoc.getBlockTags();
for (JavadocBlockTag tag : blockTags) {
if ("param".equals(tag.getTagName())) {
String paramName = tag.getName().get();
ParamInfo paramInfo = paramLookup.get(paramName);
if (null != paramInfo) {
paramInfo.setDescription(tag.getContent().toText());
}
}
}
}
}
}
private static final Set<String> actionAnnotations = C.set("Action", "GetAction", "PostAction", "PutAction", "DeleteAction");
private void exploreDeclaration(ClassOrInterfaceDeclaration classDeclaration, Map<String, Javadoc> methodJavaDocs, String prefix) {
String className = classDeclaration.getName();
String newPrefix = S.blank(prefix) ? className : S.concat(prefix, ".", className);
for (Node node : classDeclaration.getChildrenNodes()) {
if (node instanceof ClassOrInterfaceDeclaration) {
exploreDeclaration((ClassOrInterfaceDeclaration) node, methodJavaDocs, newPrefix);
} else if (node instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) node;
List<AnnotationExpr> annoList = methodDeclaration.getAnnotations();
boolean needJavadoc = false;
if (null != annoList && !annoList.isEmpty()) {
for (AnnotationExpr anno : annoList) {
String annoName = anno.getName().getName();
if (actionAnnotations.contains(annoName)) {
needJavadoc = true;
break;
}
}
}
if (!needJavadoc) {
continue;
}
Comment comment = methodDeclaration.getComment();
if (!(comment instanceof JavadocComment)) {
continue;
}
JavadocComment javadocComment = (JavadocComment) comment;
Javadoc javadoc = JavadocParser.parse(javadocComment);
methodJavaDocs.put(S.concat(newPrefix, ".", methodDeclaration.getName()), javadoc);
}
}
}
private class GetEndpointsHandler extends RequestHandlerBase {
private ApiManager api;
public GetEndpointsHandler(ApiManager api) {
this.api = api;
}
@Override
public void handle(ActionContext context) {
renderJson(api.endpoints).apply(context.req(), context.prepareRespForResultEvaluation());
}
@Override
public void prepareAuthentication(ActionContext context) {
}
@Override
public String toString() {
return "API doc handler";
}
}
}