List of usage examples for org.apache.commons.httpclient.util URIUtil encodeQuery
public static String encodeQuery(String unescaped, String charset) throws URIException
From source file:pl.nask.hsn2.normalizers.URLNormalizerUtils.java
public static String normalizeQuery(StringBuilder sb, int startIndx, int endIndx) throws URLMalformedInputException { if (sb.indexOf("?") != 0) { throw new URLMalformedInputException("Query should start with '?'"); }//www .j av a2 s. co m int end = findFirstMatch(sb, "#", startIndx); if (end < 0 || end > endIndx) { end = endIndx; } StringBuilder enc = new StringBuilder(sb.substring(startIndx, end)); removeObfuscatedEncoding(enc, new EncodingType[] { EncodingType.PLUS_AS_SPACE, EncodingType.QUERY_ALLOWED }); int i = findFirstMatch(enc, "%", 0); try { if (i < 0) { String tmp = URIUtil.encodeQuery(enc.toString(), DEFAULT_CHARSET); enc.replace(0, enc.length(), tmp); } else { int start = 0; String tmp = ""; while ((i = findFirstMatch(enc, "%", start)) > 0) { if (percentEnc.matcher(enc.substring(i)).matches()) { tmp = URIUtil.encodeQuery(enc.substring(start, i), DEFAULT_CHARSET); enc.replace(start, i, tmp); start = i + 1 - (tmp.length() - (i - start)); } else { enc.replace(i, i + 1, "%25"); start = i + 1; } } tmp = URIUtil.encodeQuery(enc.substring(start), DEFAULT_CHARSET); enc.replace(start, enc.length(), tmp); } } catch (URIException e) { throw new URLMalformedInputException(e); } i = 0; while ((i = findFirstMatch(enc, ";", i)) > 0) { enc.replace(i, i + 1, "&"); } i = 0; while ((i = findFirstMatch(enc, "+ ", i)) > 0) { enc.replace(i, i + 1, "%20"); } sb.replace(startIndx, end, enc.toString()); return enc.toString(); }