001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.util;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.apache.myfaces.tobago.portlet.PortletUtils;
025    
026    import javax.faces.context.ExternalContext;
027    import javax.faces.context.FacesContext;
028    import javax.servlet.http.HttpServletRequest;
029    import java.io.UnsupportedEncodingException;
030    
031    /*
032     * Date: 03.07.2006
033     * Time: 21:19:38
034     */
035    public class RequestUtils {
036    
037      private static final Log LOG = LogFactory.getLog(RequestUtils.class);
038    
039      @Deprecated
040      public static void ensureEncoding(ExternalContext externalContext) {
041        FacesContext facesContext = FacesContext.getCurrentInstance();
042        if (facesContext.getExternalContext() != externalContext) {
043          throw new RuntimeException("Unexpected behaviour.");
044        }
045        ensureEncoding(facesContext);
046      }
047    
048      public static void ensureEncoding(FacesContext facesContext) {
049        Object requestObject = facesContext.getExternalContext().getRequest();
050        try {
051          if (requestObject instanceof HttpServletRequest) {
052            HttpServletRequest request = (HttpServletRequest) requestObject;
053            if (request.getCharacterEncoding() == null) {
054              request.setCharacterEncoding("UTF-8");
055            }
056          } else if (PortletUtils.isPortletRequest(facesContext)) {
057            PortletUtils.ensureEncoding(facesContext);
058          }
059    
060        } catch (UnsupportedEncodingException e) {
061          LOG.error("" + e, e);
062        }
063      }
064    }