1 package org.apache.turbine.pipeline;
2
3
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22
23
24 import java.io.IOException;
25 import java.io.UnsupportedEncodingException;
26 import java.nio.charset.Charset;
27
28 import javax.servlet.http.HttpServletRequest;
29
30 import org.apache.logging.log4j.LogManager;
31 import org.apache.logging.log4j.Logger;
32 import org.apache.turbine.Turbine;
33 import org.apache.turbine.util.LocaleUtils;
34 import org.apache.turbine.util.TurbineException;
35
36 /**
37 * Set default encoding of the request. The default behavior is to respond
38 * with the charset that was requested. If the configuration sets a property named
39 * "locale.override.charset", the output encoding will always be set to its value,
40 * no matter what the input encoding is.
41 *
42 * This valve must be situated in the pipeline before any access to the
43 * {@link org.apache.fulcrum.parser.ParameterParser} to take effect.
44 *
45 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
46 */
47 public class DefaultSetEncodingValve
48 implements Valve
49 {
50 private static final Logger log = LogManager.getLogger(DefaultSetEncodingValve.class);
51
52 /**
53 * @see org.apache.turbine.pipeline.Valve#invoke(PipelineData, ValveContext)
54 */
55 @Override
56 public void invoke(PipelineData pipelineData, ValveContext context)
57 throws IOException, TurbineException
58 {
59 HttpServletRequest req = pipelineData.get(Turbine.class, HttpServletRequest.class);
60
61 // If the servlet container gives us no clear indication about the
62 // encoding of the contents, set it to our default value.
63 String requestEncoding = req.getCharacterEncoding();
64
65 if (requestEncoding == null)
66 {
67 requestEncoding = LocaleUtils.getDefaultInputEncoding();
68 log.debug("Changing Input Encoding to {}", requestEncoding);
69
70 try
71 {
72 req.setCharacterEncoding(requestEncoding);
73 }
74 catch (UnsupportedEncodingException uee)
75 {
76 throw new TurbineException("Could not change request encoding to " + requestEncoding, uee);
77 }
78 }
79
80 // Copy encoding charset to RunData to set a reasonable default for the response
81 Charset outputEncoding = LocaleUtils.getOverrideCharset();
82 if (outputEncoding == null)
83 {
84 outputEncoding = Charset.forName(requestEncoding);
85 }
86
87 pipelineData.getRunData().setCharset(outputEncoding);
88
89 // Pass control to the next Valve in the Pipeline
90 context.invokeNext(pipelineData);
91 }
92 }