コード例 #1
0
 /**
  * @param value
  * @return
  */
 String encodeUrl(String value) {
   try {
     return URLEncoder.encode(
         value,
         StringUtils.isNotBlank(charsetName) ? charsetName : Charset.defaultCharset().name());
   } catch (UnsupportedEncodingException e) {
     // Nothing to do
     logger.warn(e.getMessage());
   }
   return value;
 }
コード例 #2
0
    /** @return */
    public String build() {
      String path = prefixPath(baseUrl.getPath(), this.path);
      int port = baseUrl.getPort();
      if (baseUrl.getPort() == baseUrl.getDefaultPort()) port = -1;

      String host = baseUrl.getHost();
      if (StringUtils.isNotBlank(host) && host.startsWith(".")) host = host.substring(1);

      final StringBuilder builder;
      try {
        builder = new StringBuilder(new URL(baseUrl.getProtocol(), host, port, path).toString());
      } catch (MalformedURLException e) {
        throw new RuntimeException(e);
      }

      StringBuilder query = new StringBuilder();
      for (Entry<String, Object> entry : urlParams.entrySet()) {
        final String key = entry.getKey();
        Object value = entry.getValue();
        if (value == null) continue;

        if (value instanceof Object[]) {
          for (final Object v : (Object[]) value) {
            appendQueryString(key, v, query);
          }
        } else if (value instanceof Collection) {
          for (final Object v : (Collection<?>) value) {
            appendQueryString(key, v, query);
          }
        } else appendQueryString(key, value, query);
      }
      if (query.length() > 0) query.replace(0, 1, "?");
      builder.append(query);

      if (values.get() != null && values.get().size() > 0)
        return MessageFormat.format(builder.toString(), values.get().toArray());
      return builder.toString();
    }