Esempio n. 1
0
 protected HttpUriRequest constructHttpMethod(String methodName, String url, Object data)
     throws UnsupportedEncodingException {
   if (methodName.equalsIgnoreCase("POST")) {
     HttpPost httpPost = new HttpPost(url);
     log.debug("POST method created based on client request");
     if (data != null) httpPost.setEntity(new StringEntity(createJsonStringEntity(data), "UTF-8"));
     return httpPost;
   } else if (methodName.equalsIgnoreCase("PUT")) {
     HttpPut httpPut = new HttpPut(url);
     log.debug("PUT method created based on client request");
     if (data != null) httpPut.setEntity(new StringEntity(createJsonStringEntity(data), "UTF-8"));
     return httpPut;
   } else if (methodName.equalsIgnoreCase("DELETE")) {
     log.debug("DELETE method created based on client request");
     return new HttpDelete(url);
   } else if (methodName.equalsIgnoreCase("GET")) {
     log.debug("GET method created based on client request");
     HttpGetWithEntity httpGet = new HttpGetWithEntity(url);
     if (data != null) httpGet.setEntity(new StringEntity(createJsonStringEntity(data), "UTF-8"));
     return httpGet;
   } else if (methodName.equalsIgnoreCase("HEAD")) {
     log.debug("HEAD method created based on client request");
     return new HttpHead(url);
   } else {
     return null;
   }
 }
 /**
  * Performs a HTTP PUT request, saves an attachment.
  *
  * @return {@link Response}
  */
 Response put(URI uri, InputStream instream, String contentType) {
   HttpResponse response = null;
   try {
     HttpPut httpPut = new HttpPut(uri);
     InputStreamEntity entity = new InputStreamEntity(instream, -1);
     entity.setContentType(contentType);
     httpPut.setEntity(entity);
     response = executeRequest(httpPut);
     return getResponse(response);
   } finally {
     close(response);
   }
 }
Esempio n. 3
0
 static HttpUriRequest createMultiPartRequest(
     Request<?> request, Map<String, String> additionalHeaders) throws AuthFailureError {
   switch (request.getMethod()) {
     case Method.DEPRECATED_GET_OR_POST:
       {
         // This is the deprecated way that needs to be handled for backwards compatibility.
         // If the request's post body is null, then the assumption is that the request is
         // GET.  Otherwise, it is assumed that the request is a POST.
         byte[] postBody = request.getBody();
         if (postBody != null) {
           HttpPost postRequest = new HttpPost(request.getUrl());
           if (request.getBodyContentType() != null)
             postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
           HttpEntity entity;
           entity = new ByteArrayEntity(postBody);
           postRequest.setEntity(entity);
           return postRequest;
         } else {
           return new HttpGet(request.getUrl());
         }
       }
     case Method.GET:
       return new HttpGet(request.getUrl());
     case Method.DELETE:
       return new HttpDelete(request.getUrl());
     case Method.POST:
       {
         HttpPost postRequest = new HttpPost(request.getUrl());
         if (request.getBodyContentType() != null) {
           postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
         }
         setMultiPartBody(postRequest, request);
         return postRequest;
       }
     case Method.PUT:
       {
         HttpPut putRequest = new HttpPut(request.getUrl());
         if (request.getBodyContentType() != null)
           putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
         setMultiPartBody(putRequest, request);
         return putRequest;
       }
     default:
       throw new IllegalStateException("Unknown request method.");
   }
 }
Esempio n. 4
0
  /**
   * Sends a POST request and returns the response.
   *
   * @param endpoint The endpoint to send the request to.
   * @param requestMessage A message to send in the request body. Can be null.
   * @param responseClass The class to deserialise the Json response to. Can be null if no response
   *     message is expected.
   * @param headers Any additional headers to send with this request. You can use {@link
   *     org.apache.http.HttpHeaders} constants for header names.
   * @param <T> The type to deserialise the response to.
   * @return A {@link Response} containing the deserialised body, if any.
   * @throws IOException If an error occurs.
   */
  public <T> Response<T> put(
      Endpoint endpoint, Object requestMessage, Class<T> responseClass, NameValuePair... headers)
      throws IOException {

    // Create the request
    HttpPut put = new HttpPut(endpoint.url());
    put.setHeaders(combineHeaders(headers));

    // Add the request message if there is one
    put.setEntity(serialiseRequestMessage(requestMessage));

    // Send the request and process the response
    try (CloseableHttpResponse response = httpClient().execute(put)) {
      T body = deserialiseResponseMessage(response, responseClass);
      return new Response<>(response.getStatusLine(), body);
    }
  }
  // create http request
  private HttpRequestBase getHttpRequest() {
    HttpRequestBase httpRequest = null;

    if (method == null) {
      method = RestMethod.GET;
    }

    // set method
    switch (method) {
      case GET:
        httpRequest = new HttpGet(getFinalURL().toString());
        break;
      case DELETE:
        httpRequest = new HttpDelete(getFinalURL().toString());
        break;
      case POST:
        HttpPost postRequest = new HttpPost(getFinalURL().toString());
        if (multipartEntity != null) {
          postRequest.setEntity(multipartEntity);
        } else {
          postRequest.setEntity(getBody());
        }
        httpRequest = postRequest;
        break;
      case PUT:
        HttpPut putRequest = new HttpPut(getFinalURL().toString());
        putRequest.setEntity(getBody());
        httpRequest = putRequest;
        break;
      default:
        break;
    }

    // set headers
    if (headers != null) {
      for (String key : headers.keySet()) {
        httpRequest.addHeader(key, headers.get(key));
      }
    }

    return httpRequest;
  }
Esempio n. 6
0
 private HttpUriRequest getRequest(int method, String url, String data)
     throws UnsupportedEncodingException {
   switch (method) {
     case METHOD_GET:
       return new HttpGet(url);
     case METHOD_POST:
       HttpPost post = new HttpPost(url);
       if (data != null) {
         post.setEntity(new StringEntity(data));
       }
       return post;
     case METHOD_PUT:
       HttpPut put = new HttpPut(url);
       if (data != null) {
         put.setEntity(new StringEntity(data));
       }
       return put;
     case METHOD_DELETE:
       return new HttpDelete(url);
     default:
       return null;
   }
 }
  public void Execute(RequestMethod method) throws Exception {
    switch (method) {
      case GET:
        {
          // add parameters
          String combinedParams = "";
          if (!params.isEmpty()) {
            combinedParams += "?";
            for (NameValuePair p : params) {
              String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(), "UTF-8");
              if (combinedParams.length() > 1) {
                combinedParams += "&" + paramString;
              } else {
                combinedParams += paramString;
              }
            }
          }

          HttpGet request = new HttpGet(url + combinedParams);

          // add headers
          for (NameValuePair h : headers) {
            request.addHeader(h.getName(), h.getValue());
          }

          executeRequest(request, url);
          break;
        }
      case POST:
        {
          HttpPost request = new HttpPost(url);

          // add headers
          for (NameValuePair h : headers) {
            request.addHeader(h.getName(), h.getValue());
          }

          if (!params.isEmpty()) {
            request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
          }

          executeRequest(request, url);
          break;
        }
      case PUT:
        {
          HttpPut request = new HttpPut(url);

          // add headers
          for (NameValuePair h : headers) {
            request.addHeader(h.getName(), h.getValue());
          }

          if (!params.isEmpty()) {
            request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
          }

          executeRequest(request, url);
          break;
        }

      case DELETE:
        {
          HttpDelete request = new HttpDelete(url);

          for (NameValuePair h : headers) {
            request.addHeader(h.getName(), h.getValue());
          }

          executeRequest(request, url);
          break;
        }
    }
  }