001package org.apache.turbine.pipeline; 002 003 004/* 005 * Licensed to the Apache Software Foundation (ASF) under one 006 * or more contributor license agreements. See the NOTICE file 007 * distributed with this work for additional information 008 * regarding copyright ownership. The ASF licenses this file 009 * to you under the Apache License, Version 2.0 (the 010 * "License"); you may not use this file except in compliance 011 * with the License. You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, 016 * software distributed under the License is distributed on an 017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 018 * KIND, either express or implied. See the License for the 019 * specific language governing permissions and limitations 020 * under the License. 021 */ 022 023 024import java.io.IOException; 025import java.io.UnsupportedEncodingException; 026import java.nio.charset.Charset; 027 028import javax.servlet.http.HttpServletRequest; 029 030import org.apache.logging.log4j.LogManager; 031import org.apache.logging.log4j.Logger; 032import org.apache.turbine.Turbine; 033import org.apache.turbine.util.LocaleUtils; 034import org.apache.turbine.util.TurbineException; 035 036/** 037 * Set default encoding of the request. The default behavior is to respond 038 * with the charset that was requested. If the configuration sets a property named 039 * "locale.override.charset", the output encoding will always be set to its value, 040 * no matter what the input encoding is. 041 * 042 * This valve must be situated in the pipeline before any access to the 043 * {@link org.apache.fulcrum.parser.ParameterParser} to take effect. 044 * 045 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a> 046 */ 047public class DefaultSetEncodingValve 048 implements Valve 049{ 050 private static final Logger log = LogManager.getLogger(DefaultSetEncodingValve.class); 051 052 /** 053 * @see org.apache.turbine.pipeline.Valve#invoke(PipelineData, ValveContext) 054 */ 055 @Override 056 public void invoke(PipelineData pipelineData, ValveContext context) 057 throws IOException, TurbineException 058 { 059 HttpServletRequest req = pipelineData.get(Turbine.class, HttpServletRequest.class); 060 061 // If the servlet container gives us no clear indication about the 062 // encoding of the contents, set it to our default value. 063 String requestEncoding = req.getCharacterEncoding(); 064 065 if (requestEncoding == null) 066 { 067 requestEncoding = LocaleUtils.getDefaultInputEncoding(); 068 log.debug("Changing Input Encoding to {}", requestEncoding); 069 070 try 071 { 072 req.setCharacterEncoding(requestEncoding); 073 } 074 catch (UnsupportedEncodingException uee) 075 { 076 throw new TurbineException("Could not change request encoding to " + requestEncoding, uee); 077 } 078 } 079 080 // Copy encoding charset to RunData to set a reasonable default for the response 081 Charset outputEncoding = LocaleUtils.getOverrideCharset(); 082 if (outputEncoding == null) 083 { 084 outputEncoding = Charset.forName(requestEncoding); 085 } 086 087 pipelineData.getRunData().setCharset(outputEncoding); 088 089 // Pass control to the next Valve in the Pipeline 090 context.invokeNext(pipelineData); 091 } 092}