Skip to content

Commit 3cc36ad

Browse files
committed
Add context completion listener + access log implementation
1 parent 6be1453 commit 3cc36ad

File tree

17 files changed

+718
-45
lines changed

17 files changed

+718
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.jooby/jooby/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.jooby/jooby)
2-
[![javadoc](https://javadoc.io/badge/io.jooby/jooby.svg)](https://javadoc.io/doc/io.jooby/jooby/2.0.0)
2+
[![javadoc](https://javadoc.io/badge/io.jooby/jooby.svg)](https://javadoc.io/doc/io.jooby/jooby/latest)
33
[![Become a Patreon](https://img.shields.io/badge/patreon-donate-orange.svg)](https://patreon.com/edgarespina)
44
[![Build Status](https://travis-ci.org/jooby-project/jooby.svg?branch=master)](https://travis-ci.org/jooby-project/jooby)
55
[![Join the chat at https://gitter.im/jooby-project/jooby](https://badges.gitter.im/jooby-project/jooby.svg)](https://gitter.im/jooby-project/jooby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

docs/asciidoc/handlers.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
This section describes some built-in handler provided by Jooby.
44

5+
include::handlers/access-log.adoc[]
6+
57
include::handlers/cors.adoc[]
68

79
include::handlers/head.adoc[]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
=== AccessLogHandler
2+
3+
The javadoc:AccessLogHandler[] logs incoming requests using the https://en.wikipedia.org/wiki/Common_Log_Format[NCSA format] (a.k.a common log format).
4+
5+
.Usage
6+
[source, java, role = "primary"]
7+
----
8+
import io.jooby.Jooby;
9+
import io.jooby.AccessLogHandler;
10+
...
11+
{
12+
13+
decorator(new AccessLogHandler()); <1>
14+
15+
get("/", ctx -> {
16+
...
17+
});
18+
}
19+
----
20+
21+
.Kotlin
22+
[source, kotlin, role = "secondary"]
23+
----
24+
import io.jooby.Jooby
25+
import io.jooby.AccessLogHandler
26+
...
27+
{
28+
decorator(AccessLogHandler()) <1>
29+
30+
get("/") {
31+
...
32+
}
33+
}
34+
----
35+
36+
<1> Install AccessLogHandler
37+
38+
Prints a message like:
39+
40+
127.0.0.1 - - [04/Oct/2016:17:51:42 +0000] "GET / HTTP/1.1" 200 2 3
41+
42+
Message is represented by:
43+
44+
- Remote Address.
45+
- User ID (or dash when missing)
46+
- Date and time
47+
- HTTP method, requestPath and protocol
48+
- Response Status Code
49+
- Response Content-Length (or dash when missing)
50+
- Time took to process the request in milliseconds
51+
52+
Extra request or response headers can be appended at the end using the available methods:
53+
54+
- javadoc:AccessLogHandler[requestHeader, java.lang.String...]
55+
- javadoc:AccessLogHandler[responseHeader, java.lang.String...]
56+

docs/asciidoc/routing.adoc

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,8 @@ An after handler is always invoked.
629629
====
630630

631631
The next examples demonstrate some use cases for dealing with errored responses, but keep in mind
632-
that an after handler is not a mechanism for handling and reporting exceptions that's is a task for an <<error-handler, Error Handler>>.
632+
that an after handler is not a mechanism for handling and reporting exceptions that's is a task
633+
for an <<error-handler, Error Handler>>.
633634

634635
.Run code depending of success or failure responses:
635636
[source,java,role="primary"]
@@ -736,6 +737,47 @@ In case where the after handler produces a new exception, that exception will be
736737
<1> Will be `OriginalException`
737738
<2> Will be `AnotherException`
738739

740+
==== Complete
741+
742+
The javadoc:Route.Complete[text=complte] listener run at the completion of a request/response cycle
743+
(i.e. when the request has been completely read, and the response has been fully written).
744+
745+
At this point it is too late to modify the exchange further. They are attached to a running context
746+
(not like a decorator/before/after filters).
747+
748+
.Example
749+
[source, java, role="primary"]
750+
----
751+
{
752+
decorator(next -> ctx -> {
753+
long start = System.currentTimeInMillis();
754+
ctx.onComplete(context -> { <1>
755+
long end = System.currentTimeInMillis(); <2>
756+
System.out.println("Took: " + (end - start));
757+
});
758+
});
759+
}
760+
----
761+
762+
.Kotlin
763+
[source, kotlin, role="secondary"]
764+
----
765+
{
766+
decorator {
767+
val start = System.currentTimeInMillis()
768+
ctx.onComplete { <1>
769+
val end = System.currentTimeInMillis() <2>
770+
println("Took: " + (end - start))
771+
}
772+
}
773+
}
774+
----
775+
776+
<1> Attach a completion listener
777+
<2> Run after response has been fully written
778+
779+
Completion listeners are invoked in reverse order.
780+
739781
=== Pipeline
740782

741783
Route pipeline (a.k.a route stack) is a composition of one or more decorator(s) tied to a single `handler`:

docs/src/main/java/io/jooby/adoc/JavadocProcessor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public JavadocProcessor(String name) {
2323

2424
@Override
2525
public Object process(ContentNode parent, String clazz, Map<String, Object> attributes) {
26-
StringBuilder link = new StringBuilder("https://static.javadoc.io/io.jooby/jooby/");
26+
27+
StringBuilder link = new StringBuilder("https://www.javadoc.io/doc/io.jooby/jooby/latest/io/jooby/");
2728
StringBuilder text = new StringBuilder();
28-
link.append(DocGenerator.version());
2929
String[] names = clazz.split("\\.");
3030
List<String> pkg = new ArrayList<>();
3131
List<String> nameList = new ArrayList<>();
@@ -36,7 +36,6 @@ public Object process(ContentNode parent, String clazz, Map<String, Object> attr
3636
pkg.add(name);
3737
}
3838
}
39-
link.append("/io/jooby/");
4039
if (pkg.size() > 0) {
4140
link.append(pkg.stream().collect(Collectors.joining("/"))).append("/");
4241
}

0 commit comments

Comments
 (0)