|
5 | 5 | */ |
6 | 6 | package io.jooby; |
7 | 7 |
|
| 8 | +import static java.util.Optional.ofNullable; |
| 9 | + |
8 | 10 | import java.io.Serializable; |
9 | 11 | import java.lang.invoke.MethodHandle; |
10 | 12 | import java.lang.invoke.MethodHandles; |
11 | 13 | import java.lang.reflect.Method; |
12 | | -import java.util.ArrayList; |
13 | | -import java.util.Arrays; |
14 | | -import java.util.Collection; |
15 | | -import java.util.Collections; |
16 | | -import java.util.HashSet; |
17 | | -import java.util.List; |
18 | | -import java.util.Map; |
19 | | -import java.util.Optional; |
20 | | -import java.util.Set; |
21 | | -import java.util.TreeMap; |
| 14 | +import java.util.*; |
22 | 15 | import java.util.concurrent.Executor; |
| 16 | +import java.util.function.Consumer; |
| 17 | +import java.util.function.Predicate; |
23 | 18 |
|
24 | 19 | import edu.umd.cs.findbugs.annotations.NonNull; |
25 | 20 | import edu.umd.cs.findbugs.annotations.Nullable; |
@@ -455,7 +450,7 @@ public MethodHandle toMethodHandle() { |
455 | 450 |
|
456 | 451 | private Map<String, Object> attributes = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
457 | 452 |
|
458 | | - private Set<String> supportedMethod; |
| 453 | + private java.util.Set<String> supportedMethod; |
459 | 454 |
|
460 | 455 | private String executorKey; |
461 | 456 |
|
@@ -1101,4 +1096,239 @@ private Route.Handler computePipeline() { |
1101 | 1096 | } |
1102 | 1097 | return pipeline; |
1103 | 1098 | } |
| 1099 | + |
| 1100 | + /** |
| 1101 | + * Give you access to all routes created inside a {@link Router#path(String, Runnable)}. Allow |
| 1102 | + * globally applying attributes or metadata. |
| 1103 | + * |
| 1104 | + * @author edgar |
| 1105 | + * @since 2.7.3 |
| 1106 | + */ |
| 1107 | + public static class Set implements Iterable<Route> { |
| 1108 | + |
| 1109 | + private List<Route> routes; |
| 1110 | + |
| 1111 | + private List<String> tags; |
| 1112 | + |
| 1113 | + private String summary; |
| 1114 | + |
| 1115 | + private String description; |
| 1116 | + |
| 1117 | + public Set(List<Route> routes) { |
| 1118 | + this.routes = routes; |
| 1119 | + } |
| 1120 | + |
| 1121 | + /** |
| 1122 | + * Sub-routes. Always empty except when used it from {@link Router#path(String, Runnable)} or |
| 1123 | + * {@link Router#routes(Runnable)}. |
| 1124 | + * |
| 1125 | + * @return Sub-routes. |
| 1126 | + */ |
| 1127 | + public @NonNull List<Route> getRoutes() { |
| 1128 | + return routes; |
| 1129 | + } |
| 1130 | + |
| 1131 | + /** |
| 1132 | + * Set sub-routes. |
| 1133 | + * |
| 1134 | + * @param routes Sub-routes. |
| 1135 | + * @return This route. |
| 1136 | + */ |
| 1137 | + public @NonNull Set setRoutes(@NonNull List<Route> routes) { |
| 1138 | + this.routes = routes; |
| 1139 | + return this; |
| 1140 | + } |
| 1141 | + |
| 1142 | + /** |
| 1143 | + * Add one or more response types (format) produces by this route. |
| 1144 | + * |
| 1145 | + * @param produces Produce types. |
| 1146 | + * @return This route. |
| 1147 | + */ |
| 1148 | + public @NonNull Set produces(@NonNull MediaType... produces) { |
| 1149 | + return setProduces(Arrays.asList(produces)); |
| 1150 | + } |
| 1151 | + |
| 1152 | + /** |
| 1153 | + * Add one or more response types (format) produces by this route. |
| 1154 | + * |
| 1155 | + * @param produces Produce types. |
| 1156 | + * @return This route. |
| 1157 | + */ |
| 1158 | + public @NonNull Set setProduces(@NonNull Collection<MediaType> produces) { |
| 1159 | + routes.forEach( |
| 1160 | + it -> { |
| 1161 | + if (it.getProduces().isEmpty()) { |
| 1162 | + it.setProduces(produces); |
| 1163 | + } |
| 1164 | + }); |
| 1165 | + return this; |
| 1166 | + } |
| 1167 | + |
| 1168 | + /** |
| 1169 | + * Add one or more request types (format) consumed by this route. |
| 1170 | + * |
| 1171 | + * @param consumes Consume types. |
| 1172 | + * @return This route. |
| 1173 | + */ |
| 1174 | + public @NonNull Set consumes(@NonNull MediaType... consumes) { |
| 1175 | + return setConsumes(Arrays.asList(consumes)); |
| 1176 | + } |
| 1177 | + |
| 1178 | + /** |
| 1179 | + * Add one or more request types (format) consumed by this route. |
| 1180 | + * |
| 1181 | + * @param consumes Consume types. |
| 1182 | + * @return This route. |
| 1183 | + */ |
| 1184 | + public @NonNull Set setConsumes(@NonNull Collection<MediaType> consumes) { |
| 1185 | + routes.forEach( |
| 1186 | + it -> { |
| 1187 | + if (it.getConsumes().isEmpty()) { |
| 1188 | + it.setConsumes(consumes); |
| 1189 | + } |
| 1190 | + }); |
| 1191 | + return this; |
| 1192 | + } |
| 1193 | + |
| 1194 | + /** |
| 1195 | + * Add one or more attributes applied to this route. |
| 1196 | + * |
| 1197 | + * @param attributes . |
| 1198 | + * @return This route. |
| 1199 | + */ |
| 1200 | + public @NonNull Set setAttributes(@NonNull Map<String, Object> attributes) { |
| 1201 | + routes.forEach(it -> attributes.forEach((k, v) -> it.getAttributes().putIfAbsent(k, v))); |
| 1202 | + return this; |
| 1203 | + } |
| 1204 | + |
| 1205 | + /** |
| 1206 | + * Add one or more attributes applied to this route. |
| 1207 | + * |
| 1208 | + * @param name attribute name |
| 1209 | + * @param value attribute value |
| 1210 | + * @return This route. |
| 1211 | + */ |
| 1212 | + public @NonNull Set setAttribute(@NonNull String name, @NonNull Object value) { |
| 1213 | + routes.forEach(it -> it.getAttributes().putIfAbsent(name, value)); |
| 1214 | + return this; |
| 1215 | + } |
| 1216 | + |
| 1217 | + /** |
| 1218 | + * Set executor key. The route is going to use the given key to fetch an executor. Possible |
| 1219 | + * values are: |
| 1220 | + * |
| 1221 | + * <p>- <code>null</code>: no specific executor, uses the default Jooby logic to choose one, |
| 1222 | + * based on the value of {@link ExecutionMode}; - <code>worker</code>: use the executor provided |
| 1223 | + * by the server. - <code>arbitrary name</code>: use an named executor which as registered using |
| 1224 | + * {@link Router#executor(String, Executor)}. |
| 1225 | + * |
| 1226 | + * @param executorKey Executor key. |
| 1227 | + * @return This route. |
| 1228 | + */ |
| 1229 | + public @NonNull Set setExecutorKey(@Nullable String executorKey) { |
| 1230 | + routes.forEach(it -> it.setExecutorKey(ofNullable(it.getExecutorKey()).orElse(executorKey))); |
| 1231 | + return this; |
| 1232 | + } |
| 1233 | + |
| 1234 | + /** |
| 1235 | + * Route tags. |
| 1236 | + * |
| 1237 | + * @return Route tags. |
| 1238 | + */ |
| 1239 | + public @NonNull List<String> getTags() { |
| 1240 | + return tags == null ? List.of() : tags; |
| 1241 | + } |
| 1242 | + |
| 1243 | + /** |
| 1244 | + * Tag this route. Tags are used for documentation purpose from openAPI generator. |
| 1245 | + * |
| 1246 | + * @param tags Tags. |
| 1247 | + * @return This route. |
| 1248 | + */ |
| 1249 | + public @NonNull Set setTags(@NonNull List<String> tags) { |
| 1250 | + this.tags = tags; |
| 1251 | + routes.forEach(it -> tags.forEach(it::addTag)); |
| 1252 | + return this; |
| 1253 | + } |
| 1254 | + |
| 1255 | + /** |
| 1256 | + * Tag this route. Tags are used for documentation purpose from openAPI generator. |
| 1257 | + * |
| 1258 | + * @param tags Tags. |
| 1259 | + * @return This route. |
| 1260 | + */ |
| 1261 | + public @NonNull Set tags(@NonNull String... tags) { |
| 1262 | + return setTags(Arrays.asList(tags)); |
| 1263 | + } |
| 1264 | + |
| 1265 | + /** |
| 1266 | + * Route summary useful for documentation purpose from openAPI generator. |
| 1267 | + * |
| 1268 | + * @return Summary. |
| 1269 | + */ |
| 1270 | + public @Nullable String getSummary() { |
| 1271 | + return summary; |
| 1272 | + } |
| 1273 | + |
| 1274 | + /** |
| 1275 | + * Route summary useful for documentation purpose from openAPI generator. |
| 1276 | + * |
| 1277 | + * @param summary Summary. |
| 1278 | + * @return This route. |
| 1279 | + */ |
| 1280 | + public @NonNull Set summary(@Nullable String summary) { |
| 1281 | + return setSummary(summary); |
| 1282 | + } |
| 1283 | + |
| 1284 | + /** |
| 1285 | + * Route summary useful for documentation purpose from openAPI generator. |
| 1286 | + * |
| 1287 | + * @param summary Summary. |
| 1288 | + * @return This route. |
| 1289 | + */ |
| 1290 | + public @NonNull Set setSummary(@Nullable String summary) { |
| 1291 | + this.summary = summary; |
| 1292 | + return this; |
| 1293 | + } |
| 1294 | + |
| 1295 | + /** |
| 1296 | + * Route description useful for documentation purpose from openAPI generator. |
| 1297 | + * |
| 1298 | + * @return Route description. |
| 1299 | + */ |
| 1300 | + public @Nullable String getDescription() { |
| 1301 | + return description; |
| 1302 | + } |
| 1303 | + |
| 1304 | + /** |
| 1305 | + * Route description useful for documentation purpose from openAPI generator. |
| 1306 | + * |
| 1307 | + * @param description Description. |
| 1308 | + * @return This route. |
| 1309 | + */ |
| 1310 | + public @NonNull Set setDescription(@Nullable String description) { |
| 1311 | + this.description = description; |
| 1312 | + return this; |
| 1313 | + } |
| 1314 | + |
| 1315 | + /** |
| 1316 | + * Route description useful for documentation purpose from openAPI generator. |
| 1317 | + * |
| 1318 | + * @param description Description. |
| 1319 | + * @return This route. |
| 1320 | + */ |
| 1321 | + public @NonNull Set description(@Nullable String description) { |
| 1322 | + return setDescription(description); |
| 1323 | + } |
| 1324 | + |
| 1325 | + public void forEach(Predicate<Route> predicate, Consumer<? super Route> action) { |
| 1326 | + routes.stream().filter(predicate).forEach(action); |
| 1327 | + } |
| 1328 | + |
| 1329 | + @Override |
| 1330 | + public Iterator<Route> iterator() { |
| 1331 | + return routes.iterator(); |
| 1332 | + } |
| 1333 | + } |
1104 | 1334 | } |
0 commit comments