1111 *******************************************************************************/
1212package org .sonatype .spice .jersey .client .ahc ;
1313
14- import com .ning .http .client .*;
14+ import java .util .ArrayList ;
15+ import java .util .Iterator ;
16+ import java .util .List ;
17+ import java .util .Map ;
18+
19+ import javax .ws .rs .core .Context ;
20+
21+ import org .sonatype .spice .jersey .client .ahc .config .AhcConfig ;
22+ import org .sonatype .spice .jersey .client .ahc .config .DefaultAhcConfig ;
23+
24+ import com .ning .http .client .AsyncHttpClient ;
25+ import com .ning .http .client .FluentCaseInsensitiveStringsMap ;
26+ import com .ning .http .client .RequestBuilder ;
27+ import com .ning .http .client .Response ;
28+ import com .ning .http .client .cookie .Cookie ;
1529import com .sun .jersey .api .client .ClientHandler ;
1630import com .sun .jersey .api .client .ClientHandlerException ;
1731import com .sun .jersey .api .client .ClientRequest ;
1832import com .sun .jersey .api .client .ClientResponse ;
1933import com .sun .jersey .core .header .InBoundHeaders ;
2034import com .sun .jersey .spi .MessageBodyWorkers ;
21- import org .sonatype .spice .jersey .client .ahc .config .AhcConfig ;
22- import org .sonatype .spice .jersey .client .ahc .config .DefaultAhcConfig ;
23-
24- import javax .ws .rs .core .Context ;
25- import java .util .ArrayList ;
26- import java .util .Iterator ;
27- import java .util .List ;
28- import java .util .Map ;
2935
3036/**
3137 * A root handler with Sonatype AsyncHttpClient acting as a backend.
@@ -55,7 +61,7 @@ public final class AhcClientHandler implements ClientHandler {
5561
5662 private final AhcRequestWriter requestWriter = new AhcRequestWriter ();
5763
58- private List <Cookie > cookies = new ArrayList <Cookie >();
64+ private final List <Cookie > cookies = new ArrayList <Cookie >();
5965
6066 @ Context
6167 private MessageBodyWorkers workers ;
@@ -65,7 +71,7 @@ public final class AhcClientHandler implements ClientHandler {
6571 *
6672 * @param client the {@link AsyncHttpClient}.
6773 */
68- public AhcClientHandler (AsyncHttpClient client ) {
74+ public AhcClientHandler (final AsyncHttpClient client ) {
6975 this (client , new DefaultAhcConfig ());
7076 }
7177
@@ -75,7 +81,7 @@ public AhcClientHandler(AsyncHttpClient client) {
7581 * @param client the {@link AsyncHttpClient}.
7682 * @param config the client configuration.
7783 */
78- public AhcClientHandler (AsyncHttpClient client , AhcConfig config ) {
84+ public AhcClientHandler (final AsyncHttpClient client , final AhcConfig config ) {
7985 this .client = client ;
8086 this .config = config ;
8187 }
@@ -105,6 +111,7 @@ public AsyncHttpClient getHttpClient() {
105111 * @return the {@link ClientResponse}
106112 * @throws ClientHandlerException
107113 */
114+ @ Override
108115 public ClientResponse handle (final ClientRequest cr )
109116 throws ClientHandlerException {
110117
@@ -117,7 +124,7 @@ public ClientResponse handle(final ClientRequest cr)
117124
118125 applyResponseCookies (response .getCookies ());
119126
120- ClientResponse r = new ClientResponse (response .getStatusCode (),
127+ final ClientResponse r = new ClientResponse (response .getStatusCode (),
121128 getInBoundHeaders (response ),
122129 response .getResponseBodyAsStream (),
123130 workers );
@@ -126,7 +133,7 @@ public ClientResponse handle(final ClientRequest cr)
126133 r .close ();
127134 }
128135 return r ;
129- } catch (Exception e ) {
136+ } catch (final Exception e ) {
130137 throw new ClientHandlerException (e );
131138 }
132139 }
@@ -136,13 +143,13 @@ public ClientResponse handle(final ClientRequest cr)
136143 *
137144 * @param responseCookies list of cookies from response
138145 */
139- private void applyResponseCookies (List <Cookie > responseCookies ) {
146+ private void applyResponseCookies (final List <Cookie > responseCookies ) {
140147 if (responseCookies != null ) {
141- for (Cookie rc : responseCookies ) {
148+ for (final Cookie rc : responseCookies ) {
142149 // remove existing cookie
143- Iterator <Cookie > it = cookies .iterator ();
150+ final Iterator <Cookie > it = cookies .iterator ();
144151 while (it .hasNext ()) {
145- Cookie c = it .next ();
152+ final Cookie c = it .next ();
146153 if (isSame (rc , c )) {
147154 it .remove ();
148155 break ;
@@ -154,13 +161,13 @@ private void applyResponseCookies(List<Cookie> responseCookies) {
154161 }
155162 }
156163
157- private boolean isSame (Cookie c , Cookie o ) {
164+ private boolean isSame (final Cookie c , final Cookie o ) {
158165 return isEquals (c .getDomain (), o .getDomain ()) &&
159166 isEquals (c .getPath (), o .getPath ()) &&
160167 isEquals (c .getName (), o .getName ());
161168 }
162169
163- private boolean isEquals (Object o , Object o2 ) {
170+ private boolean isEquals (final Object o , final Object o2 ) {
164171 return (o == null && o2 == null ) || o != null && o .equals (o2 );
165172 }
166173
@@ -170,7 +177,7 @@ private boolean isEquals(Object o, Object o2) {
170177 * @param method An HTTP method
171178 * @return true if s body can be allowed.
172179 */
173- private boolean allowBody (String method ) {
180+ private boolean allowBody (final String method ) {
174181 if (method .equalsIgnoreCase ("GET" ) || method .equalsIgnoreCase ("OPTIONS" )
175182 && method .equalsIgnoreCase ("TRACE" )
176183 && method .equalsIgnoreCase ("HEAD" )) {
@@ -186,7 +193,7 @@ private boolean allowBody(String method) {
186193 * @param cr the HTTP request.
187194 * @return {@link RequestBuilder}
188195 */
189- private RequestBuilder getRequestBuilder (ClientRequest cr ) {
196+ private RequestBuilder getRequestBuilder (final ClientRequest cr ) {
190197 final String strMethod = cr .getMethod ();
191198 final String uri = cr .getURI ().toString ();
192199
@@ -207,10 +214,10 @@ private RequestBuilder getRequestBuilder(ClientRequest cr) {
207214 }
208215 }
209216
210- private InBoundHeaders getInBoundHeaders (Response response ) {
211- InBoundHeaders headers = new InBoundHeaders ();
212- FluentCaseInsensitiveStringsMap respHeaders = response .getHeaders ();
213- for (Map .Entry <String , List <String >> header : respHeaders ) {
217+ private InBoundHeaders getInBoundHeaders (final Response response ) {
218+ final InBoundHeaders headers = new InBoundHeaders ();
219+ final FluentCaseInsensitiveStringsMap respHeaders = response .getHeaders ();
220+ for (final Map .Entry <String , List <String >> header : respHeaders ) {
214221 headers .put (header .getKey (), header .getValue ());
215222 }
216223 return headers ;
@@ -226,8 +233,8 @@ public AhcRequestWriter getAhcRequestWriter() {
226233 return requestWriter ;
227234 }
228235
229- private void handleCookie (RequestBuilder requestBuilder ) {
230- for (Cookie c : cookies ) {
236+ private void handleCookie (final RequestBuilder requestBuilder ) {
237+ for (final Cookie c : cookies ) {
231238 requestBuilder .addCookie (c );
232239 }
233240 }
0 commit comments