All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.catalina.filters.SetCharacterEncodingFilter Maven / Gradle / Ivy

There is a newer version: 11.0.0-M24
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.catalina.filters;

import java.io.IOException;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;


/**
 * 

* Example filter that sets the character encoding to be used in parsing the incoming request, either unconditionally or * only if the client did not specify a character encoding. Configuration of this filter is based on the following * initialization parameters: *

*
    *
  • encoding - The character encoding to be configured for this request, either conditionally or * unconditionally based on the ignore initialization parameter. This parameter is required, so there is no * default.
  • *
  • ignore - If set to "true", any character encoding specified by the client is ignored, and the * value returned by the selectEncoding() method is set. If set to "false, selectEncoding() is * called only if the client has not already specified an encoding. By default, this parameter is set * to "false".
  • *
*

* Although this filter can be used unchanged, it is also easy to subclass it and make the selectEncoding() * method more intelligent about what encoding to choose, based on characteristics of the incoming request (such as the * values of the Accept-Language and User-Agent headers, or a value stashed in the current * user's session. *

*/ public class SetCharacterEncodingFilter extends FilterBase { // Log must be non-static as loggers are created per class-loader and this // Filter may be used in multiple class loaders private final Log log = LogFactory.getLog(SetCharacterEncodingFilter.class); // must not be static // ----------------------------------------------------- Instance Variables /** * The default character encoding to set for requests that pass through this filter. */ private String encoding = null; public void setEncoding(String encoding) { this.encoding = encoding; } public String getEncoding() { return encoding; } /** * Should a character encoding specified by the client be ignored? */ private boolean ignore = false; public void setIgnore(boolean ignore) { this.ignore = ignore; } public boolean isIgnore() { return ignore; } // --------------------------------------------------------- Public Methods /** * Select and set (if specified) the character encoding to be used to interpret request parameters for this request. * * @param request The servlet request we are processing * @param response The servlet response we are creating * @param chain The filter chain we are processing * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet error occurs */ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // Conditionally select and set the character encoding to be used if (ignore || (request.getCharacterEncoding() == null)) { String characterEncoding = selectEncoding(request); if (characterEncoding != null) { request.setCharacterEncoding(characterEncoding); } } // Pass control on to the next filter chain.doFilter(request, response); } // ------------------------------------------------------ Protected Methods @Override protected Log getLogger() { return log; } /** * Select an appropriate character encoding to be used, based on the characteristics of the current request and/or * filter initialization parameters. If no character encoding should be set, return null. *

* The default implementation unconditionally returns the value configured by the encoding * initialization parameter for this filter. * * @param request The servlet request we are processing * * @return the encoding that was configured */ protected String selectEncoding(ServletRequest request) { return this.encoding; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy