1 package org.jahia.urls; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.IOException ; 5 6 14 15 public class URICodec { 16 17 private static org.apache.log4j.Logger logger = 18 org.apache.log4j.Logger.getLogger (URICodec.class); 19 20 private static final String hexString = "0123456789ABCDEF"; 21 22 public static final String ALPHANUM_CHARS = 23 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 24 public static final String MARK_CHARS = "-_.!~*'()"; 25 public static final String UNRESERVED_CHARS = ALPHANUM_CHARS + MARK_CHARS; 26 36 public static final String RESERVED_CHARS = 37 ";/?:@&=$,"; 38 public static final String AUTHORITY_AUTHORIZEDCHARS = UNRESERVED_CHARS + "$,;:@&="; 39 public static final String PATH_AUTHORIZEDCHARS = UNRESERVED_CHARS + "/;:@&=$,"; 40 41 public static final String QUERY_AUTHORIZEDCHARS = UNRESERVED_CHARS + "=&"; 42 public static final String FRAGMENT_AUTHORIZEDCHARS = UNRESERVED_CHARS; 43 44 public static final String DEFAULT_AUTHORIZEDCHARS = ALPHANUM_CHARS + 45 MARK_CHARS + RESERVED_CHARS; 46 47 private static String defaultEncoding = "UTF-8"; 49 private static String defaultAuthorizedChars = DEFAULT_AUTHORIZEDCHARS; 50 51 public static String encode(String input, String encoding, String authorizedChars) 52 throws java.io.UnsupportedEncodingException { 53 if ((input == null) || (encoding == null)) { 54 return null; 55 } 56 StringBuffer result = new StringBuffer (); 57 for (int i = 0; i < input.length(); i++) { 58 String curChar = input.substring(i, i+1); 59 if (authorizedChars.indexOf(curChar) == -1) { 60 byte[] charBytes = curChar.getBytes(encoding); 64 for (int j=0; j < charBytes.length; j++) { 65 result.append("%"); 66 char hiHex = hexString.charAt((charBytes[j] & 0xFF) >> 4); 67 result.append(hiHex); 68 char lowHex = hexString.charAt(charBytes[j] & 0xF); 69 result.append(lowHex); 70 } 71 } else { 72 result.append(curChar); 73 } 74 } 75 return result.toString(); 76 } 77 78 public static String encode(String input, String authorizedChars) 79 throws java.io.UnsupportedEncodingException { 80 return encode(input, defaultEncoding, authorizedChars); 81 } 82 83 public static String encode(String input) 84 throws java.io.UnsupportedEncodingException { 85 return encode(input, defaultEncoding, defaultAuthorizedChars ); 86 } 87 88 public static String decode(String input, String encoding) 89 throws java.io.UnsupportedEncodingException { 90 if ((input == null) || (encoding == null)) { 91 return null; 92 } 93 ByteArrayOutputStream byteOut = new ByteArrayOutputStream (); 94 int currentPos = 0; 95 try { 96 while ( (input.indexOf("%", currentPos) != -1) && 97 (currentPos < input.length())) { 98 int encodedBytePos = input.indexOf("%", currentPos); 99 if (encodedBytePos > currentPos) { 100 byteOut.write(input.substring(currentPos, encodedBytePos). 101 getBytes(encoding)); 102 } 103 char hiHexChar = input.charAt(encodedBytePos + 1); 104 char lowHexChar = input.charAt(encodedBytePos + 2); 105 int hiHex = hexString.indexOf(hiHexChar); 106 int lowHex = hexString.indexOf(lowHexChar); 107 if ( (hiHex >= 0) && (lowHex >= 0)) { 108 byte curByte = (byte) (( (hiHex << 4) + lowHex) & 0xFF); 109 byteOut.write(curByte); 110 currentPos = encodedBytePos + 3; 111 } else { 112 currentPos = encodedBytePos + 1; 113 } 114 } 115 byteOut.write(input.substring(currentPos).getBytes(encoding)); 116 byteOut.flush(); 117 } catch (IOException ioe) { 118 logger.error("Error while writing to ByteArrayOutputStream", ioe); 119 } 120 return byteOut.toString(encoding); 121 } 122 123 public static String decode(String input) 124 throws java.io.UnsupportedEncodingException { 125 return decode(input, defaultEncoding); 126 } 127 128 static public String getDefaultEncoding() { 129 return defaultEncoding; 130 } 131 132 static public void setDefaultEncoding(String encoding) { 133 defaultEncoding = encoding; 134 } 135 136 static public String getDefaultAuthorizedChars () { 137 return defaultAuthorizedChars; 138 } 139 140 static public void setDefaultAuthorizedChars (String authorizedChars) { 141 defaultAuthorizedChars = authorizedChars; 142 } 143 144 } 145 | Popular Tags |