Example #1
0
  public static URI toURI(String strUrl, int port) throws URISyntaxException {

    // print.o((strUrl));
    URI uri = new URI(strUrl);

    String host = uri.getHost();
    String fragment = uri.getRawFragment();
    String path = uri.getRawPath();
    String query = uri.getRawQuery();

    String scheme = uri.getScheme();
    String userInfo = uri.getRawUserInfo();
    if (port <= 0) port = uri.getPort();

    // decode path
    if (!StringUtil.isEmpty(path)) {

      int sqIndex = path.indexOf(';');
      String q = null;
      if (sqIndex != -1) {
        q = path.substring(sqIndex + 1);
        path = path.substring(0, sqIndex);
      }

      StringBuilder res = new StringBuilder();

      StringList list = ListUtil.toListTrim(path, '/');
      String str;

      while (list.hasNext()) {
        str = list.next();
        // str=URLDecoder.decode(str);

        if (StringUtil.isEmpty(str)) continue;
        res.append("/");
        res.append(escapeQSValue(str));
      }
      if (StringUtil.endsWith(path, '/')) res.append('/');
      path = res.toString();

      if (sqIndex != -1) {
        path += decodeQuery(q, ';');
      }
    }

    // decode query
    query = decodeQuery(query, '?');

    // decode ref/anchor
    if (!StringUtil.isEmpty(fragment)) {
      fragment = escapeQSValue(fragment);
    }

    // user/pasword
    if (!StringUtil.isEmpty(userInfo)) {
      int index = userInfo.indexOf(':');
      if (index != -1) {
        userInfo =
            escapeQSValue(userInfo.substring(0, index))
                + ":"
                + escapeQSValue(userInfo.substring(index + 1));
      } else userInfo = escapeQSValue(userInfo);
    }

    /*print.o("- fragment:"+fragment);
    print.o("- host:"+host);
    print.o("- path:"+path);
    print.o("- query:"+query);
    print.o("- scheme:"+scheme);
    print.o("- userInfo:"+userInfo);
    print.o("- port:"+port);
    print.o("- absolute:"+uri.isAbsolute());
    print.o("- opaque:"+uri.isOpaque());*/

    StringBuilder rtn = new StringBuilder();
    if (scheme != null) {
      rtn.append(scheme);
      rtn.append("://");
    }
    if (userInfo != null) {
      rtn.append(userInfo);
      rtn.append("@");
    }
    if (host != null) {
      rtn.append(host);
    }
    if (port > 0) {
      rtn.append(":");
      rtn.append(port);
    }
    if (path != null) {
      rtn.append(path);
    }
    if (query != null) {
      // rtn.append("?");
      rtn.append(query);
    }
    if (fragment != null) {
      rtn.append("#");
      rtn.append(fragment);
    }

    return new URI(rtn.toString());
  }
Example #2
0
  public static URL encodeURL(URL url, int port) throws MalformedURLException {

    // file
    String path = url.getPath();
    // String file=url.getFile();
    String query = url.getQuery();
    String ref = url.getRef();
    String user = url.getUserInfo();
    if (port <= 0) port = url.getPort();

    // decode path
    if (!StringUtil.isEmpty(path)) {
      int sqIndex = path.indexOf(';');
      String q = null;
      if (sqIndex != -1) {
        q = path.substring(sqIndex + 1);
        path = path.substring(0, sqIndex);
      }

      StringBuilder res = new StringBuilder();

      StringList list = ListUtil.toListTrim(path, '/');
      String str;

      while (list.hasNext()) {
        str = list.next();
        // str=URLDecoder.decode(str);

        if (StringUtil.isEmpty(str)) continue;
        res.append("/");
        res.append(escapeQSValue(str));
      }
      if (StringUtil.endsWith(path, '/')) res.append('/');
      path = res.toString();

      if (sqIndex != -1) {
        path += decodeQuery(q, ';');
      }
    }

    // decode query
    query = decodeQuery(query, '?');

    String file = path + query;

    // decode ref/anchor
    if (ref != null) {
      file += "#" + escapeQSValue(ref);
    }

    // user/pasword
    if (!StringUtil.isEmpty(user)) {
      int index = user.indexOf(':');
      if (index != -1) {
        user =
            escapeQSValue(user.substring(0, index))
                + ":"
                + escapeQSValue(user.substring(index + 1));
      } else user = escapeQSValue(user);

      String strUrl = getProtocol(url) + "://" + user + "@" + url.getHost();
      if (port > 0) strUrl += ":" + port;
      strUrl += file;
      return new URL(strUrl);
    }

    // port
    if (port <= 0) return new URL(url.getProtocol(), url.getHost(), file);
    return new URL(url.getProtocol(), url.getHost(), port, file);
  }