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.chain.commands;
22  
23  import org.apache.struts.action.Action;
24  import org.apache.struts.chain.contexts.ActionContext;
25  import org.apache.struts.config.ActionConfig;
26  import org.apache.struts.config.ForwardConfig;
27  import org.apache.struts.config.ModuleConfig;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * <p>Select and cache a <code>ForwardConfig</code> that returns us to the
33   * input page for the current action, if any.</p>
34   *
35   * @version $Rev$ $Date: 2005-06-04 10:58:46 -0400 (Sat, 04 Jun 2005)
36   *          $
37   */
38  public abstract class AbstractSelectInput extends ActionCommandBase {
39      // ------------------------------------------------------ Instance Variables
40  
41      /**
42       * The {@code Log} instance for this class.
43       */
44      private final Logger log =
45          LoggerFactory.getLogger(AbstractSelectInput.class);
46  
47      // ---------------------------------------------------------- Public Methods
48  
49      /**
50       * <p>Select and cache a <code>ForwardConfig</code> for the input page for
51       * the current request.</p>
52       *
53       * @param actionCtx The <code>Context</code> for the current request
54       * @return <code>false</code> so that processing continues
55       * @throws Exception if thrown by the Action class
56       */
57      @Override
58      protected boolean execute_(ActionContext actionCtx)
59          throws Exception {
60          // Skip processing if the current request is valid
61          Boolean valid = actionCtx.getFormValid();
62  
63          if ((valid != null) && valid.booleanValue()) {
64              return CONTINUE_PROCESSING;
65          }
66  
67          // Acquire configuration objects that we need
68          ActionConfig actionConfig = actionCtx.getActionConfig();
69          ModuleConfig moduleConfig = actionConfig.getModuleConfig();
70  
71          // Cache an ForwardConfig back to our input page
72          ForwardConfig forwardConfig = null;
73          String input = actionConfig.getInput();
74  
75          if (moduleConfig.getControllerConfig().getInputForward()) {
76              log.trace("Finding ForwardConfig for '{}'", input);
77              forwardConfig = inputForward(actionConfig, moduleConfig, input);
78              if (forwardConfig == null) {
79                  log.atError().log(() -> getErrorMessage(actionCtx, actionConfig));
80              }
81          } else {
82              log.trace("Delegating to forward() for '{}'", input);
83              forwardConfig = forward(actionCtx, moduleConfig, input);
84          }
85  
86          log.debug("Forwarding back to {}", forwardConfig);
87  
88          actionCtx.setForwardConfig(forwardConfig);
89  
90          return CONTINUE_PROCESSING;
91      }
92  
93      // ------------------------------------------------------- Protected Methods
94  
95      /**
96       * <p>Create and return a <code>ForwardConfig</code> representing the
97       * specified module-relative destination.</p>
98       *
99       * @param context      The context for this request
100      * @param moduleConfig The <code>ModuleConfig</code> for this request
101      * @param uri          The module-relative URI to be the destination
102      * @return ForwardConfig representing destination
103      */
104     protected abstract ForwardConfig forward(ActionContext context,
105         ModuleConfig moduleConfig, String uri);
106 
107     /**
108      * <p> Retrieve error message from context. </p>
109      *
110      * @param context      The <code>Context</code> for the current request
111      * @param actionConfig The current action mapping
112      * @return error message
113      */
114     protected abstract String getErrorMessage(ActionContext context,
115         ActionConfig actionConfig);
116 
117     /**
118      * Attempts to resolve the input as a {@link ForwardConfig} attribute.
119      * This method should only invoked if the Controller has its
120      * <code>inputForward</code> property set to <code>true</code>.
121      * If the input parameter is specified, use that, otherwise try
122      * to find one in the mapping or the module under the standard
123      * conventional <code>input</code> name.
124      *
125      * @param actionConfig the config for the target action
126      * @param moduleConfig the config for the module of the action
127      * @param input the name of the input
128      * @return ForwardConfig representing destination
129      * @see Action#INPUT
130      */
131     protected ForwardConfig inputForward(ActionConfig actionConfig,
132             ModuleConfig moduleConfig, String input) {
133         ForwardConfig forwardConfig;
134 
135         if (input != null) {
136             forwardConfig = actionConfig.findForwardConfig(input);
137             if (forwardConfig == null) {
138                 forwardConfig = moduleConfig.findForwardConfig(input);
139             }
140         } else {
141             forwardConfig = actionConfig.findForwardConfig(Action.INPUT);
142             if (forwardConfig == null) {
143                 forwardConfig = moduleConfig.findForwardConfig(Action.INPUT);
144             }
145         }
146 
147         return forwardConfig;
148     }
149 }