View Javadoc

1   // ========================================================================
2   // Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.http;
15  
16  import java.io.UnsupportedEncodingException;
17  
18  import org.eclipse.jetty.util.MultiMap;
19  import org.eclipse.jetty.util.StringUtil;
20  import org.eclipse.jetty.util.TypeUtil;
21  import org.eclipse.jetty.util.URIUtil;
22  import org.eclipse.jetty.util.UrlEncoded;
23  import org.eclipse.jetty.util.Utf8StringBuffer;
24  
25  public class EncodedHttpURI extends HttpURI
26  {
27      private final String _encoding;
28      
29      public EncodedHttpURI(String encoding)
30      {
31          super();
32          _encoding = encoding;
33      }
34      
35      
36      public String getScheme()
37      {
38          if (_scheme==_authority)
39              return null;
40          int l=_authority-_scheme;
41          if (l==5 && 
42              _raw[_scheme]=='h' && 
43              _raw[_scheme+1]=='t' && 
44              _raw[_scheme+2]=='t' && 
45              _raw[_scheme+3]=='p' )
46              return HttpSchemes.HTTP;
47          if (l==6 && 
48              _raw[_scheme]=='h' && 
49              _raw[_scheme+1]=='t' && 
50              _raw[_scheme+2]=='t' && 
51              _raw[_scheme+3]=='p' && 
52              _raw[_scheme+4]=='s' )
53              return HttpSchemes.HTTPS;
54          
55          return StringUtil.toString(_raw,_scheme,_authority-_scheme-1,_encoding);
56      }
57      
58      public String getAuthority()
59      {
60          if (_authority==_path)
61              return null;
62          return StringUtil.toString(_raw,_authority,_path-_authority,_encoding);
63      }
64      
65      public String getHost()
66      {
67          if (_host==_port)
68              return null;
69          return StringUtil.toString(_raw,_host,_port-_host,_encoding);
70      }
71      
72      public int getPort()
73      {
74          if (_port==_path)
75              return -1;
76          return TypeUtil.parseInt(_raw, _port+1, _path-_port-1,10);
77      }
78      
79      public String getPath()
80      {
81          if (_path==_param)
82              return null;
83          return StringUtil.toString(_raw,_path,_param-_path,_encoding);
84      }
85      
86      public String getDecodedPath()
87      {
88          if (_path==_param)
89              return null;
90          return URIUtil.decodePath(_raw,_path,_param-_path);
91      }
92      
93      public String getPathAndParam()
94      {
95          if (_path==_query)
96              return null;
97          return StringUtil.toString(_raw,_path,_query-_path,_encoding);
98      }
99      
100     public String getCompletePath()
101     {
102         if (_path==_end)
103             return null;
104         return StringUtil.toString(_raw,_path,_end-_path,_encoding);
105     }
106     
107     public String getParam()
108     {
109         if (_param==_query)
110             return null;
111         return StringUtil.toString(_raw,_param+1,_query-_param-1,_encoding);
112     }
113     
114     public String getQuery()
115     {
116         if (_query==_fragment)
117             return null;
118         return StringUtil.toString(_raw,_query+1,_fragment-_query-1,_encoding);
119     }
120     
121     public boolean hasQuery()
122     {
123         return (_fragment>_query);
124     }
125     
126     public String getFragment()
127     {
128         if (_fragment==_end)
129             return null;
130         return StringUtil.toString(_raw,_fragment+1,_end-_fragment-1,_encoding);
131     }
132 
133     public void decodeQueryTo(MultiMap parameters) 
134     {
135         if (_query==_fragment)
136             return;
137         UrlEncoded.decodeTo(StringUtil.toString(_raw,_query+1,_fragment-_query-1,_encoding),parameters,_encoding);
138     }
139 
140     public void decodeQueryTo(MultiMap parameters, String encoding) 
141         throws UnsupportedEncodingException
142     {
143         if (_query==_fragment)
144             return;
145        
146         if (encoding==null)
147             encoding=_encoding;
148         UrlEncoded.decodeTo(StringUtil.toString(_raw,_query+1,_fragment-_query-1,encoding),parameters,encoding);
149     }
150     
151     public String toString()
152     {
153         if (_rawString==null)
154             _rawString= StringUtil.toString(_raw,_scheme,_end-_scheme,_encoding);
155         return _rawString;
156     }
157     
158     public void writeTo(Utf8StringBuffer buf)
159     {
160         buf.getStringBuffer().append(toString());
161     }
162     
163 }