| 1 | |
package org.webslinger.rules; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.util.List; |
| 5 | |
import javax.servlet.ServletException; |
| 6 | |
import javax.servlet.ServletRequest; |
| 7 | |
|
| 8 | |
import org.webslinger.Webslinger; |
| 9 | |
import org.webslinger.collections.CollectionUtil; |
| 10 | |
|
| 11 | |
public class CSSReqParamSelector extends AbstractSelector { |
| 12 | |
private String name; |
| 13 | |
private String value; |
| 14 | 0 | private Operator operator = Operator.HAS; |
| 15 | |
|
| 16 | |
public CSSReqParamSelector(int id) { |
| 17 | 0 | super(id); |
| 18 | 0 | } |
| 19 | |
|
| 20 | |
public CSSReqParamSelector(Rules p, int id) { |
| 21 | 0 | super(p, id); |
| 22 | 0 | } |
| 23 | |
|
| 24 | |
|
| 25 | |
public Object jjtAccept(RulesVisitor visitor, Object data) { |
| 26 | 0 | return visitor.visit(this, data); |
| 27 | |
} |
| 28 | |
|
| 29 | |
void setName(String name) { |
| 30 | 0 | this.name = name; |
| 31 | 0 | } |
| 32 | |
|
| 33 | |
public String getName() { |
| 34 | 0 | return name; |
| 35 | |
} |
| 36 | |
|
| 37 | |
void setOperator(Operator operator) { |
| 38 | 0 | this.operator = operator; |
| 39 | 0 | } |
| 40 | |
|
| 41 | |
public Operator getOperator() { |
| 42 | 0 | return operator; |
| 43 | |
} |
| 44 | |
|
| 45 | |
void setValue(String value) { |
| 46 | 0 | this.value = value; |
| 47 | 0 | } |
| 48 | |
|
| 49 | |
public String getValue() { |
| 50 | 0 | return value; |
| 51 | |
} |
| 52 | |
|
| 53 | |
public boolean matches(ServletRequest request, Webslinger top) throws IOException, ServletException { |
| 54 | 0 | switch (operator) { |
| 55 | |
case HAS: |
| 56 | 0 | return request.getParameter(getName()) != null; |
| 57 | |
case EQUALS: |
| 58 | 0 | return getValue().equals(request.getParameter(getName())); |
| 59 | |
case CONTAINS: |
| 60 | 0 | String value = (String) request.getParameter(getName()); |
| 61 | 0 | if (value == null) return false; |
| 62 | 0 | List parts = CollectionUtil.split(value, "[, ]"); |
| 63 | 0 | for (int i = 0; i < parts.size(); i++) { |
| 64 | 0 | if (parts.get(i).equals(getValue())) return true; |
| 65 | |
} |
| 66 | 0 | return false; |
| 67 | |
} |
| 68 | 0 | return false; |
| 69 | |
} |
| 70 | |
|
| 71 | |
public void updateSpecificity(int[] specificity, boolean[] isConstantHolder) { |
| 72 | 0 | isConstantHolder[0] = false; |
| 73 | 0 | specificity[Selector.ACTION.ordinal()]++; |
| 74 | 0 | super.updateSpecificity(specificity, isConstantHolder); |
| 75 | 0 | } |
| 76 | |
|
| 77 | |
public String toString() { |
| 78 | 0 | return "ReqParamSelector(" + getName() + ":" + getOperator() + ":" + getValue() + ")"; |
| 79 | |
} |
| 80 | |
} |