Skip to content
This repository was archived by the owner on May 3, 2023. It is now read-only.

Commit 5cb85e3

Browse files
add: events when cookie was updated
1 parent a877c95 commit 5cb85e3

File tree

8 files changed

+150
-8
lines changed

8 files changed

+150
-8
lines changed

src/main/java/net/httpclient/wrapper/HttpClientWrapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package net.httpclient.wrapper;
22

3+
import org.apache.http.impl.client.BasicCookieStore;
34
import org.apache.logging.log4j.LogManager;
45
import org.apache.logging.log4j.Logger;
6+
import org.json.Cookie;
57

68
public class HttpClientWrapper {
79

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package net.httpclient.wrapper.events;
2+
3+
import net.httpclient.wrapper.session.HttpClientSession;
4+
5+
import java.util.ArrayList;
6+
7+
/**
8+
* This static class manage the HttpClientSession events.
9+
*/
10+
public class HttpClientSessionEvent {
11+
12+
private static final ArrayList<HttpClientSessionListener> httpClientSessionBasics = new ArrayList<>();
13+
14+
/*
15+
$ Add and remove listeners
16+
*/
17+
18+
/**
19+
* Add a listener to the list of listeners.
20+
* @param listener The listener to add.
21+
*/
22+
public static void addHttpClientSessionListener(HttpClientSessionListener listener) {
23+
httpClientSessionBasics.add(listener);
24+
}
25+
26+
/**
27+
* Remove a listener from the list of listeners.
28+
* @param listener The listener to remove.
29+
*/
30+
public static void removeHttpClientSessionListener(HttpClientSessionListener listener) {
31+
httpClientSessionBasics.remove(listener);
32+
}
33+
34+
/*
35+
$ Trigger events
36+
*/
37+
38+
/**
39+
* Trigger the event onHttpClientCookiesUpdated in all listeners.
40+
* @param httpClientSession The HttpClientSession who has updated his cookies.
41+
*/
42+
public static void triggerHttpClientCookiesUpdated(HttpClientSession httpClientSession) {
43+
httpClientSessionBasics.forEach(listener -> listener.onHttpClientCookiesUpdated(httpClientSession));
44+
}
45+
46+
47+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package net.httpclient.wrapper.events;
2+
3+
import net.httpclient.wrapper.session.HttpClientSession;
4+
5+
/**
6+
* This class should be implemented and registered to listen events.
7+
*/
8+
public interface HttpClientSessionListener {
9+
10+
/**
11+
* This method should be call when a session has updated his cookies.
12+
* @param httpClientSession The session who has the cookies updated.
13+
*/
14+
void onHttpClientCookiesUpdated(HttpClientSession httpClientSession);
15+
16+
17+
}

src/main/java/net/httpclient/wrapper/exception/HttpException.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package net.httpclient.wrapper.exception;
22

33
import net.httpclient.wrapper.response.RequestResponse;
4-
import org.apache.http.HttpResponse;
5-
6-
import java.io.IOException;
74

85
public class HttpException extends Exception {
96

@@ -14,7 +11,7 @@ public HttpException(RequestResponse requestResponse) {
1411
this.requestResponse = requestResponse;
1512
}
1613

17-
public RequestResponse getRequestResponse() throws IOException {
14+
public RequestResponse getRequestResponse() {
1815
return (requestResponse);
1916
}
2017

src/main/java/net/httpclient/wrapper/session/HttpClientSessionBasic.java renamed to src/main/java/net/httpclient/wrapper/session/HttpClientSession.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package net.httpclient.wrapper.session;
22

33
import net.httpclient.wrapper.HttpClientWrapper;
4+
import net.httpclient.wrapper.events.HttpClientSessionEvent;
45
import net.httpclient.wrapper.exception.HttpClientException;
56
import net.httpclient.wrapper.exception.HttpServerException;
67
import net.httpclient.wrapper.response.RequestResponse;
8+
import net.httpclient.wrapper.utils.BasicCookieStoreSerializerUtils;
79
import net.httpclient.wrapper.utils.RandomUserAgent;
810
import org.apache.http.*;
911
import org.apache.http.auth.AuthScope;
@@ -36,11 +38,13 @@
3638
import java.util.List;
3739
import java.util.concurrent.TimeUnit;
3840

41+
import static net.httpclient.wrapper.HttpClientWrapper.logger;
42+
3943
/**
4044
* The basic class for all the http requests.
4145
* No security for multithreaded application.
4246
*/
43-
public class HttpClientSessionBasic {
47+
public class HttpClientSession {
4448

4549
/*
4650
$ Variable of the class
@@ -57,7 +61,7 @@ public class HttpClientSessionBasic {
5761
$ Constructor
5862
*/
5963

60-
public HttpClientSessionBasic() {
64+
public HttpClientSession() {
6165
try {
6266
this.httpClient = newHttpClient();
6367
this.requestConfig = setRequestConfig();
@@ -121,60 +125,92 @@ public HttpClient newHttpClient() throws NoSuchAlgorithmException, KeyManagement
121125

122126
public RequestResponse sendGet(String url) throws IOException, HttpClientException, HttpServerException {
123127
Date start = new Date();
128+
String oldCookieStoreSerialized = BasicCookieStoreSerializerUtils.serializableToBase64(httpCookieStore);
129+
124130
HttpGet httpGet = new HttpGet(url);
125131
httpGet.setConfig(getRequestConfig().build());
126132
httpGet.addHeader(HttpHeaders.ACCEPT, "application/json, text/plain, */*");
127133
HttpResponse httpResponse = getHttpClient().execute(httpGet);
134+
128135
assertRequest(httpResponse, start);
136+
verifyCookiesEvents(oldCookieStoreSerialized);
129137
return (new RequestResponse(httpResponse, start));
130138
}
131139

132140
public RequestResponse sendPost(String url, String content, ContentType contentType) throws IOException, HttpClientException, HttpServerException {
133141
Date start = new Date();
142+
String oldCookieStoreSerialized = BasicCookieStoreSerializerUtils.serializableToBase64(httpCookieStore);
143+
134144
HttpPost httpPost = new HttpPost(url);
135145
httpPost.setConfig(getRequestConfig().build());
136146
httpPost.addHeader(HttpHeaders.CONTENT_TYPE, contentType.getMimeType());
137147
httpPost.addHeader(HttpHeaders.ACCEPT, "application/json, text/plain, */*");
138148
httpPost.setEntity(new StringEntity(content));
139149
HttpResponse httpResponse = getHttpClient().execute(httpPost);
150+
140151
assertRequest(httpResponse, start);
152+
verifyCookiesEvents(oldCookieStoreSerialized);
141153
return (new RequestResponse(httpResponse, start));
142154
}
143155

144156
public RequestResponse sendForm(String url, List<NameValuePair> form) throws IOException, HttpClientException, HttpServerException {
145157
Date start = new Date();
158+
String oldCookieStoreSerialized = BasicCookieStoreSerializerUtils.serializableToBase64(httpCookieStore);
159+
146160
HttpPost httpPost = new HttpPost(url);
147161
httpPost.setConfig(getRequestConfig().build());
148162
httpPost.addHeader(HttpHeaders.ACCEPT, "application/json, text/plain, */*");
149163
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form, Consts.UTF_8);
150164
httpPost.setEntity(entity);
151165
HttpResponse httpResponse = httpClient.execute(httpPost);
166+
152167
assertRequest(httpResponse, start);
168+
verifyCookiesEvents(oldCookieStoreSerialized);
153169
return (new RequestResponse(httpResponse, start));
154170
}
155171

156172
public RequestResponse sendDelete(String url) throws IOException, HttpClientException, HttpServerException {
157173
Date start = new Date();
174+
String oldCookieStoreSerialized = BasicCookieStoreSerializerUtils.serializableToBase64(httpCookieStore);
175+
158176
HttpDelete httpDelete = new HttpDelete(url);
159177
httpDelete.setConfig(getRequestConfig().build());
160178
httpDelete.addHeader(HttpHeaders.ACCEPT, "application/json, text/plain, */*");
161179
HttpResponse httpResponse = httpClient.execute(httpDelete);
180+
162181
assertRequest(httpResponse, start);
182+
verifyCookiesEvents(oldCookieStoreSerialized);
163183
return (new RequestResponse(httpResponse, start));
164184
}
165185

166186
public RequestResponse sendPut(String url, String content, ContentType contentType) throws IOException, HttpClientException, HttpServerException {
167187
Date start = new Date();
188+
String oldCookieStoreSerialized = BasicCookieStoreSerializerUtils.serializableToBase64(httpCookieStore);
189+
168190
HttpPut httpPut = new HttpPut(url);
169191
httpPut.setConfig(getRequestConfig().build());
170192
httpPut.addHeader(HttpHeaders.CONTENT_TYPE, contentType.getMimeType());
171193
httpPut.addHeader(HttpHeaders.ACCEPT, "application/json, text/plain, */*");
172194
httpPut.setEntity(new StringEntity(content));
173195
HttpResponse httpResponse = getHttpClient().execute(httpPut);
196+
174197
assertRequest(httpResponse, start);
198+
verifyCookiesEvents(oldCookieStoreSerialized);
175199
return (new RequestResponse(httpResponse, start));
176200
}
177201

202+
/*
203+
$ Private mehtods
204+
*/
205+
206+
/**
207+
* Assert the request and throw an exception if the request is not valid.
208+
* @param httpResponse The response of the request.
209+
* @param startDate The start date of the request.
210+
* @throws HttpClientException If the request is not successful.
211+
* @throws HttpServerException If the server is not available.
212+
* @throws IOException If the request is not successful because of an network error.
213+
*/
178214
private void assertRequest(HttpResponse httpResponse, Date startDate) throws HttpClientException, HttpServerException, IOException {
179215
int statusCode = httpResponse.getStatusLine().getStatusCode();
180216
if (statusCode >= 500 && statusCode <= 599)
@@ -183,6 +219,23 @@ else if (statusCode >= 400 && statusCode <= 499)
183219
throw new HttpClientException(new RequestResponse(httpResponse, startDate));
184220
}
185221

222+
/**
223+
* Verify if the cookie store has been modified.
224+
* Some requests can modify the cookie store, and this method will verify if the cookie store has been modified.
225+
* This methods can trigger HttpClientCookiesUpdatedEvent.
226+
* This methods should be called after a request.
227+
* @param serializedOldCookieStore The old cookie store before the request serialized as string.
228+
*/
229+
private void verifyCookiesEvents(String serializedOldCookieStore) {
230+
try {
231+
String serializedNewCookieStore = BasicCookieStoreSerializerUtils.serializableToBase64(httpCookieStore);
232+
if (!serializedOldCookieStore.equals(serializedNewCookieStore))
233+
HttpClientSessionEvent.triggerHttpClientCookiesUpdated(this);
234+
} catch (Exception e) {
235+
logger.warn("Error while verifying cookies events", e);
236+
}
237+
}
238+
186239
/*
187240
* Getters and Setters
188241
*/

src/main/java/net/httpclient/wrapper/session/HttpClientSessionAsync.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
import static net.httpclient.wrapper.HttpClientWrapper.logger;
3434

35-
public class HttpClientSessionAsync extends HttpClientSessionBasic {
35+
public class HttpClientSessionAsync extends HttpClientSession {
3636

3737
/*
3838
$ Variable of the class

src/main/java/net/httpclient/wrapper/session/HttpClientSessionRateLimited.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.io.IOException;
1111
import java.util.List;
1212

13-
public class HttpClientSessionRateLimited extends HttpClientSessionBasic {
13+
public class HttpClientSessionRateLimited extends HttpClientSession {
1414

1515
private final RateLimiter rateLimiter;
1616

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.httpclient.wrapper.utils;
2+
3+
import org.apache.http.impl.client.BasicCookieStore;
4+
5+
import java.io.*;
6+
import java.util.Base64;
7+
8+
public class BasicCookieStoreSerializerUtils {
9+
10+
public static String serializableToBase64(BasicCookieStore serializable) throws IOException {
11+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
12+
ObjectOutputStream oos = new ObjectOutputStream(baos);
13+
oos.writeObject(serializable);
14+
oos.close();
15+
return Base64.getEncoder().encodeToString(baos.toByteArray());
16+
}
17+
18+
public static BasicCookieStore base64ToSerializable(String base64) throws IOException, ClassNotFoundException {
19+
byte[] data = Base64.getDecoder().decode(base64);
20+
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
21+
BasicCookieStore object = (BasicCookieStore)ois.readObject();
22+
ois.close();
23+
return (object);
24+
}
25+
26+
}

0 commit comments

Comments
 (0)