View Javadoc
1   /*
2    * Copyright 2011 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.fatwire.gst.foundation.controller.action;
17  
18  import javax.servlet.ServletContext;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import COM.FutureTense.Interfaces.ICS;
22  import COM.FutureTense.Util.ftErrors;
23  
24  import com.fatwire.gst.foundation.CSRuntimeException;
25  import com.fatwire.gst.foundation.controller.AbstractController;
26  import com.fatwire.gst.foundation.facade.RenderUtils;
27  
28  /**
29   * Dispatching Controller. Relies on ActionLocator to dispatch control to Action
30   * classes. An ActionNameResolver is used to resolve the action name on the
31   * current request.
32   * 
33   * @author Dolf Dijkstra
34   * @since May 26, 2011
35   */
36  public abstract class AbstractActionController extends AbstractController {
37  
38      public AbstractActionController(ICS ics) {
39          super(ics);
40      }
41  
42      @Override
43      public final void doExecute() {
44  
45          // record seid and eid
46          RenderUtils.recordBaseCompositionalDependencies(ics);
47  
48          // find the action locator
49          LOG.trace("Dispatcher looking for action locator");
50          final ActionLocator locator = getActionLocator();
51          if (locator == null)
52              throw new IllegalStateException("No ActionLocator returned by class " + getClass().getName());
53          if (LOG.isTraceEnabled()) {
54              LOG.trace("Using action locator: " + locator.getClass().getName());
55          }
56          ActionNameResolver resolver = getActionNameResolver();
57          if (resolver == null)
58              throw new IllegalStateException("No ActionNameResolver returned by class " + getClass().getName());
59          String actionName = resolver.resolveActionName(ics);
60          // get the action
61          final Action action = locator.getAction(ics, actionName);
62          if (LOG.isTraceEnabled()) {
63              LOG.trace("Using action: " + action.getClass().getName());
64          }
65          if (action == null)
66              throw new IllegalStateException(
67                      "No Action found. An ActionLocator should always return a Action. ActionLocation "
68                              + locator.getClass().getName() + " did not return an action for '" + actionName + "'.");
69  
70          // execute the command
71          action.handleRequest(ics);
72          LOG.trace("Request handling complete");
73      }
74  
75      /**
76       * @return the ActionNameResolver
77       */
78      protected abstract ActionNameResolver getActionNameResolver();
79  
80      /**
81       * @return the ActionLocator.
82       */
83      protected abstract ActionLocator getActionLocator();
84  
85      @SuppressWarnings("deprecation")
86      protected ServletContext getServletContext() {
87          return ics.getIServlet().getServlet().getServletContext();
88      }
89  
90      @Override
91      public final void handleException(final Exception e) {
92          if (LOG.isTraceEnabled()) {
93              // Give developer a clue in case error pages aren't configured
94              // properly.
95              LOG.trace("Action threw an exception and an error code will be returned.  Exception: " + e, e);
96          }
97          if (e instanceof CSRuntimeException) {
98              handleCSRuntimeException((CSRuntimeException) e);
99          } else {
100             sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
101         }
102     }
103 
104     /**
105      * This method transforms errno values into http status codes and sets them
106      * using the X-Fatwire-Status header.
107      * <p/>
108      * Only some errnos are handled by this base class.
109      * <p/>
110      * More info coming soon
111      * 
112      * @param e exception
113      */
114     protected final void handleCSRuntimeException(final CSRuntimeException e) {
115         switch (e.getErrno()) {
116             case HttpServletResponse.SC_BAD_REQUEST:
117             case ftErrors.badparams:
118                 sendError(HttpServletResponse.SC_BAD_REQUEST, e);
119                 break;
120             case HttpServletResponse.SC_NOT_FOUND:
121             case ftErrors.pagenotfound:
122                 sendError(HttpServletResponse.SC_NOT_FOUND, e);
123                 break;
124             case HttpServletResponse.SC_FORBIDDEN:
125             case ftErrors.noprivs:
126                 sendError(HttpServletResponse.SC_FORBIDDEN, e);
127                 break;
128             default:
129                 sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
130                 break;
131         }
132     }
133 
134 }