View Javadoc

1   // ========================================================================
2   // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // Licensed under the Apache License, Version 2.0 (the "License");
5   // you may not use this file except in compliance with the License.
6   // You may obtain a copy of the License at 
7   // http://www.apache.org/licenses/LICENSE-2.0
8   // Unless required by applicable law or agreed to in writing, software
9   // distributed under the License is distributed on an "AS IS" BASIS,
10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  // See the License for the specific language governing permissions and
12  // limitations under the License.
13  // ========================================================================
14  
15  package org.mortbay.jetty;
16  
17  import java.io.InputStream;
18  import java.io.InputStreamReader;
19  import java.io.LineNumberReader;
20  
21  import org.mortbay.io.Buffer;
22  import org.mortbay.io.BufferCache;
23  import org.mortbay.io.ByteArrayBuffer;
24  import org.mortbay.log.Log;
25  
26  /**
27   * Cached HTTP Header values.
28   * This class caches the conversion of common HTTP Header values to and from {@link ByteArrayBuffer} instances.
29   * The resource "/org/mortbay/jetty/useragents" is checked for a list of common user agents, so that repeated
30   * creation of strings for these agents can be avoided.
31   * 
32   * @author gregw
33   */
34  public class HttpHeaderValues extends BufferCache
35  {
36      public final static String
37          CLOSE="close",
38          CHUNKED="chunked",
39          GZIP="gzip",
40          IDENTITY="identity",
41          KEEP_ALIVE="keep-alive",
42          CONTINUE="100-continue",
43          PROCESSING="102-processing",
44          TE="TE",
45          BYTES="bytes",
46          NO_CACHE="no-cache";
47  
48      public final static int
49          CLOSE_ORDINAL=1,
50          CHUNKED_ORDINAL=2,
51          GZIP_ORDINAL=3,
52          IDENTITY_ORDINAL=4,
53          KEEP_ALIVE_ORDINAL=5,
54          CONTINUE_ORDINAL=6,
55          PROCESSING_ORDINAL=7,
56          TE_ORDINAL=8,
57          BYTES_ORDINAL=9,
58          NO_CACHE_ORDINAL=10;
59      
60      public final static HttpHeaderValues CACHE= new HttpHeaderValues();
61  
62      public final static Buffer 
63          CLOSE_BUFFER=CACHE.add(CLOSE,CLOSE_ORDINAL),
64          CHUNKED_BUFFER=CACHE.add(CHUNKED,CHUNKED_ORDINAL),
65          GZIP_BUFFER=CACHE.add(GZIP,GZIP_ORDINAL),
66          IDENTITY_BUFFER=CACHE.add(IDENTITY,IDENTITY_ORDINAL),
67          KEEP_ALIVE_BUFFER=CACHE.add(KEEP_ALIVE,KEEP_ALIVE_ORDINAL),
68          CONTINUE_BUFFER=CACHE.add(CONTINUE, CONTINUE_ORDINAL),
69          PROCESSING_BUFFER=CACHE.add(PROCESSING, PROCESSING_ORDINAL),
70          TE_BUFFER=CACHE.add(TE,TE_ORDINAL),
71          BYTES_BUFFER=CACHE.add(BYTES,BYTES_ORDINAL),
72          NO_CACHE_BUFFER=CACHE.add(NO_CACHE,NO_CACHE_ORDINAL);
73          
74      static
75      {  
76          int index=100;
77          CACHE.add("gzip",index++);
78          CACHE.add("gzip,deflate",index++);
79          CACHE.add("deflate",index++);
80          
81          try
82          {
83              InputStream ua = HttpHeaderValues.class.getResourceAsStream("/org/mortbay/jetty/useragents");
84              if (ua!=null)
85              {
86                  LineNumberReader in = new LineNumberReader(new InputStreamReader(ua));
87                  String line = in.readLine();
88                  while (line!=null)
89                  {
90                      CACHE.add(line,index++);
91                      line = in.readLine();
92                  }
93              }
94          }
95          catch(Exception e)
96          {
97              e.printStackTrace();
98              Log.ignore(e);
99          }
100     }
101 }