Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

GroundWork RESTClient Filters

Patrick Bösch edited this page Nov 30, 2016 · 1 revision

How to use filters

With filters you can inject your own override code at specific points during the request. Possible injection points are:

  • Before the request definition (RequestFilter)
  • Before the request execution (RequestWriteFilter)
  • On an exception during the request (RequestExceptionFilter)
  • After the request (ResponseReadFilter)
  • After the deserialization of the response (ResponseFilter)
  • On an exception during the response processing (ResponseExceptionFilter)
  • On a successful response code (SuccessResponseCodeFilter)
  • On a error response code (ErrorResponseCodeFilter)

Preinstalled filter:

  • BasicAuthFilter
  • BearerTokenFilter
  • DefaultSuccessResponseCodeFilter
  • DefaultErrorResponseCodeFilter

To register/unregister a filter you can user request.register(<Your Filter>) and request.unregister(<Your Filter-Class>)

Example:

String url = "http://httpbin.org";
String path = "/basic-auth/user/passwd";

SimpleGetRequest<JSONResponse> request = new SimpleGetRequest<>(url, JSONResponse.class);
request.setPath(path);

request.register(new BasicAuthFilter("user","passwd"));

JSONResponse response = request.execute();
System.out.println(response.getResponseHeader());
System.out.println(response.Json());

Response code handling

By default every status code that doesn't match >= 200 and < 300 is handled as a RESTClientException (based on DefaultErrorResponseCodeFilter) and the body will not be parsed. You can override/extend this definition by using this two methods:

//Handle a port as successful port
request.addAdditionalAllowedStatusCode(404);

//Handle a port as error port
request.addAdditionalDeniedStatusCode(200);