1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.latka.jelly; |
18 | |
|
19 | |
import java.io.IOException; |
20 | |
import java.net.URL; |
21 | |
|
22 | |
import org.apache.commons.jelly.JellyTagException; |
23 | |
import org.apache.commons.jelly.TagSupport; |
24 | |
import org.apache.commons.jelly.XMLOutput; |
25 | |
|
26 | |
import org.apache.commons.latka.LatkaException; |
27 | |
import org.apache.commons.latka.event.LatkaEventInfo; |
28 | |
import org.apache.commons.latka.event.RequestErrorEvent; |
29 | |
import org.apache.commons.latka.event.RequestSkippedEvent; |
30 | |
import org.apache.commons.latka.event.RequestSucceededEvent; |
31 | |
import org.apache.commons.latka.http.Proxy; |
32 | |
import org.apache.commons.latka.http.Request; |
33 | |
import org.apache.commons.latka.http.Response; |
34 | |
import org.apache.commons.latka.http.Session; |
35 | |
import org.apache.commons.latka.http.SessionImpl; |
36 | |
|
37 | |
import org.apache.log4j.Category; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class RequestTag extends TagSupport { |
44 | |
|
45 | 0 | protected String _host = null; |
46 | 0 | protected int _port = -1; |
47 | 0 | protected String _proxyHost = null; |
48 | 0 | protected int _proxyPort = -1; |
49 | 0 | protected String _label = null; |
50 | 0 | protected int _method = Request.HTTP_METHOD_GET; |
51 | 0 | protected String _path = null; |
52 | 0 | protected boolean _secure = false; |
53 | 0 | protected boolean _followRedirects = true; |
54 | 0 | protected String _httpVersion = "1.1"; |
55 | |
|
56 | 0 | protected Request _request = null; |
57 | 0 | protected Response _response = null; |
58 | 0 | protected Session _session = null; |
59 | 0 | protected boolean _requestExecuted = false; |
60 | |
|
61 | 0 | protected static final Category _log = Category.getInstance(RequestTag.class); |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public void doTag(XMLOutput xmlOutput) throws JellyTagException { |
70 | |
try { |
71 | 0 | _request = createRequest(); |
72 | 0 | } catch (LatkaException e) { |
73 | 0 | throw new JellyTagException("could not create HTTP request",e); |
74 | 0 | } |
75 | |
|
76 | 0 | LatkaEventInfo listener = |
77 | |
JellyUtils.getInstance().getLatkaEventInfo(getContext()); |
78 | 0 | if (listener.didSessionSucceed(findSession()) == false) { |
79 | 0 | listener.requestSkipped(new RequestSkippedEvent(_request,null)); |
80 | 0 | return; |
81 | |
} |
82 | |
|
83 | |
|
84 | 0 | invokeBody(xmlOutput); |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | 0 | Response response = null; |
92 | |
try { |
93 | 0 | response = getResponse(); |
94 | 0 | } catch (LatkaException e) { |
95 | 0 | throw new JellyTagException("could not obtain HTTP response",e); |
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
|
100 | 0 | if (response != null && _label != null) { |
101 | 0 | getContext().setVariable(_label, response); |
102 | |
} |
103 | |
|
104 | 0 | if (listener.didRequestSucceed(_request)) { |
105 | 0 | listener.requestSucceeded(new RequestSucceededEvent( |
106 | |
response.getRequest(), response)); |
107 | |
} |
108 | |
|
109 | 0 | } |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
private Request createRequest() throws LatkaException { |
119 | 0 | String host = _host; |
120 | 0 | int port = _port; |
121 | 0 | String proxyHost = _proxyHost; |
122 | 0 | int proxyPort = _proxyPort; |
123 | |
|
124 | 0 | if (host == null || port == -1 || proxyHost == null || proxyPort == -1) { |
125 | 0 | SuiteSettings settings = getSuiteSettings(); |
126 | 0 | if (host == null) { |
127 | 0 | host = settings.getDefaultHost(); |
128 | |
} |
129 | 0 | if (port == -1) { |
130 | 0 | port = settings.getDefaultPort(); |
131 | |
} |
132 | 0 | if (proxyHost == null) { |
133 | 0 | proxyHost = settings.getDefaultProxyHost(); |
134 | |
} |
135 | 0 | if (proxyPort == -1) { |
136 | 0 | proxyPort = settings.getDefaultProxyPort(); |
137 | |
} |
138 | |
} |
139 | |
|
140 | 0 | Session session = findSession(); |
141 | |
|
142 | 0 | Proxy proxy = null; |
143 | 0 | if (proxyHost != null) { |
144 | 0 | proxy = new Proxy(proxyHost,proxyPort); |
145 | |
} |
146 | |
|
147 | 0 | URL url = null; |
148 | |
try { |
149 | 0 | url = new URL(_secure ? "https" : "http", host, port, _path); |
150 | 0 | } catch (IOException e) { |
151 | 0 | throw new LatkaException(e); |
152 | 0 | } |
153 | 0 | return session.createRequest(_label,url,_method,_httpVersion,_followRedirects,proxy); |
154 | |
} |
155 | |
|
156 | |
public Request getRequest() { |
157 | 0 | return _request; |
158 | |
} |
159 | |
|
160 | |
protected Session findSession() { |
161 | 0 | if (_session == null) { |
162 | 0 | SessionTag tag = (SessionTag) findAncestorWithClass(SessionTag.class); |
163 | 0 | if (tag == null) { |
164 | 0 | _session = new SessionImpl(); |
165 | |
} else { |
166 | 0 | _session = tag.getSession(); |
167 | |
} |
168 | |
} |
169 | |
|
170 | 0 | return _session; |
171 | |
} |
172 | |
|
173 | |
public boolean getRequestExecuted() { |
174 | 0 | return _requestExecuted; |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public Response getResponse() throws LatkaException { |
188 | 0 | if (_requestExecuted == false) { |
189 | 0 | _requestExecuted = true; |
190 | |
|
191 | 0 | LatkaEventInfo listener = |
192 | |
JellyUtils.getInstance().getLatkaEventInfo(getContext()); |
193 | |
try { |
194 | 0 | _response = _request.execute(); |
195 | |
|
196 | 0 | _log.warn("Eventually this debug needs to go."); |
197 | 0 | if (_log.isDebugEnabled()) { |
198 | 0 | _log.debug(_response.getResource()); |
199 | |
} |
200 | 0 | } catch (IOException e) { |
201 | 0 | listener.requestError(new RequestErrorEvent(_request, null, e)); |
202 | 0 | return null; |
203 | 0 | } |
204 | |
|
205 | |
|
206 | |
|
207 | 0 | _request = _response.getRequest(); |
208 | |
} |
209 | |
|
210 | 0 | return _response; |
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
protected SuiteSettings getSuiteSettings() { |
219 | 0 | SuiteTag tag = |
220 | |
(SuiteTag) findAncestorWithClass(org.apache.commons.latka.jelly.SuiteTag.class); |
221 | 0 | return tag.getSuiteSettings(); |
222 | |
} |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
public void setHost(String host) { |
231 | 0 | _host = host; |
232 | 0 | } |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
public void setPort(int port) { |
241 | 0 | _port = port; |
242 | 0 | } |
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
public void setProxyHost(String host) { |
252 | 0 | _proxyHost = host; |
253 | 0 | } |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
public void setProxyPort(int port) { |
263 | 0 | _proxyPort = port; |
264 | 0 | } |
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
public void setLabel(String label) { |
272 | 0 | _label = label; |
273 | 0 | } |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
public void setMethod(String method) throws UnsupportedOperationException { |
284 | 0 | if (method.equals("get")) { |
285 | 0 | _method = Request.HTTP_METHOD_GET; |
286 | 0 | } else if (method.equals("post")) { |
287 | 0 | _method = Request.HTTP_METHOD_POST; |
288 | 0 | } else if (method.equals("head")) { |
289 | 0 | _method = Request.HTTP_METHOD_HEAD; |
290 | |
} else { |
291 | 0 | throw new UnsupportedOperationException("Unkonwn HTTP method: " + method); |
292 | |
} |
293 | 0 | } |
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
public void setPath(String path) { |
302 | 0 | _path = path; |
303 | 0 | } |
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
public void setSecure(String secure) { |
312 | 0 | _secure = Boolean.valueOf(secure).booleanValue(); |
313 | 0 | } |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
public void setFollowRedirects(String followRedirects) { |
323 | 0 | _followRedirects = Boolean.valueOf(followRedirects).booleanValue(); |
324 | 0 | } |
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
public void setVersion(String version) { |
333 | 0 | _httpVersion = version; |
334 | 0 | } |
335 | |
|
336 | |
} |