Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTEasy client framework authentication credentials

RESTEasy (a JAX-RS implementation) has a nice client framework, eg:

ServiceApi client = ProxyFactory.create(ServiceApi.class, baseUri);

How do you provide HTTP authentication credentials to this client?

like image 892
jnorris Avatar asked Dec 11 '09 04:12

jnorris


2 Answers

jnorris's answer uses some deprecated classes. Here is an updated way that uses non-deprecated classes.

    import org.apache.http.HttpStatus;
    import org.apache.http.auth.Credentials;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.impl.client.DefaultHttpClient;
    ...
    DefaultHttpClient httpClient = new DefaultHttpClient();

    Credentials credentials = new UsernamePasswordCredentials(userName,
            password);
    httpClient.getCredentialsProvider().setCredentials(
            org.apache.http.auth.AuthScope.ANY, credentials);

    ClientExecutor clientExecutor = new ApacheHttpClient4Executor(
            httpClient);
    proxy = ProxyFactory
            .create(UserAccessProxy.class, host, clientExecutor);
like image 152
Z. Cass Avatar answered Sep 30 '22 19:09

Z. Cass


Credentials can be provided by using ClientExecutor.

   Credentials credentials = new UsernamePasswordCredentials(userId, password);
   HttpClient httpClient = new HttpClient();
   httpClient.getState().setCredentials(AuthScope.ANY, credentials);
   httpClient.getParams().setAuthenticationPreemptive(true);

   ClientExecutor clientExecutor = new ApacheHttpClientExecutor(httpClient);

   ServiceApi client = ProxyFactory.create(ServiceApi.class, baseUri, clientExecutor);
like image 22
jnorris Avatar answered Sep 30 '22 19:09

jnorris