1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.taglib.logic;
22
23 import org.apache.struts.taglib.TagUtils;
24
25 import javax.servlet.http.Cookie;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.jsp.JspException;
28
29
30
31
32
33
34
35
36 public class MatchTag extends ConditionalTagBase {
37
38
39
40
41
42
43 protected String location = null;
44
45
46
47
48
49 protected String value = null;
50
51 public String getLocation() {
52 return (this.location);
53 }
54
55 public void setLocation(String location) {
56 this.location = location;
57 }
58
59 public String getValue() {
60 return (this.value);
61 }
62
63 public void setValue(String value) {
64 this.value = value;
65 }
66
67
68
69
70
71
72 public void release() {
73 super.release();
74 location = null;
75 value = null;
76 }
77
78
79
80
81
82
83
84
85
86
87
88 protected boolean condition()
89 throws JspException {
90 return (condition(true));
91 }
92
93
94
95
96
97
98
99
100
101
102 protected boolean condition(boolean desired)
103 throws JspException {
104
105 String variable = null;
106
107 if (cookie != null) {
108 Cookie[] cookies =
109 ((HttpServletRequest) pageContext.getRequest()).getCookies();
110
111 if (cookies == null) {
112 cookies = new Cookie[0];
113 }
114
115 for (int i = 0; i < cookies.length; i++) {
116 if (cookie.equals(cookies[i].getName())) {
117 variable = cookies[i].getValue();
118
119 break;
120 }
121 }
122 } else if (header != null) {
123 variable =
124 ((HttpServletRequest) pageContext.getRequest()).getHeader(header);
125 } else if (name != null) {
126 Object value =
127 TagUtils.getInstance().lookup(pageContext, name, property, scope);
128
129 if (value != null) {
130 variable = value.toString();
131 }
132 } else if (parameter != null) {
133 variable = pageContext.getRequest().getParameter(parameter);
134 } else {
135 JspException e =
136 new JspException(messages.getMessage("logic.selector"));
137
138 TagUtils.getInstance().saveException(pageContext, e);
139 throw e;
140 }
141
142 if (variable == null) {
143 JspException e =
144 new JspException(messages.getMessage("logic.variable", value));
145
146 TagUtils.getInstance().saveException(pageContext, e);
147 throw e;
148 }
149
150
151 boolean matched = false;
152
153 if (location == null) {
154 matched = (variable.indexOf(value) >= 0);
155 } else if (location.equals("start")) {
156 matched = variable.startsWith(value);
157 } else if (location.equals("end")) {
158 matched = variable.endsWith(value);
159 } else {
160 JspException e =
161 new JspException(messages.getMessage("logic.location", location));
162
163 TagUtils.getInstance().saveException(pageContext, e);
164 throw e;
165 }
166
167
168 return (matched == desired);
169 }
170 }