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.io.IOException;
22  import java.io.InputStream;
23  import java.nio.ByteBuffer;
24  import java.nio.channels.ReadableByteChannel;
25  import org.eclipse.jetty.http.MimeTypes.Type;
26  
27  import org.eclipse.jetty.util.resource.Resource;
28  
29  /* ------------------------------------------------------------ */
30  public class GzipHttpContent implements HttpContent
31  {
32      private final HttpContent _content; 
33      private final HttpContent _contentGz;
34      public final static String ETAG_GZIP="--gzip";
35      public final static String ETAG_GZIP_QUOTE="--gzip\"";
36      public final static PreEncodedHttpField CONTENT_ENCODING_GZIP=new PreEncodedHttpField(HttpHeader.CONTENT_ENCODING,"gzip");
37      
38      public static String removeGzipFromETag(String etag)
39      {
40          if (etag==null)
41              return null;
42          int i = etag.indexOf(ETAG_GZIP_QUOTE);
43          if (i<0)
44              return etag;
45          return etag.substring(0,i)+'"';
46      }
47      
48      public GzipHttpContent(HttpContent content, HttpContent contentGz)
49      {  
50          _content=content;
51          _contentGz=contentGz;
52      }
53  
54      @Override
55      public int hashCode()
56      {
57          return _content.hashCode();
58      }
59  
60      @Override
61      public boolean equals(Object obj)
62      {
63          return _content.equals(obj);
64      }
65  
66      @Override
67      public Resource getResource()
68      {
69          return _content.getResource();
70      }
71  
72      @Override
73      public HttpField getETag()
74      {
75          return new HttpField(HttpHeader.ETAG,getETagValue());
76      }
77  
78      @Override
79      public String getETagValue()
80      {
81          return _content.getResource().getWeakETag(ETAG_GZIP);
82      }
83  
84      @Override
85      public HttpField getLastModified()
86      {
87          return _content.getLastModified();
88      }
89  
90      @Override
91      public String getLastModifiedValue()
92      {
93          return _content.getLastModifiedValue();
94      }
95  
96      @Override
97      public HttpField getContentType()
98      {
99          return _content.getContentType();
100     }
101 
102     @Override
103     public String getContentTypeValue()
104     {
105         return _content.getContentTypeValue();
106     }
107 
108     @Override
109     public HttpField getContentEncoding()
110     {
111         return CONTENT_ENCODING_GZIP;
112     }
113 
114     @Override
115     public String getContentEncodingValue()
116     {
117         return CONTENT_ENCODING_GZIP.getValue();
118     }
119 
120     @Override
121     public String getCharacterEncoding()
122     {
123         return _content.getCharacterEncoding();
124     }
125 
126     @Override
127     public Type getMimeType()
128     {
129         return _content.getMimeType();
130     }
131 
132     @Override
133     public void release()
134     {
135         _content.release();
136     }
137 
138     @Override
139     public ByteBuffer getIndirectBuffer()
140     {
141         return _contentGz.getIndirectBuffer();
142     }
143 
144     @Override
145     public ByteBuffer getDirectBuffer()
146     {
147         return _contentGz.getDirectBuffer();
148     }
149 
150     @Override
151     public HttpField getContentLength()
152     {
153         return _contentGz.getContentLength();
154     }
155 
156     @Override
157     public long getContentLengthValue()
158     {
159         return _contentGz.getContentLengthValue();
160     }
161 
162     @Override
163     public InputStream getInputStream() throws IOException
164     {
165         return _contentGz.getInputStream();
166     }
167 
168     @Override
169     public ReadableByteChannel getReadableByteChannel() throws IOException
170     {
171         return _contentGz.getReadableByteChannel();
172     }
173 
174     @Override
175     public String toString()
176     {
177         return String.format("GzipHttpContent@%x{r=%s|%s,lm=%s|%s,ct=%s}",hashCode(),
178                 _content.getResource(),_contentGz.getResource(),
179                 _content.getResource().lastModified(),_contentGz.getResource().lastModified(),
180                 getContentType());
181     }
182 
183     @Override
184     public HttpContent getGzipContent()
185     {
186         return null;
187     }
188 }