@Override
 public HttpClientRequestExecutor build() {
   if (eventManager == null) {
     throw new ConfigurationException("eventManager is mandatory");
   }
   if (driver == null) {
     throw new ConfigurationException("driver is mandatory");
   }
   if (properties == null) {
     throw new ConfigurationException("properties is mandatory");
   }
   HttpClientRequestExecutor result = new HttpClientRequestExecutor();
   result.eventManager = eventManager;
   result.preserveHost = Parameters.PRESERVE_HOST.getValue(properties);
   if (cookieManager == null) {
     cookieManager =
         ExtensionFactory.getExtension(properties, Parameters.COOKIE_MANAGER, driver);
   }
   result.cookieManager = cookieManager;
   result.connectTimeout = Parameters.CONNECT_TIMEOUT.getValue(properties);
   result.socketTimeout = Parameters.SOCKET_TIMEOUT.getValue(properties);
   result.httpClient = buildHttpClient();
   String firstBaseURL = Parameters.REMOTE_URL_BASE.getValue(properties)[0];
   result.firstBaseUrlHost = UriUtils.extractHost(firstBaseURL);
   return result;
 }
  @Override
  public OutgoingRequest createOutgoingRequest(
      DriverRequest originalRequest, String uri, boolean proxy) {
    // Extract the host in the URI. This is the host we have to send the
    // request to physically.
    HttpHost physicalHost = UriUtils.extractHost(uri);

    if (!originalRequest.isExternal()) {
      if (preserveHost) {
        // Preserve host if required
        HttpHost virtualHost = HttpRequestHelper.getHost(originalRequest.getOriginalRequest());
        // Rewrite the uri with the virtualHost
        uri = UriUtils.rewriteURI(uri, virtualHost);
      } else {
        uri = UriUtils.rewriteURI(uri, firstBaseUrlHost);
      }
    }

    RequestConfig.Builder builder = RequestConfig.custom();
    builder.setConnectTimeout(connectTimeout);
    builder.setSocketTimeout(socketTimeout);

    // Use browser compatibility cookie policy. This policy is the closest
    // to the behavior of a real browser.
    builder.setCookieSpec(CustomBrowserCompatSpecFactory.CUSTOM_BROWSER_COMPATIBILITY);

    builder.setRedirectsEnabled(false);
    RequestConfig config = builder.build();

    OutgoingRequestContext context = new OutgoingRequestContext();

    String method = "GET";
    if (proxy) {
      method = originalRequest.getOriginalRequest().getRequestLine().getMethod().toUpperCase();
    }
    OutgoingRequest outgoingRequest =
        new OutgoingRequest(
            method,
            uri,
            originalRequest.getOriginalRequest().getProtocolVersion(),
            originalRequest,
            config,
            context);
    if (ENTITY_METHODS.contains(method)) {
      outgoingRequest.setEntity(originalRequest.getOriginalRequest().getEntity());
    } else if (!SIMPLE_METHODS.contains(method)) {
      throw new UnsupportedHttpMethodException(method + " " + uri);
    }

    context.setPhysicalHost(physicalHost);
    context.setOutgoingRequest(outgoingRequest);
    context.setProxy(proxy);

    return outgoingRequest;
  }