View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.chain.web.servlet;
18  
19  
20  import java.io.IOException;
21  import javax.servlet.ServletException;
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  import org.apache.commons.chain.Catalog;
25  import org.apache.commons.chain.CatalogFactory;
26  import org.apache.commons.chain.Command;
27  import org.apache.commons.chain.web.ChainServlet;
28  
29  
30  /**
31   * <p>Custom subclass of {@link ChainServlet} that also dispatches incoming
32   * requests to a configurable {@link Command} loaded from the specified
33   * {@link Catalog}.</p>
34   *
35   * <p>In addition to the <em>servlet</em> init parameters supported by
36   * {@link ChainServlet}, this class supports the following additional
37   * parameters:</p>
38   * <ul>
39   * <li><strong>org.apache.commons.chain.CATALOG</strong> - Name of the
40   *     catalog from which to acquire commands to be executed.  If not
41   *     specified, the default catalog for this application will be used.</li>
42   * <li><strong>org.apache.commons.chain.COMMAND</strong> - Name of the
43   *     {@link Command} (looked up in our configured {@link Catalog} used
44   *     to process all incoming servlet requests.  If not specified,
45   *     defaults to <code>command</code>.</li>
46   * </ul>
47   *
48   * <p>Also, the <code>org.apache.commons.chain.CONFIG_ATTR</code>
49   * init parameter is also used to identify the
50   * {@link org.apache.commons.chain.Context} attribute under
51   * which our configured {@link Catalog} will be made available to
52   * {@link Command}s processing our requests, in addition to its definition
53   * of the <code>ServletContext</code> attribute key under which the
54   * {@link Catalog} is available.</p>
55   */
56  
57  public class ChainProcessor extends ChainServlet {
58  
59  
60      // ------------------------------------------------------ Manifest Constants
61  
62  
63      /**
64       * <p>The name of the servlet init parameter containing the name of the
65       * {@link Catalog} to use for processing incoming requests.</p>
66       */
67      public static final String CATALOG =
68          "org.apache.commons.chain.CATALOG";
69  
70  
71      /**
72       * <p>The default request attribute under which we expose the
73       * {@link Catalog} being used to subordinate {@link Command}s.</p>
74       */
75      public static final String CATALOG_DEFAULT =
76          "org.apache.commons.chain.CATALOG";
77  
78  
79      /**
80       * <p>The name of the servlet init parameter containing the name of the
81       * {@link Command} (loaded from our configured {@link Catalog} to use
82       * for processing each incoming request.</p>
83       */
84      public static final String COMMAND =
85          "org.apache.commons.chain.COMMAND";
86  
87  
88      /**
89       * <p>The default command name.</p>
90       */
91      private static final String COMMAND_DEFAULT = "command";
92  
93  
94      // ------------------------------------------------------ Instance Variables
95  
96  
97      /**
98       * <p>The name of the context attribute under which our {@link Catalog}
99       * is stored.  This value is also used as the name of the
100      * context attribute under which the catalog is exposed to commands.
101      * If not specified, we will look up commands in the appropriate
102      * {@link Catalog} retrieved from our {@link CatalogFactory}.</p>
103      */
104     private String attribute = null;
105 
106 
107     /**
108      * <p>The name of the {@link Catalog} to retrieve from the
109      * {@link CatalogFactory} for this application, or <code>null</code>
110      * to select the default {@link Catalog}.</p>
111      */
112     private String catalog = null;
113 
114 
115     /**
116      * <p>The name of the {@link Command} to be executed for each incoming
117      * request.</p>
118      */
119     private String command = null;
120 
121 
122     // --------------------------------------------------------- Servlet Methods
123 
124 
125     /**
126      * <p>Clean up as this application is shut down.</p>
127      */
128     public void destroy() {
129 
130         super.destroy();
131         attribute = null;
132         catalog = null;
133         command = null;
134 
135     }
136 
137 
138     /**
139      * <p>Cache the name of the command we should execute for each request.</p>
140      *
141      * @exception ServletException if an initialization error occurs
142      */
143     public void init() throws ServletException {
144 
145         super.init();
146         attribute = getServletConfig().getInitParameter(CONFIG_ATTR);
147         catalog = getServletConfig().getInitParameter(CATALOG);
148         command = getServletConfig().getInitParameter(COMMAND);
149         if (command == null) {
150             command = COMMAND_DEFAULT;
151         }
152 
153     }
154 
155 
156     /**
157      * <p>Configure a {@link ServletWebContext} for the current request, and
158      * pass it to the <code>execute()</code> method of the specified
159      * {@link Command}, loaded from our configured {@link Catalog}.</p>
160      *
161      * @param request The request we are processing
162      * @param response The response we are creating
163      *
164      * @exception IOException if an input/output error occurs
165      * @exception ServletException if a servlet exception occurs
166      */
167     public void service(HttpServletRequest request,
168                         HttpServletResponse response)
169         throws IOException, ServletException {
170 
171         ServletWebContext context =
172             new ServletWebContext(getServletContext(), request, response);
173         Catalog theCatalog = null;
174         if (attribute != null) {
175             theCatalog = (Catalog) getServletContext().getAttribute
176                 (this.attribute);
177         } else if (catalog != null) {
178             theCatalog = CatalogFactory.getInstance().getCatalog(catalog);
179         } else {
180             theCatalog = CatalogFactory.getInstance().getCatalog();
181         }
182         if (attribute == null) {
183             request.setAttribute(CATALOG_DEFAULT, theCatalog);
184         }
185         Command command = theCatalog.getCommand(this.command);
186         try {
187             command.execute(context);
188         } catch (Exception e) {
189             throw new ServletException(e);
190         }
191 
192     }
193 
194 
195 }