Skip to content

Commit c6f5d06

Browse files
committed
more javadoc
1 parent 56a0df4 commit c6f5d06

13 files changed

Lines changed: 745 additions & 136 deletions

TODO

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
* Review decorator and responseHandler. Seems we kill decorator when a response handler is used it?
2+
* Remove Route.Handler.next find another way of doing it
3+
* Review and try to remove Route.Handler.execute
14
* check sendBytes implementation and don't set content-length if there is one already
25

36
* doc

etc/source/jooby-style.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,11 @@
116116
<!-- See http://checkstyle.sf.net/config_sizes.html -->
117117
<module name="LineLength">
118118
<property name="max" value="100"/>
119-
<!-- Ignore length of javadoc or @Api annotations -->
120-
<property name="ignorePattern" value="^ *(\* *[^ ]+$|@Api)"/>
119+
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://|^\\*"/>
121120
</module>
122121
<module name="MethodLength">
123122
<property name="tokens" value="METHOD_DEF"/>
124-
<property name="max" value="150"/>
123+
<property name="max" value="400"/>
125124
<property name="countEmpty" value="false"/>
126125
</module>
127126

jooby/src/main/java/io/jooby/AssetHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @author edgar
2727
* @since 2.0.0
2828
*/
29-
public class AssetHandler implements Route.Handler, Route.Aware {
29+
public class AssetHandler implements Route.Handler {
3030
private final AssetSource[] sources;
3131

3232
private boolean etag = true;
@@ -147,11 +147,12 @@ private Asset resolve(String filepath) {
147147
return null;
148148
}
149149

150-
@Override public void setRoute(Route route) {
150+
@Override public Route.Handler setRoute(Route route) {
151151
List<String> keys = route.getPathKeys();
152152
this.filekey = keys.size() == 0 ? "*" : keys.get(0);
153153

154154
// NOTE: It send an inputstream we don't need a renderer
155155
route.setReturnType(Context.class);
156+
return this;
156157
}
157158
}

jooby/src/main/java/io/jooby/ContentNegotiation.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Map;
2121

2222
import static io.jooby.MediaType.ALL;
23+
import static io.jooby.MediaType.MOST_SPECIFIC;
2324

2425
public class ContentNegotiation {
2526

@@ -43,16 +44,21 @@ public ContentNegotiation accept(Throwing.Supplier<Object> fallback) {
4344

4445
public Object render(String accept) {
4546
List<MediaType> types = MediaType.parse(accept);
46-
int maxScore = Integer.MIN_VALUE;
47+
MediaType highest = null;
4748
Throwing.Supplier<Object> result = fallback;
4849
for (Map.Entry<MediaType, Throwing.Supplier<Object>> entry : options.entrySet()) {
4950
MediaType contentType = entry.getKey();
5051
for (MediaType type : types) {
5152
if (contentType.matches(type)) {
52-
int score = type.getScore();
53-
if (score > maxScore) {
54-
maxScore = score;
53+
if (highest == null) {
54+
highest = type;
5555
result = entry.getValue();
56+
} else {
57+
MediaType max = MOST_SPECIFIC.apply(highest, type);
58+
if (max != highest) {
59+
highest = max;
60+
result = entry.getValue();
61+
}
5662
}
5763
}
5864
}

jooby/src/main/java/io/jooby/Context.java

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
import java.util.Map;
4242
import java.util.concurrent.Executor;
4343

44-
import static sun.security.krb5.internal.crypto.Nonce.value;
45-
4644
/**
4745
* HTTP context allows you to interact with the HTTP Request and manipulate the HTTP Response.
4846
*
@@ -128,9 +126,9 @@ public interface Context {
128126
*/
129127
@Nonnull default Value path(@Nonnull String name) {
130128
String value = pathMap().get(name);
131-
return value == null ?
132-
new Value.Missing(name) :
133-
new Value.Simple(name, UrlParser.decodePath(value));
129+
return value == null
130+
? new Value.Missing(name)
131+
: new Value.Simple(name, UrlParser.decodePath(value));
134132
}
135133

136134
/**
@@ -148,6 +146,7 @@ public interface Context {
148146
* Convert the {@link #pathMap()} to the given type.
149147
*
150148
* @param type Target type.
149+
* @param <T> Target type.
151150
* @return Instance of target type.
152151
*/
153152
@Nonnull default <T> T path(@Nonnull Class<T> type) {
@@ -253,7 +252,7 @@ public interface Context {
253252
}
254253

255254
/**
256-
* Query string as simple map:
255+
* Query string as simple map.
257256
*
258257
* <pre>{@code/search?q=jooby&sort=name}</pre>
259258
*
@@ -268,7 +267,7 @@ public interface Context {
268267
}
269268

270269
/**
271-
* Query string as multi-value map:
270+
* Query string as multi-value map.
272271
*
273272
* <pre>{@code/search?q=jooby&sort=name&sort=id}</pre>
274273
*
@@ -344,6 +343,17 @@ default boolean accept(@Nonnull MediaType contentType) {
344343
return contentType.isMissing() ? null : MediaType.valueOf(contentType.value());
345344
}
346345

346+
/**
347+
* Request <code>Content-Type</code> header or <code>null</code> when missing.
348+
*
349+
* @param defaults Default content type to use when the header is missing.
350+
* @return Request <code>Content-Type</code> header or <code>null</code> when missing.
351+
*/
352+
@Nonnull default MediaType getContentType(MediaType defaults) {
353+
Value contentType = header("Content-Type");
354+
return contentType.isMissing() ? defaults : MediaType.valueOf(contentType.value());
355+
}
356+
347357
/**
348358
* Request <code>Content-Length</code> header or <code>-1</code> when missing.
349359
*
@@ -599,17 +609,14 @@ default long getContentLength() {
599609
* @return Instance of conversion type.
600610
*/
601611
default @Nonnull <T> T body(@Nonnull Reified<T> type) {
602-
MediaType contentType = getContentType();
603-
if (contentType == null) {
604-
return body(type, MediaType.text);
605-
}
606-
return body(type, contentType);
612+
return body(type, getContentType(MediaType.text));
607613
}
608614

609615
/**
610616
* Convert the HTTP body to the given type.
611617
*
612618
* @param type Reified type.
619+
* @param contentType Body content type.
613620
* @param <T> Conversion type.
614621
* @return Instance of conversion type.
615622
*/
@@ -629,15 +636,14 @@ default long getContentLength() {
629636
* @return Instance of conversion type.
630637
*/
631638
default @Nonnull <T> T body(@Nonnull Class type) {
632-
MediaType contentType = MediaType.valueOf(header("Content-Type")
633-
.value("text/plain"));
634-
return body(type, contentType);
639+
return body(type, getContentType(MediaType.text));
635640
}
636641

637642
/**
638643
* Convert the HTTP body to the given type.
639644
*
640645
* @param type Reified type.
646+
* @param contentType Body content type.
641647
* @param <T> Conversion type.
642648
* @return Instance of conversion type.
643649
*/
@@ -907,6 +913,7 @@ default long getContentLength() {
907913
* @param contentType Content type.
908914
* @param consumer Output stream consumer.
909915
* @return HTTP channel as output stream. Usually for chunked responses.
916+
* @throws Exception Is something goes wrong.
910917
*/
911918
default @Nonnull Context responseStream(@Nonnull MediaType contentType,
912919
@Nonnull Throwing.Consumer<OutputStream> consumer) throws Exception {
@@ -919,6 +926,7 @@ default long getContentLength() {
919926
*
920927
* @param consumer Output stream consumer.
921928
* @return HTTP channel as output stream. Usually for chunked responses.
929+
* @throws Exception Is something goes wrong.
922930
*/
923931
default @Nonnull Context responseStream(@Nonnull Throwing.Consumer<OutputStream> consumer)
924932
throws Exception {
@@ -968,6 +976,7 @@ default long getContentLength() {
968976
*
969977
* @param consumer Writer consumer.
970978
* @return This context.
979+
* @throws Exception Is something goes wrong.
971980
*/
972981
default @Nonnull Context responseWriter(@Nonnull Throwing.Consumer<PrintWriter> consumer)
973982
throws Exception {
@@ -980,10 +989,10 @@ default long getContentLength() {
980989
* @param contentType Content type.
981990
* @param consumer Writer consumer.
982991
* @return This context.
992+
* @throws Exception Is something goes wrong.
983993
*/
984994
default @Nonnull Context responseWriter(@Nonnull MediaType contentType,
985-
@Nonnull Throwing.Consumer<PrintWriter> consumer)
986-
throws Exception {
995+
@Nonnull Throwing.Consumer<PrintWriter> consumer) throws Exception {
987996
return responseWriter(contentType, contentType.getCharset(), consumer);
988997
}
989998

@@ -994,6 +1003,7 @@ default long getContentLength() {
9941003
* @param charset Charset.
9951004
* @param consumer Writer consumer.
9961005
* @return This context.
1006+
* @throws Exception Is something goes wrong.
9971007
*/
9981008
default @Nonnull Context responseWriter(@Nonnull MediaType contentType, @Nullable Charset charset,
9991009
@Nonnull Throwing.Consumer<PrintWriter> consumer) throws Exception {
@@ -1131,6 +1141,12 @@ default long getContentLength() {
11311141
*/
11321142
@Nonnull Context sendFile(@Nonnull FileChannel file);
11331143

1144+
/**
1145+
* Send an empty response with the given status code.
1146+
*
1147+
* @param statusCode Status code.
1148+
* @return This context.
1149+
*/
11341150
@Nonnull default Context sendStatusCode(@Nonnull StatusCode statusCode) {
11351151
return sendStatusCode(statusCode.value());
11361152
}

0 commit comments

Comments
 (0)