View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.http;
20  
21  import java.util.Collections;
22  import java.util.Iterator;
23  
24  public class MetaData implements Iterable<HttpField>
25  {
26      private HttpVersion _httpVersion;
27      private HttpFields _fields;
28      private long _contentLength;
29  
30      public MetaData(HttpVersion version, HttpFields fields)
31      {
32          this(version, fields, Long.MIN_VALUE);
33      }
34  
35      public MetaData(HttpVersion version, HttpFields fields, long contentLength)
36      {
37          _httpVersion = version;
38          _fields = fields;
39          _contentLength = contentLength;
40      }
41  
42      protected void recycle()
43      {
44          _httpVersion = null;
45          if (_fields != null)
46              _fields.clear();
47          _contentLength = Long.MIN_VALUE;
48      }
49  
50      public boolean isRequest()
51      {
52          return false;
53      }
54  
55      public boolean isResponse()
56      {
57          return false;
58      }
59  
60      /**
61       * @return the HTTP version of this MetaData object
62       */
63      public HttpVersion getVersion()
64      {
65          return _httpVersion;
66      }
67  
68      /**
69       * @param httpVersion the HTTP version to set
70       */
71      public void setHttpVersion(HttpVersion httpVersion)
72      {
73          _httpVersion = httpVersion;
74      }
75  
76      /**
77       * @return the HTTP fields of this MetaData object
78       */
79      public HttpFields getFields()
80      {
81          return _fields;
82      }
83  
84      /**
85       * @return the content length if available, otherwise {@link Long#MIN_VALUE}
86       */
87      public long getContentLength()
88      {
89          if (_contentLength == Long.MIN_VALUE)
90          {
91              if (_fields != null)
92              {
93                  HttpField field = _fields.getField(HttpHeader.CONTENT_LENGTH);
94                  _contentLength = field == null ? -1 : field.getLongValue();
95              }
96          }
97          return _contentLength;
98      }
99  
100     /**
101      * @return an iterator over the HTTP fields
102      * @see #getFields()
103      */
104     public Iterator<HttpField> iterator()
105     {
106         HttpFields fields = getFields();
107         return fields == null ? Collections.<HttpField>emptyIterator() : fields.iterator();
108     }
109 
110     @Override
111     public String toString()
112     {
113         StringBuilder out = new StringBuilder();
114         for (HttpField field : this)
115             out.append(field).append(System.lineSeparator());
116         return out.toString();
117     }
118 
119     public static class Request extends MetaData
120     {
121         private String _method;
122         private HttpURI _uri;
123 
124         public Request(HttpFields fields)
125         {
126             this(null, null, null, fields);
127         }
128 
129         public Request(String method, HttpURI uri, HttpVersion version, HttpFields fields)
130         {
131             this(method, uri, version, fields, Long.MIN_VALUE);
132         }
133 
134         public Request(String method, HttpURI uri, HttpVersion version, HttpFields fields, long contentLength)
135         {
136             super(version, fields, contentLength);
137             _method = method;
138             _uri = uri;
139         }
140 
141         public Request(String method, HttpScheme scheme, HostPortHttpField hostPort, String uri, HttpVersion version, HttpFields fields)
142         {
143             this(method, new HttpURI(scheme == null ? null : scheme.asString(), hostPort.getHost(), hostPort.getPort(), uri), version, fields);
144         }
145 
146         public Request(String method, HttpScheme scheme, HostPortHttpField hostPort, String uri, HttpVersion version, HttpFields fields, long contentLength)
147         {
148             this(method, new HttpURI(scheme == null ? null : scheme.asString(), hostPort.getHost(), hostPort.getPort(), uri), version, fields, contentLength);
149         }
150 
151         public Request(String method, String scheme, HostPortHttpField hostPort, String uri, HttpVersion version, HttpFields fields, long contentLength)
152         {
153             this(method, new HttpURI(scheme, hostPort.getHost(), hostPort.getPort(), uri), version, fields, contentLength);
154         }
155 
156         public Request(Request request)
157         {
158             this(request.getMethod(),new HttpURI(request.getURI()), request.getVersion(), new HttpFields(request.getFields()), request.getContentLength());
159         }
160 
161         // TODO MetaData should be immuttable!!! 
162         public void recycle()
163         {
164             super.recycle();
165             _method = null;
166             if (_uri != null)
167                 _uri.clear();
168         }
169 
170         @Override
171         public boolean isRequest()
172         {
173             return true;
174         }
175 
176         /**
177          * @return the HTTP method
178          */
179         public String getMethod()
180         {
181             return _method;
182         }
183 
184         /**
185          * @param method the HTTP method to set
186          */
187         public void setMethod(String method)
188         {
189             _method = method;
190         }
191 
192         /**
193          * @return the HTTP URI
194          */
195         public HttpURI getURI()
196         {
197             return _uri;
198         }
199 
200         /**
201          * @return the HTTP URI in string form
202          */
203         public String getURIString()
204         {
205             return _uri == null ? null : _uri.toString();
206         }
207 
208         /**
209          * @param uri the HTTP URI to set
210          */
211         public void setURI(HttpURI uri)
212         {
213             _uri = uri;
214         }
215 
216         @Override
217         public String toString()
218         {
219             HttpFields fields = getFields();
220             return String.format("%s{u=%s,%s,h=%d}",
221                     getMethod(), getURI(), getVersion(), fields == null ? -1 : fields.size());
222         }
223     }
224 
225     public static class Response extends MetaData
226     {
227         private int _status;
228         private String _reason;
229 
230         public Response()
231         {
232             this(null, 0, null);
233         }
234 
235         public Response(HttpVersion version, int status, HttpFields fields)
236         {
237             this(version, status, fields, Long.MIN_VALUE);
238         }
239 
240         public Response(HttpVersion version, int status, HttpFields fields, long contentLength)
241         {
242             super(version, fields, contentLength);
243             _status = status;
244         }
245 
246         public Response(HttpVersion version, int status, String reason, HttpFields fields, long contentLength)
247         {
248             super(version, fields, contentLength);
249             _reason = reason;
250             _status = status;
251         }
252 
253         @Override
254         public boolean isResponse()
255         {
256             return true;
257         }
258 
259         /**
260          * @return the HTTP status
261          */
262         public int getStatus()
263         {
264             return _status;
265         }
266 
267         /**
268          * @return the HTTP reason
269          */
270         public String getReason()
271         {
272             return _reason;
273         }
274 
275         /**
276          * @param status the HTTP status to set
277          */
278         public void setStatus(int status)
279         {
280             _status = status;
281         }
282 
283         /**
284          * @param reason the HTTP reason to set
285          */
286         public void setReason(String reason)
287         {
288             _reason = reason;
289         }
290 
291         @Override
292         public String toString()
293         {
294             HttpFields fields = getFields();
295             return String.format("%s{s=%d,h=%d}", getVersion(), getStatus(), fields == null ? -1 : fields.size());
296         }
297     }
298 }