4141import java .util .Map ;
4242import 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