Coverage Report - org.apache.turbine.pipeline.DefaultSetEncodingValve
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultSetEncodingValve
88%
15/17
75%
3/4
5
 
 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  
 
 27  
 import javax.servlet.http.HttpServletRequest;
 28  
 
 29  
 import org.apache.logging.log4j.LogManager;
 30  
 import org.apache.logging.log4j.Logger;
 31  
 import org.apache.turbine.Turbine;
 32  
 import org.apache.turbine.util.LocaleUtils;
 33  
 import org.apache.turbine.util.TurbineException;
 34  
 
 35  
 /**
 36  
  * Set default encoding of the request. The default behavior is to respond
 37  
  * with the charset that was requested. If the configuration sets a property named
 38  
  * "locale.override.charset", the output encoding will always be set to its value,
 39  
  * no matter what the input encoding is.
 40  
  *
 41  
  * This valve must be situated in the pipeline before any access to the
 42  
  * {@link org.apache.fulcrum.parser.ParameterParser} to take effect.
 43  
  *
 44  
  * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
 45  
  */
 46  138
 public class DefaultSetEncodingValve
 47  
     implements Valve
 48  
 {
 49  102
     private static final Logger log = LogManager.getLogger(DefaultSetEncodingValve.class);
 50  
 
 51  
     /**
 52  
      * @see org.apache.turbine.pipeline.Valve#invoke(PipelineData, ValveContext)
 53  
      */
 54  
     @Override
 55  
     public void invoke(PipelineData pipelineData, ValveContext context)
 56  
         throws IOException, TurbineException
 57  
     {
 58  9
         HttpServletRequest req = pipelineData.get(Turbine.class, HttpServletRequest.class);
 59  
 
 60  
         // If the servlet container gives us no clear indication about the
 61  
         // encoding of the contents, set it to our default value.
 62  9
         String requestEncoding = req.getCharacterEncoding();
 63  
 
 64  9
         if (requestEncoding == null)
 65  
         {
 66  6
             requestEncoding = LocaleUtils.getDefaultInputEncoding();
 67  6
             log.debug("Changing Input Encoding to {}", requestEncoding);
 68  
 
 69  
             try
 70  
             {
 71  6
                 req.setCharacterEncoding(requestEncoding);
 72  
             }
 73  0
             catch (UnsupportedEncodingException uee)
 74  
             {
 75  0
                 throw new TurbineException("Could not change request encoding to " + requestEncoding, uee);
 76  6
             }
 77  
         }
 78  
 
 79  
         // Copy encoding charset to RunData to set a reasonable default for the response
 80  9
         String outputEncoding = LocaleUtils.getOverrideCharSet();
 81  9
         if (outputEncoding == null)
 82  
         {
 83  9
             outputEncoding = requestEncoding;
 84  
         }
 85  
 
 86  9
         pipelineData.getRunData().setCharSet(outputEncoding);
 87  
 
 88  
         // Pass control to the next Valve in the Pipeline
 89  9
         context.invokeNext(pipelineData);
 90  6
     }
 91  
 }