1 package org.mortbay.jetty; 2 3 import java.io.UnsupportedEncodingException; 4 5 import org.mortbay.util.MultiMap; 6 import org.mortbay.util.StringUtil; 7 import org.mortbay.util.TypeUtil; 8 import org.mortbay.util.URIUtil; 9 import org.mortbay.util.UrlEncoded; 10 import org.mortbay.util.Utf8StringBuffer; 11 12 public class EncodedHttpURI extends HttpURI 13 { 14 private String _encoding; 15 16 public EncodedHttpURI(String encoding) 17 { 18 super(); 19 _encoding = encoding; 20 } 21 22 23 public String getScheme() 24 { 25 if (_scheme==_authority) 26 return null; 27 int l=_authority-_scheme; 28 if (l==5 && 29 _raw[_scheme]=='h' && 30 _raw[_scheme+1]=='t' && 31 _raw[_scheme+2]=='t' && 32 _raw[_scheme+3]=='p' ) 33 return HttpSchemes.HTTP; 34 if (l==6 && 35 _raw[_scheme]=='h' && 36 _raw[_scheme+1]=='t' && 37 _raw[_scheme+2]=='t' && 38 _raw[_scheme+3]=='p' && 39 _raw[_scheme+4]=='s' ) 40 return HttpSchemes.HTTPS; 41 42 return StringUtil.toString(_raw,_scheme,_authority-_scheme-1,_encoding); 43 } 44 45 public String getAuthority() 46 { 47 if (_authority==_path) 48 return null; 49 return StringUtil.toString(_raw,_authority,_path-_authority,_encoding); 50 } 51 52 public String getHost() 53 { 54 if (_host==_port) 55 return null; 56 return StringUtil.toString(_raw,_host,_port-_host,_encoding); 57 } 58 59 public int getPort() 60 { 61 if (_port==_path) 62 return -1; 63 return TypeUtil.parseInt(_raw, _port+1, _path-_port-1,10); 64 } 65 66 public String getPath() 67 { 68 if (_path==_param) 69 return null; 70 return StringUtil.toString(_raw,_path,_param-_path,_encoding); 71 } 72 73 public String getDecodedPath() 74 { 75 if (_path==_param) 76 return null; 77 return URIUtil.decodePath(_raw,_path,_param-_path); 78 } 79 80 public String getPathAndParam() 81 { 82 if (_path==_query) 83 return null; 84 return StringUtil.toString(_raw,_path,_query-_path,_encoding); 85 } 86 87 public String getCompletePath() 88 { 89 if (_path==_end) 90 return null; 91 return StringUtil.toString(_raw,_path,_end-_path,_encoding); 92 } 93 94 public String getParam() 95 { 96 if (_param==_query) 97 return null; 98 return StringUtil.toString(_raw,_param+1,_query-_param-1,_encoding); 99 } 100 101 public String getQuery() 102 { 103 if (_query==_fragment) 104 return null; 105 return StringUtil.toString(_raw,_query+1,_fragment-_query-1,_encoding); 106 } 107 108 public boolean hasQuery() 109 { 110 return (_fragment>_query); 111 } 112 113 public String getFragment() 114 { 115 if (_fragment==_end) 116 return null; 117 return StringUtil.toString(_raw,_fragment+1,_end-_fragment-1,_encoding); 118 } 119 120 public void decodeQueryTo(MultiMap parameters) 121 { 122 if (_query==_fragment) 123 return; 124 UrlEncoded.decodeTo(StringUtil.toString(_raw,_query+1,_fragment-_query-1,_encoding),parameters,_encoding); 125 } 126 127 public void decodeQueryTo(MultiMap parameters, String encoding) 128 throws UnsupportedEncodingException 129 { 130 if (_query==_fragment) 131 return; 132 133 if (encoding==null) 134 encoding=_encoding; 135 UrlEncoded.decodeTo(StringUtil.toString(_raw,_query+1,_fragment-_query-1,encoding),parameters,encoding); 136 } 137 138 public String toString() 139 { 140 if (_rawString==null) 141 _rawString= StringUtil.toString(_raw,_scheme,_end-_scheme,_encoding); 142 return _rawString; 143 } 144 145 public void writeTo(Utf8StringBuffer buf) 146 { 147 buf.getStringBuffer().append(toString()); 148 } 149 150 }