1 64 65 package com.jcorporate.expresso.core.security.filters; 66 67 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 68 import org.apache.log4j.Logger; 69 70 import java.util.HashMap ; 71 72 73 94 public class FilterManager { 95 static private FilterManager theManager; 96 private HashMap filterList; 97 private static Logger log = Logger.getLogger(FilterManager.class); 98 99 102 public static final String STANDARD_FILTER = "standardFilter"; 103 106 public static final String STRIP_FILTER = "stripFilter"; 107 110 public static final String RAW_FILTER = "rawFilter"; 111 112 120 public FilterManager() { 121 filterList = new HashMap (3); 125 } 126 127 134 synchronized static public FilterManager getInstance() { 135 if (theManager == null) { 136 theManager = new FilterManager(); 137 } 138 139 return theManager; 140 } 141 142 155 private String prepareFilterClassName(String characterSetName) { 156 char c; 157 int length = characterSetName.length(); 158 FastStringBuffer result = FastStringBuffer.getInstance(); 159 String returnValue = null; 160 try { 161 result.append("com.jcorporate.expresso.core.security.filters."); 162 for (int i = 0; i < length; i++) { 163 c = characterSetName.charAt(i); 164 result.append((c == '-') ? '_' : c); 165 } 166 returnValue = result.toString(); 167 } finally { 168 result.release(); 169 result = null; 170 } 171 172 return returnValue; 173 } 174 175 192 public String filterString(String data, Class filterClass, String filterMethod) 193 throws IllegalArgumentException , Exception { 194 if (data == null) { 195 return null; 196 } 197 198 if (filterClass == null) { 199 filterClass = HtmlFilter.class; 200 } 201 202 Filter f = (Filter) filterList.get(filterClass.getName()); 203 204 if (f == null) { 207 208 try { 209 f = (Filter) filterClass.newInstance(); 210 filterList.put(filterClass.getName(), f); 211 } catch (IllegalAccessException ex) { 212 log.error("Unable to get access to Filters package" + 213 "You must allow the security manager to have access to: " + 214 filterClass.getName()); 215 216 return data; 217 } catch (InstantiationException ex) { 218 log.error("Unable to instantiate Filter class ", ex); 219 return data; 220 221 } catch (ClassCastException ex) { 222 log.error("Classes used for filters must extend " 223 + Filter.class.getName(), ex); 224 return data; 225 } 226 } 227 if (STANDARD_FILTER.equalsIgnoreCase(filterMethod)) { 231 return f.standardFilter(data); 232 } else if (STRIP_FILTER.equalsIgnoreCase(filterMethod)) { 233 return f.stripFilter(data); 234 } else if (RAW_FILTER.equalsIgnoreCase(filterMethod)) { 235 return f.rawFilter(data); 236 } else { 237 throw new IllegalArgumentException ("Undefined Filter Method: " + 238 filterMethod); 239 } 240 } 241 242 } 243 | Popular Tags |