001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.extensions.util.encoding; 018 019import java.io.IOException; 020import java.io.InputStream; 021 022import javax.servlet.ServletContext; 023 024import org.apache.wicket.Application; 025import org.apache.wicket.Session; 026import org.apache.wicket.WicketRuntimeException; 027import org.apache.wicket.protocol.http.WebApplication; 028import org.apache.wicket.request.cycle.RequestCycle; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032 033/** 034 * Utility class. Initialize Locale to character encoding mapping based on defaults and optionally a 035 * properties file CharSetMap.CHARSET_RESOURCE to be found in WEB-INF. 036 * 037 * @author Jonathan Locke 038 * @author Juergen Donnerstag 039 */ 040public class CharSetUtil 041{ 042 /** Logging */ 043 private static final Logger log = LoggerFactory.getLogger(CharSetUtil.class); 044 045 /** Locale to character encoding mapping */ 046 private static CharSetMap charSetMap; 047 048 /** 049 * Constructor 050 */ 051 public CharSetUtil() 052 { 053 } 054 055 /** 056 * Initialize the mapping table based on defaults and optionally modified by entries from a 057 * properties file to be found in WEB-INF. 058 * 059 * @param application 060 * Wicket application object 061 */ 062 private synchronized static void initialize(final WebApplication application) 063 { 064 if (charSetMap == null) 065 { 066 // Get servlet context 067 final ServletContext context = application.getServletContext(); 068 069 final InputStream inputStream = context.getResourceAsStream("/WEB-INF/" + 070 CharSetMap.CHARSET_RESOURCE); 071 072 if (inputStream == null) 073 { 074 charSetMap = new CharSetMap(); 075 if (log.isDebugEnabled()) 076 { 077 log.debug("File '" + CharSetMap.CHARSET_RESOURCE + "' not found"); 078 } 079 } 080 else 081 { 082 try 083 { 084 charSetMap = new CharSetMap(inputStream); 085 } 086 catch (IOException ex) 087 { 088 throw new WicketRuntimeException("Error while reading CharSetMap", ex); 089 } 090 } 091 } 092 } 093 094 /** 095 * Based on the Session's locale determine the associated character encoding. 096 * 097 * @param cycle 098 * @return Char set to use for response. 099 */ 100 public static String getEncoding(final RequestCycle cycle) 101 { 102 if (charSetMap == null) 103 { 104 initialize((WebApplication)Application.get()); 105 } 106 107 return charSetMap.getCharSet(Session.get().getLocale()); 108 } 109}