import java.nio.*; import java.nio.charset.*; import org.apache.commons.codec.net.*; public void main(Template this, Cms cms) { List charsets = new LinkedList(["utf-8", "windows-1251"]); cms.writebr("qs: " cms.getRequest().getQueryString()); cms.writebr("искать: " cms.decodeFromQueryString("искать", charsets)); if (charsets.size() == 1) cms.writebr("кодировка найдена: " charsets); cms.writebr("проверка"); } /** * Find the character set which matches the encoding of the given parameter.
* For this charset detection to work, parameter must be susceptible * to charset and URL encodings, for example, * parameter "искать" will be charset and URL encoded by browsers. * @param pName the name of the CGI parameter to decode. * @param charsets the list of charsets to try to decode the parameter with. * @return {@code null} if the given parameter wasn't found in these charsets. */ ?String decodeFromQueryString(Cms cms, String pName, List charsets) { let request = cms.getRequest(); let qs = request.getQueryString(); if (qs == null) return null; return decodeFromQueryString(qs, pName, charsets); } /** * Perform parameter character set detection on the given query string. * @see decodeFromQueryString(Cms, String, List) */ ?String decodeFromQueryString(String qs, String pName, List charsets) { let parameters = new java.util.StringTokenizer(qs, "&"); let urlCodec = new URLCodec(); HashMap decoders = new HashMap(charsets.size()); while (parameters.hasMoreTokens()) { let pair = parameters.nextToken(); int eqIndex = pair.indexOf('='); String name, value; if (eqIndex == -1) { name = pair; value = ""; } else { name = pair.substring(0, eqIndex); value = pair.substring(++eqIndex); } if (name.equals(pName)) return value; for (charsetName : charsets) { // MSIE will send the data in the UTF-8 or any other encoding. // The data is left unmodified by servlet container in the characters of the query string // (becouse first 256 unicode code points are identical to ISO-8859-1). ?CharsetDecoder decoder = decoders.get(charsetName); if (decoder == null) decoders.put(charsetName, decoder = Charset.forName(charsetName).newDecoder()); byte[] bytes = new byte[name.length()]; int num = bytes.length; while(--num >= 0) bytes[num] = byte(int(name.charAt(num))); try { let msieName = decoder.decode(ByteBuffer.wrap(bytes)).toString(); if (pName.equals(msieName)) { byte[] vBytes = new byte[value.length()]; num = vBytes.length; while(--num >= 0) vBytes[num] = byte(int(value.charAt(num))); return decoder.decode(ByteBuffer.wrap(vBytes)).toString(); } } catch (MalformedInputException skip) {} // Try using URL-encoded name. byte[] uBytes = urlCodec.decode(bytes); try { String got = decoder.decode(ByteBuffer.wrap(uBytes)).toString(); if (pName.equals(got)) { byte[] vBytes = new byte[value.length()]; num = vBytes.length; while(--num >= 0) vBytes[num] = byte(int(value.charAt(num))); return decoder.decode(ByteBuffer.wrap(urlCodec.decode(vBytes))).toString(); } } catch (MalformedInputException skip) {} } } return null; }