OSChina 其中很重要的一类——RequestContext

时间:2023-03-09 17:33:40
OSChina 其中很重要的一类——RequestContext
RequestContext 这个类在 OSChina 中是很重要的一个类。该类由全局 Filter 进行初始化。并传递给包含 Action 和 页面中直接使用。使用时通过 RequestContext.get() 来获取当前请求上下文实例。 这种方法基本的功能包含:自己主动转码、处理文件上传、登录信息处理,以及一些跟请求相关的经常用法封装。 假设你从中发现了什么问题。请指教。

标签: OSCHINA

[1].[代码] java代码 跳至 [1]

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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
package

my.mvc;
import

java.io.*;
import

java.net.*;
import

java.text.*;
import

java.util.*;
import

javax.servlet.*;
import

javax.servlet.http.*;
import

my.util.CryptUtils;
import

my.util.Multimedia;
import

my.util.RequestUtils;
import

my.util.ResourceUtils;
import

net.oschina.beans.User;
import

org.apache.commons.beanutils.BeanUtils;
import

org.apache.commons.beanutils.ConvertUtils;
import

org.apache.commons.beanutils.Converter;
import

org.apache.commons.beanutils.converters.SqlDateConverter;
import

org.apache.commons.codec.binary.Base64;
import

org.apache.commons.io.FileUtils;
import

org.apache.commons.lang.RandomStringUtils;
import

org.apache.commons.lang.StringUtils;
import

org.apache.commons.lang.math.NumberUtils;
import

org.apache.commons.logging.Log;
import

org.apache.commons.logging.LogFactory;
/**
 *
请求上下文
 *
@author Winter Lau
 *
@date 2010-1-13 下午04:18:00
 */
public

class

RequestContext {
     
    private

final

static

Log log = LogFactory.getLog(RequestContext.
class);
    private

final

static

int

MAX_FILE_SIZE =
10*1024*1024;
    private

final

static

String UTF_8 =
"UTF-8";
     
    private

final

static

ThreadLocal<RequestContext> contexts =
new

ThreadLocal<RequestContext>();
    private

final

static

boolean

isResin;
    private

final

static

String upload_tmp_path;
    private

final

static

String TEMP_UPLOAD_PATH_ATTR_NAME =
"$OSCHINA_TEMP_UPLOAD_PATH$";
    private

static

String webroot =
null;
     
    private

ServletContext context;
    private

HttpSession session;
    private

HttpServletRequest request;
    private

HttpServletResponse response;
    private

Map<String, Cookie> cookies;
     
    static

{
        webroot
= getWebrootPath();
        isResin
= _CheckResinVersion();
        //上传的暂时文件夹
        upload_tmp_path
= webroot +
"WEB-INF"

+ File.separator +
"tmp"

+ File.separator;
        try

{
            FileUtils.forceMkdir(new

File(upload_tmp_path));
        }
catch

(IOException excp) {}
         
        //BeanUtils对时间转换的初始化设置
        ConvertUtils.register(new

SqlDateConverter(
null),
java.sql.Date.
class);
        ConvertUtils.register(new

Converter(){
            SimpleDateFormat
sdf =
new

SimpleDateFormat(
"yyyy-M-d");
            SimpleDateFormat
sdf_time =
new

SimpleDateFormat(
"yyyy-M-d
H:m"
);
            @SuppressWarnings("rawtypes")
            public

Object convert(Class type, Object value) {
                if(value
==
null)
return

null
;
                if

(value
instanceof

Date)
return

(value);
                try

{
                    return

sdf_time.parse(value.toString());
                }
catch

(ParseException e) {
                    try

{
                        return

sdf.parse(value.toString());
                    }
catch

(ParseException e1) {
                        return

null
;
                    }
                }
            }},
java.util.Date.
class);
    }
     
    private

final

static

String getWebrootPath() {
        String
root = RequestContext.
class.getResource("/").getFile();
        try

{
            root
=
new

File(root).getParentFile().getParentFile().getCanonicalPath();
            root
+= File.separator;
        }
catch

(IOException e) {
            throw

new

RuntimeException(e);
        }
        return

root;
    }
     
    /**
     *
初始化请求上下文
     *
@param ctx
     *
@param req
     *
@param res
     */
    public

static

RequestContext begin(ServletContext ctx, HttpServletRequest req, HttpServletResponse res) {
        RequestContext
rc =
new

RequestContext();
        rc.context
= ctx;
        rc.request
= _AutoUploadRequest(_AutoEncodingRequest(req));
        rc.response
= res;
        rc.response.setCharacterEncoding(UTF_8);
        rc.session
= req.getSession(
false);
        rc.cookies
=
new

HashMap<String, Cookie>();
        Cookie[]
cookies = req.getCookies();
        if(cookies
!=
null)
            for(Cookie
ck : cookies) {
                rc.cookies.put(ck.getName(),
ck);
            }
        contexts.set(rc);
        return

rc;
    }
    /**
     *
返回Web应用的路径
     *
@return
     */
    public

static

String root() {
return

webroot; }
     
    /**
     *
获取当前请求的上下文
     *
@return
     */
    public

static

RequestContext get(){
        return

contexts.get();
    }
     
    public

void

end() {
        String
tmpPath = (String)request.getAttribute(TEMP_UPLOAD_PATH_ATTR_NAME);
        if(tmpPath
!=
null){
            try

{
                FileUtils.deleteDirectory(new

File(tmpPath));
            }
catch

(IOException e) {
                log.fatal("Failed
to cleanup upload directory: "

+ tmpPath, e);
            }
        }
        this.context
=
null;
        this.request
=
null;
        this.response
=
null;
        this.session
=
null;
        this.cookies
=
null;
        contexts.remove();
    }
     
    public

Locale locale(){
return

request.getLocale(); }
    public

void

closeCache(){
        header("Pragma","No-cache");
        header("Cache-Control","no-cache");
        header("Expires",
0L);
    }
     
    /**
     *
自己主动编码处理
     *
@param req
     *
@return
     */
    private

static

HttpServletRequest _AutoEncodingRequest(HttpServletRequest req) {
        if(req
instanceof

RequestProxy)
            return

req;
        HttpServletRequest
auto_encoding_req = req;
        if("POST".equalsIgnoreCase(req.getMethod())){
            try

{
                auto_encoding_req.setCharacterEncoding(UTF_8);
            }
catch

(UnsupportedEncodingException e) {}
        }
        else

if
(!isResin)
            auto_encoding_req
=
new

RequestProxy(req, UTF_8);
         
        return

auto_encoding_req;
    }
     
    /**
     *
自己主动文件上传请求的封装
     *
@param req
     *
@return
     */
    private

static

HttpServletRequest _AutoUploadRequest(HttpServletRequest req){
        if(_IsMultipart(req)){
            String
path = upload_tmp_path + RandomStringUtils.randomAlphanumeric(
10);
            File
dir =
new

File(path);
            if(!dir.exists()
&& !dir.isDirectory()) dir.mkdirs();
            try{
                req.setAttribute(TEMP_UPLOAD_PATH_ATTR_NAME,path);
                return

new

MultipartRequest(req, dir.getCanonicalPath(), MAX_FILE_SIZE, UTF_8);
            }catch(NullPointerException
e){            
            }catch(IOException
e){
                log.fatal("Failed
to save upload files into temp directory: "

+ path, e);
            }
        }
        return

req;
    }
     
    public

long

id() {
        return

param(
"id",
0L);
    }
     
    public

String ip(){
        return

RequestUtils.getRemoteAddr(request);
    }
     
    @SuppressWarnings("unchecked")
    public

Enumeration<String> params() {
        return

request.getParameterNames();
    }
     
    public

String param(String name, String...def_value) {
        String
v = request.getParameter(name);
        return

(v!=
null)?v:((def_value.length>0)?

def_value[

0]:null);
    }
     
    public

long

param(String name,
long

def_value) {
        return

NumberUtils.toLong(param(name), def_value);
    }
    public

int

param(String name,
int

def_value) {
        return

NumberUtils.toInt(param(name), def_value);
    }
    public

byte

param(String name,
byte

def_value) {
        return

(
byte)NumberUtils.toInt(param(name),
def_value);
    }
    public

String[] params(String name) {
        return

request.getParameterValues(name);
    }
    public

long
[]
lparams(String name){
        String[]
values = params(name);
        if(values==null)
return

null
;
        return

(
long[])ConvertUtils.convert(values,
long.class);
    }
     
    public

String uri(){
        return

request.getRequestURI();
    }
     
    public

String contextPath(){
        return

request.getContextPath();
    }
     
    public

void

redirect(String uri)
throws

IOException {
        response.sendRedirect(uri);
    }
     
    public

void

forward(String uri)
throws

ServletException, IOException {
        RequestDispatcher
rd = context.getRequestDispatcher(uri);
        rd.forward(request,
response);
    }
    public

void

include(String uri)
throws

ServletException, IOException {
        RequestDispatcher
rd = context.getRequestDispatcher(uri);
        rd.include(request,
response);
    }
     
    public

boolean

isUpload(){
        return

(request
instanceof

MultipartRequest);
    }
    public

File file(String fieldName) {
        if(request
instanceof

MultipartRequest)
            return

((MultipartRequest)request).getFile(fieldName);
        return

null
;
    }
    public

File image(String fieldname) {
        File
imgFile = file(fieldname);
        return

(imgFile!=
null&&Multimedia.isImageFile(imgFile.getName()))?imgFile:null;
    }
     
    public

boolean

isRobot(){
        return

RequestUtils.isRobot(request);
    }
    public

ActionException fromResource(String bundle, String key, Object...args){
        String
res = ResourceUtils.getStringForLocale(request.getLocale(), bundle, key, args);
        return

new

ActionException(res);
    }
    public

ActionException error(String key, Object...args){       
        return

fromResource(
"error",
key, args);
    }
     
    /**
     *
输出信息到浏览器
     *
@param msg
     *
@throws IOException
     */
    public

void

print(Object msg)
throws

IOException {
        if(!UTF_8.equalsIgnoreCase(response.getCharacterEncoding()))
            response.setCharacterEncoding(UTF_8);
        response.getWriter().print(msg);
    }
    public

void

output_json(String[] key, Object[] value)
throws

IOException {
        StringBuilder
json =
new

StringBuilder(
"{");
        for(int

i=
0;i<key.length;i++){
            if(i>0)
                json.append(',');
            boolean

isNum = value[i]
instanceof

Number ;
            json.append("\"");
            json.append(key[i]);
            json.append("\":");
            if(!isNum)
json.append(
"\"");
            json.append(value[i]);
            if(!isNum)
json.append(
"\"");
        }
        json.append("}");
        print(json.toString());
    }
    public

void

output_json(String key, Object value)
throws

IOException {
        output_json(new

String[]{key},
new

Object[]{value});
    }
    public

void

error(
int

code, String...msg)
throws

IOException {
        if(msg.length>0)
            response.sendError(code,
msg[
0]);
        else
            response.sendError(code);
    }
     
    public

void

forbidden()
throws

IOException {
        error(HttpServletResponse.SC_FORBIDDEN);
    }
    public

void

not_found()
throws

IOException {
        error(HttpServletResponse.SC_NOT_FOUND);
    }
    public

ServletContext context() {
return

context; }
    public

HttpSession session() {
return

session; }
    public

HttpSession session(
boolean

create) {
        return

(session==
null

&& create)?

(session=request.getSession()):session;

    }
    public

Object sessionAttr(String attr) {
        HttpSession
ssn = session();
        return

(ssn!=
null)?

ssn.getAttribute(attr):

null;
    }
    public

HttpServletRequest request() {
return

request; }
    public

HttpServletResponse response() {
return

response; }
    public

Cookie cookie(String name) {
return

cookies.get(name); }
    public

void

cookie(String name, String value,
int

max_age,
boolean

all_sub_domain) {
        RequestUtils.setCookie(request,
response, name, value, max_age, all_sub_domain);
    }
    public

void

deleteCookie(String name,
boolean

all_domain) { RequestUtils.deleteCookie(request, response, name, all_domain); }
    public

String header(String name) {
return

request.getHeader(name); }
    public

void

header(String name, String value) { response.setHeader(name, value); }
    public

void

header(String name,
int

value) { response.setIntHeader(name, value); }
    public

void

header(String name,
long

value) { response.setDateHeader(name, value); }
    /**
     *
将HTTP请求參数映射到bean对象中
     *
@param req
     *
@param beanClass
     *
@return
     *
@throws Exception
     */
    public

<T> T form(Class<T> beanClass) {
        try{
            T
bean = beanClass.newInstance();
            BeanUtils.populate(bean,
request.getParameterMap());
            return

bean;
        }catch(Exception
e) {
            throw

new

ActionException(e.getMessage());
        }
    }
     
    /**
     *
返回当前登录的用户资料
     *
@return
     */
    public

IUser user() {
        return

User.GetLoginUser(request);
    }
     
    /**
     *
保存登录信息
     *
@param req
     *
@param res
     *
@param user
     *
@param save
     */
    public

void

saveUserInCookie(IUser user,
boolean

save) {
        String
new_value = _GenLoginKey(user, ip(), header(
"user-agent"));
        int

max_age = save ?

MAX_AGE : -

1;
        deleteCookie(COOKIE_LOGIN,
true);
        cookie(COOKIE_LOGIN,new_value,max_age,true);
    }
    public

void

deleteUserInCookie() {
        deleteCookie(COOKIE_LOGIN,
true);
    }
     
    /**
     *
3.0 以上版本号的 Resin 无需对URL參数进行转码
     *
@return
     */
    private

final

static

boolean

_CheckResinVersion() {
        try{
            Class<?

>
verClass = Class.forName(

"com.caucho.Version");
            String
ver = (String)verClass.getDeclaredField(
"VERSION").get(verClass);
            String
mainVer = ver.substring(
0,
ver.lastIndexOf(
'.'));
            /**
            float
fVer = Float.parseFloat(mainVer);
            System.out.println("---------------->
" + fVer);
            */
            return

Float.parseFloat(mainVer) >
3.0;
        }catch(Throwable
t) {}
        return

false
;
    }
    /**
     *
自己主动解码
     *
@author liudong
     */
    private

static

class

RequestProxy
extends

HttpServletRequestWrapper {
        private

String uri_encoding;
        RequestProxy(HttpServletRequest
request, String encoding){
            super(request);
            this.uri_encoding
= encoding;
        }
         
        /**
         *
重载getParameter
         */
        public

String getParameter(String paramName) {
            String
value =
super.getParameter(paramName);
            return

_DecodeParamValue(value);
        }
        /**
         *
重载getParameterMap
         */
        @SuppressWarnings({
"unchecked",
"rawtypes"

})
        public

Map<String, Object> getParameterMap() {
            Map
params =
super.getParameterMap();
            HashMap<String,
Object> new_params =
new

HashMap<String, Object>();
            Iterator<String>
iter = params.keySet().iterator();
            while(iter.hasNext()){
                String
key = (String)iter.next();
                Object
oValue = params.get(key);
                if(oValue.getClass().isArray()){
                    String[]
values = (String[])params.get(key);
                    String[]
new_values =
new

String[values.length];
                    for(int

i=
0;i<values.length;i++)
                        new_values[i]
= _DecodeParamValue(values[i]);
                     
                    new_params.put(key,
new_values);
                }
                else{
                    String
value = (String)params.get(key);
                    String
new_value = _DecodeParamValue(value);
                    if(new_value!=null)
                        new_params.put(key,new_value);
                }
            }
            return

new_params;
        }
        /**
         *
重载getParameterValues
         */
        public

String[] getParameterValues(String arg0) {
            String[]
values =
super.getParameterValues(arg0);
            for(int

i=
0;values!=null&&i<values.length;i++)
                values[i]
= _DecodeParamValue(values[i]);
            return

values;
        }
        /**
         *
參数转码
         *
@param value
         *
@return
         */
        private

String _DecodeParamValue(String value){
            if

(StringUtils.isBlank(value) || StringUtils.isBlank(uri_encoding)
                    ||
StringUtils.isNumeric(value))
                return

value;      
            try{
                return

new

String(value.getBytes(
"8859_1"),
uri_encoding);
            }catch(Exception
e){}
            return

value;
        }
    }
     
    private

static

boolean

_IsMultipart(HttpServletRequest req) {
        return

((req.getContentType() !=
null)
&& (req.getContentType()
                .toLowerCase().startsWith("multipart")));
    }
    /**
     *
生成用户登录标识字符串
     *
@param user
     *
@param ip
     *
@param user_agent
     *
@return
     */
    public

static

String _GenLoginKey(IUser user, String ip, String user_agent) {
        StringBuilder
sb =
new

StringBuilder();
        sb.append(user.getId());
        sb.append('|');
        sb.append(user.getPwd());
        sb.append('|');
        sb.append(ip);
        sb.append('|');
        sb.append((user_agent==null)?0:user_agent.hashCode());
        sb.append('|');
        sb.append(System.currentTimeMillis());
        return

_Encrypt(sb.toString());
    }
    /**
     *
加密
     *
@param value
     *
@return
     *
@throws Exception
     */
    private

static

String _Encrypt(String value) {
        byte[]
data = CryptUtils.encrypt(value.getBytes(), E_KEY);
        try{
            return

URLEncoder.encode(
new

String(Base64.encodeBase64(data)), UTF_8);
        }catch(UnsupportedEncodingException
e){
            return

null
;
        }
    }
    /**
     *
解密
     *
@param value
     *
@return
     *
@throws Exception
     */
    private

static

String _Decrypt(String value) {
        try

{
            value
= URLDecoder.decode(value,UTF_8);
            if(StringUtils.isBlank(value))
return

null
;
            byte[]
data = Base64.decodeBase64(value.getBytes());
            return

new

String(CryptUtils.decrypt(data, E_KEY));
        }
catch

(UnsupportedEncodingException excp) {
            return

null
;
        }
    }  
    /**
     *
从cookie中读取保存的用户信息
     *
@param req
     *
@return
     */
    public

IUser getUserFromCookie() {
        try{
            Cookie
cookie = cookie(COOKIE_LOGIN);
            if(cookie!=null

&& StringUtils.isNotBlank(cookie.getValue())){
                return

userFromUUID(cookie.getValue());
            }
        }catch(Exception
e){}
        return

null
;
    }
    /**
     *
从cookie中读取保存的用户信息
     *
@param req
     *
@return
     */
    public

IUser userFromUUID(String uuid) {
        if(StringUtils.isBlank(uuid))
            return

null
;
        String
ck = _Decrypt(uuid);
        final

String[] items = StringUtils.split(ck,
'|');
        if(items.length
==
5){
            String
ua = header(
"user-agent");
            int

ua_code = (ua==
null)?0:ua.hashCode();
            int

old_ua_code = Integer.parseInt(items[
3]);
            if(ua_code
== old_ua_code){
                return

new

IUser(){
                    public

boolean

IsBlocked() {
return

false
;
}
                    public

long

getId() {
return

NumberUtils.toLong(items[
0],-1L);
}
                    public

String getPwd() {
return

items[
1];
}
                    public

byte

getRole() {
return

IUser.ROLE_GENERAL; }
                };
            }
        }
        return

null
;
    }
     
    public

final

static

String COOKIE_LOGIN =
"oscid";
    private

final

static

int

MAX_AGE =
86400

*
365;
    private

final

static

byte
[]
E_KEY =
new

byte
[]{'1','2','3','4','5','6','7','8'};
}

版权声明:本文博主原创文章,博客,未经同意不得转载。