Skip to content

Commit 6195b9f

Browse files
committed
use set* for response methods
1 parent 78e853b commit 6195b9f

File tree

17 files changed

+105
-92
lines changed

17 files changed

+105
-92
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public AssetHandler(AssetSource... sources) {
5050
asset.release();
5151
return ctx;
5252
} else {
53-
ctx.header("ETag", asset.etag());
53+
ctx.setHeader("ETag", asset.etag());
5454
}
5555
}
5656

@@ -64,20 +64,20 @@ public AssetHandler(AssetSource... sources) {
6464
asset.release();
6565
return ctx;
6666
}
67-
ctx.header("Last-Modified", new Date(lastModified));
67+
ctx.setHeader("Last-Modified", new Date(lastModified));
6868
}
6969
}
7070

7171
// cache max-age
7272
if (maxAge > 0) {
73-
ctx.header("Cache-Control", "max-age=" + maxAge);
73+
ctx.setHeader("Cache-Control", "max-age=" + maxAge);
7474
}
7575

7676
long length = asset.length();
7777
if (length != -1) {
78-
ctx.responseLength(length);
78+
ctx.setContentLength(length);
7979
}
80-
ctx.responseType(asset.type());
80+
ctx.setContentType(asset.type());
8181
return ctx.sendStream(asset.content());
8282
}
8383

jooby/src/main/java/io/jooby/AttachedFile.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
* Copyright 2014 Edgar Espina
15+
*/
116
package io.jooby;
217

318
import org.apache.commons.io.FilenameUtils;

jooby/src/main/java/io/jooby/ByteRange.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ public ByteRange apply(Context ctx, long contentLength) {
5555
}
5656
// offset
5757
long limit = (end - start + 1);
58-
ctx.header("Accept-Ranges", "bytes");
59-
ctx.header("Content-Range", "bytes " + start + "-" + end + "/" + contentLength);
60-
ctx.header("Content-Length", limit);
58+
ctx.setHeader("Accept-Ranges", "bytes");
59+
ctx.setHeader("Content-Range", "bytes " + start + "-" + end + "/" + contentLength);
60+
ctx.setHeader("Content-Length", limit);
6161
ctx.statusCode(StatusCode.PARTIAL_CONTENT);
6262
return new ByteRange(start, limit);
6363
}

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ default boolean accept(MediaType contentType) {
153153
return contentType.matches(accept);
154154
}
155155

156-
@Nonnull default MediaType requestType() {
156+
@Nullable default MediaType contentType() {
157157
return header("Content-Type").toOptional().map(MediaType::valueOf).orElse(null);
158158
}
159159

160-
default long requestLength() {
160+
default long contentLength() {
161161
return header("Content-Length").longValue(-1);
162162
}
163163

@@ -321,45 +321,45 @@ default long requestLength() {
321321
* **********************************************************************************************
322322
*/
323323

324-
@Nonnull default Context header(@Nonnull String name, @Nonnull Date value) {
325-
return header(name, RFC1123.format(Instant.ofEpochMilli(value.getTime())));
324+
@Nonnull default Context setHeader(@Nonnull String name, @Nonnull Date value) {
325+
return setHeader(name, RFC1123.format(Instant.ofEpochMilli(value.getTime())));
326326
}
327327

328-
@Nonnull default Context header(@Nonnull String name, @Nonnull Instant value) {
329-
return header(name, RFC1123.format(value));
328+
@Nonnull default Context setHeader(@Nonnull String name, @Nonnull Instant value) {
329+
return setHeader(name, RFC1123.format(value));
330330
}
331331

332-
@Nonnull default Context header(@Nonnull String name, @Nonnull Object value) {
332+
@Nonnull default Context setHeader(@Nonnull String name, @Nonnull Object value) {
333333
if (value instanceof Date) {
334-
return header(name, (Date) value);
334+
return setHeader(name, (Date) value);
335335
}
336336
if (value instanceof Instant) {
337-
return header(name, (Instant) value);
337+
return setHeader(name, (Instant) value);
338338
}
339-
return header(name, value.toString());
339+
return setHeader(name, value.toString());
340340
}
341341

342-
@Nonnull Context header(@Nonnull String name, @Nonnull String value);
342+
@Nonnull Context setHeader(@Nonnull String name, @Nonnull String value);
343343

344-
@Nonnull Context responseLength(long length);
344+
@Nonnull Context setContentLength(long length);
345345

346-
@Nonnull default Context responseType(@Nonnull String contentType) {
347-
return responseType(MediaType.valueOf(contentType));
346+
@Nonnull default Context setContentType(@Nonnull String contentType) {
347+
return setContentType(MediaType.valueOf(contentType));
348348
}
349349

350-
@Nonnull default Context responseType(@Nonnull MediaType contentType) {
351-
return responseType(contentType, contentType.charset());
350+
@Nonnull default Context setContentType(@Nonnull MediaType contentType) {
351+
return setContentType(contentType, contentType.charset());
352352
}
353353

354-
@Nonnull default Context defaultResponseType(@Nonnull String contentType) {
355-
return defaultResponseType(MediaType.valueOf(contentType));
354+
@Nonnull default Context setDefaultContentType(@Nonnull String contentType) {
355+
return setDefaultContentType(MediaType.valueOf(contentType));
356356
}
357357

358-
@Nonnull Context defaultResponseType(@Nonnull MediaType contentType);
358+
@Nonnull Context setDefaultContentType(@Nonnull MediaType contentType);
359359

360-
@Nonnull Context responseType(@Nonnull MediaType contentType, @Nullable Charset charset);
360+
@Nonnull Context setContentType(@Nonnull MediaType contentType, @Nullable Charset charset);
361361

362-
@Nonnull MediaType responseType();
362+
@Nonnull MediaType responseContentType();
363363

364364
@Nonnull default Context statusCode(StatusCode statusCode) {
365365
return statusCode(statusCode.value());
@@ -384,13 +384,13 @@ default long requestLength() {
384384
@Nonnull OutputStream responseStream();
385385

386386
default @Nonnull OutputStream responseStream(MediaType contentType) {
387-
responseType(contentType);
387+
setContentType(contentType);
388388
return responseStream();
389389
}
390390

391391
default @Nonnull Context responseStream(MediaType contentType,
392392
Throwing.Consumer<OutputStream> consumer) throws Exception {
393-
responseType(contentType);
393+
setContentType(contentType);
394394
return responseStream(consumer);
395395
}
396396

@@ -436,7 +436,7 @@ default long requestLength() {
436436
}
437437

438438
default @Nonnull Context sendRedirect(@Nonnull StatusCode redirect, @Nonnull String location) {
439-
header("location", location);
439+
setHeader("location", location);
440440
return sendStatusCode(redirect);
441441
}
442442

@@ -457,13 +457,13 @@ default long requestLength() {
457457
@Nonnull Context sendStream(@Nonnull InputStream input);
458458

459459
default Context sendAttachment(AttachedFile file) {
460-
header("Content-Disposition", file.contentDisposition());
460+
setHeader("Content-Disposition", file.contentDisposition());
461461
InputStream content = file.content();
462462
long length = file.length();
463463
if (length > 0) {
464-
responseLength(length);
464+
setContentLength(length);
465465
}
466-
defaultResponseType(file.contentType());
466+
setDefaultContentType(file.contentType());
467467
if (content instanceof FileInputStream) {
468468
sendFile(((FileInputStream) content).getChannel());
469469
} else {

jooby/src/main/java/io/jooby/Decorators.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public String compute() {
5050
* @return A filter that set the `server` header.
5151
*/
5252
public static final Route.Decorator server() {
53-
return next -> ctx -> next.apply(ctx.header(H_SERVER, ctx.name()));
53+
return next -> ctx -> next.apply(ctx.setHeader(H_SERVER, ctx.name()));
5454
}
5555

5656
/**
@@ -70,7 +70,7 @@ public static final Route.Decorator contentType(@Nonnull String type) {
7070
* @return A filter that set the `content-type` header.
7171
*/
7272
public static final Route.Decorator contentType(@Nonnull MediaType type) {
73-
return next -> ctx -> next.apply(ctx.responseType(type));
73+
return next -> ctx -> next.apply(ctx.setContentType(type));
7474
}
7575

7676
/**
@@ -80,7 +80,7 @@ public static final Route.Decorator contentType(@Nonnull MediaType type) {
8080
*/
8181
public static final Route.Decorator date() {
8282
DateHeader date = new DateHeader();
83-
return next -> ctx -> next.apply(ctx.header(H_DATE, date.compute()));
83+
return next -> ctx -> next.apply(ctx.setHeader(H_DATE, date.compute()));
8484
}
8585

8686
/**
@@ -113,9 +113,9 @@ public static final Route.Decorator defaultHeaders() {
113113
private static final Route.Decorator defaultHeaders(MediaType contentType, Charset charset) {
114114
DateHeader date = new DateHeader();
115115
return next -> ctx -> {
116-
ctx.header(H_SERVER, ctx.name());
117-
ctx.responseType(contentType, charset);
118-
ctx.header(H_DATE, date.compute());
116+
ctx.setHeader(H_SERVER, ctx.name());
117+
ctx.setContentType(contentType, charset);
118+
ctx.setHeader(H_DATE, date.compute());
119119
return next.apply(ctx);
120120
};
121121
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static ErrorHandler log(Logger log, StatusCode... quiet) {
5353
new ContentNegotiation()
5454
.accept(json, () -> {
5555
String message = Optional.ofNullable(cause.getMessage()).orElse(statusCode.reason());
56-
return ctx.responseType(json)
56+
return ctx.setContentType(json)
5757
.statusCode(statusCode)
5858
.sendString("{\"message\":\"" + message + "\",\"statusCode\":" + statusCode.value()
5959
+ ",\"reason\":\"" + statusCode.reason() + "\"}");
@@ -90,7 +90,7 @@ static ErrorHandler log(Logger log, StatusCode... quiet) {
9090
.append("</html>");
9191

9292
return ctx
93-
.responseType(MediaType.html)
93+
.setContentType(MediaType.html)
9494
.statusCode(statusCode)
9595
.sendString(html.toString());
9696
}).render(ctx);

jooby/src/main/java/io/jooby/Renderer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
package io.jooby;
1717

1818
import javax.annotation.Nonnull;
19-
import java.io.InputStream;
2019
import java.nio.charset.StandardCharsets;
2120

2221
public interface Renderer {
2322

2423
Renderer TO_STRING = (ctx, value) -> {
25-
ctx.defaultResponseType(MediaType.text);
24+
ctx.setDefaultContentType(MediaType.text);
2625
return value.toString().getBytes(StandardCharsets.UTF_8);
2726
};
2827

jooby/src/main/java/io/jooby/TemplateEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public interface TemplateEngine extends Renderer {
2323
String apply(Context ctx, ModelAndView modelAndView) throws Exception;
2424

2525
@Override default byte[] encode(@Nonnull Context ctx, @Nonnull Object value) throws Exception {
26-
ctx.defaultResponseType(MediaType.html);
26+
ctx.setDefaultContentType(MediaType.html);
2727
String output = apply(ctx, (ModelAndView) value);
2828
return output.getBytes(StandardCharsets.UTF_8);
2929
}

jooby/src/main/java/io/jooby/internal/handler/reactive/ChunkedSubscriber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void onNext(Object item) {
5050
byte[] data = renderer.encode(ctx, item);
5151

5252
if (responseType == null) {
53-
responseType = ctx.responseType();
53+
responseType = ctx.responseContentType();
5454
if (responseType.isJson()) {
5555
data = prepend(data, JSON_LBRACKET);
5656
}

jooby/src/test/java/io/jooby/MockContext.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,27 +195,27 @@ public MockContext dispatch(@Nonnull Executor executor, @Nonnull Runnable action
195195
return locals;
196196
}
197197

198-
@Nonnull @Override public MockContext header(@Nonnull String name, @Nonnull Date value) {
199-
Context.super.header(name, value);
198+
@Nonnull @Override public MockContext setHeader(@Nonnull String name, @Nonnull Date value) {
199+
Context.super.setHeader(name, value);
200200
return this;
201201
}
202202

203-
@Nonnull @Override public MockContext header(@Nonnull String name, @Nonnull Instant value) {
204-
Context.super.header(name, value);
203+
@Nonnull @Override public MockContext setHeader(@Nonnull String name, @Nonnull Instant value) {
204+
Context.super.setHeader(name, value);
205205
return this;
206206
}
207207

208-
@Nonnull @Override public MockContext header(@Nonnull String name, @Nonnull Object value) {
209-
Context.super.header(name, value);
208+
@Nonnull @Override public MockContext setHeader(@Nonnull String name, @Nonnull Object value) {
209+
Context.super.setHeader(name, value);
210210
return this;
211211
}
212212

213-
@Nonnull @Override public MockContext header(@Nonnull String name, @Nonnull String value) {
213+
@Nonnull @Override public MockContext setHeader(@Nonnull String name, @Nonnull String value) {
214214
responseHeaders.put(name, value);
215215
return this;
216216
}
217217

218-
@Nonnull @Override public MockContext responseLength(long length) {
218+
@Nonnull @Override public MockContext setContentLength(long length) {
219219
this.length = length;
220220
return this;
221221
}
@@ -225,7 +225,7 @@ public long getResponseLength() {
225225
}
226226

227227
@Nonnull @Override
228-
public MockContext responseType(@Nonnull MediaType contentType, @Nullable Charset charset) {
228+
public MockContext setContentType(@Nonnull MediaType contentType, @Nullable Charset charset) {
229229
this.responseType = contentType;
230230
this.responseCharset = charset;
231231
return this;
@@ -284,7 +284,7 @@ public String getResultText() {
284284

285285
@Nonnull @Override public Writer responseWriter(MediaType type, Charset charset) {
286286
responseStarted = true;
287-
responseType(type, charset);
287+
setContentType(type, charset);
288288
Writer writer = new StringWriter();
289289
result = writer;
290290
return writer;
@@ -344,15 +344,15 @@ public String getResultText() {
344344
return this;
345345
}
346346

347-
@Nonnull @Override public Context defaultResponseType(@Nonnull MediaType contentType) {
347+
@Nonnull @Override public Context setDefaultContentType(@Nonnull MediaType contentType) {
348348
if (responseType == null) {
349349
responseType = contentType;
350350
responseCharset = contentType.charset();
351351
}
352352
return this;
353353
}
354354

355-
@Nonnull @Override public MediaType responseType() {
355+
@Nonnull @Override public MediaType responseContentType() {
356356
return responseType == null ? MediaType.text : responseType;
357357
}
358358

0 commit comments

Comments
 (0)