package com.terradue.warhol;

/*
 *    Copyright 2011-2012 Terradue srl
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

import static org.sonatype.spice.jersey.client.ahc.AhcHttpClient.create;

import org.sonatype.spice.jersey.client.ahc.config.DefaultAhcConfig;

import com.ning.http.client.AsyncHttpClientConfig.Builder;
import com.ning.http.client.resumable.ResumableIOExceptionFilter;
import com.sun.jersey.api.client.Client;
import com.terradue.warhol.settings.DataSource;
import com.terradue.warhol.settings.HttpSettings;

final class RestClientFactory
{

    public static Client newRestClient( DataSource dataSource )
    {
        DefaultAhcConfig config = new DefaultAhcConfig();

        Builder builder = config.getAsyncHttpClientConfigBuilder();

        // basic http settings
        HttpSettings settings = dataSource.getHttpSettings();
        builder.setRequestTimeoutInMs( settings.getConnectionTimeout() * 60 * 60 * 1000 ) // 45 minutes
               .setAllowPoolingConnection( settings.isAllowPoolingConnection() )
               .addIOExceptionFilter( new ResumableIOExceptionFilter() )
               .setMaximumConnectionsPerHost( settings.getHostMaximumConnection() )
               .setMaximumConnectionsTotal( settings.getTotalMaximumConnection() )
               .setFollowRedirects( settings.isFollowRedirects() );

        return create( config );
    }

    private RestClientFactory()
    {
        // do nothing
    }

}

@@ -654,16 +654,84 @@
 e.%0A */%0A%0A
+import static com.terradue.warhol.lang.Preconditions.checkNotNull;%0A%0A
 import s
@@ -1414,17 +1414,21 @@
 ettings 
-s
+httpS
 ettings 
@@ -1428,16 +1428,30 @@
 ttings =
+ checkNotNull(
  dataSou
@@ -1470,16 +1470,64 @@
 ettings(
+), %22HTTP settings cannot be null in DataSource%22 
 );%0A     
@@ -1560,17 +1560,21 @@
 utInMs( 
-s
+httpS
 ettings.
@@ -1672,17 +1672,21 @@
 ection( 
-s
+httpS
 ettings.
@@ -1833,17 +1833,21 @@
 erHost( 
-s
+httpS
 ettings.
@@ -1919,17 +1919,21 @@
 sTotal( 
-s
+httpS
 ettings.
@@ -1998,17 +1998,21 @@
 irects( 
-s
+httpS
 ettings.

check http settings is not null when configuring the http client