aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/federation/http/HTTPSearcher.java
blob: edf347bd84e454ec681ff0f4c9be09c900a89df1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.federation.http;

import com.google.inject.Inject;
import com.yahoo.component.ComponentId;
import com.yahoo.jdisc.http.CertificateStore;
import com.yahoo.log.LogLevel;
import com.yahoo.prelude.Ping;
import com.yahoo.prelude.Pong;
import com.yahoo.yolean.Exceptions;
import com.yahoo.search.Query;
import com.yahoo.search.cluster.ClusterSearcher;
import com.yahoo.search.federation.ProviderConfig.PingOption;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.result.Hit;
import com.yahoo.statistics.Counter;
import com.yahoo.statistics.Statistics;
import com.yahoo.text.Utf8;

import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultHttpRoutePlanner;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Generic superclass of searchers making connections to some HTTP service. This
 * supports clustered connections - a list of alternative servers may be given,
 * requests will be hashed across these and failed over in case some are down.
 * <p>
 * This simply provides some utility methods for working with http connections
 * and implements ping against the service.
 *
 * <p>This searcher contains code from the Apache httpcomponents client library,
 * licensed to the Apache Software Foundation under the Apache License, Version
 * 2.0. Please refer to http://www.apache.org/licenses/LICENSE-2.0 for details.
 *
 * <p>This class automatically adds a meta hit containing latency and other
 * meta information about the obtained HTTP data using createRequestMeta().
 * The fields available in the hit are:</p>
 *
 * <dl><dt>
 * HTTPSearcher.LOG_LATENCY_START
 * <dd>
 *     The latency of the external provider answering a request.
 * <dt>
 * HTTPSearcher.LOG_LATENCY_FINISH
 * <dd>
 *     Total time of the HTTP traffic, but also decoding of the data, as this
 *     happens at the same time.
 * <dt>
 * HTTPSearcher.LOG_HITCOUNT
 * <dd>
 *     Number of concrete hits in the result returned by this provider.
 * <dt>
 * HTTPSearcher.LOG_URI
 * <dd>
 *     The complete URI used for external service.
 * <dt>
 * HTTPSearcher.LOG_SCHEME
 * <dd>
 *     The scheme of the request URI sent.
 * <dt>
 * HTTPSearcher.LOG_HOST
 * <dd>
 *     The host used for the request URI sent.
 * <dt>
 * HTTPSearcher.LOG_PORT
 * <dd>
 *     The port used for the request URI sent.
 * <dt>
 * HTTPSearcher.LOG_PATH
 * <dd>
 *     Path element of the request URI sent.
 * <dt>
 * HTTPSearcher.LOG_STATUS
 * <dd>
 *     Status code of the HTTP response.
 * <dt>
 * HTTPSearcher.LOG_PROXY_TYPE
 * <dd>
 *     The proxy type used, if any. Default is "http".
 * <dt>
 * HTTPSearcher.LOG_PROXY_HOST
 * <dd>
 *     The proxy host, if any.
 * <dt>
 * HTTPSearcher.LOG_PROXY_PORT
 * <dd>
 *     The proxy port, if any.
 * <dt>
 * HTTPSearcher.LOG_HEADER_PREFIX prepended to request header field name
 * <dd>
 *     The content of any additional request header fields.
 * <dt>
 * HTTPSearcher.LOG_RESPONSE_HEADER_PREFIX prepended to response header field name
 * <dd>
 *     The content of any additional response header fields.
 * </dl>
 *
 * @author Arne Bergene Fossaa
 * @deprecated
 */
// TODO: Remove on Vespa 7
@Deprecated // OK
public abstract class HTTPSearcher extends ClusterSearcher<Connection> {

    protected static final String YCA_HTTP_HEADER = "Yahoo-App-Auth";

    private static final Charset iso8859Charset = Charset.forName("ISO-8859-1");

    // Logging field name constants
    public static final String LOG_PATH = "path";
    public static final String LOG_PORT = "port";
    public static final String LOG_HOST = "host";
    public static final String LOG_IP_ADDRESS = "ip_address";
    public static final String IP_ADDRESS_UNKNOWN = "unknown";

    public static final String LOG_SCHEME = "scheme";
    public static final String LOG_URI = "uri";
    public static final String LOG_PROXY_PORT = "proxy_port";
    public static final String LOG_PROXY_HOST = "proxy_host";
    public static final String LOG_PROXY_TYPE = "proxy_type";
    public static final String LOG_STATUS = "status";
    public static final String LOG_LATENCY_FINISH = "latency_finish";
    public static final String LOG_LATENCY_START = "latency_start";
    public static final String LOG_LATENCY_CONNECT = "latency_connect";
    public static final String LOG_QUERY_PARAM_PREFIX = "query_param_";
    public static final String LOG_HEADER_PREFIX = "header_";
    public static final String LOG_RESPONSE_HEADER_PREFIX = "response_header_";
    public static final String LOG_HITCOUNT = "hit_count";
    public static final String LOG_CONNECT_TIMEOUT_PREFIX = "connect_timeout_";
    public static final String LOG_READ_TIMEOUT_PREFIX = "read_timeout_";

    protected final Logger log = Logger.getLogger(HTTPSearcher.class.getName());

    /** The HTTP parameters to use. Assigned in the constructor */
    private HTTPParameters httpParameters;

    private final Counter connectTimeouts;

    /** Whether to use certificates */
    protected boolean useCertificate = false;

    private final CertificateStore certificateStore;

    /** The (optional) certificate application ID. */
    private String certificateApplicationId = null;

    /** The (optional) certificate server proxy */
    protected HttpHost certificateProxy = null;

    /** Certificate cache TTL in ms */
    private long certificateTtl = 0L;

    /** Certificate server retry rate in the cache if no cert is found, in ms */
    private long certificateRetry = 0L;

    /** Set at construction if this is using persistent connections */
    private ClientConnectionManager sharedConnectionManager = null;

    /** Set at construction if using non-persistent connections */
    private ThreadLocal<SingleClientConnManager> singleClientConnManagerThreadLocal = null;

    private static final SchemeRegistry schemeRegistry = new SchemeRegistry();

    static {
        schemeRegistry.register(new Scheme("http", PlainSocketFactory
                .getSocketFactory(), 80));
        schemeRegistry.register(new Scheme("https", SSLSocketFactory
                .getSocketFactory(), 443));
    }

    public HTTPSearcher(ComponentId componentId, List<Connection> connections,String path, Statistics statistics) {
        this(componentId, connections, new HTTPParameters(path), statistics, new ThrowingCertificateStore());
    }

    /** Creates a http searcher with default connection and read timeouts (currently 2 and 5s respectively) */
    public HTTPSearcher(ComponentId componentId, List<Connection> connections,String path, Statistics statistics,
                        CertificateStore certificateStore) {
        this(componentId, connections, new HTTPParameters(path), statistics, certificateStore);
    }

    public HTTPSearcher(ComponentId componentId, List<Connection> connections, HTTPParameters parameters,
                        Statistics statistics) {
        this(componentId, connections, parameters, statistics, new ThrowingCertificateStore());
    }
    /**
     * Creates a http searcher
     *
     * @param componentId the id of this instance
     * @param connections the connections to establish to the backend nodes
     * @param parameters the http parameters to use. This object will be frozen if it isn't already
     */
    @Inject
    public HTTPSearcher(ComponentId componentId, List<Connection> connections, HTTPParameters parameters,
                        Statistics statistics, CertificateStore certificateStore) {
        super(componentId,connections,false);
        String suffix = "_" + getId().getName().replace('.', '_');

        connectTimeouts = new Counter(LOG_CONNECT_TIMEOUT_PREFIX + suffix, statistics, false);

        parameters.freeze();
        this.httpParameters = parameters;
        this.certificateStore = certificateStore;

        if (parameters.getPersistentConnections()) {
            HttpParams params=parameters.toHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            ConnManagerParams.setTimeout(params, 10);
            sharedConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);
            Thread connectionPurgerThread = new Thread(() -> {
                //this is the default value in yahoo jvm installations
                long DNSTTLSec = 120;
                while (true) {
                    try {
                        Thread.sleep(DNSTTLSec * 1000);
                        if (sharedConnectionManager == null)
                            continue;

                        sharedConnectionManager.closeExpiredConnections();
                        DNSTTLSec = Long.valueOf(java.security.Security
                                .getProperty("networkaddress.cache.ttl"));
                        //No DNS TTL, no need to close idle connections
                        if (DNSTTLSec <= 0) {
                            DNSTTLSec = 120;
                            continue;
                        }
                        sharedConnectionManager.closeIdleConnections(2 * DNSTTLSec, TimeUnit.SECONDS);
                    } catch (InterruptedException e) {
                        return;
                    } catch (NumberFormatException e) {
                        continue;
                    }
                }
            });
            connectionPurgerThread.setDaemon(true);
            connectionPurgerThread.start();

        }
        else {
            singleClientConnManagerThreadLocal =new ThreadLocal<>();
        }

        initializeCertificate(httpParameters, certificateStore);
    }

    /**
     * Initialize certificate store and proxy if they have been set to non-null,
     * non-empty values. It will wrap thrown exceptions from the certificate store into
     * RuntimeException and propagate them.
     */
    private void initializeCertificate(HTTPParameters parameters, CertificateStore certificateStore) {
        String applicationId = parameters.getYcaApplicationId();
        String proxy = parameters.getYcaProxy();
        int port = parameters.getYcaPort();
        long ttl = parameters.getYcaTtl();
        long retry = parameters.getYcaRetry();

        if (applicationId != null && !applicationId.trim().isEmpty()) {
            initializeCertificate(applicationId, ttl, retry, certificateStore);
        }

        if (parameters.getYcaUseProxy()) {
            initializeProxy(proxy, port);
        }
    }

    /** Returns the HTTP parameters used in this. This is always frozen */
    public HTTPParameters getParameters() { return httpParameters; }

    /**
     * Returns the key-value pairs that should be added as properties to the request url sent to the service.
     * Must be overridden in subclasses to add the key-values expected by the service in question, unless
     * {@link #getURI} (from which this is called) is overridden.
     * <p>
     * This default implementation returns an empty LinkedHashMap.
     */
    public Map<String,String> getQueryMap(Query query) {
        return new LinkedHashMap<>();
    }

    /**
     * Initialize the certificate.
     * This will warn but not throw if certificates could not be loaded, as the certificates
     * are external state which can fail independently.
     */
    private void initializeCertificate(String applicationId, long ttl, long retry, CertificateStore certificateStore) {
        try {
            // get the certificate, i.e. init the cache and check integrity
            String certificate = certificateStore.getCertificate(applicationId, ttl, retry);
            if (certificate == null) {
                getLogger().log(LogLevel.WARNING, "No certificate found for application '" + applicationId + "'");
                return;
            }

            this.useCertificate = true;
            this.certificateApplicationId = applicationId;
            this.certificateTtl = ttl;
            this.certificateRetry = retry;
            getLogger().log(LogLevel.CONFIG, "Got certificate: " + certificate);
        }
        catch (Exception e) {
            getLogger().log(LogLevel.WARNING,"Exception while initializing certificate for application '" +
                                             applicationId + "' in " + this, e);
        }
    }

    /**
     * Initialize the certificate proxy setting.
     */
    private void initializeProxy(String host, int port) {
        certificateProxy = new HttpHost(host, port);
        getLogger().log(LogLevel.CONFIG, "Proxy is configured; will use proxy: " + certificateProxy);
    }

    /**
     * Same a {@code getURI(query, offset, hits, null)}.
     * @see #getURI(Query, Hit, Connection)
     */
    protected URI getURI(Query query,Connection connection) throws MalformedURLException, URISyntaxException {
        Hit requestMeta;
        try {
            requestMeta = (Hit) query.properties().get(HTTPClientSearcher.REQUEST_META_CARRIER);
        } catch (ClassCastException e) {
            requestMeta = null;
        }
        return getURI(query, requestMeta, connection);
    }

    /**
     * Creates the URI for a query.
     * Populates the {@code requestMeta} meta hit with the created URI HTTP properties.
     *
     * @param requestMeta a meta hit that holds logging information about this request (may be {@code null}).
     */
    protected URI getURI(Query query, Hit requestMeta, Connection connection)
            throws MalformedURLException, URISyntaxException {
        StringBuilder parameters = new StringBuilder();

        Map<String, String> queries = getQueryMap(query);
        if (queries.size() > 0) {
            Iterator<Map.Entry<String, String>> mapIterator = queries.entrySet().iterator();
            parameters.append("?");
            try {
                Map.Entry<String, String> entry;
                while (mapIterator.hasNext()) {
                    entry = mapIterator.next();

                    if (requestMeta != null)
                        requestMeta.setField(LOG_QUERY_PARAM_PREFIX
                                + entry.getKey(), entry.getValue());

                    parameters.append(entry.getKey() + "=" + URLEncoder.encode(entry.getValue(),
                                                                               httpParameters.getInputEncoding()));
                    if (mapIterator.hasNext()) {
                        parameters.append("&");
                    }
                }
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException("Unknown input encoding set in " + this, e);
            }
        }

        URI uri = new URL(httpParameters.getSchema(), connection.getHost(),
                          connection.getPort(), getPath() + parameters.toString()).toURI();
        if (requestMeta != null) {
            requestMeta.setField(LOG_URI, uri.toString());
            requestMeta.setField(LOG_SCHEME, uri.getScheme());
            requestMeta.setField(LOG_HOST, uri.getHost());
            requestMeta.setField(LOG_PORT, uri.getPort());
            requestMeta.setField(LOG_PATH, uri.getPath());
        }
        return uri;
    }

    /**
     * Called by getURI() to get the path of the URI for the external service.
     * The default implementation returns httpParameters.getPath(); subclasses
     * which only wants to override the path from httpParameters may use this
     * method instead of overriding all of getURI().
     *
     * @return the path to use for getURI
     */
    protected String getPath() {
        return httpParameters.getPath();
    }

    /**
     * The URI that is used to check if the provider is up or down. This will again be used in the
     * checkPing method by checking that we get a response that has a good status code (below 300). If better
     * validation than just status code checking is needed, override the checkPing method.
     */
    protected URI getPingURI(Connection connection) throws MalformedURLException, URISyntaxException {
        return new URL(httpParameters.getSchema(),connection.getHost(),connection.getPort(),getPingPath()).toURI();
    }

    /**
     * Called by getPingURI() to get the path of the URI for pinging the
     * external service. The default implementation returns
     * httpParameters.getPath(); subclasses which only wants to override the
     * path from httpParameters may use this method instead of overriding all of
     * getPingURI().
     *
     * @return the path to use for getPingURI
     */
    protected String getPingPath() {
        return httpParameters.getPath();
    }

    /**
     * Checks if the response is valid.
     * @param response The response from the ping request
     * @param pong The pong result to return back to the calling method. This method
     * will add an error to the pong result (using addError) if the status of the HTTP response is 300 or above.
     */
    protected void checkPing(HttpResponse response, Pong pong) {
        if (response.getStatusLine().getStatusCode() >= 300) {
            pong.addError(com.yahoo.search.result.ErrorMessage.createBackendCommunicationError(
                    "Got error " + response.getStatusLine().getStatusCode()
                    + " when contacting backend")
            );
        }
    }

    /**
     * Pinging in HTTPBackend is done by creating a PING uri from http://host:port/path.
     * If this returns a status that is below 300, the ping is considered good.
     *
     * If another uri is needed for pinging, reimplement getPingURI.
     *
     * Override either this method to change how ping
     */
    @Override
    public Pong ping(Ping ping, Connection connection) {
        URI uri = null;
        Pong pong = new Pong();
        HttpResponse response = null;

        if (httpParameters.getPingOption() == PingOption.DISABLE)
            return pong;

        try {
            uri = getPingURI(connection);
            if (uri == null)
                pong.addError(ErrorMessage.createIllegalQuery("Ping uri is null"));
            if (uri.getHost()==null) {
                pong.addError(ErrorMessage.createIllegalQuery("Ping uri has no host"));
                uri=null;
            }
        } catch (MalformedURLException | URISyntaxException e) {
            pong.addError(ErrorMessage.createIllegalQuery("Malformed ping uri '" + uri + "': " +
                                                          Exceptions.toMessageString(e)));
        } catch (RuntimeException e) {
            log.log(Level.WARNING,"Unexpected exception while attempting to ping " + connection + 
                                  " using uri '" + uri + "'",e);
            pong.addError(ErrorMessage.createIllegalQuery("Unexpected problem with ping uri '" + uri + "': " +
                                                          Exceptions.toMessageString(e)));
        }

        if (uri == null) return pong;
        pong.setPingInfo("using uri '" + uri + "'");

        try {
            response = getPingResponse(uri, ping);
            checkPing(response, pong);
        } catch (IOException e) {
            // We do not have a valid ping
            pong.addError(ErrorMessage.createBackendCommunicationError(
                          "Exception thrown when pinging with url '" + uri + "': " + Exceptions.toMessageString(e)));
        } catch (TimeoutException e) {
            pong.addError(ErrorMessage.createTimeout("Timeout for ping " + uri + " in " + this + ": " + e.getMessage()));
        } catch (RuntimeException e) {
            log.log(Level.WARNING,"Unexpected exception while attempting to ping " + connection + " using uri '" + uri + "'",e);
            pong.addError(ErrorMessage.createIllegalQuery("Unexpected problem with ping uri '" + uri + "': " +
                                                          Exceptions.toMessageString(e)));
        } finally {
            if (response != null) {
                cleanupHttpEntity(response.getEntity());
            }
        }

        return pong;
    }

    private HttpResponse getPingResponse(URI uri, Ping ping) throws IOException {
        long timeLeft = ping.getTimeout();
        int connectionTimeout = (int) (timeLeft / 4L);
        int readTimeout = (int) (timeLeft * 3L / 4L);

        Map<String, String> requestHeaders = null;
        if (httpParameters.getPingOption() == PingOption.YCA)
            requestHeaders = generateYCAHeaders();

        return getResponse(uri, null, requestHeaders, null, connectionTimeout, readTimeout);
    }

    /**
     * Same a {@code getEntity(uri, null)}.
     * @param uri resource to fetch
     * @param query the originating query
     * @throws TimeoutException If query.timeLeft() equal to or lower than 0
     */
    protected HttpEntity getEntity(URI uri, Query query) throws IOException{
    	return getEntity(uri, null, query);
    }


    /**
     * Gets the HTTP entity that holds the response contents.
     * @param uri the request URI.
     * @param requestMeta a meta hit that holds logging information about this request (may be {@code null}).
     * @param query the originating query
     * @return the http entity, or null if none
     * @throws java.io.IOException Whenever HTTP status code is in the 300 or higher range.
     * @throws TimeoutException If query.timeLeft() equal to or lower than 0
     */
    protected HttpEntity getEntity(URI uri, Hit requestMeta, Query query) throws IOException {
        if (query.getTimeLeft() <= 0) {
            throw new TimeoutException("No time left for querying external backend.");
        }
        HttpResponse response = getResponse(uri, requestMeta, query);
        StatusLine statusLine = response.getStatusLine();

        // Logging
        if (requestMeta != null) {
        	requestMeta.setField(LOG_STATUS, statusLine.getStatusCode());
        	for (HeaderIterator headers = response.headerIterator(); headers.hasNext(); ) {
        		Header h = headers.nextHeader();
        		requestMeta.setField(LOG_RESPONSE_HEADER_PREFIX + h.getName(), h.getValue());
        	}
        }

        if (statusLine.getStatusCode() >= 300) {
            HttpEntity entity = response.getEntity();
            String message = createServerReporterErrorMessage(statusLine, entity);
            cleanupHttpEntity(response.getEntity());
            throw new IOException(message);
        }

        return response.getEntity();
    }

    private String createServerReporterErrorMessage(StatusLine statusLine, HttpEntity entity) {
        String message = "Error when trying to connect to HTTP backend: "
                         + statusLine.getStatusCode() + " : " + statusLine.getReasonPhrase();

        try {
            if (entity != null) {
                message += "(Message = " + EntityUtils.toString(entity) + ")";
            }
        } catch (Exception e) {
            log.log(LogLevel.WARNING, "Could not get message.", e);
        }

        return message;
    }

    /**
     * Creates a meta hit dedicated to holding logging information. This hit has
     * the 'logging:[searcher's ID]' type.
     */
    protected Hit createRequestMeta() {
        Hit requestMeta = new Hit("logging:" + getId().toString());
        requestMeta.setMeta(true);
        requestMeta.types().add("logging");
        return requestMeta;
    }

    protected void cleanupHttpEntity(HttpEntity entity) {
        if (entity == null) return;

        try {
            entity.consumeContent();
        } catch (IOException e) {
            // It is ok if do not consume it, the resource will be freed after
            // timeout.
            // But log it just in case.
            log.log(LogLevel.getVespaLogLevel(LogLevel.DEBUG),
                    "Not able to consume after processing: " + Exceptions.toMessageString(e));
        }
    }

    /**
     * Same as {@code getResponse(uri, null)}.
     */
    protected HttpResponse getResponse(URI uri, Query query) throws IOException{
    	return getResponse(uri, null, query);
    }

    /**
     * Executes an HTTP request and gets the response.
     * @param uri the request URI.
     * @param requestMeta a meta hit that holds logging information about this request (may be {@code null}).
     * @param query the originating query, used to calculate timeouts
     */
    protected HttpResponse getResponse(URI uri, Hit requestMeta, Query query) throws IOException {
        long timeLeft = query.getTimeLeft();
        int connectionTimeout = (int) (timeLeft / 4L);
        int readTimeout = (int) (timeLeft * 3L / 4L);
        connectionTimeout = connectionTimeout <= 0 ? 1 : connectionTimeout;
        readTimeout = readTimeout <= 0 ? 1 : readTimeout;
        HttpEntity reqEntity = getRequestEntity(query, requestMeta);
        Map<String, String> reqHeaders = getRequestHeaders(query, requestMeta);
        if ((reqEntity == null) && (reqHeaders == null)) {
            return getResponse(uri, requestMeta, connectionTimeout, readTimeout);
        } else {
            return getResponse(uri, reqEntity, reqHeaders, requestMeta, connectionTimeout, readTimeout);
        }
    }

    /**
     * Returns the set of headers to be passed in the http request to provider backend. The default
     * implementation returns null, unless certificates are in use. If certificates are used, it will return a map
     * only containing the needed certificate headers.
     */
    protected Map<String, String> getRequestHeaders(Query query, Hit requestMeta) {
        if (useCertificate) {
            return generateYCAHeaders();
        }
        return null;
    }

 /**
     * Returns the HTTP request entity to use when making the request for this query.
     * This default implementation returns null.
     *
     * <p> Do return a repeatable entity if HTTP retry is active.
     *
     * @return the http request entity to use, or null to use the default entity
     */
    protected HttpEntity getRequestEntity(Query query, Hit requestMeta) {
        return null;
    }

    /**
     * Executes an HTTP request and gets the response.
     * @param uri the request URI.
     * @param requestMeta a meta hit that holds logging information about this request (may be {@code null}).
     * @param connectionTimeout how long to wait for getting a connection
     * @param readTimeout timeout for reading HTTP data
     */
    protected HttpResponse getResponse(URI uri, Hit requestMeta, int connectionTimeout, int readTimeout)
            throws IOException {
        return getResponse(uri, null, null, requestMeta, connectionTimeout, readTimeout);
    }


    /**
     * Executes an HTTP request and gets the response.
     * @param uri the request URI.
     * @param requestMeta a meta hit that holds logging information about this request (may be {@code null}).
     * @param connectionTimeout how long to wait for getting a connection
     * @param readTimeout timeout for reading HTTP data
     */
    protected HttpResponse getResponse(URI uri, HttpEntity reqEntity,
                                       Map<String, String> reqHeaders, Hit requestMeta,
                                       int connectionTimeout, int readTimeout) throws IOException {

        HttpParams httpParams = httpParameters.toHttpParams(connectionTimeout, readTimeout);
        HttpClient httpClient = createClient(httpParams);
        long start = 0L;
        HttpUriRequest request;
        if (httpParameters.getEnableProxy() && "http".equals(httpParameters.getProxyType())) {
            HttpHost proxy = new HttpHost(httpParameters.getProxyHost(),
                                          httpParameters.getProxyPort(), httpParameters.getProxyType());
            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            // Logging
            if (requestMeta != null) {
                requestMeta.setField(LOG_PROXY_TYPE, httpParameters.getProxyType());
                requestMeta.setField(LOG_PROXY_HOST, httpParameters.getProxyHost());
                requestMeta.setField(LOG_PROXY_PORT, httpParameters.getProxyPort());
            }
        }
        if (reqEntity == null) {
            request = createRequest(httpParameters.getMethod(), uri);
        } else {
            request = createRequest(httpParameters.getMethod(), uri, reqEntity);
        }

        if (reqHeaders != null) {
            for (Entry<String, String> entry : reqHeaders.entrySet()) {
                if (entry.getValue() == null || isAscii(entry.getValue())) {
                    request.addHeader(entry.getKey(), entry.getValue());
                } else {
                    byte[] asBytes = Utf8.toBytes(entry.getValue());
                    String asLyingString = new String(asBytes, 0, asBytes.length, iso8859Charset);
                    request.addHeader(entry.getKey(), asLyingString);
                }
            }
        }

        // Logging
        if (requestMeta != null) {
            for (HeaderIterator headers = request.headerIterator(); headers.hasNext();) {
                Header h = headers.nextHeader();
                requestMeta.setField(LOG_HEADER_PREFIX + h.getName(), h.getValue());
            }
            start = System.currentTimeMillis();
        }

        HttpResponse response;

        try {
            HttpContext context = new BasicHttpContext();
            response = httpClient.execute(request, context);

            if (requestMeta != null) {
                requestMeta.setField(LOG_IP_ADDRESS, getIpAddress(context));
            }
         } catch (ConnectTimeoutException e) {
            connectTimeouts.increment();
            throw e;
        }

        // Logging
        long latencyStart = System.currentTimeMillis() - start;
        if (requestMeta != null) {
            requestMeta.setField(LOG_LATENCY_START, latencyStart);
        }
        logResponseLatency(latencyStart);
        return response;
    }

    private String getIpAddress(HttpContext context) {
        HttpConnection connection = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
        if (connection instanceof HttpInetConnection) {
            InetAddress address = ((HttpInetConnection) connection).getRemoteAddress();
            String hostAddress = address.getHostAddress();
            return hostAddress == null ?
                    IP_ADDRESS_UNKNOWN:
                    hostAddress;
        } else {
            getLogger().log(LogLevel.DEBUG, "Unexpected connection type: " + connection.getClass().getName());
            return IP_ADDRESS_UNKNOWN;
        }
    }

    private boolean isAscii(String value) {
        char[] scanBuffer = new char[value.length()];
        value.getChars(0, value.length(), scanBuffer, 0);
        for (char c: scanBuffer)
            if (c > 127) return false;
        return true;
    }

    protected void logResponseLatency(long latency) { }

    /**
     * Creates a http client for one request. Override to customize the client
     * to use, e.g for testing. This default implementation will add a certificate store
     * proxy to params if is necessary, and then do
     * <code>return new SearcherHttpClient(getConnectionManager(params), params);</code>
     */
    protected HttpClient createClient(HttpParams params) {
        if (certificateProxy != null) {
            params.setParameter(ConnRoutePNames.DEFAULT_PROXY, certificateProxy);
        }
        return new SearcherHttpClient(getConnectionManager(params), params);
    }

    /**
     * Creates a HttpRequest. Override to customize the request.
     * This default implementation does <code>return new HttpRequest(method,uri);</code>
     */
    protected HttpUriRequest createRequest(String method,URI uri) {
        return createRequest(method, uri, null);
    }

    /**
     * Creates a HttpRequest. Override to customize the request.
     * This default implementation does <code>return new HttpRequest(method,uri);</code>
     */
    protected HttpUriRequest createRequest(String method,URI uri, HttpEntity entity) {
        return new SearcherHttpRequest(method,uri);
    }

    /** Get a connection manager which may be used safely from this thread */
    protected ClientConnectionManager getConnectionManager(HttpParams params) {
        if (sharedConnectionManager != null) {// We are using shared connections
            return sharedConnectionManager;
        } else {
            SingleClientConnManager singleClientConnManager = singleClientConnManagerThreadLocal.get();
            if (singleClientConnManager == null) {
                singleClientConnManager = new SingleClientConnManager(params, schemeRegistry);
                singleClientConnManagerThreadLocal.set(singleClientConnManager);
            }
            return singleClientConnManager;
        }
    }

    /** Utility method for creating error messages when a url is incorrect */
    protected ErrorMessage createMalformedUrlError(Query query,Exception e) {
        return ErrorMessage.createErrorInPluginSearcher("Malformed url in " + this + " for " + query +
                                                        ": " + Exceptions.toMessageString(e));
    }

    private Map<String, String> generateYCAHeaders() {
        Map<String, String> headers = new HashMap<>();
        String certificate = certificateStore.getCertificate(certificateApplicationId, certificateTtl, certificateRetry);
        headers.put(YCA_HTTP_HEADER, certificate);
        return headers;
    }

    protected static class SearcherHttpClient extends DefaultHttpClient {

        private final int retries;

        public SearcherHttpClient(final ClientConnectionManager conman, final HttpParams params) {
            super(conman, params);
            retries = params.getIntParameter(HTTPParameters.RETRIES, 1);
            addRequestInterceptor((request, context) -> {
                if (!request.containsHeader("Accept-Encoding")) {
                    request.addHeader("Accept-Encoding", "gzip");
                }
            });
            addResponseInterceptor((response, context) -> {
                HttpEntity entity = response.getEntity();
                if (entity == null) return;
                Header ceheader = entity.getContentEncoding();
                if (ceheader == null) return;
                for (HeaderElement codec : ceheader.getElements()) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            });
        }

        @Override
        protected HttpRequestExecutor createRequestExecutor() {
            return new HttpRequestExecutor();
        }

        @Override
        protected HttpRoutePlanner createHttpRoutePlanner() {
            return new DefaultHttpRoutePlanner(getConnectionManager().getSchemeRegistry());
        }

        @Override
        protected HttpRequestRetryHandler createHttpRequestRetryHandler() {
            return new SearcherHttpRequestRetryHandler(retries);
        }
    }

    /** A retry handler which avoids retrying forever on errors misclassified as transient */
    private static class SearcherHttpRequestRetryHandler implements HttpRequestRetryHandler {
        private final int retries;

        public SearcherHttpRequestRetryHandler(int retries) {
            this.retries = retries;
        }

        @Override
        public boolean retryRequest(IOException e, int executionCount, HttpContext httpContext) {
            if (e == null) {
                throw new IllegalArgumentException("Exception parameter may not be null");
            }
            if (executionCount > retries) {
                return false;
            }
            if (e instanceof NoHttpResponseException) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (e instanceof InterruptedIOException) {
                // Timeout from federation layer
                return false;
            }
            if (e instanceof UnknownHostException) {
                // Unknown host
                return false;
            }
            if (e instanceof SSLHandshakeException) {
                // SSL handshake exception
                return false;
            }
            return true;
        }


    }

    private static class SearcherHttpRequest extends HttpRequestBase {
        String method;

        public SearcherHttpRequest(String method, final URI uri) {
            super();
            this.method = method;
            setURI(uri);
        }

        @Override
        public String getMethod() {
            return method;
        }
    }

    /**
     * Only for testing.
     */
    public void shutdownConnectionManagers() {
        ClientConnectionManager manager;
        if (sharedConnectionManager != null) {
            manager = sharedConnectionManager;
        } else {
            manager = singleClientConnManagerThreadLocal.get();
        }
        if (manager != null) {
            manager.shutdown();
        }
    }

    protected static final class ThrowingCertificateStore implements CertificateStore {

        @Override
        public String getCertificate(String key, long ttl, long retry) {
            throw new UnsupportedOperationException("A certificate store is not available");
        }

    }

}