December 3, 2013

Play 2.x WS API and self signed certificates

5  comments

The play web service API provides an easy way to use your play application as an http client. Usually you should use Play's asynchronous mechanisms to use the web service response. But sometime you just want to wait for the web service responce (in a job). You can do this with the following method:
public static String call(String url, String contentType) {
    String result = null;
    try {
        F.Promise wsPromise = WS.url(url).setContentType(contentType).get();
        result = wsPromise.get(THIRTY_SECONDS).getBody();
    } catch (Exception e){
        log.error("Error in webservice call to: " + url);
    }
    return result;
}
When the web service you want to call uses self signed certificates you will run into an exception:
javax.net.ssl.SSLHandshakeException: General SSLEngine problem
To allow self-signed ssl certificates you just have to add the following to your application.conf.
# this is needed for self signed certificates
ws.acceptAnyCertificate=true
When you unit test your web service call make sure your test runs inside a FakeApplication.

Tags

application.conf, certificates, Play Framework, rest, SSLEngine problem, SSLHandshakeException, webservice


You may also like

Blog url changed to https

I just changed the url of this blog to https://jensjaeger.com. TLS encryption is now the default for all request to this page. It might be possible that some image links on some articles are hard coded http. If you find such an error it would be nice if you leave me comment so i can

Read More

Format date and time in java with prettytime

Prettytime is a nice java library to format a java Date()s in a nice humanized ago format, like: moments ago 2 minutes ago 13 hours ago 7 months ago 2 years ago Prettytime is localized in over 30 languages. It’s super simple to use Add the dependency to your maven pom: org.ocpsoft.prettytime prettytime 3.2.7.Final or

Read More