1
2
3
4
5
6
7
8
9
10
11 package org.mule.routing.filters;
12
13 import org.mule.umo.UMOFilter;
14 import org.mule.umo.UMOMessage;
15
16
17
18
19
20
21
22
23
24
25
26 public class MessagePropertyFilter implements UMOFilter
27 {
28 private boolean caseSensitive = true;
29 private boolean not = false;
30
31 private String propertyName;
32 private String propertyValue;
33
34 public MessagePropertyFilter()
35 {
36 super();
37 }
38
39 public MessagePropertyFilter(String expression)
40 {
41 setExpression(expression);
42 }
43
44 public boolean accept(UMOMessage message)
45 {
46 if (message == null)
47 {
48 return false;
49 }
50
51 Object value = message.getProperty(propertyName);
52
53 if (value == null)
54 {
55 return compare(null, propertyValue);
56 }
57 else
58 {
59 return compare(value.toString(), propertyValue);
60 }
61 }
62
63 protected boolean compare(String value1, String value2)
64 {
65 if (value1 == null && value2 != null && !"null".equals(value2) && not)
66 {
67 return true;
68 }
69
70 if (value1 == null)
71 {
72 value1 = "null";
73 }
74
75 if (value2 == null)
76 {
77 value2 = "null";
78 }
79
80 boolean result = false;
81
82 if (caseSensitive)
83 {
84 result = value1.equals(value2);
85 }
86 else
87 {
88 result = value1.equalsIgnoreCase(value2);
89 }
90
91 return (not ? !result : result);
92 }
93
94 public String getExpression()
95 {
96 return propertyName + '=' + propertyValue;
97 }
98
99 public void setExpression(String expression)
100 {
101 int i = expression.indexOf('=');
102 if (i == -1)
103 {
104 throw new IllegalArgumentException(
105 "Pattern is malformed - it should be a key value pair, i.e. property=value: " + expression);
106 }
107 else
108 {
109 if (expression.charAt(i - 1) == '!')
110 {
111 not = true;
112 propertyName = expression.substring(0, i - 1).trim();
113 }
114 else
115 {
116 propertyName = expression.substring(0, i).trim();
117 }
118 propertyValue = expression.substring(i + 1).trim();
119 }
120 }
121
122 public boolean isCaseSensitive()
123 {
124 return caseSensitive;
125 }
126
127 public void setCaseSensitive(boolean caseSensitive)
128 {
129 this.caseSensitive = caseSensitive;
130 }
131
132 }