private <R> void populateHeaders(Invocation.Builder invocation, HttpRequest<R> request) {

    if (!request.hasHeaders()) return;

    for (Map.Entry<String, Object> h : request.getHeaders().entrySet()) {
      invocation.header(h.getKey(), h.getValue());
    }
  }
  private <R> WebTarget populateQueryParams(WebTarget target, HttpRequest<R> request) {

    if (!request.hasQueryParams()) return target;

    for (Map.Entry<String, List<Object>> entry : request.getQueryParams().entrySet()) {
      for (Object o : entry.getValue()) {
        target = target.queryParam(entry.getKey(), o);
      }
    }
    return target;
  }
  /**
   * Invokes the given request
   *
   * @param <R> the return type
   * @param request the request to invoke
   * @return the response
   * @throws Exception the exception
   */
  private <R> HttpResponse invoke(HttpRequest<R> request, boolean useNonStrictSSL)
      throws Exception {
    Client client = ClientFactory.create(useNonStrictSSL);
    WebTarget target = client.target(request.getEndpoint()).path(request.getPath());

    if (Boolean.getBoolean(HttpLoggingFilter.class.getName()))
      target.register(new HttpLoggingFilter(Logger.getLogger("os"), 10000));

    target = populateQueryParams(target, request);

    Invocation.Builder invocation = target.request(MediaType.APPLICATION_JSON);
    populateHeaders(invocation, request);

    Entity<?> entity =
        (request.getEntity() == null)
            ? null
            : Entity.entity(request.getEntity(), request.getContentType());
    try {
      if (entity != null) {
        return HttpResponse.wrap(invocation.method(request.getMethod().name(), entity));
      }
      if (HttpMethod.PUT == request.getMethod() || request.hasJson()) {
        return HttpResponse.wrap(
            invocation.method(
                request.getMethod().name(),
                Entity.entity(request.getJson(), ClientConstants.CONTENT_TYPE_JSON)));
      }
      return HttpResponse.wrap(invocation.method(request.getMethod().name()));
    } catch (ProcessingException pe) {
      throw new ConnectionException(pe.getMessage(), 0, pe);
    } catch (ClientErrorException e) {
      throw HttpResponse.mapException(
          e.getResponse().getStatusInfo().toString(), e.getResponse().getStatus(), e);
    }
  }
 /** {@inheritDoc} */
 @Override
 public <R> HttpResponse execute(HttpRequest<R> request) {
   return execute(request, request.useNonStrictSSLClient());
 }