DefaultModule.java
Upload User: demmber
Upload Date: 2007-12-22
Package Size: 717k
Code Size: 4k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * @(#)DefaultModule.java 0.3-3 06/05/2001
  3.  *
  4.  *  This file is part of the HTTPClient package
  5.  *  Copyright (C) 1996-2001 Ronald Tschal鋜
  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.  *  The HTTPClient's home page is located at:
  28.  *
  29.  *  http://www.innovation.ch/java/HTTPClient/ 
  30.  *
  31.  */
  32. package HTTPClient;
  33. import java.io.IOException;
  34. import java.net.ProtocolException;
  35. /**
  36.  * This is the default module which gets called after all other modules
  37.  * have done their stuff.
  38.  *
  39.  * @version 0.3-3  06/05/2001
  40.  * @author Ronald Tschal鋜
  41.  */
  42. class DefaultModule implements HTTPClientModule
  43. {
  44.     /** number of times the request will be retried */
  45.     private int req_timeout_retries;
  46.     // Constructors
  47.     /**
  48.      * Three retries upon receipt of a 408.
  49.      */
  50.     DefaultModule()
  51.     {
  52. req_timeout_retries = 3;
  53.     }
  54.     // Methods
  55.     /**
  56.      * Invoked by the HTTPClient.
  57.      */
  58.     public int requestHandler(Request req, Response[] resp)
  59.     {
  60. return REQ_CONTINUE;
  61.     }
  62.     /**
  63.      * Invoked by the HTTPClient.
  64.      */
  65.     public void responsePhase1Handler(Response resp, RoRequest req)
  66.     {
  67.     }
  68.     /**
  69.      * Invoked by the HTTPClient.
  70.      */
  71.     public int responsePhase2Handler(Response resp, Request req)
  72.     throws IOException
  73.     {
  74. /* handle various response status codes until satisfied */
  75. int sts  = resp.getStatusCode();
  76. switch(sts)
  77. {
  78.     case 408: // Request Timeout
  79. if (req_timeout_retries-- == 0  ||  req.getStream() != null)
  80. {
  81.     Log.write(Log.MODS, "DefM:  Status " + sts + " " +
  82.     resp.getReasonLine() + " not handled - " +
  83.     "maximum number of retries exceeded");
  84.     return RSP_CONTINUE;
  85. }
  86. else
  87. {
  88.     Log.write(Log.MODS, "DefM:  Handling " + sts + " " +
  89. resp.getReasonLine() + " - " +
  90. "resending request");
  91.     return RSP_REQUEST;
  92. }
  93.     case 411: // Length Required
  94. if (req.getStream() != null  &&
  95.     req.getStream().getLength() == -1)
  96.     return RSP_CONTINUE;
  97. try { resp.getInputStream().close(); }
  98. catch (IOException ioe) { }
  99. if (req.getData() != null)
  100.     throw new ProtocolException("Received status code 411 even"+
  101.     " though Content-Length was sent");
  102. Log.write(Log.MODS, "DefM:  Handling " + sts + " " +
  103.     resp.getReasonLine() + " - resending " +
  104.     "request with 'Content-length: 0'");
  105. req.setData(new byte[0]); // will send Content-Length: 0
  106. return RSP_REQUEST;
  107.     case 505: // HTTP Version not supported
  108. return RSP_CONTINUE;
  109.     default:
  110. return RSP_CONTINUE;
  111. }
  112.     }
  113.     /**
  114.      * Invoked by the HTTPClient.
  115.      */
  116.     public void responsePhase3Handler(Response resp, RoRequest req)
  117.     {
  118.     }
  119.     /**
  120.      * Invoked by the HTTPClient.
  121.      */
  122.     public void trailerHandler(Response resp, RoRequest req)
  123.     {
  124.     }
  125. }