View Javadoc

1   package org.apache.myfaces.tobago.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.myfaces.tobago.portlet.PortletUtils;
23  
24  import javax.faces.context.FacesContext;
25  import javax.servlet.http.HttpServletRequest;
26  import java.io.UnsupportedEncodingException;
27  
28  /*
29   * Date: 03.07.2006
30   * Time: 21:19:38
31   */
32  public class RequestUtils {
33  
34    private static final Log LOG = LogFactory.getLog(RequestUtils.class);
35  
36    public static void ensureEncoding(FacesContext facesContext) {
37      Object requestObject = facesContext.getExternalContext().getRequest();
38      try {
39        if (requestObject instanceof HttpServletRequest) {
40          HttpServletRequest request = (HttpServletRequest) requestObject;
41          if (request.getCharacterEncoding() == null) {
42            request.setCharacterEncoding("UTF-8");
43          }
44        } else if (PortletUtils.isPortletRequest(facesContext)) {
45          PortletUtils.ensureEncoding(facesContext);
46        }
47  
48      } catch (UnsupportedEncodingException e) {
49        LOG.error("" + e, e);
50      }
51    }
52  }