View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts.tiles.actions;
23  
24  import java.io.IOException;
25  import java.io.PrintWriter;
26  
27  import jakarta.servlet.http.HttpServletRequest;
28  import jakarta.servlet.http.HttpServletResponse;
29  
30  import org.apache.struts.action.Action;
31  import org.apache.struts.action.ActionForm;
32  import org.apache.struts.action.ActionForward;
33  import org.apache.struts.action.ActionMapping;
34  import org.apache.struts.tiles.ComponentDefinition;
35  import org.apache.struts.tiles.DefinitionsFactoryException;
36  import org.apache.struts.tiles.FactoryNotFoundException;
37  import org.apache.struts.tiles.NoSuchDefinitionException;
38  import org.apache.struts.tiles.TilesUtil;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  /**
43   * <p>An <strong>Action</strong> that dispatches to a Tiles Definition
44   * that is named by the request parameter whose name is specified
45   * by the <code>parameter</code> property of the corresponding
46   * ActionMapping.
47   * This action is useful in following situations:
48   * <li>
49   * <ul>To associate an Url to a definition</ul>
50   * <ul>To use Struts &lt;html:link&gt; tag on a definition</ul>
51   * </li>
52   * <p>To configure the use of this action in your
53   * <code>struts-config.xml</code> file, create an entry like this:</p>
54   *
55   * <code>
56   *   &lt;action path="/saveSubscription"
57   *           type="org.apache.struts.tiles.actions.DefinitionDispatcherAction"
58   *           parameter="def"/&gt;
59   *     &lt;forward name="success"   path="anything" //&gt;
60   *     &lt;forward name="error"     path="path.to.error.page" //&gt;
61   * </code>
62   *
63   * <p>which will use the value of the request parameter named "def"
64   * to pick the appropriate definition name.
65   * <p>  The value for success doesn't matter. The forward will forward to
66   * appropriate definition.
67   * <p> The value for error should denote a valid jsp path or definition name.
68   *
69   * @version $Rev$ $Date$
70   */
71  public class DefinitionDispatcherAction extends Action {
72      private static final long serialVersionUID = 8663194696860741591L;
73  
74      /**
75       * The {@code Log} instance for this class.
76       */
77      private transient final Logger log =
78          LoggerFactory.getLogger(DefinitionDispatcherAction.class);
79  
80      /**
81       * Process the specified HTTP request, and create the corresponding HTTP
82       * response (or forward to another web component that will create it),
83       * with provision for handling exceptions thrown by the business logic.
84       *
85       * @param mapping The ActionMapping used to select this instance
86       * @param form The optional ActionForm bean for this request (if any)
87       * @param request The HTTP request we are processing
88       * @param response The HTTP response we are creating
89       *
90       * @exception Exception if the application business logic throws
91       *  an exception
92       * @since Struts 1.1
93       */
94      @SuppressWarnings("deprecation")
95      public ActionForward execute(
96          ActionMapping mapping,
97          ActionForm form,
98          HttpServletRequest request,
99          HttpServletResponse response)
100         throws Exception {
101 
102         // Identify the request parameter containing the method name
103         // If none defined, use "def"
104         String parameter = mapping.getParameter();
105         if (parameter == null) {
106             parameter = "def";
107         }
108 
109         // Identify the method name to be dispatched to
110         String name = request.getParameter(parameter);
111         if (name == null) {
112             log.error("Can't get parameter '{}'.", parameter);
113 
114             return mapping.findForward("error");
115         }
116 
117         // Try to dispatch to requested definition
118         try {
119             // Read definition from factory, but we can create it here.
120             ComponentDefinition definition =
121                 TilesUtil.getDefinition(
122                     name,
123                     request,
124                     getServlet().getServletContext());
125 
126             log.debug("Get Definition {}", definition);
127 
128             org.apache.struts.tiles.DefinitionsUtil.setActionDefinition(request, definition);
129 
130         } catch (FactoryNotFoundException e) {
131             log.error("Can't get definition factory.", e);
132             return mapping.findForward("error");
133 
134         } catch (NoSuchDefinitionException e) {
135             log.error("Can't get definition '{}'.", name, e);
136             return mapping.findForward("error");
137 
138         } catch (DefinitionsFactoryException e) {
139             log.error("General Factory error '{}'.", e.getMessage(), e);
140             return mapping.findForward("error");
141 
142         } catch (Exception e) {
143             log.error("General error '{}'.", e.getMessage(), e);
144             return mapping.findForward("error");
145         }
146 
147         return mapping.findForward("success");
148 
149     }
150 
151     /**
152      * @deprecated This will be removed after Struts 1.2.
153      */
154     @Deprecated
155     protected void printError(HttpServletResponse response, String msg)
156         throws IOException {
157         response.setContentType("text/plain");
158         PrintWriter writer = response.getWriter();
159         writer.println(msg);
160         writer.flush();
161         writer.close();
162     }
163 }