1 18 package org.apache.activemq.util; 19 20 import java.io.UnsupportedEncodingException ; 21 import java.net.URI ; 22 import java.net.URISyntaxException ; 23 import java.net.URLDecoder ; 24 import java.net.URLEncoder ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 34 public class URISupport { 35 36 public static class CompositeData { 37 String scheme; 38 String path; 39 URI components[]; 40 Map parameters; 41 String fragment; 42 public String host; 43 44 public URI [] getComponents() { 45 return components; 46 } 47 public String getFragment() { 48 return fragment; 49 } 50 public Map getParameters() { 51 return parameters; 52 } 53 public String getScheme() { 54 return scheme; 55 } 56 public String getPath() { 57 return path; 58 } 59 public String getHost() { 60 return host; 61 } 62 63 public URI toURI() throws URISyntaxException { 64 StringBuffer sb = new StringBuffer (); 65 if( scheme!=null ) { 66 sb.append(scheme); 67 sb.append(':'); 68 } 69 70 if( host!=null && host.length()!=0 ) { 71 sb.append(host); 72 } else { 73 sb.append('('); 74 for (int i = 0; i < components.length; i++) { 75 if( i!=0 ) 76 sb.append(','); 77 sb.append(components[i].toString()); 78 } 79 sb.append(')'); 80 } 81 82 if( path !=null ) { 83 sb.append('/'); 84 sb.append(path); 85 } 86 if(!parameters.isEmpty()) { 87 sb.append("?"); 88 sb.append(createQueryString(parameters)); 89 } 90 if( fragment!=null ) { 91 sb.append("#"); 92 sb.append(fragment); 93 } 94 return new URI (sb.toString()); 95 } 96 } 97 98 public static Map parseQuery(String uri) throws URISyntaxException { 99 try{ 100 Map rc=new HashMap (); 101 if(uri!=null){ 102 String [] parameters=uri.split("&"); 103 for(int i=0;i<parameters.length;i++){ 104 int p=parameters[i].indexOf("="); 105 if(p>=0){ 106 String name=URLDecoder.decode(parameters[i].substring(0,p),"UTF-8"); 107 String value=URLDecoder.decode(parameters[i].substring(p+1),"UTF-8"); 108 rc.put(name,value); 109 }else{ 110 rc.put(parameters[i],null); 111 } 112 } 113 } 114 return rc; 115 }catch(UnsupportedEncodingException e){ 116 throw (URISyntaxException ) new URISyntaxException (e.toString(),"Invalid encoding").initCause(e); 117 } 118 } 119 120 public static Map parseParamters(URI uri) throws URISyntaxException { 121 return uri.getQuery()==null ? Collections.EMPTY_MAP : parseQuery(stripPrefix(uri.getQuery(), "?")); 122 } 123 124 127 public static URI removeQuery(URI uri) throws URISyntaxException { 128 return createURIWithQuery(uri, null); 129 } 130 131 134 public static URI createURIWithQuery(URI uri, String query) throws URISyntaxException { 135 return new URI (uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), query, uri.getFragment()); 136 } 137 138 public static CompositeData parseComposite(URI uri) throws URISyntaxException { 139 140 CompositeData rc = new CompositeData(); 141 rc.scheme = uri.getScheme(); 142 String ssp = stripPrefix(uri.getSchemeSpecificPart().trim(), "//").trim(); 143 144 parseComposite(uri, rc, ssp); 145 146 rc.fragment = uri.getFragment(); 147 return rc; 148 } 149 150 157 private static void parseComposite(URI uri, CompositeData rc, String ssp) throws URISyntaxException { 158 String componentString; 159 String params; 160 161 if(!checkParenthesis(ssp)){ 162 throw new URISyntaxException (uri.toString(), "Not a matching number of '(' and ')' parenthesis"); 163 } 164 165 int p; 166 int intialParen = ssp.indexOf("("); 167 if( intialParen==0 ) { 168 rc.host = ssp.substring(0, intialParen); 169 p = rc.host.indexOf("/"); 170 if( p >= 0 ) { 171 rc.path = rc.host.substring(p); 172 rc.host = rc.host.substring(0,p); 173 } 174 p = ssp.lastIndexOf(")"); 175 componentString = ssp.substring(intialParen+1,p); 176 params = ssp.substring(p+1).trim(); 177 178 } else { 179 componentString = ssp; 180 params=""; 181 } 182 183 String components[] = splitComponents(componentString); 184 rc.components=new URI [components.length]; 185 for (int i = 0; i < components.length; i++) { 186 rc.components[i] = new URI (components[i].trim()); 187 } 188 189 p = params.indexOf("?"); 190 if( p >= 0 ) { 191 if( p > 0) { 192 rc.path = stripPrefix(params.substring(0, p), "/"); 193 } 194 rc.parameters = parseQuery(params.substring(p+1)); 195 } else { 196 if( params.length() > 0 ) 197 rc.path = stripPrefix(params, "/"); 198 rc.parameters = Collections.EMPTY_MAP; 199 } 200 } 201 202 206 private static String [] splitComponents(String str) { 207 ArrayList l = new ArrayList (); 208 209 int last=0; 210 int depth = 0; 211 char chars[] = str.toCharArray(); 212 for( int i=0; i < chars.length; i ++ ) { 213 switch( chars[i] ) { 214 case '(': 215 depth++; 216 break; 217 case ')': 218 depth--; 219 break; 220 case ',': 221 if( depth == 0 ) { 222 String s = str.substring(last, i); 223 l.add(s); 224 last=i+1; 225 } 226 } 227 } 228 229 String s = str.substring(last); 230 if( s.length() !=0 ) 231 l.add(s); 232 233 String rc[] = new String [l.size()]; 234 l.toArray(rc); 235 return rc; 236 } 237 238 public static String stripPrefix(String value, String prefix) { 239 if( value.startsWith(prefix) ) 240 return value.substring(prefix.length()); 241 return value; 242 } 243 244 public static URI stripScheme(URI uri) throws URISyntaxException { 245 return new URI (stripPrefix(uri.getSchemeSpecificPart().trim(), "//")); 246 } 247 248 public static String createQueryString(Map options) throws URISyntaxException { 249 try { 250 if(options.size()>0) { 251 StringBuffer rc = new StringBuffer (); 252 boolean first=true; 253 for (Iterator iter = options.keySet().iterator(); iter.hasNext();) { 254 if( first ) 255 first=false; 256 else 257 rc.append("&"); 258 259 String key = (String ) iter.next(); 260 String value = (String )options.get(key); 261 rc.append(URLEncoder.encode(key, "UTF-8")); 262 rc.append("="); 263 rc.append(URLEncoder.encode(value, "UTF-8")); 264 } 265 return rc.toString(); 266 } else { 267 return ""; 268 } 269 } catch (UnsupportedEncodingException e) { 270 throw (URISyntaxException )new URISyntaxException (e.toString(), "Invalid encoding").initCause(e); 271 } 272 } 273 274 278 public static URI createRemainingURI(URI originalURI, Map params) throws URISyntaxException { 279 String s = createQueryString(params); 280 if( s.length()==0 ) 281 s = null; 282 return createURIWithQuery(originalURI, s); 283 } 284 285 static public URI changeScheme(URI bindAddr, String scheme) throws URISyntaxException { 286 return new URI (scheme, bindAddr.getUserInfo(), bindAddr.getHost(), bindAddr.getPort(), bindAddr.getPath(), bindAddr.getQuery(), bindAddr.getFragment()); 287 } 288 289 public static boolean checkParenthesis(String str){ 290 boolean result=true; 291 if(str!=null){ 292 int open=0; 293 int closed=0; 294 295 int i=0; 296 while((i=str.indexOf('(',i)) >=0 ){ 297 i++; 298 open++; 299 } 300 i=0; 301 while((i=str.indexOf(')',i)) >=0 ){ 302 i++; 303 closed++; 304 } 305 result = open == closed; 306 } 307 return result; 308 } 309 310 public int indexOfParenthesisMatch(String str){ 311 int result = -1; 312 313 return result; 314 } 315 316 } 317 | Popular Tags |