KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > SetupRequestEncoding


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.services.impl;
16
17 import java.io.IOException JavaDoc;
18 import java.io.UnsupportedEncodingException JavaDoc;
19
20 import javax.servlet.ServletException JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 import org.apache.hivemind.ApplicationRuntimeException;
25 import org.apache.tapestry.services.ServletRequestServicer;
26 import org.apache.tapestry.services.ServletRequestServicerFilter;
27
28 /**
29  * Analyzes the incoming request to set the correct output encoding.
30  *
31  * @author Howard M. Lewis Ship
32  * @since 4.0
33  */

34 public class SetupRequestEncoding implements ServletRequestServicerFilter
35 {
36     private boolean _skipSet;
37
38     private String JavaDoc _outputEncoding;
39
40     public void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
41             ServletRequestServicer servicer) throws IOException JavaDoc, ServletException JavaDoc
42     {
43         if (!_skipSet)
44         {
45             String JavaDoc encoding = request.getCharacterEncoding();
46
47             if (encoding == null)
48                 setRequestEncodingToOutputEncoding(request);
49         }
50
51         // Next in chain
52

53         servicer.service(request, response);
54     }
55
56     private void setRequestEncodingToOutputEncoding(HttpServletRequest JavaDoc request)
57     {
58         try
59         {
60             // We compile against the 2.3 API but allow
61
// the code to execute against the 2.2 In the
62
// later case, we can get some interesting errors.
63

64             request.setCharacterEncoding(_outputEncoding);
65         }
66         catch (UnsupportedEncodingException JavaDoc ex)
67         {
68             throw new ApplicationRuntimeException(
69                     ImplMessages.invalidEncoding(_outputEncoding, ex), ex);
70         }
71         catch (NoSuchMethodError JavaDoc ex)
72         {
73             _skipSet = true;
74         }
75         catch (AbstractMethodError JavaDoc ex)
76         {
77             _skipSet = true;
78         }
79     }
80
81     public void setOutputEncoding(String JavaDoc outputEncoding)
82     {
83         _outputEncoding = outputEncoding;
84     }
85 }
Popular Tags