1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts.connector;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.springframework.extensions.surf.exception.AuthenticationException;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public class AuthenticatingConnector implements Connector
62 {
63 protected static Log logger = LogFactory.getLog(AuthenticatingConnector.class);
64 protected Connector connector = null;
65 protected Authenticator authenticator = null;
66
67
68
69
70
71
72
73 public AuthenticatingConnector(Connector connector, Authenticator authenticator)
74 {
75 this.connector = connector;
76 this.authenticator = authenticator;
77 }
78
79
80
81
82 public Response call(String uri)
83 {
84 Response response = null;
85 boolean handshake = false;
86 boolean firstcall = true;
87
88 if (isAuthenticated())
89 {
90
91 response = this.connector.call(uri);
92 firstcall = false;
93
94 if (logger.isDebugEnabled())
95 logger.debug("Received " + response.getStatus().getCode() + " on first call to: " + uri);
96
97
98 if (response.getStatus().getCode() == ResponseStatus.STATUS_UNAUTHORIZED)
99 {
100 handshake = true;
101 }
102 }
103 else
104 {
105 handshake = true;
106 }
107
108 if (handshake)
109 {
110 handshake();
111
112
113 response = this.connector.call(uri);
114
115 if (logger.isDebugEnabled())
116 logger.debug("Received " + response.getStatus().getCode() + " on " +
117 (firstcall ? "first" : "second") + " call to: " + uri);
118 }
119
120 return response;
121 }
122
123
124
125
126 public Response call(String uri, ConnectorContext context)
127 {
128 Response response = null;
129 boolean handshake = false;
130 boolean firstcall = true;
131
132 if (isAuthenticated())
133 {
134
135 response = this.connector.call(uri, context);
136 firstcall = false;
137
138 if (logger.isDebugEnabled())
139 logger.debug("Received " + response.getStatus().getCode() + " on first call to: " + uri);
140
141
142 if (response.getStatus().getCode() == ResponseStatus.STATUS_UNAUTHORIZED)
143 {
144 handshake = true;
145 }
146 }
147 else
148 {
149 handshake = true;
150 }
151
152 if (handshake)
153 {
154 handshake();
155
156
157 response = this.connector.call(uri, context);
158
159 if (logger.isDebugEnabled())
160 logger.debug("Received " + response.getStatus().getCode() + " on " +
161 (firstcall ? "first" : "second") + " call to: " + uri);
162 }
163
164 return response;
165 }
166
167
168
169
170 public Response call(String uri, ConnectorContext context, InputStream in)
171 {
172 Response response = null;
173 boolean handshake = false;
174 boolean firstcall = true;
175
176 if (isAuthenticated())
177 {
178
179 response = this.connector.call(uri, context, in);
180 firstcall = false;
181
182 if (logger.isDebugEnabled())
183 logger.debug("Received " + response.getStatus().getCode() + " on first call to: " + uri);
184
185
186 if (response.getStatus().getCode() == ResponseStatus.STATUS_UNAUTHORIZED)
187 {
188 handshake = true;
189 }
190 }
191 else
192 {
193 handshake = true;
194 }
195
196 if (handshake)
197 {
198 handshake();
199
200
201 if (in.markSupported())
202 {
203 try
204 {
205 in.reset();
206 }
207 catch (IOException ioErr)
208 {
209
210 }
211 }
212 response = this.connector.call(uri, context, in);
213
214 if (logger.isDebugEnabled())
215 logger.debug("Received " + response.getStatus().getCode() + " on " +
216 (firstcall ? "first" : "second") + " call to: " + uri);
217 }
218
219 return response;
220 }
221
222
223
224
225 public Response call(String uri, ConnectorContext context, InputStream in, OutputStream out)
226 {
227 Response response = null;
228 boolean handshake = false;
229 boolean firstcall = true;
230
231 if (isAuthenticated())
232 {
233
234 response = this.connector.call(uri, context, in, out);
235 firstcall = false;
236
237 if (logger.isDebugEnabled())
238 logger.debug("Received " + response.getStatus().getCode() + " on first call to: " + uri);
239
240
241 if (response.getStatus().getCode() == ResponseStatus.STATUS_UNAUTHORIZED)
242 {
243 handshake = true;
244 }
245 }
246 else
247 {
248 handshake = true;
249 }
250
251 if (handshake)
252 {
253 handshake();
254
255
256 if (in.markSupported())
257 {
258 try
259 {
260 in.reset();
261 }
262 catch (IOException ioErr)
263 {
264
265 }
266 }
267 response = this.connector.call(uri, context, in, out);
268
269 if (logger.isDebugEnabled())
270 logger.debug("Received " + response.getStatus().getCode() + " on " +
271 (firstcall ? "first" : "second") + " call to: " + uri);
272 }
273
274 return response;
275 }
276
277
278
279
280 public Response call(String uri, ConnectorContext context, HttpServletRequest req, HttpServletResponse res)
281 {
282 Response response = null;
283 boolean handshake = false;
284 boolean firstcall = true;
285
286 if (isAuthenticated())
287 {
288
289 response = this.connector.call(uri, context, req, res);
290 firstcall = false;
291
292 if (logger.isDebugEnabled())
293 logger.debug("Received " + response.getStatus().getCode() + " on first call to: " + uri);
294
295
296 if (response.getStatus().getCode() == ResponseStatus.STATUS_UNAUTHORIZED)
297 {
298 handshake = true;
299 }
300 }
301 else
302 {
303 handshake = true;
304 }
305
306 if (handshake)
307 {
308 handshake();
309
310
311 response = this.connector.call(uri, context, req, res);
312
313 if (logger.isDebugEnabled())
314 logger.debug("Received " + response.getStatus().getCode() + " on " +
315 (firstcall ? "first" : "second") + " call to: " + uri);
316 }
317
318 return response;
319 }
320
321
322
323
324 public void setCredentials(Credentials credentials)
325 {
326 this.connector.setCredentials(credentials);
327 }
328
329
330
331
332 public Credentials getCredentials()
333 {
334 return this.connector.getCredentials();
335 }
336
337
338
339
340 public void setEndpoint(String endpoint)
341 {
342 this.connector.setEndpoint(endpoint);
343 }
344
345
346
347
348 public String getEndpoint()
349 {
350 return connector.getEndpoint();
351 }
352
353
354
355
356 public void setConnectorSession(ConnectorSession connectorSession)
357 {
358 this.connector.setConnectorSession(connectorSession);
359 }
360
361
362
363
364 public ConnectorSession getConnectorSession()
365 {
366 return this.connector.getConnectorSession();
367 }
368
369
370
371
372
373
374 protected boolean isAuthenticated()
375 {
376 return this.authenticator.isAuthenticated(getEndpoint(), getConnectorSession());
377 }
378
379
380
381
382
383
384
385 final public boolean handshake()
386 {
387 boolean success = false;
388
389 if (logger.isDebugEnabled())
390 logger.debug("Performing authentication handshake");
391
392 if (EndpointManager.allowConnect(getEndpoint()))
393 {
394 ConnectorSession cs = null;
395 try
396 {
397 if (logger.isDebugEnabled())
398 {
399 logger.debug("Authentication handshake using credentials: " + getCredentials());
400 logger.debug("Authentication handshake using connectorSession: " + getConnectorSession());
401 }
402
403 cs = this.authenticator.authenticate(getEndpoint(), getCredentials(), getConnectorSession());
404 }
405 catch (AuthenticationException ae)
406 {
407 logger.error("An exception occurred while attempting authentication handshake for endpoint: " + getEndpoint(), ae);
408 }
409 if (cs != null)
410 {
411 this.setConnectorSession(cs);
412 success = true;
413 }
414 }
415 else
416 {
417 if (logger.isDebugEnabled())
418 logger.debug("Skipping authentication handshake, waiting for reconnect on: " + getEndpoint());
419 }
420
421 return success;
422 }
423
424
425
426
427 @Override
428 public String toString()
429 {
430 return this.connector.toString();
431 }
432 }