Skip to content

Commit

Permalink
Revise cookie handling in CookieClientFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasdarimont committed Jul 12, 2023
1 parent 65d38b1 commit 266f4f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static ResteasyClient getClient(boolean sslVerification, URL httpProxy, D
);
}

clientBuilder.register(new CookieClientFilter());
clientBuilder.register(CookieClientFilter.class);

return clientBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,25 @@
// Instead, build the httpEngine from scratch, we are using a RESTeasy filter to grab a re-attach cookie.
// Currently, this filter does not valide cookie or is able to remove cookies.
// A cookie managed is required to handle sticky sessions at cookie base

public class CookieClientFilter implements ClientRequestFilter, ClientResponseFilter {
private final Map<String, String> cookies = new HashMap<>();

/**
* Hold the additional cookies across mutliple interactions in the same thread.
*/
private static final ThreadLocal<Map<String, String>> cookies = ThreadLocal.withInitial(HashMap::new);

@Override
public void filter(ClientRequestContext clientRequestContext) {
clientRequestContext.getHeaders().put("Cookie", new ArrayList<>(cookies.values()));
clientRequestContext.getHeaders().put("Cookie", new ArrayList<>(cookies.get().values()));
}

@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) {
responseContext.getCookies().forEach((name, cookie) -> cookies.put(name, String.format("%s=%s",

cookies.remove(); // clear dangling cookies

responseContext.getCookies().forEach((name, cookie) -> cookies.get().put(name, String.format("%s=%s",
cookie.toCookie().getName(),
cookie.toCookie().getValue()
)));
Expand Down

0 comments on commit 266f4f7

Please sign in to comment.