001: /*
002: * @(#)DefaultModule.java 0.3-2 18/06/1999
003: *
004: * This file is part of the HTTPClient package
005: * Copyright (C) 1996-1999 Ronald Tschalär
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free
019: * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
020: * MA 02111-1307, USA
021: *
022: * For questions, suggestions, bug-reports, enhancement-requests etc.
023: * I may be contacted at:
024: *
025: * ronald@innovation.ch
026: *
027: */
028:
029: package HTTPClient;
030:
031: import java.io.IOException;
032: import java.net.ProtocolException;
033:
034: /**
035: * This is the default module which gets called after all other modules
036: * have done their stuff.
037: *
038: * @version 0.3-2 18/06/1999
039: * @author Ronald Tschalär
040: */
041:
042: class DefaultModule implements HTTPClientModule, GlobalConstants {
043: /** number of times the request will be retried */
044: private int req_timeout_retries;
045:
046: // Constructors
047:
048: /**
049: * Three retries upon receipt of a 408.
050: */
051: DefaultModule() {
052: req_timeout_retries = 3;
053: }
054:
055: // Methods
056:
057: /**
058: * Invoked by the HTTPClient.
059: */
060: public int requestHandler(Request req, Response[] resp) {
061: return REQ_CONTINUE;
062: }
063:
064: /**
065: * Invoked by the HTTPClient.
066: */
067: public void responsePhase1Handler(Response resp, RoRequest req) {
068: }
069:
070: /**
071: * Invoked by the HTTPClient.
072: */
073: public int responsePhase2Handler(Response resp, Request req)
074: throws IOException {
075: /* handle various response status codes until satisfied */
076:
077: int sts = resp.getStatusCode();
078: switch (sts) {
079: case 408: // Request Timeout
080:
081: if (req_timeout_retries-- == 0 || req.getStream() != null) {
082: if (DebugMods)
083: System.err.println("DefM: Status " + sts + " "
084: + resp.getReasonLine() + " not handled - "
085: + "maximum number of retries exceeded");
086:
087: return RSP_CONTINUE;
088: } else {
089: if (DebugMods)
090: System.err.println("DefM: Handling " + sts + " "
091: + resp.getReasonLine() + " - "
092: + "resending request");
093:
094: return RSP_REQUEST;
095: }
096:
097: case 411: // Length Required
098: if (req.getStream() != null
099: && req.getStream().getLength() == -1)
100: return RSP_CONTINUE;
101:
102: try {
103: resp.getInputStream().close();
104: } catch (IOException ioe) {
105: }
106: if (req.getData() != null)
107: throw new ProtocolException(
108: "Received status code 411 even"
109: + " though Content-Length was sent");
110:
111: if (DebugMods)
112: System.err.println("DefM: Handling " + sts + " "
113: + resp.getReasonLine() + " - resending "
114: + "request with 'Content-length: 0'");
115:
116: req.setData(new byte[0]); // will send Content-Length: 0
117: return RSP_REQUEST;
118:
119: case 505: // HTTP Version not supported
120: return RSP_CONTINUE;
121:
122: default:
123: return RSP_CONTINUE;
124: }
125: }
126:
127: /**
128: * Invoked by the HTTPClient.
129: */
130: public void responsePhase3Handler(Response resp, RoRequest req) {
131: }
132:
133: /**
134: * Invoked by the HTTPClient.
135: */
136: public void trailerHandler(Response resp, RoRequest req) {
137: }
138: }
|