1 24 package org.riotfamily.common.web.resource; 25 26 import java.io.FilterReader ; 27 import java.io.Reader ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 32 import javax.servlet.http.HttpServletRequest ; 33 34 import org.riotfamily.common.io.PropertyFilterReader; 35 import org.springframework.util.Assert; 36 import org.springframework.web.servlet.support.RequestContextUtils; 37 38 public class PropertyResourceFilter extends AbstractPathMatchingResourceFilter { 39 40 public static final String CONTEXT_PATH_PROPERTY = "contextPath"; 41 42 public static final String LANGUAGE_PROPERTY = "language"; 43 44 private Properties properties; 45 46 private boolean exposeContextPath = true; 47 48 private boolean exposeLanguage = true; 49 50 public void setProperties(Properties properties) { 51 this.properties = properties; 52 } 53 54 public void setPropertiesMap(Map map) { 55 properties = new Properties (); 56 Iterator it = map.entrySet().iterator(); 57 while (it.hasNext()) { 58 Map.Entry entry = (Map.Entry ) it.next(); 59 Assert.isInstanceOf(String .class, entry.getKey(), 60 "Map must only contain String keys."); 61 62 Assert.isInstanceOf(String .class, entry.getValue(), 63 "Map must only contain String values."); 64 65 String key = (String ) entry.getKey(); 66 String value = (String ) entry.getValue(); 67 properties.setProperty(key, value); 68 } 69 } 70 71 public void setExposeContextPath(boolean exposeContextPath) { 72 this.exposeContextPath = exposeContextPath; 73 } 74 75 public void setExposeLanguage(boolean exposeLanguage) { 76 this.exposeLanguage = exposeLanguage; 77 } 78 79 public FilterReader createFilterReader(Reader in, HttpServletRequest request) { 80 Properties props = new Properties (properties); 81 if (exposeContextPath) { 82 props.setProperty(CONTEXT_PATH_PROPERTY, request.getContextPath()); 83 } 84 if (exposeLanguage) { 85 props.setProperty(LANGUAGE_PROPERTY, 86 RequestContextUtils.getLocale(request).getLanguage().toLowerCase()); 87 } 88 return new PropertyFilterReader(in, props); 89 } 90 91 } 92 | Popular Tags |