Skip to content

Commit 0966186

Browse files
committed
Value API: checkstyle fixes
1 parent d4f750c commit 0966186

File tree

19 files changed

+188
-18
lines changed

19 files changed

+188
-18
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1+
/**
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
16
package io.jooby;
27

8+
/**
9+
* Bean value converter, works like {@link ValueConverter} except that the input value is always
10+
* a hash/object value, not a simple string like it is required by {@link ValueConverter}.
11+
*
12+
* @since 2.1.1
13+
*/
314
public interface BeanConverter extends ValueConverter {
415
}

jooby/src/main/java/io/jooby/Body.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ public interface Body extends Value {
8282
return to((Type) type);
8383
}
8484

85+
/**
86+
* Convert this body into the given type.
87+
*
88+
* @param type Type to use.
89+
* @param <T> Generic type.
90+
* @return Converted value.
91+
*/
8592
@Nonnull <T> T to(@Nonnull Type type);
8693

8794
/* **********************************************************************************************
@@ -92,40 +99,44 @@ public interface Body extends Value {
9299
/**
93100
* Empty body.
94101
*
102+
* @param ctx Current context.
95103
* @return Empty body.
96104
*/
97-
static Body empty(Context ctx) {
105+
static @Nonnull Body empty(@Nonnull Context ctx) {
98106
return ByteArrayBody.empty(ctx);
99107
}
100108

101109
/**
102110
* Creates a HTTP body from input stream.
103111
*
112+
* @param ctx Current context.
104113
* @param stream Input stream.
105114
* @param size Size in bytes or <code>-1</code>.
106115
* @return A new body.
107116
*/
108-
static @Nonnull Body of(Context ctx, @Nonnull InputStream stream, long size) {
117+
static @Nonnull Body of(@Nonnull Context ctx, @Nonnull InputStream stream, long size) {
109118
return new InputStreamBody(ctx, stream, size);
110119
}
111120

112121
/**
113122
* Creates a HTTP body from byte array.
114123
*
124+
* @param ctx Current context.
115125
* @param bytes byte array.
116126
* @return A new body.
117127
*/
118-
static @Nonnull Body of(Context ctx, @Nonnull byte[] bytes) {
128+
static @Nonnull Body of(@Nonnull Context ctx, @Nonnull byte[] bytes) {
119129
return new ByteArrayBody(ctx, bytes);
120130
}
121131

122132
/**
123133
* Creates a HTTP body from file.
124134
*
135+
* @param ctx Current context.
125136
* @param file File.
126137
* @return A new body.
127138
*/
128-
static @Nonnull Body of(Context ctx, @Nonnull Path file) {
139+
static @Nonnull Body of(@Nonnull Context ctx, @Nonnull Path file) {
129140
return new FileBody(ctx, file);
130141
}
131142
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,15 @@ public interface Context extends Registry {
8686
*/
8787
@Nonnull Router getRouter();
8888

89-
@Nullable <T> T convert(Value value, Class<T> type);
89+
/**
90+
* Converts a value (single or hash) into the given type.
91+
*
92+
* @param value Value to convert.
93+
* @param type Expected type.
94+
* @param <T> Generic type.
95+
* @return Converted value.
96+
*/
97+
@Nullable <T> T convert(@Nonnull Value value, @Nonnull Class<T> type);
9098

9199
/*
92100
* **********************************************************************************************
@@ -563,7 +571,8 @@ public interface Context extends Registry {
563571
/**
564572
* Convert the HTTP body to the given type.
565573
*
566-
* @param type Reified type.
574+
* @param type Generic type.
575+
* @param contentType Content type to use.
567576
* @param <T> Conversion type.
568577
* @return Instance of conversion type.
569578
*/

jooby/src/main/java/io/jooby/DefaultContext.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import io.jooby.internal.SingleValue;
1111
import io.jooby.internal.UrlParser;
1212
import io.jooby.internal.ValueConverters;
13-
import io.jooby.internal.reflect.$Types;
1413

1514
import javax.annotation.Nonnull;
1615
import javax.annotation.Nullable;

jooby/src/main/java/io/jooby/ErrorHandler.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import static io.jooby.MediaType.html;
1313
import static io.jooby.MediaType.json;
14+
import static io.jooby.MediaType.text;
1415

1516
/**
1617
* Catch and encode application errors.
@@ -27,8 +28,24 @@ public interface ErrorHandler {
2728
ErrorHandler DEFAULT = (ctx, cause, statusCode) -> {
2829
ctx.getRouter().getLog().error(errorMessage(ctx, statusCode), cause);
2930

30-
MediaType type = ctx.accept(Arrays.asList(json, html));
31-
if (type == null || type.equals(html)) {
31+
MediaType type = ctx.accept(Arrays.asList(html, json, text));
32+
if (json.equals(type)) {
33+
String message = Optional.ofNullable(cause.getMessage()).orElse(statusCode.reason());
34+
ctx.setResponseType(json)
35+
.setResponseCode(statusCode)
36+
.send("{\"message\":\"" + XSS.json(message) + "\",\"statusCode\":" + statusCode.value()
37+
+ ",\"reason\":\"" + statusCode.reason() + "\"}");
38+
} else if (text.equals(type)) {
39+
StringBuilder message = new StringBuilder();
40+
message.append(ctx.getMethod()).append(" ").append(ctx.pathString()).append(" ");
41+
message.append(statusCode.value()).append(" ").append(statusCode.reason());
42+
if (cause.getMessage() != null) {
43+
message.append("\n").append(XSS.json(cause.getMessage()));
44+
}
45+
ctx.setResponseType(text)
46+
.setResponseCode(statusCode)
47+
.send(message.toString());
48+
} else {
3249
String message = cause.getMessage();
3350
StringBuilder html = new StringBuilder("<!doctype html>\n")
3451
.append("<html>\n")
@@ -63,12 +80,6 @@ public interface ErrorHandler {
6380
.setResponseType(MediaType.html)
6481
.setResponseCode(statusCode)
6582
.send(html.toString());
66-
} else {
67-
String message = Optional.ofNullable(cause.getMessage()).orElse(statusCode.reason());
68-
ctx.setResponseType(json)
69-
.setResponseCode(statusCode)
70-
.send("{\"message\":\"" + XSS.json(message) + "\",\"statusCode\":" + statusCode.value()
71-
+ ",\"reason\":\"" + statusCode.reason() + "\"}");
7283
}
7384
};
7485

jooby/src/main/java/io/jooby/Formdata.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public interface Formdata extends Value {
5050
/**
5151
* Creates a formdata object.
5252
*
53+
* @param ctx Current context.
5354
* @return Formdata.
5455
*/
5556
static @Nonnull Formdata create(@Nonnull Context ctx) {

jooby/src/main/java/io/jooby/Multipart.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ public interface Multipart extends Formdata {
2222
/**
2323
* Creates a new multipart object.
2424
*
25+
* @param ctx Current context.
2526
* @return Multipart instance.
2627
*/
2728
static @Nonnull Multipart create(@Nonnull Context ctx) {
28-
return new HashValue(ctx,null);
29+
return new HashValue(ctx, null);
2930
}
3031
}

jooby/src/main/java/io/jooby/QueryString.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public interface QueryString extends Value {
3434
*
3535
* <pre>{@code {q: foo, sort: name}}</pre>
3636
*
37+
* @param ctx Current context.
3738
* @param queryString Query string.
3839
* @return A query string.
3940
*/

jooby/src/main/java/io/jooby/Router.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,14 +676,30 @@ default Router error(@Nonnull Predicate<StatusCode> predicate,
676676
*/
677677
@Nonnull Router setFlashCookie(@Nonnull String name);
678678

679-
@Nonnull Router converter(ValueConverter converter);
679+
/**
680+
* Add a custom string value converter.
681+
*
682+
* @param converter Custom value converter.
683+
* @return This router.
684+
*/
685+
@Nonnull Router converter(@Nonnull ValueConverter converter);
680686

687+
/**
688+
* Get all simple/string value converters.
689+
*
690+
* @return All simple/string value converters.
691+
*/
681692
@Nonnull List<ValueConverter> getConverters();
682693

694+
/**
695+
* Get all complex/bean value converters.
696+
*
697+
* @return All complex/bean value converters.
698+
*/
683699
@Nonnull List<BeanConverter> getBeanConverters();
684700

685701
/**
686-
* Normalize a path by removing consecutives <code>/</code>(slashes), make it lower case and
702+
* Normalize a path by removing consecutive <code>/</code>(slashes), make it lower case and
687703
* removing trailing slash.
688704
*
689705
* @param path Path to process.

jooby/src/main/java/io/jooby/Value.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ default int size() {
613613
/**
614614
* Creates a single value.
615615
*
616+
* @param ctx Current context.
616617
* @param name Name of value.
617618
* @param value Value.
618619
* @return Single value.
@@ -624,6 +625,7 @@ default int size() {
624625
/**
625626
* Creates a sequence/array of values.
626627
*
628+
* @param ctx Current context.
627629
* @param name Name of array.
628630
* @param values Field values.
629631
* @return Array value.
@@ -641,6 +643,7 @@ default int size() {
641643
* - For single element (size==1). It produces a single value
642644
* - For multi-value elements (size&gt;1). It produces an array value.
643645
*
646+
* @param ctx Current context.
644647
* @param name Field name.
645648
* @param values Field values.
646649
* @return A value.
@@ -662,6 +665,7 @@ default int size() {
662665
* - For null/empty values. It produces a missing value.
663666
* - For single element (size==1). It produces a single value
664667
*
668+
* @param ctx Current context.
665669
* @param name Field name.
666670
* @param value Field values.
667671
* @return A value.
@@ -676,6 +680,7 @@ default int size() {
676680
/**
677681
* Create a hash/object value using the map values.
678682
*
683+
* @param ctx Current context.
679684
* @param values Map values.
680685
* @return A hash/object value.
681686
*/

0 commit comments

Comments
 (0)