KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HTTPClient > TransferEncodingModule


1 /*
2  * @(#)TransferEncodingModule.java 0.3-2 18/06/1999
3  *
4  * This file is part of the HTTPClient package
5  * Copyright (C) 1996-1999 Ronald Tschalär
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free
19  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307, USA
21  *
22  * For questions, suggestions, bug-reports, enhancement-requests etc.
23  * I may be contacted at:
24  *
25  * ronald@innovation.ch
26  *
27  */

28
29 package HTTPClient;
30
31 import java.io.IOException JavaDoc;
32 import java.util.Vector JavaDoc;
33 import java.util.zip.InflaterInputStream JavaDoc;
34 import java.util.zip.GZIPInputStream JavaDoc;
35
36
37 /**
38  * This module handles the TransferEncoding response header. It currently
39  * handles the "gzip", "deflate", "compress", "chunked" and "identity"
40  * tokens.
41  *
42  * Note: This module requires JDK 1.1 or later.
43  *
44  * @version 0.3-2 18/06/1999
45  * @author Ronald Tschalär
46  */

47
48 class TransferEncodingModule implements HTTPClientModule, GlobalConstants
49 {
50     static
51     {
52     /* This ensures that the loading of this class is only successful
53      * if we're in JDK 1.1 (or later) and have access to java.util.zip
54      */

55     try
56         { new InflaterInputStream JavaDoc(null); }
57     catch (NullPointerException JavaDoc npe)
58         { /* JDK 1.2 Final started throwing this */ }
59     }
60
61
62     // Constructors
63

64     TransferEncodingModule()
65     {
66     }
67
68
69     // Methods
70

71     /**
72      * Invoked by the HTTPClient.
73      */

74     public int requestHandler(Request req, Response[] resp)
75         throws ModuleException
76     {
77     // Parse TE header
78

79     int idx;
80     NVPair[] hdrs = req.getHeaders();
81     for (idx=0; idx<hdrs.length; idx++)
82         if (hdrs[idx].getName().equalsIgnoreCase("TE"))
83         break;
84
85     Vector JavaDoc pte;
86     if (idx == hdrs.length)
87     {
88         hdrs = Util.resizeArray(hdrs, idx+1);
89         req.setHeaders(hdrs);
90         pte = new Vector JavaDoc();
91     }
92     else
93     {
94         try
95         { pte = Util.parseHeader(hdrs[idx].getValue()); }
96         catch (ParseException pe)
97         { throw new ModuleException(pe.toString()); }
98     }
99
100
101         // done if "*;q=1.0" present
102

103         HttpHeaderElement all = Util.getElement(pte, "*");
104         if (all != null)
105     {
106         NVPair[] params = all.getParams();
107         for (idx=0; idx<params.length; idx++)
108         if (params[idx].getName().equalsIgnoreCase("q")) break;
109
110         if (idx == params.length) // no qvalue, i.e. q=1.0
111
return REQ_CONTINUE;
112
113         if (params[idx].getValue() == null ||
114         params[idx].getValue().length() == 0)
115         throw new ModuleException("Invalid q value for \"*\" in TE " +
116                       "header: ");
117
118         try
119         {
120         if (Float.valueOf(params[idx].getValue()).floatValue() > 0.)
121             return REQ_CONTINUE;
122         }
123         catch (NumberFormatException JavaDoc nfe)
124         {
125         throw new ModuleException("Invalid q value for \"*\" in TE " +
126                       "header: " + nfe.getMessage());
127         }
128     }
129  
130
131     // Add gzip, deflate, and compress tokens to the TE header
132

133     if (!pte.contains(new HttpHeaderElement("deflate")))
134         pte.addElement(new HttpHeaderElement("deflate"));
135     if (!pte.contains(new HttpHeaderElement("gzip")))
136         pte.addElement(new HttpHeaderElement("gzip"));
137     if (!pte.contains(new HttpHeaderElement("compress")))
138         pte.addElement(new HttpHeaderElement("compress"));
139
140     hdrs[idx] = new NVPair("TE", Util.assembleHeader(pte));
141
142     return REQ_CONTINUE;
143     }
144
145
146     /**
147      * Invoked by the HTTPClient.
148      */

149     public void responsePhase1Handler(Response resp, RoRequest req)
150     {
151     }
152
153
154     /**
155      * Invoked by the HTTPClient.
156      */

157     public int responsePhase2Handler(Response resp, Request req)
158     {
159     return RSP_CONTINUE;
160     }
161
162
163     /**
164      * Invoked by the HTTPClient.
165      */

166     public void responsePhase3Handler(Response resp, RoRequest req)
167         throws IOException JavaDoc, ModuleException
168     {
169     String JavaDoc te = resp.getHeader("Transfer-Encoding");
170     if (te == null || req.getMethod().equals("HEAD"))
171         return;
172
173     Vector JavaDoc pte;
174     try
175         { pte = Util.parseHeader(te); }
176     catch (ParseException pe)
177         { throw new ModuleException(pe.toString()); }
178
179     while (pte.size() > 0)
180     {
181         String JavaDoc encoding = ((HttpHeaderElement) pte.lastElement()).getName();
182         if (encoding.equalsIgnoreCase("gzip"))
183         {
184         if (DebugMods)
185             System.err.println("TEM: pushing gzip-input-stream");
186
187         resp.inp_stream = new GZIPInputStream JavaDoc(resp.inp_stream);
188         }
189         else if (encoding.equalsIgnoreCase("deflate"))
190         {
191         if (DebugMods)
192             System.err.println("TEM: pushing inflater-input-stream");
193
194         resp.inp_stream = new InflaterInputStream JavaDoc(resp.inp_stream);
195         }
196         else if (encoding.equalsIgnoreCase("compress"))
197         {
198         if (DebugMods)
199             System.err.println("TEM: pushing uncompress-input-stream");
200
201         resp.inp_stream = new UncompressInputStream(resp.inp_stream);
202         }
203         else if (encoding.equalsIgnoreCase("chunked"))
204         {
205         if (DebugMods)
206             System.err.println("TEM: pushing chunked-input-stream");
207
208         resp.inp_stream = new ChunkedInputStream(resp.inp_stream);
209         }
210         else if (encoding.equalsIgnoreCase("identity"))
211         {
212         if (DebugMods)
213             System.err.println("TEM: ignoring 'identity' token");
214         }
215         else
216         {
217         if (DebugMods)
218             System.err.println("TEM: Unknown transfer encoding '" +
219                     encoding + "'");
220
221         break;
222         }
223
224         pte.removeElementAt(pte.size()-1);
225     }
226
227     if (pte.size() > 0)
228         resp.setHeader("Transfer-Encoding", Util.assembleHeader(pte));
229     else
230         resp.deleteHeader("Transfer-Encoding");
231     }
232
233
234     /**
235      * Invoked by the HTTPClient.
236      */

237     public void trailerHandler(Response resp, RoRequest req)
238     {
239     }
240 }
241
242
Popular Tags