View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-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.InputStream;
17  import java.io.InputStreamReader;
18  import java.io.LineNumberReader;
19  
20  import org.eclipse.jetty.io.Buffer;
21  import org.eclipse.jetty.io.BufferCache;
22  import org.eclipse.jetty.io.ByteArrayBuffer;
23  import org.eclipse.jetty.util.log.Log;
24  
25  /**
26   * Cached HTTP Header values.
27   * This class caches the conversion of common HTTP Header values to and from {@link ByteArrayBuffer} instances.
28   * The resource "/org/eclipse/jetty/useragents" is checked for a list of common user agents, so that repeated
29   * creation of strings for these agents can be avoided.
30   * 
31   * 
32   */
33  public class HttpHeaderValues extends BufferCache
34  {
35      public final static String
36          CLOSE="close",
37          CHUNKED="chunked",
38          GZIP="gzip",
39          IDENTITY="identity",
40          KEEP_ALIVE="keep-alive",
41          CONTINUE="100-continue",
42          PROCESSING="102-processing",
43          TE="TE",
44          BYTES="bytes",
45          NO_CACHE="no-cache";
46  
47      public final static int
48          CLOSE_ORDINAL=1,
49          CHUNKED_ORDINAL=2,
50          GZIP_ORDINAL=3,
51          IDENTITY_ORDINAL=4,
52          KEEP_ALIVE_ORDINAL=5,
53          CONTINUE_ORDINAL=6,
54          PROCESSING_ORDINAL=7,
55          TE_ORDINAL=8,
56          BYTES_ORDINAL=9,
57          NO_CACHE_ORDINAL=10;
58      
59      public final static HttpHeaderValues CACHE= new HttpHeaderValues();
60  
61      public final static Buffer 
62          CLOSE_BUFFER=CACHE.add(CLOSE,CLOSE_ORDINAL),
63          CHUNKED_BUFFER=CACHE.add(CHUNKED,CHUNKED_ORDINAL),
64          GZIP_BUFFER=CACHE.add(GZIP,GZIP_ORDINAL),
65          IDENTITY_BUFFER=CACHE.add(IDENTITY,IDENTITY_ORDINAL),
66          KEEP_ALIVE_BUFFER=CACHE.add(KEEP_ALIVE,KEEP_ALIVE_ORDINAL),
67          CONTINUE_BUFFER=CACHE.add(CONTINUE, CONTINUE_ORDINAL),
68          PROCESSING_BUFFER=CACHE.add(PROCESSING, PROCESSING_ORDINAL),
69          TE_BUFFER=CACHE.add(TE,TE_ORDINAL),
70          BYTES_BUFFER=CACHE.add(BYTES,BYTES_ORDINAL),
71          NO_CACHE_BUFFER=CACHE.add(NO_CACHE,NO_CACHE_ORDINAL);
72          
73      static
74      {  
75          int index=100;
76          CACHE.add("gzip",index++);
77          CACHE.add("gzip,deflate",index++);
78          CACHE.add("deflate",index++);
79  
80          InputStream ua = HttpHeaderValues.class.getResourceAsStream("/org/eclipse/jetty/http/useragents");
81          try
82          {
83              if (ua!=null)
84              {
85                  try
86                  {
87                      LineNumberReader in = new LineNumberReader(new InputStreamReader(ua));
88                      String line = in.readLine();
89                      while (line!=null)
90                      {
91                          CACHE.add(line,index++);
92                          line = in.readLine();
93                      }
94                  }
95                  finally
96                  {
97                      ua.close();
98                  }
99              }
100         }
101         catch(Exception e)
102         {
103             Log.warn(e.toString());
104             Log.debug(e);
105         }
106     }
107 }