11package net .httpclient .wrapper .session ;
22
33import net .httpclient .wrapper .HttpClientWrapper ;
4+ import net .httpclient .wrapper .events .HttpClientSessionEvent ;
45import net .httpclient .wrapper .exception .HttpClientException ;
56import net .httpclient .wrapper .exception .HttpServerException ;
67import net .httpclient .wrapper .response .RequestResponse ;
8+ import net .httpclient .wrapper .utils .BasicCookieStoreSerializerUtils ;
79import net .httpclient .wrapper .utils .RandomUserAgent ;
810import org .apache .http .*;
911import org .apache .http .auth .AuthScope ;
3638import java .util .List ;
3739import 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 */
0 commit comments