KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HTTPClient > DefaultModule


1 /*
2  * @(#)DefaultModule.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.net.ProtocolException JavaDoc;
33
34
35 /**
36  * This is the default module which gets called after all other modules
37  * have done their stuff.
38  *
39  * @version 0.3-2 18/06/1999
40  * @author Ronald Tschalär
41  */

42
43 class DefaultModule implements HTTPClientModule, GlobalConstants
44 {
45     /** number of times the request will be retried */
46     private int req_timeout_retries;
47
48
49     // Constructors
50

51     /**
52      * Three retries upon receipt of a 408.
53      */

54     DefaultModule()
55     {
56     req_timeout_retries = 3;
57     }
58
59
60     // Methods
61

62     /**
63      * Invoked by the HTTPClient.
64      */

65     public int requestHandler(Request req, Response[] resp)
66     {
67     return REQ_CONTINUE;
68     }
69
70
71     /**
72      * Invoked by the HTTPClient.
73      */

74     public void responsePhase1Handler(Response resp, RoRequest req)
75     {
76     }
77
78
79     /**
80      * Invoked by the HTTPClient.
81      */

82     public int responsePhase2Handler(Response resp, Request req)
83         throws IOException JavaDoc
84     {
85     /* handle various response status codes until satisfied */
86
87     int sts = resp.getStatusCode();
88     switch(sts)
89     {
90         case 408: // Request Timeout
91

92         if (req_timeout_retries-- == 0 || req.getStream() != null)
93         {
94             if (DebugMods)
95             System.err.println("DefM: Status " + sts + " " +
96                     resp.getReasonLine() + " not handled - " +
97                     "maximum number of retries exceeded");
98
99             return RSP_CONTINUE;
100         }
101         else
102         {
103             if (DebugMods)
104             System.err.println("DefM: Handling " + sts + " " +
105                         resp.getReasonLine() + " - " +
106                         "resending request");
107
108             return RSP_REQUEST;
109         }
110
111         case 411: // Length Required
112
if (req.getStream() != null &&
113             req.getStream().getLength() == -1)
114             return RSP_CONTINUE;
115
116         try { resp.getInputStream().close(); }
117         catch (IOException JavaDoc ioe) { }
118         if (req.getData() != null)
119             throw new ProtocolException JavaDoc("Received status code 411 even"+
120                         " though Content-Length was sent");
121
122         if (DebugMods)
123             System.err.println("DefM: Handling " + sts + " " +
124                     resp.getReasonLine() + " - resending " +
125                     "request with 'Content-length: 0'");
126
127         req.setData(new byte[0]); // will send Content-Length: 0
128
return RSP_REQUEST;
129
130         case 505: // HTTP Version not supported
131
return RSP_CONTINUE;
132
133         default:
134         return RSP_CONTINUE;
135     }
136     }
137
138
139     /**
140      * Invoked by the HTTPClient.
141      */

142     public void responsePhase3Handler(Response resp, RoRequest req)
143     {
144     }
145
146
147     /**
148      * Invoked by the HTTPClient.
149      */

150     public void trailerHandler(Response resp, RoRequest req)
151     {
152     }
153 }
154
155
Popular Tags