Skip to content

Commit f97b6a5

Browse files
committed
1 parent 38bf9ea commit f97b6a5

3 files changed

Lines changed: 49 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# ActFramework Change Log
22

3+
**1.4.14**
4+
5+
* Send back `400 Bad Request` on response trigger an obscure error page when content type is `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` #394
6+
37
**1.4.13 16/Oct/2017**
48

59
* Update riotjs to 3.7.2

src/main/java/act/app/ActionContext.java

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -510,19 +510,47 @@ public void disableCORS() {
510510
this.disableCors = true;
511511
}
512512

513-
public ActionContext applyContentType
514-
() {
513+
/**
514+
* Apply content type to response with result provided.
515+
*
516+
* If `result` is an error then it might not apply content type as requested:
517+
* * If request is not ajax request, then use `text/html`
518+
* * If request is ajax request then apply requested content type only when `json` or `xml` is requested
519+
* * otherwise use `text/html`
520+
*
521+
* @param result
522+
* the result used to check if it is error result
523+
* @return
524+
* this `ActionContext`.
525+
*/
526+
public ActionContext applyContentType(Result result) {
527+
if (!result.status().isError()) {
528+
return applyContentType();
529+
}
530+
if (req().isAjax()) {
531+
H.Request req = req();
532+
H.Format fmt = req.accept();
533+
if (H.Format.UNKNOWN == fmt) {
534+
fmt = req.contentType();
535+
}
536+
if (H.Format.JSON == fmt || H.Format.XML == fmt) {
537+
applyContentType(fmt);
538+
} else {
539+
applyContentType(H.Format.HTML);
540+
}
541+
} else {
542+
applyContentType(H.Format.HTML);
543+
}
544+
return this;
545+
}
546+
547+
public ActionContext applyContentType() {
515548
H.Request req = req();
516549
H.Format fmt = req.accept();
517550
if (H.Format.UNKNOWN == fmt) {
518551
fmt = req.contentType();
519552
}
520-
521-
ResponseImplBase resp = $.cast(resp());
522-
if (null != fmt) {
523-
resp.initContentType(fmt.contentType());
524-
}
525-
resp.commitContentType();
553+
applyContentType(fmt);
526554
return this;
527555
}
528556

@@ -536,6 +564,14 @@ public ActionContext applyCorsSpec() {
536564
return this;
537565
}
538566

567+
private void applyContentType(H.Format fmt) {
568+
if (null != fmt) {
569+
ResponseImplBase resp = $.cast(resp());
570+
resp.initContentType(fmt.contentType());
571+
resp.commitContentType();
572+
}
573+
}
574+
539575
private void applyGlobalCorsSetting() {
540576
if (this.disableCors) {
541577
return;

src/main/java/act/handler/event/ResultEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public H.Response response() {
5858
@Override
5959
public Void apply(Result result, H.Request<?> request, H.Response<?> response) throws NotAppliedException, Osgl.Break {
6060
ActionContext context = request.context();
61-
context.applyCorsSpec().applyContentType();
61+
context.applyCorsSpec().applyContentType(result);
6262
context.app().eventBus().trigger(new BeforeResultCommit(result, request, response));
6363
return null;
6464
}

0 commit comments

Comments
 (0)