-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebActivity.java
executable file
·245 lines (210 loc) · 9.08 KB
/
WebActivity.java
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright (c) 2008-2024 The Aspectran Project
*
* 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.
*/
package com.aspectran.web.activity;
import com.aspectran.core.activity.ActivityPrepareException;
import com.aspectran.core.activity.ActivityTerminatedException;
import com.aspectran.core.activity.AdapterException;
import com.aspectran.core.activity.CoreActivity;
import com.aspectran.core.activity.TransletNotFoundException;
import com.aspectran.core.activity.request.RequestParseException;
import com.aspectran.core.adapter.ResponseAdapter;
import com.aspectran.core.adapter.SessionAdapter;
import com.aspectran.core.context.rule.TransletRule;
import com.aspectran.core.context.rule.type.MethodType;
import com.aspectran.core.support.i18n.locale.LocaleChangeInterceptor;
import com.aspectran.core.support.i18n.locale.LocaleResolver;
import com.aspectran.utils.Assert;
import com.aspectran.utils.StringUtils;
import com.aspectran.utils.annotation.jsr305.NonNull;
import com.aspectran.web.activity.request.WebRequestBodyParser;
import com.aspectran.web.adapter.HttpServletRequestAdapter;
import com.aspectran.web.adapter.HttpServletResponseAdapter;
import com.aspectran.web.adapter.HttpSessionAdapter;
import com.aspectran.web.adapter.WebRequestAdapter;
import com.aspectran.web.service.WebService;
import com.aspectran.web.support.http.HttpHeaders;
import com.aspectran.web.support.http.MediaType;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import static com.aspectran.core.context.rule.RequestRule.LOCALE_CHANGE_INTERCEPTOR_SETTING_NAME;
import static com.aspectran.web.activity.request.WebRequestBodyParser.MAX_REQUEST_SIZE_SETTING_NAME;
/**
* An activity that processes a web request.
*
* @since 2008. 4. 28.
*/
public class WebActivity extends CoreActivity {
private final String reverseContextPath;
private final HttpServletRequest request;
private final HttpServletResponse response;
private String requestName;
private MethodType requestMethod;
private boolean async;
private Long timeout;
/**
* Instantiates a new WebActivity.
* @param webService the {@code WebService} instance
* @param request the HTTP request
* @param response the HTTP response
*/
public WebActivity(@NonNull WebService webService, String contextPath, String reverseContextPath,
HttpServletRequest request, HttpServletResponse response) {
super(webService.getActivityContext(), contextPath);
this.reverseContextPath = reverseContextPath;
this.request = request;
this.response = response;
}
@Override
public String getReverseContextPath() {
return reverseContextPath;
}
public HttpServletRequest getRequest() {
return request;
}
public HttpServletResponse getResponse() {
return response;
}
public String getRequestName() {
return requestName;
}
public void setRequestName(String requestName) {
this.requestName = requestName;
}
public MethodType getRequestMethod() {
return requestMethod;
}
public void setRequestMethod(MethodType requestMethod) {
this.requestMethod = requestMethod;
}
public String getFullRequestName() {
StringBuilder sb = new StringBuilder();
if (requestMethod != null) {
sb.append(requestMethod).append(" ");
}
if (StringUtils.hasLength(reverseContextPath)) {
sb.append(reverseContextPath);
}
if (requestName != null) {
sb.append(requestName);
}
return sb.toString();
}
public boolean isAsync() {
return async;
}
public Long getTimeout() {
return timeout;
}
public void prepare() throws TransletNotFoundException, ActivityPrepareException {
Assert.state(requestName != null, "requestName is not set");
Assert.state(requestMethod != null, "requestMethod is not set");
prepare(requestName, requestMethod);
}
@Override
protected void prepare(String requestName, MethodType requestMethod, @NonNull TransletRule transletRule)
throws ActivityPrepareException {
this.async = transletRule.isAsync();
this.timeout = transletRule.getTimeout();
// Check for HTTP POST with the X-HTTP-Method-Override header
if (requestMethod == MethodType.POST) {
String method = request.getHeader(HttpHeaders.X_METHOD_OVERRIDE);
if (method != null) {
// Check if the header value is in our methods list
MethodType hiddenRequestMethod = MethodType.resolve(method);
if (hiddenRequestMethod != null) {
// Change the request method
requestMethod = hiddenRequestMethod;
}
}
}
super.prepare(requestName, requestMethod, transletRule);
}
@Override
protected void adapt() throws AdapterException {
try {
SessionAdapter sessionAdapter = new HttpSessionAdapter(request);
setSessionAdapter(sessionAdapter);
HttpServletRequestAdapter requestAdapter = new HttpServletRequestAdapter(
getTranslet().getRequestMethod(), request);
if (getParentActivity() == null) {
String maxRequestSizeSetting = getSetting(MAX_REQUEST_SIZE_SETTING_NAME);
if (StringUtils.hasLength(maxRequestSizeSetting)) {
try {
long maxRequestSize = Long.parseLong(maxRequestSizeSetting);
if (maxRequestSize >= 0L) {
requestAdapter.setMaxRequestSize(maxRequestSize);
}
} catch (NumberFormatException e) {
throw new RequestParseException("Illegal value for " + MAX_REQUEST_SIZE_SETTING_NAME +
": " + maxRequestSizeSetting, e);
}
}
String requestEncoding = getIntendedRequestEncoding();
if (requestEncoding != null) {
try {
requestAdapter.setEncoding(requestEncoding);
} catch (UnsupportedEncodingException e) {
throw new RequestParseException("Unable to set request encoding to " + requestEncoding, e);
}
}
}
setRequestAdapter(requestAdapter);
ResponseAdapter responseAdapter = new HttpServletResponseAdapter(response, this);
if (getParentActivity() == null) {
String responseEncoding = getIntendedResponseEncoding();
if (responseEncoding != null) {
responseAdapter.setEncoding(responseEncoding);
}
}
setResponseAdapter(responseAdapter);
} catch (Exception e) {
throw new AdapterException("Failed to adapt for the web activity", e);
}
super.adapt();
}
@Override
protected void parseRequest() throws RequestParseException, ActivityTerminatedException {
if (getParentActivity() == null) {
((HttpServletRequestAdapter)getRequestAdapter()).preparse();
} else {
((HttpServletRequestAdapter)getRequestAdapter()).preparse(
(HttpServletRequestAdapter)getParentActivity().getRequestAdapter());
}
MediaType mediaType = ((HttpServletRequestAdapter)getRequestAdapter()).getMediaType();
if (mediaType != null) {
if (WebRequestBodyParser.isMultipartForm(getRequestAdapter().getRequestMethod(), mediaType)) {
WebRequestBodyParser.parseMultipartFormData(this);
} else if (WebRequestBodyParser.isURLEncodedForm(mediaType)) {
WebRequestBodyParser.parseURLEncodedFormData((WebRequestAdapter)getRequestAdapter());
}
}
super.parseRequest();
}
@Override
protected LocaleResolver resolveLocale() {
LocaleResolver localeResolver = super.resolveLocale();
if (localeResolver != null) {
String localeChangeInterceptorId = getSetting(LOCALE_CHANGE_INTERCEPTOR_SETTING_NAME);
if (localeChangeInterceptorId != null) {
LocaleChangeInterceptor localeChangeInterceptor = getBean(LocaleChangeInterceptor.class,
localeChangeInterceptorId);
localeChangeInterceptor.handle(getTranslet(), localeResolver);
}
}
return localeResolver;
}
}