forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActResponse.java
More file actions
169 lines (141 loc) · 3.91 KB
/
ActResponse.java
File metadata and controls
169 lines (141 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package act;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.app.ActionContext;
import act.conf.AppConfig;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.mvc.MvcConfig;
import org.osgl.mvc.result.Ok;
import org.osgl.mvc.result.Redirect;
import org.osgl.util.E;
import java.util.Locale;
public abstract class ActResponse<T extends ActResponse> extends H.Response<T> {
private boolean onResult;
private boolean closed;
protected String charset;
protected Locale locale;
protected String contentType;
protected H.Format fmt;
private boolean charsetSet;
private boolean committed;
protected int statusCode = -1;
protected ActResponse() {}
protected ActResponse(AppConfig config) {
charset = config.encoding();
locale = config.locale();
}
@Override
public String characterEncoding() {
return charset;
}
@Override
public T characterEncoding(String encoding) {
charset = encoding;
charsetSet = true;
return me();
}
@Override
public T sendError(int sc, String msg) {
throw E.unsupport();
}
@Override
public T sendError(int sc) {
throw E.unsupport();
}
@Override
public T sendRedirect(String location) {
throw new Redirect(location);
}
public T contentType(H.Format fmt) {
contentType(fmt.contentType());
this.fmt = fmt;
return me();
}
public H.Format lastContentType() {
return fmt;
}
/**
* This response is ready for Result evaluation.
*/
public void onResult() {
this.onResult = true;
}
public void commitContentType() {
header(H.Header.Names.CONTENT_TYPE, _getContentType());
}
@Override
public void close() {
super.close();
markClosed();
}
public boolean isClosed() {
return closed;
}
protected void markClosed() {
this.closed = true;
ActionContext ctx = context();
ctx.markAsReadyForClose();
}
protected abstract void _setStatusCode(int sc);
@Override
public T status(int sc) {
E.illegalArgumentIf(sc < 100, "Invalid status code");
statusCode = sc;
_setStatusCode(sc);
return me();
}
@Override
public int statusCode() {
return statusCode;
}
public ActResponse beforeWritingContent() {
if (onResult) {
return this;
}
ActionContext ctx = context();
ctx.dissolve();
MvcConfig.applyBeforeCommitResultHandler(Ok.get(), ctx.req(), this);
return this;
}
public ActResponse afterWritingContent() {
ActionContext ctx = context();
if (committed) {
return this;
}
committed = true;
commit();
MvcConfig.applyAfterCommitResultHandler(Ok.get(), ctx.req(), this);
return this;
}
@Override
protected void _setContentType(String type) {
this.contentType = type;
}
protected String _getContentType() {
return this.contentType != null ? (this.charsetSet ? this.contentType + ";charset=" + this.charset : this.contentType) : null;
}
protected final T me() {
return (T) this;
}
private ActionContext ctx() {
return $.cast(this.context());
}
}