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       * @deprecated use {@link #getHttpVersion()} instead
62       */
63      @Deprecated
64      public HttpVersion getVersion()
65      {
66          return getHttpVersion();
67      }
68  
69      /**
70       * @return the HTTP version of this MetaData object
71       */
72      public HttpVersion getHttpVersion()
73      {
74          return _httpVersion;
75      }
76  
77      /**
78       * @param httpVersion the HTTP version to set
79       */
80      public void setHttpVersion(HttpVersion httpVersion)
81      {
82          _httpVersion = httpVersion;
83      }
84  
85      /**
86       * @return the HTTP fields of this MetaData object
87       */
88      public HttpFields getFields()
89      {
90          return _fields;
91      }
92  
93      /**
94       * @return the content length if available, otherwise {@link Long#MIN_VALUE}
95       */
96      public long getContentLength()
97      {
98          if (_contentLength == Long.MIN_VALUE)
99          {
100             if (_fields != null)
101             {
102                 HttpField field = _fields.getField(HttpHeader.CONTENT_LENGTH);
103                 _contentLength = field == null ? -1 : field.getLongValue();
104             }
105         }
106         return _contentLength;
107     }
108 
109     /**
110      * @return an iterator over the HTTP fields
111      * @see #getFields()
112      */
113     public Iterator<HttpField> iterator()
114     {
115         HttpFields fields = getFields();
116         return fields == null ? Collections.<HttpField>emptyIterator() : fields.iterator();
117     }
118 
119     @Override
120     public String toString()
121     {
122         StringBuilder out = new StringBuilder();
123         for (HttpField field : this)
124             out.append(field).append(System.lineSeparator());
125         return out.toString();
126     }
127 
128     public static class Request extends MetaData
129     {
130         private String _method;
131         private HttpURI _uri;
132 
133         public Request(HttpFields fields)
134         {
135             this(null, null, null, fields);
136         }
137 
138         public Request(String method, HttpURI uri, HttpVersion version, HttpFields fields)
139         {
140             this(method, uri, version, fields, Long.MIN_VALUE);
141         }
142 
143         public Request(String method, HttpURI uri, HttpVersion version, HttpFields fields, long contentLength)
144         {
145             super(version, fields, contentLength);
146             _method = method;
147             _uri = uri;
148         }
149 
150         public Request(String method, HttpScheme scheme, HostPortHttpField hostPort, String uri, HttpVersion version, HttpFields fields)
151         {
152             this(method, new HttpURI(scheme == null ? null : scheme.asString(), hostPort.getHost(), hostPort.getPort(), uri), version, fields);
153         }
154 
155         public Request(String method, HttpScheme scheme, HostPortHttpField hostPort, String uri, HttpVersion version, HttpFields fields, long contentLength)
156         {
157             this(method, new HttpURI(scheme == null ? null : scheme.asString(), hostPort.getHost(), hostPort.getPort(), uri), version, fields, contentLength);
158         }
159 
160         public Request(String method, String scheme, HostPortHttpField hostPort, String uri, HttpVersion version, HttpFields fields, long contentLength)
161         {
162             this(method, new HttpURI(scheme, hostPort.getHost(), hostPort.getPort(), uri), version, fields, contentLength);
163         }
164 
165         public Request(Request request)
166         {
167             this(request.getMethod(),new HttpURI(request.getURI()), request.getHttpVersion(), new HttpFields(request.getFields()), request.getContentLength());
168         }
169 
170         // TODO MetaData should be immuttable!!! 
171         public void recycle()
172         {
173             super.recycle();
174             _method = null;
175             if (_uri != null)
176                 _uri.clear();
177         }
178 
179         @Override
180         public boolean isRequest()
181         {
182             return true;
183         }
184 
185         /**
186          * @return the HTTP method
187          */
188         public String getMethod()
189         {
190             return _method;
191         }
192 
193         /**
194          * @param method the HTTP method to set
195          */
196         public void setMethod(String method)
197         {
198             _method = method;
199         }
200 
201         /**
202          * @return the HTTP URI
203          */
204         public HttpURI getURI()
205         {
206             return _uri;
207         }
208 
209         /**
210          * @return the HTTP URI in string form
211          */
212         public String getURIString()
213         {
214             return _uri == null ? null : _uri.toString();
215         }
216 
217         /**
218          * @param uri the HTTP URI to set
219          */
220         public void setURI(HttpURI uri)
221         {
222             _uri = uri;
223         }
224 
225         @Override
226         public String toString()
227         {
228             HttpFields fields = getFields();
229             return String.format("%s{u=%s,%s,h=%d}",
230                     getMethod(), getURI(), getHttpVersion(), fields == null ? -1 : fields.size());
231         }
232     }
233 
234     public static class Response extends MetaData
235     {
236         private int _status;
237         private String _reason;
238 
239         public Response()
240         {
241             this(null, 0, null);
242         }
243 
244         public Response(HttpVersion version, int status, HttpFields fields)
245         {
246             this(version, status, fields, Long.MIN_VALUE);
247         }
248 
249         public Response(HttpVersion version, int status, HttpFields fields, long contentLength)
250         {
251             super(version, fields, contentLength);
252             _status = status;
253         }
254 
255         public Response(HttpVersion version, int status, String reason, HttpFields fields, long contentLength)
256         {
257             super(version, fields, contentLength);
258             _reason = reason;
259             _status = status;
260         }
261 
262         @Override
263         public boolean isResponse()
264         {
265             return true;
266         }
267 
268         /**
269          * @return the HTTP status
270          */
271         public int getStatus()
272         {
273             return _status;
274         }
275 
276         /**
277          * @return the HTTP reason
278          */
279         public String getReason()
280         {
281             return _reason;
282         }
283 
284         /**
285          * @param status the HTTP status to set
286          */
287         public void setStatus(int status)
288         {
289             _status = status;
290         }
291 
292         /**
293          * @param reason the HTTP reason to set
294          */
295         public void setReason(String reason)
296         {
297             _reason = reason;
298         }
299 
300         @Override
301         public String toString()
302         {
303             HttpFields fields = getFields();
304             return String.format("%s{s=%d,h=%d}", getHttpVersion(), getStatus(), fields == null ? -1 : fields.size());
305         }
306     }
307 }