01: /*
02: * @(#)ServletEncoding.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.servlet;
10:
11: import javax.servlet.*;
12: import javax.servlet.http.*;
13: import pnuts.servlet.*;
14: import pnuts.lang.Context;
15:
16: class ServletEncoding {
17:
18: /**
19: * Gets the default encoding for input.
20: * This function returns request.getCharacterEncoding() value if it is non-null,
21: * otherwise response.getCharacterEncoding() value, if it is non-null.
22: * If both are null, it returns "UTF8".
23: *
24: * @param context the context
25: * @return the encoding name
26: */
27: public static String getDefaultInputEncoding(Context context) {
28: HttpServletRequest request = (HttpServletRequest) context
29: .get(PnutsServlet.SERVLET_REQUEST);
30: if (request == null) {
31: throw new IllegalStateException();
32: }
33: String encoding = request.getCharacterEncoding();
34: if (encoding == null) {
35: ServletResponse response = (ServletResponse) context
36: .get(PnutsServlet.SERVLET_RESPONSE);
37: encoding = response.getCharacterEncoding();
38: }
39: if (encoding == null) {
40: encoding = "UTF-8";
41: }
42: return encoding;
43: }
44:
45: /**
46: * Gets the default encoding for output.
47: * This function returns response.getCharacterEncoding() if it is non-null, otherwise "UTF8".
48: *
49: * @param context the context
50: * @return the encoding name
51: */
52: public static String getDefaultOutputEncoding(Context context) {
53: ServletResponse response = (ServletResponse) context
54: .get(PnutsServlet.SERVLET_RESPONSE);
55: if (response == null) {
56: throw new IllegalStateException();
57: }
58: String encoding = response.getCharacterEncoding();
59: if (encoding == null) {
60: encoding = "UTF-8";
61: }
62: return encoding;
63: }
64: }
|