alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (NingWSAPI.java)

This example Play Framework source code file (NingWSAPI.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

api, asynchttpclientconfig, defaultwsconfigparser, lib, library, ningasynchttpclientconfigbuilder, ningwsapi, ningwsclient, override, play, play framework, throwable, web service, ws, wsapi, wsclient, wsclientconfig

The NingWSAPI.java Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.libs.ws.ning;

import com.ning.http.client.AsyncHttpClientConfig;
import play.Application;
import play.api.libs.ws.DefaultWSConfigParser;
import play.api.libs.ws.WSClientConfig;
import play.api.libs.ws.ning.NingAsyncHttpClientConfigBuilder;
import play.libs.F;
import play.libs.ws.WSAPI;
import play.libs.ws.WSClient;
import play.libs.ws.WSRequestHolder;

import java.util.concurrent.atomic.AtomicReference;

/**
 *
 */
public class NingWSAPI implements WSAPI {

    private final AtomicReference<F.Option<NingWSClient>> clientHolder = new AtomicReference<F.Option<NingWSClient>>(F.None());

    private Application app;

    public NingWSAPI(Application app) {
        this.app = app;
    }

    private NingWSClient newClient() {
        play.api.Configuration playConfig = app.configuration().getWrappedConfiguration();
        DefaultWSConfigParser parser = new DefaultWSConfigParser(playConfig, app.classloader());
        WSClientConfig clientConfig = parser.parse();
        NingAsyncHttpClientConfigBuilder builder = new NingAsyncHttpClientConfigBuilder(clientConfig, new AsyncHttpClientConfig.Builder());
        AsyncHttpClientConfig httpClientConfig = builder.build();
        return new NingWSClient(httpClientConfig);
    }

    /**
     * resets the underlying AsyncHttpClient
     */
    protected void resetClient() {
        clientHolder.getAndSet(F.None()).map(new F.Function<NingWSClient, F.Option<NingWSClient>>() {
            @Override
            public F.Option<NingWSClient> apply(NingWSClient ningWSClient) throws Throwable {
                ningWSClient.close();
                return F.Option.None();
            }
        });
    }

    @Override
    public synchronized WSClient client() {
        F.Option<NingWSClient> clientOption = clientHolder.get();
        if (clientOption.isEmpty()) {
            NingWSClient client = newClient();
            clientHolder.set(F.Some(client));
            return client;
        } else {
            return clientOption.get();
        }
    }

    @Override
    public WSRequestHolder url(String url) {
        return client().url(url);
    }
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework NingWSAPI.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.