public void loadHttpMethod(final ClientRequest request, HttpRequestBase httpMethod)
      throws Exception {
    if (httpMethod instanceof HttpGet && request.followRedirects()) {
      HttpClientParams.setRedirecting(httpMethod.getParams(), true);
    } else {
      HttpClientParams.setRedirecting(httpMethod.getParams(), false);
    }

    if (request.getBody() != null && !request.getFormParameters().isEmpty())
      throw new RuntimeException("You cannot send both form parameters and an entity body");

    if (!request.getFormParameters().isEmpty()) {
      commitHeaders(request, httpMethod);
      HttpPost post = (HttpPost) httpMethod;

      List<NameValuePair> formparams = new ArrayList<NameValuePair>();

      for (Map.Entry<String, List<String>> formParam : request.getFormParameters().entrySet()) {
        List<String> values = formParam.getValue();
        for (String value : values) {
          formparams.add(new BasicNameValuePair(formParam.getKey(), value));
        }
      }

      UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
      post.setEntity(entity);
    } else if (request.getBody() != null) {
      if (httpMethod instanceof HttpGet)
        throw new RuntimeException("A GET request cannot have a body.");

      try {
        HttpEntity entity = buildEntity(request);
        HttpPost post = (HttpPost) httpMethod;
        commitHeaders(request, httpMethod);
        post.setEntity(entity);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    } else // no body
    {
      commitHeaders(request, httpMethod);
    }
  }
 public void commitHeaders(ClientRequest request, HttpRequestBase httpMethod) {
   MultivaluedMap<String, String> headers = request.getHeaders();
   for (Map.Entry<String, List<String>> header : headers.entrySet()) {
     List<String> values = header.getValue();
     for (String value : values) {
       //               System.out.println(String.format("setting %s = %s", header.getKey(),
       // value));
       httpMethod.addHeader(header.getKey(), value);
     }
   }
 }