org.apache.hc.core5.net.URIBuilder Java Examples

The following examples show how to use org.apache.hc.core5.net.URIBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Hc5HttpUtils.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * Create an HTTP POST Method.
 * 
 * @param url URL of the request.
 * @param properties Properties to drive the API.
 * @return POST Method
 * @throws URISyntaxException Exception if the URL is not correct.
 */
private static HttpPost createHttpPostMethod(
    String url,
    Map<String, String> properties) throws URISyntaxException {

  // Initialize POST method
  StringBuilder debugUrl = (DEBUG_URL) ? new StringBuilder("POST " + url) : null;
  HttpPost method = new HttpPost(new URIBuilder(url, StandardCharsets.UTF_8).build());
  method.addHeader("Accept-Encoding", "gzip");

  // Manage query parameters
  if (properties != null) {
    boolean first = true;
    List<org.apache.hc.core5.http.NameValuePair> params = new ArrayList<>();
    Iterator<Map.Entry<String, String>> iter = properties.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry<String, String> property = iter.next();
      String key = property.getKey();
      String value = property.getValue();
      params.add(new BasicNameValuePair(key, value));
      first = fillDebugUrl(debugUrl, first, key, value);
    }
    method.setEntity(new UrlEncodedFormEntity(params));
  }

  if (DEBUG_URL && (debugUrl != null)) {
    debugText(debugUrl.toString());
  }
  return method;
}
 
Example #2
Source File: Hc5HttpUtils.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * Create an HTTP GET Method.
 * 
 * @param url URL of the request.
 * @param properties Properties to drive the API.
 * @return GET Method
 * @throws URISyntaxException Exception if the URL is not correct.
 */
private static HttpGet createHttpGetMethod(
    String url,
    Map<String, String> properties) throws URISyntaxException {

  // Initialize URI
  StringBuilder debugUrl = (DEBUG_URL) ? new StringBuilder("GET  " + url) : null;
  URIBuilder uriBuilder = new URIBuilder(url, StandardCharsets.UTF_8);
  if (properties != null) {
    boolean first = true;
    Iterator<Map.Entry<String, String>> iter = properties.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry<String, String> property = iter.next();
      String key = property.getKey();
      String value = property.getValue();
      uriBuilder.addParameter(key, value);
      first = fillDebugUrl(debugUrl, first, key, value);
    }
  }

  // Initialize GET Method
  HttpGet method = new HttpGet(uriBuilder.build());
  method.addHeader("Accept-Encoding", "gzip");

  if (DEBUG_URL && (debugUrl != null)) {
    debugText(debugUrl.toString());
  }
  return method;
}
 
Example #3
Source File: AbstractRequest.java    From spotify-web-api-java with MIT License 5 votes vote down vote up
protected AbstractRequest(Builder<T, ?> builder) {
  assert (builder != null);
  assert (builder.path != null);
  assert (!builder.path.equals(""));

  this.httpManager = builder.httpManager;

  URIBuilder uriBuilder = new URIBuilder();
  uriBuilder
    .setScheme(builder.scheme)
    .setHost(builder.host)
    .setPort(builder.port)
    .setPath(builder.path);
  if (builder.queryParameters.size() > 0) {
    uriBuilder
      .setParameters(builder.queryParameters);
  }

  try {
    this.uri = uriBuilder.build();
  } catch (URISyntaxException e) {
    SpotifyApi.LOGGER.log(Level.SEVERE, e.getMessage());
  }

  this.headers = builder.headers;
  this.contentType = builder.contentType;
  this.body = builder.body;
  this.bodyParameters = builder.bodyParameters;
}