forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouteSet.java
More file actions
241 lines (216 loc) · 6 KB
/
RouteSet.java
File metadata and controls
241 lines (216 loc) · 6 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
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import static java.util.Collections.EMPTY_LIST;
import static java.util.Optional.ofNullable;
/**
* Give you access to all routes created inside a {@link Router#path(String, Runnable)}.
* Allow to globally apply attributes or metadata.
*
* @author edgar
* @since 2.7.3
*/
public class RouteSet {
private List<Route> routes;
private List<String> tags = EMPTY_LIST;
private String summary;
private String description;
/**
* Sub-routes. Always empty except when used it from {@link Router#path(String, Runnable)} or
* {@link Router#routes(Runnable)}.
*
* @return Sub-routes.
*/
public @Nonnull List<Route> getRoutes() {
return routes;
}
/**
* Set sub-routes.
*
* @param routes Sub-routes.
* @return This route.
*/
public @Nonnull RouteSet setRoutes(@Nonnull List<Route> routes) {
this.routes = routes;
return this;
}
/**
* Add one or more response types (format) produces by this route.
*
* @param produces Produce types.
* @return This route.
*/
public @Nonnull RouteSet produces(@Nonnull MediaType... produces) {
return setProduces(Arrays.asList(produces));
}
/**
* Add one or more response types (format) produces by this route.
*
* @param produces Produce types.
* @return This route.
*/
public @Nonnull RouteSet setProduces(@Nonnull Collection<MediaType> produces) {
routes.forEach(it -> {
if (it.getProduces().isEmpty()) {
it.setProduces(produces);
}
});
return this;
}
/**
* Add one or more request types (format) consumed by this route.
*
* @param consumes Consume types.
* @return This route.
*/
public @Nonnull RouteSet consumes(@Nonnull MediaType... consumes) {
return setConsumes(Arrays.asList(consumes));
}
/**
* Add one or more request types (format) consumed by this route.
*
* @param consumes Consume types.
* @return This route.
*/
public @Nonnull RouteSet setConsumes(@Nonnull Collection<MediaType> consumes) {
routes.forEach(it -> {
if (it.getConsumes().isEmpty()) {
it.setConsumes(consumes);
}
});
return this;
}
/**
* Add one or more attributes applied to this route.
*
* @param attributes .
* @return This route.
*/
public @Nonnull RouteSet setAttributes(@Nonnull Map<String, Object> attributes) {
routes.forEach(it -> attributes.forEach((k, v) -> it.getAttributes().putIfAbsent(k, v)));
return this;
}
/**
* Add one or more attributes applied to this route.
*
* @param name attribute name
* @param value attribute value
* @return This route.
*/
public @Nonnull RouteSet attribute(@Nonnull String name, @Nonnull Object value) {
routes.forEach(it -> it.getAttributes().putIfAbsent(name, value));
return this;
}
/**
* Set executor key. The route is going to use the given key to fetch an executor. Possible values
* are:
*
* - <code>null</code>: no specific executor, uses the default Jooby logic to choose one, based
* on the value of {@link ExecutionMode};
* - <code>worker</code>: use the executor provided by the server.
* - <code>arbitrary name</code>: use an named executor which as registered using
* {@link Router#executor(String, Executor)}.
*
* @param executorKey Executor key.
* @return This route.
*/
public @Nonnull RouteSet setExecutorKey(@Nullable String executorKey) {
routes.forEach(it -> it.setExecutorKey(ofNullable(it.getExecutorKey()).orElse(executorKey)));
return this;
}
/**
* Route tags.
*
* @return Route tags.
*/
public @Nonnull List<String> getTags() {
return tags;
}
/**
* Tag this route. Tags are used for documentation purpose from openAPI generator.
*
* @param tags Tags.
* @return This route.
*/
public @Nonnull RouteSet setTags(@Nonnull List<String> tags) {
if (this.tags == EMPTY_LIST) {
this.tags = new ArrayList<>();
}
routes.forEach(it -> tags.forEach(it::addTag));
return this;
}
/**
* Tag this route. Tags are used for documentation purpose from openAPI generator.
*
* @param tags Tags.
* @return This route.
*/
public @Nonnull RouteSet tags(@Nonnull String... tags) {
return setTags(Arrays.asList(tags));
}
/**
* Route summary useful for documentation purpose from openAPI generator.
*
* @return Summary.
*/
public @Nullable String getSummary() {
return summary;
}
/**
* Route summary useful for documentation purpose from openAPI generator.
*
* @param summary Summary.
* @return This route.
*/
public @Nonnull RouteSet summary(@Nullable String summary) {
return setSummary(summary);
}
/**
* Route summary useful for documentation purpose from openAPI generator.
*
* @param summary Summary.
* @return This route.
*/
public @Nonnull RouteSet setSummary(@Nullable String summary) {
this.summary = summary;
return this;
}
/**
* Route description useful for documentation purpose from openAPI generator.
*
* @return Route description.
*/
public @Nullable String getDescription() {
return description;
}
/**
* Route description useful for documentation purpose from openAPI generator.
*
* @param description Description.
* @return This route.
*/
public @Nonnull RouteSet setDescription(@Nullable String description) {
this.description = description;
return this;
}
/**
* Route description useful for documentation purpose from openAPI generator.
*
* @param description Description.
* @return This route.
*/
public @Nonnull RouteSet description(@Nullable String description) {
return setDescription(description);
}
}