// MUST create a copy from toURL and rename toURI and rewrite for URI, pherhaps it is possible to // merge them somehow public static String encode(String relpath) { int qIndex = relpath.indexOf('?'); if (qIndex == -1) return relpath; String file = relpath.substring(0, qIndex); String query = relpath.substring(qIndex + 1); int sIndex = query.indexOf('#'); String anker = null; if (sIndex != -1) { // print.o(sIndex); anker = query.substring(sIndex + 1); query = query.substring(0, sIndex); } StringBuilder res = new StringBuilder(file); // query if (!StringUtil.isEmpty(query)) { StringList list = ListUtil.toList(query, '&'); String str; int index; char del = '?'; while (list.hasNext()) { res.append(del); del = '&'; str = list.next(); index = str.indexOf('='); if (index == -1) res.append(escapeQSValue(str)); else { res.append(escapeQSValue(str.substring(0, index))); res.append('='); res.append(escapeQSValue(str.substring(index + 1))); } } } // anker if (anker != null) { res.append('#'); res.append(escapeQSValue(anker)); } return res.toString(); }
public static Map<String, String> parseParameterList( String _str, boolean decode, String charset) { // return lucee.commons.net.HTTPUtil.toURI(strUrl,port); Map<String, String> data = new HashMap<String, String>(); StringList list = ListUtil.toList(_str, '&'); String str; int index; while (list.hasNext()) { str = list.next(); index = str.indexOf('='); if (index == -1) { data.put(decode(str, decode), ""); } else { data.put(decode(str.substring(0, index), decode), decode(str.substring(index + 1), decode)); } } return data; }
private static String decodeQuery(String query, char startDelimiter) { if (!StringUtil.isEmpty(query)) { StringBuilder res = new StringBuilder(); StringList list = ListUtil.toList(query, '&'); String str; int index; char del = startDelimiter; while (list.hasNext()) { res.append(del); del = '&'; str = list.next(); index = str.indexOf('='); if (index == -1) res.append(escapeQSValue(str)); else { res.append(escapeQSValue(str.substring(0, index))); res.append('='); res.append(escapeQSValue(str.substring(index + 1))); } } query = res.toString(); } else query = ""; return query; }
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()); }
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); }