users@jersey.java.net

Re: [Jersey] Re: Using Apache HttpClient with Jersey, with SSL protocol factory

From: Arul Dhesiaseelan <arul_at_fluxcorp.com>
Date: Mon, 21 Sep 2009 11:07:45 -0600

In your case, the below code should work:

      ApacheHttpClient client = ApacheHttpClient.create();
      HttpClient httpClient = client.getClientHandler().getHttpClient();
      Protocol authhttps = new Protocol("https", new
EasySSLProtocolSocketFactory(), 4463);
      Protocol.registerProtocol("https", authhttps);
      httpClient.getHostConfiguration().setHost("localhost", 4463,
authhttps);

-Arul

Arul Dhesiaseelan wrote:
> Hi Mark,
>
> I tried this recently and blogged about it here[1]. The difference in
> my setup is that I was using a Spring RestTemplate (which also uses
> the Apache Commons HTTPClient), so using Jersey client should work the
> same way.
>
> Cheers,
> Arul
>
> [1]
> http://aruld.info/accessing-restful-services-configured-with-ssl-using-resttemplate/
>
>
> Mark Petrovic wrote:
>> Ok, I think I may have found the answers to some of my questions.
>> I'm a little panicked, and I could have spent another hour looking at
>> list archives to get some of the basic answers. My apologies.
>>
>> Here's what I believe:
>>
>> 1) I happen to need a maven dependency on the jersey-apache-client:
>>
>> <dependency>
>> <groupId>com.sun.jersey.contribs</groupId>
>> <artifactId>jersey-apache-client</artifactId>
>> <version>1.0.3.1</version>
>> </dependency>
>>
>> 2) next install a Protocol socket factory that all HttpClient
>> instances will use when the protocol is https. Do this before you
>> create the first HttpClient:
>>
>> Protocol easyhttps = new Protocol("https", new
>> EasySSLProtocolSocketFactory(), 443);
>> Protocol.registerProtocol("https", easyhttps);
>>
>> where, per the link below, I'm using what I believe is sorta
>> well-known EasySSLProtocolSocketFactory code
>>
>> 3) next, create the client for Jersey use:
>>
>> Client client;
>> client = new ApacheHttpClient();
>> WebResource.Builder reqBuilder =
>> client.resource(uri).accept("text/plain");
>> ...
>>
>> And away we go.
>>
>> Question: will this actually work? That is, will Jersey actually
>> get an https client that draws from this Protocol factory? That
>> question is not loaded; this code will work with plain vanilla
>> HttpClient code. I'm just not sure it will work with how Jersey gets
>> that underlying client.
>>
>> Question: I'd sorta like to not install an https protocol factory
>> for all of HttpClient use. One can create clients on a per instance
>> basis that use this specific protocol factory. See
>> http://juliusdavies.ca/commons-ssl/javadocs/org/apache/commons/httpclient/contrib/ssl/EasySSLProtocolSocketFactory.html
>> . Can I actually do this with the client ApacheHttpClient? I can't
>> see any way to get at the underlying HttpClient to do the equivalent of
>>
>> client.getHostConfiguration().setHost("localhost", 443, easyhttps);
>> per the javadoc in the link above.
>> Whew. Thanks for reading this far.
>>
>> Mark
>>
>> On Sep 17, 2009, at 6:41 PM, Mark Petrovic wrote:
>>
>>> Hi.
>>>
>>> I have a need to create a Jersey client that is based on the Apache
>>> HttpClient. Furthermore, I need to be able to provide a custom SSL
>>> protocol factory so that this http client can pass client SSL
>>> authentication.
>>>
>>> I know I've seen sample code that shows how to use HttpClient in a
>>> Jersey client app, but I can't seem to find it at the moment. Once
>>> I have that client, I'm hoping I can wedge something like this SSL
>>> protocol factory onto it:
>>>
>>> http://www.jdocs.com/httpclient/3.0.1/org/apache/commons/httpclient/contrib/ssl/EasySSLProtocolSocketFactory.html
>>>
>>>
>>> Thanks much.
>>>
>>>
>>> --
>>> Mark Petrovic
>>> mark_at_petrovic.org
>>> http://www.petrovic.org
>>>
>>>
>>>
>>>
>>>
>>
>> --
>> Mark Petrovic
>> mark_at_petrovic.org
>> http://www.petrovic.org
>>
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>
>