/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Vladislav Bauer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.github.vbauer.yta.service.basic;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import javaslang.control.Try;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.HttpClients;

import javax.annotation.concurrent.ThreadSafe;
import java.util.Map;

/**
 * {@inheritDoc}
 *
 * @author Vladislav Bauer
 */

@ThreadSafe
public class RestClientImpl implements RestClient {

    private static final String SERVICE_URL = "https://translate.yandex.net/api/v1.5/tr.json";
    private static final String ATTR_KEY = "key";
    private static final int DEFAULT_TIMEOUT = 30000;
    private static volatile boolean initialized;

    private final String key;


    public RestClientImpl(final String key) {
        this.key = key;
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public String callMethod(final String method, final Map<String, Object> parameters) {
        return Try.of(() -> {
            initHttpClientIfNecessary();

            final String url = formatMethodUrl(method);
            final Map<String, Object> params = composeRequestParameters(parameters);
            final HttpResponse<String> response =
                Unirest.post(url)
                    .fields(params)
                    .asString();

            ApiStatus.check(response.getStatus());

            return response.getBody();
        }).orElseThrow(Throwables::propagate);
    }


    private String formatMethodUrl(final String method) {
        return SERVICE_URL + method;
    }

    private Map<String, Object> composeRequestParameters(final Map<String, Object> parameters) {
        final Map<String, Object> params = Maps.newHashMap();
        if (parameters != null) {
            params.putAll(parameters);
        }
        params.put(ATTR_KEY, key);
        return params;
    }


    private static void initHttpClientIfNecessary() throws Exception {
        if (!initialized) {
            synchronized (Unirest.class) {
                if (!initialized) {
                    final HttpClient httpClient = createClient();
                    Unirest.setHttpClient(httpClient);
                    initialized = true;
                }
            }
        }
    }

    private static HttpClient createClient() throws Exception {
        final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
            SSLContexts.custom()
                .loadTrustMaterial(null, new TrustSelfSignedStrategy())
                .build()
        );

        final RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(DEFAULT_TIMEOUT)
            .setConnectTimeout(DEFAULT_TIMEOUT)
            .setConnectionRequestTimeout(DEFAULT_TIMEOUT)
            .setRedirectsEnabled(true)
            .build();

        return HttpClients.custom()
            .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
            .setSSLSocketFactory(socketFactory)
            .setDefaultRequestConfig(requestConfig)
            .build();
    }

}

@@ -1356,16 +1356,113 @@
 nirest;%0A
+import com.mashape.unirest.http.options.Option;%0Aimport com.mashape.unirest.http.options.Options;%0A
 import j
@@ -1479,24 +1479,24 @@
 ontrol.Try;%0A
-
 import org.a
@@ -1864,30 +1864,40 @@
 %0A%0A/**%0A *
+ See
  %7B@
+l
 in
-heritDoc%7D
+k RestClient%7D.
 %0A *%0A * @
@@ -2134,16 +2134,16 @@
  %22key%22;%0A
+
     priv
@@ -2191,57 +2191,8 @@
 000;
-%0A    private static volatile boolean initialized;
 %0A%0A  
@@ -3443,35 +3443,41 @@
        if (!
-initialized
+hasCustomClient()
 ) %7B%0A        
@@ -3494,23 +3494,30 @@
 onized (
-Unirest
+RestClientImpl
 .class) 
@@ -3539,27 +3539,33 @@
    if (!
-initialized
+hasCustomClient()
 ) %7B%0A    
@@ -3701,31 +3701,23 @@
     
+%7D%0A
     
-initialized = true;
+        %7D
 %0A   
@@ -3725,41 +3725,162 @@
     
-    
+%7D%0A
     %7D%0A
+%0A
     
-        %7D%0A        %7D
+private static boolean hasCustomClient() %7B%0A        final Object option = Options.getOption(Option.HTTPCLIENT);%0A        return option != null;
 %0A   

Remove `volatile boolean initialized`