Home » xwork-2.1.5 » com.opensymphony » xwork2 » interceptor » [javadoc | source]

    1   /*
    2    * Copyright (c) 2002-2007 by OpenSymphony
    3    * All rights reserved.
    4    */
    5   package com.opensymphony.xwork2.interceptor;
    6   
    7   import com.opensymphony.xwork2.util.TextParseUtil;
    8   import com.opensymphony.xwork2.util.WildcardHelper;
    9   
   10   import java.util.HashMap;
   11   import java.util.Iterator;
   12   import java.util.Set;
   13   
   14   /**
   15    * Utility class contains common methods used by 
   16    * {@link com.opensymphony.xwork2.interceptor.MethodFilterInterceptor}.
   17    * 
   18    * @author tm_jee
   19    */
   20   public class MethodFilterInterceptorUtil {
   21   
   22   	/**
   23        * Static method to decide if the specified <code>method</code> should be
   24        * apply (not filtered) depending on the set of <code>excludeMethods</code> and 
   25        * <code>includeMethods</code>. 
   26        *
   27        * <ul>
   28        * <li>
   29        * 	<code>includeMethods</code> takes precedence over <code>excludeMethods</code>
   30        * </li>
   31        * </ul>
   32        * <b>Note:</b> Supports wildcard listings in includeMethods/excludeMethods
   33        *
   34        * @param excludeMethods  list of methods to exclude.
   35        * @param includeMethods  list of methods to include.
   36        * @param method the specified method to check
   37        * @return <tt>true</tt> if the method should be applied.
   38        */
   39       public static boolean applyMethod(Set<String> excludeMethods, Set<String> includeMethods, String method) {
   40           
   41           // quick check to see if any actual pattern matching is needed
   42           boolean needsPatternMatch = false;
   43           Iterator quickIter = includeMethods.iterator();
   44           for (String incMeth : includeMethods) {
   45               if (!"*".equals(incMeth) && incMeth.contains("*")) {
   46                   needsPatternMatch = true;
   47               }
   48           }
   49           
   50           for (String incMeth : excludeMethods) {
   51               if (!"*".equals(incMeth) && incMeth.contains("*")) {
   52                   needsPatternMatch = true;
   53               }
   54           }
   55   
   56           // this section will try to honor the original logic, while 
   57           // still allowing for wildcards later
   58           if (!needsPatternMatch && (includeMethods.contains("*") || includeMethods.size() == 0) ) {
   59               if (excludeMethods != null 
   60                       && excludeMethods.contains(method) 
   61                       && !includeMethods.contains(method) ) {
   62                   return false;
   63               }
   64           }
   65           
   66           // test the methods using pattern matching
   67           WildcardHelper wildcard = new WildcardHelper();
   68           String methodCopy ;
   69           if (method == null ) { // no method specified
   70               methodCopy = "";
   71           }
   72           else {
   73               methodCopy = new String(method);
   74           }
   75           for (String pattern : includeMethods) {
   76               if (pattern.contains("*")) {
   77                   int[] compiledPattern = wildcard.compilePattern(pattern);
   78                   HashMap<String,String> matchedPatterns = new HashMap<String, String>();
   79                   boolean matches = wildcard.match(matchedPatterns, methodCopy, compiledPattern);
   80                   if (matches) {
   81                       return true; // run it, includeMethods takes precedence
   82                   }
   83               }
   84               else {
   85                   if (pattern.equals(methodCopy)) {
   86                       return true; // run it, includeMethods takes precedence
   87                   }
   88               }
   89           }
   90           if (excludeMethods.contains("*") ) {
   91               return false;
   92           }
   93   
   94           // CHECK ME: Previous implementation used include method 
   95           for ( String pattern : excludeMethods) {
   96               if (pattern.contains("*")) {
   97                   int[] compiledPattern = wildcard.compilePattern(pattern);
   98                   HashMap<String,String> matchedPatterns = new HashMap<String, String>();
   99                   boolean matches = wildcard.match(matchedPatterns, methodCopy, compiledPattern);
  100                   if (matches) {
  101                       // if found, and wasn't included earlier, don't run it
  102                       return false; 
  103                   }
  104               }
  105               else {
  106                   if (pattern.equals(methodCopy)) {
  107                       // if found, and wasn't included earlier, don't run it
  108                       return false; 
  109                   }
  110               }
  111           }
  112       
  113   
  114           // default fall-back from before changes
  115           return includeMethods.size() == 0 || includeMethods.contains(method) || includeMethods.contains("*");
  116       }
  117       
  118       /**
  119        * Same as {@link #applyMethod(Set, Set, String)}, except that <code>excludeMethods</code>
  120        * and <code>includeMethods</code> are supplied as comma separated string.
  121        * 
  122        * @param excludeMethods  comma seperated string of methods to exclude.
  123        * @param includeMethods  comma seperated string of methods to include.
  124        * @param method the specified method to check
  125        * @return <tt>true</tt> if the method should be applied.
  126        */
  127       public static boolean applyMethod(String excludeMethods, String includeMethods, String method) {
  128       	Set<String> includeMethodsSet = TextParseUtil.commaDelimitedStringToSet(includeMethods == null? "" : includeMethods);
  129       	Set<String> excludeMethodsSet = TextParseUtil.commaDelimitedStringToSet(excludeMethods == null? "" : excludeMethods);
  130       	
  131       	return applyMethod(excludeMethodsSet, includeMethodsSet, method);
  132       }
  133   
  134   }

Home » xwork-2.1.5 » com.opensymphony » xwork2 » interceptor » [javadoc | source]