Both generic clients and annotation-driven clients support Client Interceptors, which may be registered in order to provide specific behaviour to each client request.

The following section shows some sample interceptors which may be used.

Security: HTTP Basic Authorization

The following example shows how to configure your client to use a specific username and password in every request.

      // Create a context and get the client factory so it can be configured
      FhirContext ctx = FhirContext.forDstu2();
      IRestfulClientFactory clientFactory = ctx.getRestfulClientFactory();

      // Create an HTTP basic auth interceptor
      String username = "foobar";
      String password = "boobear";
      BasicAuthInterceptor authInterceptor = new BasicAuthInterceptor(username, password);

      // Register the interceptor with your client (either style)
      IPatientClient annotationClient = ctx.newRestfulClient(IPatientClient.class, "http://localhost:9999/fhir");
      annotationClient.registerInterceptor(authInterceptor);

      IGenericClient genericClient = ctx.newRestfulGenericClient("http://localhost:9999/fhir");
      annotationClient.registerInterceptor(authInterceptor);

Security: HTTP Bearer Token Authorization

The following example shows how to configure your client to inject a bearer token authorization header into every request. This is used to satisfy servers which are protected using OAUTH2.

      // Create a context and get the client factory so it can be configured
      FhirContext ctx = FhirContext.forDstu2();
      IRestfulClientFactory clientFactory = ctx.getRestfulClientFactory();

      // In reality the token would have come from an authorization server 
      String token = "3w03fj.r3r3t"; 
      
      BearerTokenAuthInterceptor authInterceptor = new BearerTokenAuthInterceptor(token);

      // Register the interceptor with your client (either style)
      IPatientClient annotationClient = ctx.newRestfulClient(IPatientClient.class, "http://localhost:9999/fhir");
      annotationClient.registerInterceptor(authInterceptor);

      IGenericClient genericClient = ctx.newRestfulGenericClient("http://localhost:9999/fhir");
      annotationClient.registerInterceptor(authInterceptor);

Logging: Log Requests and Responses

The LoggingInterceptor can be used to log every transaction. The interceptor is flexible and can be configured to be extremely verbose (logging entire transactions including HTTP headers and payload bodies) or simply to log request URLs, or some combination in between.

         // Create a context and get the client factory so it can be configured
         FhirContext ctx = FhirContext.forDstu2();
         IRestfulClientFactory clientFactory = ctx.getRestfulClientFactory();

         // Create a logging interceptor
         LoggingInterceptor loggingInterceptor = new LoggingInterceptor();

         // Optionally you may configure the interceptor (by default only
         // summary info is logged)
         loggingInterceptor.setLogRequestSummary(true);
         loggingInterceptor.setLogRequestBody(true);

         // Register the interceptor with your client (either style)
         IPatientClient annotationClient = ctx.newRestfulClient(IPatientClient.class, "http://localhost:9999/fhir");
         annotationClient.registerInterceptor(loggingInterceptor);

         IGenericClient genericClient = ctx.newRestfulGenericClient("http://localhost:9999/fhir");
         genericClient.registerInterceptor(loggingInterceptor);

The CookieInterceptor can be used to add an HTTP Cookie header to each request created by the client.

      // Create a context and get the client factory so it can be configured
      FhirContext ctx = FhirContext.forDstu2();
      IRestfulClientFactory clientFactory = ctx.getRestfulClientFactory();

      // Create a cookie interceptor. This cookie will have the name "mycookie" and
      // the value "Chips Ahoy"
      CookieInterceptor interceptor = new CookieInterceptor("mycookie=Chips Ahoy");

      // Register the interceptor with your client (either style)
      IPatientClient annotationClient = ctx.newRestfulClient(IPatientClient.class, "http://localhost:9999/fhir");
      annotationClient.registerInterceptor(interceptor);

      IGenericClient genericClient = ctx.newRestfulGenericClient("http://localhost:9999/fhir");
      annotationClient.registerInterceptor(interceptor);

Performance: GZip Outgoing Request Bodies

The GZipContentInterceptor compresses outgoing contents. With this interceptor, if the client is transmitting resources to the server (e.g. for a create, update, transaction, etc.) the content will be GZipped before transmission to the server.

      // Create a context and get the client factory so it can be configured
      FhirContext ctx = FhirContext.forDstu2();
      IRestfulClientFactory clientFactory = ctx.getRestfulClientFactory();

      // Register the interceptor with your client (either style)
      IPatientClient annotationClient = ctx.newRestfulClient(IPatientClient.class, "http://localhost:9999/fhir");
      annotationClient.registerInterceptor(new GZipContentInterceptor());

Back to top

Reflow Maven skin by Andrius Velykis.