1 package net.technearts.rip;
2
3 import static java.util.Objects.hash;
4 import static spark.route.HttpMethod.connect;
5 import static spark.route.HttpMethod.delete;
6 import static spark.route.HttpMethod.get;
7 import static spark.route.HttpMethod.head;
8 import static spark.route.HttpMethod.options;
9 import static spark.route.HttpMethod.patch;
10 import static spark.route.HttpMethod.post;
11 import static spark.route.HttpMethod.put;
12 import static spark.route.HttpMethod.trace;
13
14 import java.util.Objects;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import com.google.common.collect.ComparisonChain;
20
21 import freemarker.template.Configuration;
22 import spark.Route;
23 import spark.TemplateEngine;
24 import spark.TemplateViewRoute;
25 import spark.route.HttpMethod;
26 import spark.template.freemarker.FreeMarkerEngine;
27
28
29
30
31 public class RipRoute implements Comparable<RipRoute>, AutoCloseable {
32 private static final Logger LOG = LoggerFactory.getLogger(RipRoute.class);
33 private static final Configuration CFG = FreemarkerConfiguration
34 .getDefaultConfiguration();
35 private final RipServer ripServer;
36 private HttpMethod method;
37 private String path;
38 Route route;
39 TemplateViewRoute templateRoute;
40
41 RipRoute(final RipServer ripServer) {
42 this.ripServer = ripServer;
43 }
44
45 @Override
46 public void close() {
47
48 }
49
50 @Override
51 public int compareTo(final RipRoute that) {
52 return ComparisonChain.start().compare(ripServer, that.ripServer)
53 .compare(method, that.method).compare(path, that.path).result();
54 }
55
56
57
58
59
60
61
62
63 public RipResponseBuilder connect(final String path) {
64 return create(path, connect);
65 }
66
67 private RipResponseBuilder create(final String path,
68 final HttpMethod method) {
69 this.method = method;
70 this.path = path;
71 return new RipResponseBuilder(this);
72 }
73
74 void createMethod() {
75 switch (getMethod()) {
76 case connect:
77 ripServer.service.connect(path, route);
78 break;
79 case delete:
80 ripServer.service.delete(path, route);
81 break;
82 case get:
83 ripServer.service.get(path, route);
84 break;
85 case head:
86 ripServer.service.head(path, route);
87 break;
88 case options:
89 ripServer.service.options(path, route);
90 break;
91 case patch:
92 ripServer.service.patch(path, route);
93 break;
94 case post:
95 ripServer.service.post(path, route);
96 break;
97 case put:
98 ripServer.service.put(path, route);
99 break;
100 case trace:
101 ripServer.service.trace(path, route);
102 break;
103 case after:
104 case afterafter:
105 case before:
106 case unsupported:
107 default:
108 LOG.error("A opção {} não é suportada!", method);
109 break;
110 }
111 }
112
113 void createTemplateMethod() {
114
115
116 final TemplateEngine templateEngine = new FreeMarkerEngine(CFG);
117 switch (getMethod()) {
118 case connect:
119 ripServer.service.connect(path, templateRoute, templateEngine);
120 break;
121 case delete:
122 ripServer.service.delete(path, templateRoute, templateEngine);
123 break;
124 case get:
125 ripServer.service.get(path, templateRoute, templateEngine);
126 break;
127 case head:
128 ripServer.service.head(path, templateRoute, templateEngine);
129 break;
130 case options:
131 ripServer.service.options(path, templateRoute, templateEngine);
132 break;
133 case patch:
134 ripServer.service.patch(path, templateRoute, templateEngine);
135 break;
136 case post:
137 ripServer.service.post(path, templateRoute, templateEngine);
138 break;
139 case put:
140 ripServer.service.put(path, templateRoute, templateEngine);
141 break;
142 case trace:
143 ripServer.service.trace(path, templateRoute, templateEngine);
144 break;
145 case after:
146 case afterafter:
147 case before:
148 case unsupported:
149 default:
150 LOG.error("A opção {} não é suportada!", getMethod());
151 break;
152 }
153 }
154
155
156
157
158
159
160
161
162 public RipResponseBuilder delete(final String path) {
163 return create(path, delete);
164 }
165
166 @Override
167 public boolean equals(final Object object) {
168 if (object instanceof RipRoute) {
169 final RipRoute that = (RipRoute) object;
170 return Objects.equals(ripServer, that.ripServer)
171 && Objects.equals(method, that.method)
172 && Objects.equals(path, that.path);
173 }
174 return false;
175 }
176
177
178
179
180
181
182
183
184 public RipResponseBuilder get(final String path) {
185 return create(path, get);
186 }
187
188 HttpMethod getMethod() {
189 return method;
190 }
191
192 String getPath() {
193 return path;
194 }
195
196 RipServer getRipServer() {
197 return ripServer;
198 }
199
200 @Override
201 public int hashCode() {
202 return hash(ripServer, method, path);
203 }
204
205
206
207
208
209
210
211
212 public RipResponseBuilder head(final String path) {
213 return create(path, head);
214 }
215
216
217
218
219
220
221
222
223 public RipResponseBuilder options(final String path) {
224 return create(path, options);
225 }
226
227
228
229
230
231
232
233
234 public RipResponseBuilder patch(final String path) {
235 return create(path, patch);
236 }
237
238
239
240
241
242
243
244
245 public RipResponseBuilder post(final String path) {
246 return create(path, post);
247 }
248
249
250
251
252
253
254
255
256 public RipResponseBuilder put(final String path) {
257 return create(path, put);
258 }
259
260
261
262
263 public void reset() {
264 ripServer.reset();
265 }
266
267 @Override
268 public String toString() {
269 return String.format("RipRoute %s => %s %s]", ripServer, method, path);
270 }
271
272
273
274
275
276
277
278
279 public RipResponseBuilder trace(final String path) {
280 return create(path, trace);
281 }
282
283 }