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  package org.apache.struts.action;
22  
23  import java.util.Arrays;
24  
25  import org.apache.struts.config.ActionConfig;
26  import org.apache.struts.config.ControllerConfig;
27  import org.apache.struts.config.ForwardConfig;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * <p>An <strong>ActionMapping</strong> represents the information that the
33   * controller, <code>RequestProcessor</code>, knows about the mapping of a
34   * particular request to an instance of a particular <code>Action</code>
35   * class. The <code>ActionMapping</code> instance used to select a particular
36   * <code>Action</code> is passed on to that <code>Action</code>, thereby
37   * providing access to any custom configuration information included with the
38   * <code>ActionMapping</code> object.</p>
39   *
40   * <p>Since Struts 1.1 this class extends <code>ActionConfig</code>.
41   *
42   * <p><strong>NOTE</strong> - This class would have been deprecated and
43   * replaced by <code>org.apache.struts.config.ActionConfig</code> except for
44   * the fact that it is part of the public API that existing applications are
45   * using.</p>
46   *
47   * @version $Rev$ $Date: 2005-08-26 21:58:39 -0400 (Fri, 26 Aug 2005)
48   *          $
49   */
50  public class ActionMapping extends ActionConfig {
51      private static final long serialVersionUID = 2801090844264312287L;
52  
53      /**
54       * The {@code Log} instance for this class.
55       *
56       * @since Struts 1.2.8
57       */
58      private transient final Logger log =
59          LoggerFactory.getLogger(ActionMapping.class);
60  
61      /**
62       * <p>Find and return the <code>ForwardConfig</code> instance defining how
63       * forwarding to the specified logical name should be handled. This is
64       * performed by checking local and then global configurations for the
65       * specified forwarding configuration. If no forwarding configuration can
66       * be found, return <code>null</code>.</p>
67       *
68       * @param forwardName Logical name of the forwarding instance to be
69       *                    returned
70       * @return The local or global forward with the specified name.
71       * @see #findRequiredForward(String)
72       */
73      public ActionForward findForward(String forwardName) {
74          ForwardConfig config = findForwardConfig(forwardName);
75  
76          if (config == null) {
77              config = getModuleConfig().findForwardConfig(forwardName);
78          }
79  
80          // TODO: remove warning since findRequiredForward takes care of use case?
81          if (config == null) {
82              log.warn("Unable to find '{}' forward.", forwardName);
83          }
84  
85          return ((ActionForward) config);
86      }
87  
88      /**
89       * <p>Find and return the <code>ForwardConfig</code> instance of this
90       * mapping, throwing an exception if not found locally or globally.</p>
91       *
92       * @param forwardName Logical name of the forwarding instance to be
93       *                    returned
94       * @return The local or global forward with the specified name.
95       * @throws IllegalStateException if the forward is not found
96       * @see #findForward(String)
97       * @since Struts 1.4
98       */
99      public ActionForward findRequiredForward(String forwardName) {
100         ActionForward forward = findForward(forwardName);
101         if (forward == null) {
102             throw new IllegalStateException(
103                     "Unable to find '" + forwardName +
104                     "' forward of action path '" + getPath() + "'");
105         }
106         return forward;
107     }
108 
109     /**
110      * <p>Return the logical names of all locally defined forwards for this
111      * mapping. If there are no such forwards, a zero-length array is
112      * returned.</p>
113      *
114      * @return The forward names for this action mapping.
115      */
116     public String[] findForwards() {
117         ForwardConfig[] fcs = findForwardConfigs();
118         String[] results = new String[fcs.length];
119 
120         Arrays.setAll(results, i -> fcs[i].getName());
121 
122         return results;
123     }
124 
125     /**
126      * <p>Create (if necessary) and return an {@link ActionForward} that
127      * corresponds to the <code>input</code> property of this Action.
128      * <p>
129      * <b>Since Struts 1.4: </b>
130      * If the <code>input</code> property is not specified and the Controller
131      * is configured to interpret the property as a forward, return the
132      * forward named "input" (if it exists) in this action mapping.</p>
133      *
134      * @return The input forward for this action mapping.
135      * @see ControllerConfig#getInputForward()
136      * @since Struts 1.1
137      */
138     public ActionForward getInputForward() {
139         String input = getInput();
140         if (getModuleConfig().getControllerConfig().getInputForward()) {
141             if (input != null) {
142                 return findForward(input);
143             }
144             return findForward(Action.INPUT);
145         }
146         return (new ActionForward(input));
147     }
148 }