Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: juju/reattore/server/intercept/impl/ErrorInterceptor.java


1   /*  Reattore HTTP Server
2   
3       Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
4   
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (at your option) any later version.
9   
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  
19      $Id: ErrorInterceptor.java,v 1.10 2003/03/05 04:31:57 michaelh Exp $
20  */
21  
22  package juju.reattore.server.intercept.impl;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  import juju.reattore.protocol.http.*;
28  import juju.reattore.server.intercept.Interceptor;
29  
30  /** Interceptor that serves an error document if the main path can't
31      find anything.
32  
33      The main path will be given the first chance to handle the
34      request.  If the main path cannot then the error path is given a
35      chance. 
36  
37      @tag error
38      @group Interceptor
39      @children Interceptor
40   */
41  public class ErrorInterceptor
42      implements Interceptor {
43  
44      private static Log log = LogFactory.getLog(ErrorInterceptor.class);
45  
46      private Interceptor main;
47      private Interceptor error;
48  
49      /** Accessor to help Digester configuration.  First sets the main
50          path, then the error path.
51  
52          @param child   Path to use.
53      */
54      public void addChild(Interceptor child) {
55          if (main == null) {
56              main = child;
57          }
58          else if (error == null) {
59              error = child;
60          }
61          else {
62              log.warn("An ErrorInterceptor only accepts two children");
63          }
64      }
65  
66      /** Sets the root of the main path to go through.
67          
68          @param path  The root of the main path.
69      */
70      public void setMainPath(Interceptor path) {
71          main = path;
72      }
73  
74      /** Sets the root of the error path to go through.
75          
76          @param path  The root of the main path.
77      */
78      public void setErrorPath(Interceptor path) {
79          error = path;
80      }
81  
82      /** @see Interceptor */
83      public boolean process(HttpRequest req, HttpResponse resp) {
84  
85          if (main.process(req, resp) == true
86              && resp.getBody() != null) {
87  
88              /* Done */
89              return true;
90          }
91          else {
92              /* We need to remember the error code as the display
93                 interceptor will override it. */
94              int err = resp.getStatus();
95  
96              try {
97                  if (error.process(req, resp) == true) {
98                      /* Error handlers took it */
99                      return true;
100                 }
101                 else {
102                     /* Neither handled it - perhaps something further up
103                        will. */
104                     return false;
105                 }
106             }
107             finally {
108                 resp.setStatus(err);
109             }
110         }
111     }
112 }