Skip to content

Commit cff1f5e

Browse files
committed
refactor+code cleanup around deferred +after&complete handlers
1 parent 2434585 commit cff1f5e

File tree

12 files changed

+98
-46
lines changed

12 files changed

+98
-46
lines changed

jooby/src/main/java/org/jooby/Deferred.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import java.util.concurrent.Callable;
2424

25+
import javaslang.CheckedFunction0;
26+
2527
/**
2628
* <h1>async request processing</h1>
2729
* A Deferred result, useful for async request processing. Application can produces a result from a
@@ -94,8 +96,8 @@
9496
* error.
9597
* </p>
9698
* <p>
97-
* Checkout the utility method {@link #resolve(Callable)} and/or {@link #run(Callable)}. Both of
98-
* them catch and handle exceptions for you.
99+
* Checkout the utility method {@link #resolve(CheckedFunction0)} and/or
100+
* {@link #run(CheckedFunction0)}. Both of them catch and handle exceptions for you.
99101
* </p>
100102
*
101103
* @author edgar
@@ -160,8 +162,7 @@ public static interface Handler {
160162
* @param initializer An initializer.
161163
*/
162164
public Deferred(final Initializer0 initializer) {
163-
requireNonNull(initializer, "Initializer is required.");
164-
this.initializer = (req, deferred) -> initializer.run(deferred);
165+
this((req, deferred) -> initializer.run(deferred));
165166
}
166167

167168
/**
@@ -220,42 +221,44 @@ public void reject(final Throwable cause) {
220221
}
221222

222223
/**
223-
* Produces a {@link Runnable} that runs the given {@link Callable} and {@link #resolve(Callable)}
224-
* or {@link #reject(Throwable)} the deferred.
224+
* Produces a {@link Runnable} that runs the given {@link Callable} and
225+
* {@link #resolve(CheckedFunction0)} or {@link #reject(Throwable)} the deferred.
225226
*
226227
* Please note, the given {@link Callable} runs in the caller thread.
227228
*
228229
* @param block Callable that produces a result.
229230
* @param <T> Resulting type.
230231
* @return This deferred as {@link Runnable}.
231232
*/
232-
public <T> Runnable run(final Callable<T> block) {
233+
public <T> Runnable run(final CheckedFunction0<T> block) {
233234
return () -> {
234235
resolve(block);
235236
};
236237
}
237238

238239
/**
239-
* Run the given {@link Callable} and {@link #resolve(Callable)} or {@link #reject(Throwable)} the
240+
* Run the given {@link Callable} and {@link #resolve(CheckedFunction0)} or
241+
* {@link #reject(Throwable)} the
240242
* deferred.
241243
*
242244
* Please note, the given {@link Callable} runs in the caller thread.
243245
*
244246
* @param block Callable that produces a result.
245247
* @param <T> Resulting type.
246248
*/
247-
public <T> void resolve(final Callable<T> block) {
249+
public <T> void resolve(final CheckedFunction0<T> block) {
248250
try {
249-
resolve(block.call());
250-
} catch (Exception ex) {
251-
reject(ex);
251+
resolve(block.apply());
252+
} catch (Throwable x) {
253+
reject(x);
252254
}
253255
}
254256

255257
/**
256258
* Setup a handler for this deferred. Application code should never call this method: INTERNAL USE
257259
* ONLY.
258260
*
261+
* @param req Current request.
259262
* @param handler A response handler.
260263
* @throws Exception If initializer fails to start.
261264
*/

jooby/src/main/java/org/jooby/Response.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,13 @@ public boolean committed() {
214214
}
215215

216216
@Override
217-
public void push(final Route.After handler) {
218-
rsp.push(handler);
217+
public void after(final Route.After handler) {
218+
rsp.after(handler);
219219
}
220220

221221
@Override
222-
public void push(final Route.Complete handler) {
223-
rsp.push(handler);
222+
public void complete(final Route.Complete handler) {
223+
rsp.complete(handler);
224224
}
225225

226226
@Override
@@ -636,13 +636,13 @@ default Response status(final int status) {
636636
* @param handler A handler
637637
* @see Route.After
638638
*/
639-
void push(Route.After handler);
639+
void after(Route.After handler);
640640

641641
/**
642642
* Append complete handler, will be execute after sending response.
643643
*
644644
* @param handler A handler
645645
* @see Route.After
646646
*/
647-
void push(Route.Complete handler);
647+
void complete(Route.Complete handler);
648648
}

jooby/src/main/java/org/jooby/Route.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,7 @@ default void handle(final Request req, final Response rsp, final Chain chain) th
19071907
interface After extends Filter {
19081908
@Override
19091909
default void handle(final Request req, final Response rsp, final Chain chain) throws Throwable {
1910-
rsp.push(this);
1910+
rsp.after(this);
19111911
chain.next(req, rsp);
19121912
}
19131913

@@ -2030,7 +2030,7 @@ interface Complete extends Filter {
20302030

20312031
@Override
20322032
default void handle(final Request req, final Response rsp, final Chain chain) throws Throwable {
2033-
rsp.push(this);
2033+
rsp.complete(this);
20342034
chain.next(req, rsp);
20352035
}
20362036

jooby/src/main/java/org/jooby/Status.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,13 @@ public int value() {
426426
return this.value;
427427
}
428428

429+
/**
430+
* @return True, for status code &gt;= 400.
431+
*/
432+
public boolean isError() {
433+
return this.value >= 400;
434+
}
435+
429436
/**
430437
* @return the reason phrase of this status code.
431438
*/

jooby/src/main/java/org/jooby/internal/CookieSessionManager.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
119
package org.jooby.internal;
220

321
import java.util.Collections;
@@ -49,7 +67,7 @@ public CookieSessionManager(final ParserExecutor resolver, final Session.Definit
4967
public Session create(final Request req, final Response rsp) {
5068
Session session = new SessionImpl.Builder(resolver, true, Session.COOKIE_SESSION, -1).build();
5169
log.debug("session created: {}", session);
52-
rsp.push(saveCookie());
70+
rsp.after(saveCookie());
5371
return session;
5472
}
5573

@@ -60,7 +78,7 @@ public Session get(final Request req, final Response rsp) {
6078
-1);
6179
Map<String, String> attributes = attributes(raw);
6280
session.set(attributes);
63-
rsp.push(saveCookie());
81+
rsp.after(saveCookie());
6482
return session.build();
6583
}).orElse(null);
6684
}

jooby/src/main/java/org/jooby/internal/HttpHandlerImpl.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -321,22 +321,24 @@ private void onDeferred(final Map<Object, Object> scope, final NativeRequest req
321321
try {
322322
request.startAsync();
323323

324-
deferred.handler(req, (result, ex) -> {
324+
deferred.handler(req, (success, x) -> {
325325
boolean close = false;
326-
Throwable x = null;
326+
Optional<Throwable> failure = Optional.ofNullable(x);
327327
try {
328328
requestScope.enter(scope);
329-
if (result != null) {
329+
if (success != null) {
330330
close = true;
331-
rsp.send(result);
332-
} else if (ex != null) {
333-
close = true;
334-
handleErr(req, rsp, ex);
331+
rsp.send(success);
335332
}
336333
} catch (Throwable exerr) {
337-
x = exerr;
334+
failure = Optional.of(failure.orElse(exerr));
338335
} finally {
339-
cleanup(req, rsp, close, x, true);
336+
Throwable cause = failure.orElse(null);
337+
if (cause != null) {
338+
close = true;
339+
handleErr(req, rsp, cause);
340+
}
341+
cleanup(req, rsp, close, cause, true);
340342
}
341343
});
342344
} catch (Exception ex) {

jooby/src/main/java/org/jooby/internal/ResponseImpl.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public class ResponseImpl implements Response {
8888

8989
private RequestImpl req;
9090

91+
private boolean failure;
92+
9193
public ResponseImpl(final RequestImpl req, final ParserExecutor parserExecutor,
9294
final NativeResponse rsp, final Route route, final List<Renderer> renderers,
9395
final Map<String, Renderer> rendererMap, final Map<String, Object> locals,
@@ -223,8 +225,9 @@ public Optional<Status> status() {
223225

224226
@Override
225227
public Response status(final Status status) {
226-
this.status = requireNonNull(status, "A status is required.");
228+
this.status = requireNonNull(status, "Statusrequired.");
227229
rsp.statusCode(status.value());
230+
failure = status.isError();
228231
return this;
229232
}
230233

@@ -279,9 +282,11 @@ public void send(final Result result) throws Throwable {
279282

280283
Result finalResult = result;
281284

282-
// after filter
283-
for (int i = after.size() - 1; i >= 0; i--) {
284-
finalResult = after.get(i).handle(req, this, finalResult);
285+
if (!failure) {
286+
// after filter
287+
for (int i = after.size() - 1; i >= 0; i--) {
288+
finalResult = after.get(i).handle(req, this, finalResult);
289+
}
285290
}
286291

287292
Optional<MediaType> rtype = finalResult.type();
@@ -347,12 +352,12 @@ public void send(final Result result) throws Throwable {
347352
}
348353

349354
@Override
350-
public void push(final After handler) {
355+
public void after(final After handler) {
351356
after.add(handler);
352357
}
353358

354359
@Override
355-
public void push(final Complete handler) {
360+
public void complete(final Complete handler) {
356361
complete.add(handler);
357362
}
358363

jooby/src/main/java/org/jooby/internal/SessionManager.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
119
package org.jooby.internal;
220

321
import org.jooby.Cookie;

jooby/src/main/java/org/jooby/internal/handlers/FlashScopeHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void handle(final Request req, final Response rsp, final Route.Chain chai
5555
req.set(FlashScope.NAME, flashScope);
5656

5757
// wrap & proceed
58-
rsp.push(finalizeFlash(copy, flashScope));
58+
rsp.after(finalizeFlash(copy, flashScope));
5959

6060
chain.next(req, rsp);
6161
}

jooby/src/test/java/org/jooby/ResponseForwardingTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,12 @@ public void pushAfter() throws Exception {
350350
new MockUnit(Response.class, Route.After.class)
351351
.expect(unit -> {
352352
Response rsp = unit.get(Response.class);
353-
rsp.push(unit.get(Route.After.class));
353+
rsp.after(unit.get(Route.After.class));
354354
})
355355
.run(unit -> {
356356
Response rsp = new Response.Forwarding(unit.get(Response.class));
357357

358-
rsp.push(unit.get(Route.After.class));
358+
rsp.after(unit.get(Route.After.class));
359359
});
360360
}
361361

@@ -364,12 +364,11 @@ public void pushComplete() throws Exception {
364364
new MockUnit(Response.class, Route.Complete.class)
365365
.expect(unit -> {
366366
Response rsp = unit.get(Response.class);
367-
rsp.push(unit.get(Route.Complete.class));
367+
rsp.complete(unit.get(Route.Complete.class));
368368
})
369369
.run(unit -> {
370370
Response rsp = new Response.Forwarding(unit.get(Response.class));
371-
372-
rsp.push(unit.get(Route.Complete.class));
371+
rsp.complete(unit.get(Route.Complete.class));
373372
});
374373
}
375374

0 commit comments

Comments
 (0)