Example #1
0
  /**
   * validate
   *
   * @param value
   * @param pattern
   * @param caseSensitive
   * @param trim
   * @return
   */
  public static boolean validate(
      String value, String pattern, boolean caseSensitive, boolean trim) {

    if (StringUtils.isBlank(value) || StringUtils.isBlank(pattern)) return false;

    Pattern p =
        (caseSensitive
            ? Pattern.compile(pattern)
            : Pattern.compile(pattern, Pattern.CASE_INSENSITIVE));

    if (trim) value = value.trim();

    return p.matcher(value).matches();
  }
Example #2
0
    /**
     * @param key
     * @param v
     * @param sb
     */
    void appendQueryString(String key, Object v, StringBuilder sb) {
      if (v == null) return;

      String value = String.valueOf(v);
      if (ignoreEmpty && StringUtils.isBlank(value)) return;

      sb.append("&").append(key).append("=").append(encodeUrl(value));
    }
Example #3
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;
 }
Example #4
0
 public UrlBuilder(final String baseUrl, final String charsetName, final boolean ignoreEmpty) {
   try {
     this.baseUrl = new URL(baseUrl);
     this.ignoreEmpty = ignoreEmpty;
     this.charset = Charset.forName(charsetName);
     String queryString = this.baseUrl.getQuery();
     if (StringUtils.isNotEmpty(queryString))
       queryMap = new LinkedHashMap<String, Object>(parseQuery(queryString));
     else queryMap = Collections.emptyMap();
   } catch (MalformedURLException e) {
     throw new RuntimeException(e);
   }
 }
Example #5
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();
    }