1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.esigate.authentication;
17
18 import java.util.Properties;
19
20 import org.apache.http.HttpRequest;
21 import org.apache.http.HttpResponse;
22 import org.apache.http.util.EntityUtils;
23 import org.esigate.Driver;
24 import org.esigate.HttpErrorPage;
25 import org.esigate.events.Event;
26 import org.esigate.events.EventDefinition;
27 import org.esigate.events.EventManager;
28 import org.esigate.events.IEventListener;
29 import org.esigate.events.impl.FragmentEvent;
30 import org.esigate.events.impl.ProxyEvent;
31 import org.esigate.extension.Extension;
32 import org.esigate.http.IncomingRequest;
33 import org.esigate.http.OutgoingRequest;
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 public abstract class GenericAuthentificationHandler implements IEventListener, Extension {
59
60 private Driver driver;
61
62
63
64
65
66
67
68
69
70
71
72 public abstract boolean beforeProxy(HttpRequest httpRequest);
73
74
75
76
77
78
79
80
81
82 public abstract void preRequest(OutgoingRequest outgoingRequest, IncomingRequest incomingRequest);
83
84
85
86
87
88
89
90
91
92
93
94
95 public abstract boolean needsNewRequest(HttpResponse response, OutgoingRequest outgoingRequest,
96 IncomingRequest incomingRequest);
97
98
99
100
101
102
103 @Override
104 public final void init(Driver d, Properties properties) {
105 this.driver = d;
106 this.driver.getEventManager().register(EventManager.EVENT_PROXY_PRE, this);
107 this.driver.getEventManager().register(EventManager.EVENT_FRAGMENT_PRE, this);
108 this.driver.getEventManager().register(EventManager.EVENT_FRAGMENT_POST, this);
109
110 init(properties);
111 }
112
113 public abstract void init(Properties properties);
114
115 @Override
116 public boolean event(EventDefinition id, Event event) {
117
118 if (EventManager.EVENT_FRAGMENT_PRE.equals(id)) {
119 FragmentEvent e = (FragmentEvent) event;
120 preRequest(e.getHttpRequest(), e.getOriginalRequest());
121 } else if (EventManager.EVENT_FRAGMENT_POST.equals(id)) {
122 FragmentEvent e = (FragmentEvent) event;
123
124 while (needsNewRequest(e.getHttpResponse(), e.getHttpRequest(), e.getOriginalRequest())) {
125 EntityUtils.consumeQuietly(e.getHttpResponse().getEntity());
126 preRequest(e.getHttpRequest(), e.getOriginalRequest());
127 try {
128 e.setHttpResponse(this.driver.getRequestExecutor().execute(e.getHttpRequest()));
129 } catch (HttpErrorPage e1) {
130 e.setHttpResponse(e1.getHttpResponse());
131 }
132 }
133 } else if (EventManager.EVENT_PROXY_PRE.equals(id)) {
134 ProxyEvent e = (ProxyEvent) event;
135 e.setExit(!beforeProxy(e.getOriginalRequest()));
136 }
137
138 return true;
139 }
140
141 public Driver getDriver() {
142 return driver;
143 }
144 }