1 13 package info.magnolia.cms.beans.config; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.ItemType; 17 import info.magnolia.cms.util.ClassUtil; 18 import info.magnolia.cms.util.ContentUtil; 19 import info.magnolia.cms.util.FactoryUtil; 20 import info.magnolia.cms.util.ObservationUtil; 21 import info.magnolia.context.MgnlContext; 22 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.Enumeration ; 27 import java.util.HashMap ; 28 import java.util.Hashtable ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import javax.jcr.RepositoryException; 34 import javax.jcr.observation.EventIterator; 35 import javax.jcr.observation.EventListener; 36 import javax.servlet.Filter ; 37 import javax.servlet.FilterConfig ; 38 import javax.servlet.ServletContext ; 39 import javax.servlet.ServletException ; 40 41 import org.apache.commons.lang.ArrayUtils; 42 import org.apache.commons.lang.StringUtils; 43 import org.apache.commons.lang.builder.CompareToBuilder; 44 import org.slf4j.Logger; 45 import org.slf4j.LoggerFactory; 46 47 48 54 public final class FilterManager { 55 56 59 private static final String PARAM_CONFIG = "config"; 60 61 64 private static final String PARAM_PRIORITY = "priority"; 65 66 69 private static final String PARAM_FILTERCLASS = "class"; 70 71 74 private static final String SERVER_FILTERS = "/server/filters"; 75 76 79 protected Logger log = LoggerFactory.getLogger(getClass()); 80 81 private Filter [] filterChain; 82 83 86 private FilterDefinition[] filterDefinitions; 87 88 91 private FilterConfig filterConfig; 92 93 96 public FilterManager() { 97 init(); 98 ObservationUtil.registerChangeListener(ContentRepository.CONFIG, SERVER_FILTERS, new EventListener() { 99 100 public void onEvent(EventIterator arg0) { 101 MgnlContext.setInstance(MgnlContext.getSystemContext()); 102 init(); 103 } 104 }); 105 } 106 107 111 public static FilterManager getInstance() { 112 return (FilterManager) FactoryUtil.getSingleton(FilterManager.class); 113 } 114 115 118 public Filter [] getFilters() { 119 return filterChain; 120 } 121 122 126 public FilterDefinition[] getFilterDefinitions() { 127 return this.filterDefinitions; 128 } 129 130 133 protected void init() { 134 135 extractDefinitions(); 136 137 this.filterChain = new Filter [filterDefinitions.length]; 138 139 for (int j = 0; j < filterDefinitions.length; j++) { 140 FilterDefinition definition = filterDefinitions[j]; 141 try { 142 Filter filter = (Filter ) ClassUtil.newInstance(definition.getClassName()); 143 this.filterChain[j] = filter; 144 } 145 catch (Throwable e) { 146 log.error("Failed to instantiate filter [ " + definition.getClassName() + " ]", e); 147 148 filterDefinitions = (FilterDefinition[]) ArrayUtils.remove(filterDefinitions, j); 150 j--; 151 } 152 } 153 154 if (filterConfig != null) { 157 initFilters(filterConfig); 158 } 159 } 160 161 164 private void extractDefinitions() { 165 List definitionList = new ArrayList (); 166 167 Content node = ContentUtil.getContent(ContentRepository.CONFIG, SERVER_FILTERS); 168 169 if (node != null) { 170 171 Collection children = node.getChildren(ItemType.CONTENT); 172 173 Iterator childIterator = children.iterator(); 174 175 while (childIterator.hasNext()) { 176 Content child = (Content) childIterator.next(); 177 178 String filterClass = child.getNodeData(PARAM_FILTERCLASS).getString(); 179 180 if (StringUtils.isNotEmpty(filterClass)) { 181 FilterDefinition definition = new FilterDefinition(); 182 definition.setClassName(filterClass); 183 definition.setPriority(child.getNodeData(PARAM_PRIORITY).getLong()); 184 185 try { 186 if (child.hasContent(PARAM_CONFIG)) { 187 Content config = child.getContent(PARAM_CONFIG); 188 definition.setParameters(ContentUtil.toMap(config)); 189 } 190 } 191 catch (RepositoryException e) { 192 log.error("Unable to read config parameters for filter {} due to a ", filterClass, e 193 .getClass() 194 .getName()); 195 } 196 197 log.debug("Adding filter [{}] to managed filter list", filterClass); 198 199 definitionList.add(definition); 200 201 } 202 } 203 } 204 205 Collections.sort(definitionList); 206 207 filterDefinitions = (FilterDefinition[]) definitionList.toArray(new FilterDefinition[definitionList.size()]); 208 } 209 210 214 public void initFilters(FilterConfig filterConfig) { 215 this.filterConfig = filterConfig; 216 for (int j = 0; j < getFilterDefinitions().length; j++) { 217 FilterDefinition filter = getFilterDefinitions()[j]; 218 FilterManager.CustomFilterConfig customFilterConfig = new FilterManager.CustomFilterConfig( 219 filterConfig, 220 filter.getParameters()); 221 222 try { 223 getFilters()[j].init(customFilterConfig); 224 } 225 catch (ServletException e) { 226 log.error("Error initializing filter " + filter.getClassName(), e); 227 } 228 229 log.info("Initializing filter {}", filter.getClassName()); 230 } 231 } 232 233 238 public static class FilterDefinition implements Comparable { 239 240 243 private String className; 244 245 248 private long priority; 249 250 253 private Map parameters; 254 255 259 public String getClassName() { 260 return this.className; 261 } 262 263 267 public void setClassName(String className) { 268 this.className = className; 269 } 270 271 275 public Map getParameters() { 276 return this.parameters; 277 } 278 279 283 public void setParameters(Map parameters) { 284 this.parameters = parameters; 285 } 286 287 291 public long getPriority() { 292 return this.priority; 293 } 294 295 299 public void setPriority(long priority) { 300 this.priority = priority; 301 } 302 303 306 public int compareTo(Object object) { 307 FilterDefinition myClass = (FilterDefinition) object; 308 return new CompareToBuilder().append(this.priority, myClass.priority).toComparison(); 309 } 310 311 } 312 313 public static class CustomFilterConfig implements FilterConfig { 314 315 private Map parameters; 316 317 private FilterConfig parent; 318 319 322 public CustomFilterConfig(FilterConfig parent, Map parameters) { 323 super(); 324 this.parent = parent; 325 if (parameters != null) { 326 this.parameters = parameters; 327 } 328 else { 329 this.parameters = new HashMap (); 330 } 331 } 332 333 336 public String getFilterName() { 337 return parent.getFilterName(); 338 } 339 340 343 public String getInitParameter(String name) { 344 return (String ) parameters.get(name); 345 } 346 347 350 public Enumeration getInitParameterNames() { 351 return new Hashtable (parameters).keys(); 352 } 353 354 357 public ServletContext getServletContext() { 358 return parent.getServletContext(); 359 } 360 361 } 362 363 } 364 | Popular Tags |