title: How to use a REST client with connection pooling and basic auth?
body: <p>I currently have: </p>
  
  <ul>
  <li>a REST API (Jersey) that runs as a seperate application</li>
  <li>a GUI application (JSF) that is a client of the REST API</li>
  </ul>
  
  <p>I'm wondering what the best way is to talk to the REST API from the GUI application. The REST API is stateless, but the GUI application is stateful and has to pass authentication info (<strong>basic auth</strong>) with every rest request. Because we have to support hundreds of simultaneous users, we want to configure our Jersey client for <strong>connection pooling</strong>.</p>
  
  <p>We can handle connection pooling by configuring the Jersey client with Apache's HTTP client. Authentication can be handled by using the HTTPBasicAuthFilter, which will automatically send the same credentials with every request.</p>
  
  <p><strong>However, I'm not sure if it is best to configure 1 client for the entire GUI application, or to create a new client per session.</strong></p>
  
  <p><strong>With 1 client for the application</strong>, connection pooling makes sense, but then I have to find a way to set the correct authentication info on every request. The HTTPBasicAuthFilter assumes that the credentials never change, which is not the case our app.</p>
  
  <p>If I <strong>create a client with a new HTTPBasicAuthFilter per session</strong>, then authentication is trivial, but I don't get any benefit from connection pooling, since every client will have its own pool.</p>
  
  <p>I doubt I'm the first one to run into this, so I am curious how other people have solved this.</p>
  
  <p>Kind regards,</p>
  
  <p>Glenn</p>