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.Util.ftErrors;
22  
23  import com.fatwire.gst.foundation.CSRuntimeException;
24  import com.fatwire.gst.foundation.controller.AbstractController;
25  import com.fatwire.gst.foundation.facade.RenderUtils;
26  
27  /**
28   * Dispatching Controller. Relies on ActionLocator to dispatch control to Action
29   * classes. An ActionNameResolver is used to resolve the action name on the
30   * current request.
31   * 
32   * @author Dolf Dijkstra
33   * @since May 26, 2011
34   */
35  public abstract class AbstractActionController extends AbstractController {
36  
37      public AbstractActionController() {
38          super();
39      }
40  
41      @Override
42      protected final void doExecute() {
43  
44          // record seid and eid
45          RenderUtils.recordBaseCompositionalDependencies(ics);
46  
47          // find the action locator
48          LOG.trace("Dispatcher looking for action locator");
49          final ActionLocator locator = getActionLocator();
50          if (locator == null)
51              throw new IllegalStateException("No ActionLocator returned by class " + getClass().getName());
52          if (LOG.isTraceEnabled()) {
53              LOG.trace("Using action locator: " + locator.getClass().getName());
54          }
55          ActionNameResolver resolver = getActionNameResolver();
56          if (resolver == null)
57              throw new IllegalStateException("No ActionNameResolver returned by class " + getClass().getName());
58          String actionName = resolver.resolveActionName(ics);
59          // get the action
60          final Action action = locator.getAction(ics, actionName);
61          if (LOG.isTraceEnabled()) {
62              LOG.trace("Using action: " + action.getClass().getName());
63          }
64          if (action == null)
65              throw new IllegalStateException(
66                      "No Action found. An ActionLocator should always return a Action. ActionLocation "
67                              + locator.getClass().getName() + " did not return an action for '" + actionName + "'.");
68  
69          // execute the command
70          action.handleRequest(ics);
71          LOG.trace("Request handling complete");
72      }
73  
74      /**
75       * @return the ActionNameResolver
76       */
77      protected abstract ActionNameResolver getActionNameResolver();
78  
79      /**
80       * @return the ActionLocator.
81       */
82      protected abstract ActionLocator getActionLocator();
83  
84      @SuppressWarnings("deprecation")
85      protected ServletContext getServletContext() {
86          return ics.getIServlet().getServlet().getServletContext();
87      }
88  
89      @Override
90      protected final void handleException(final Exception e) {
91          if (LOG.isTraceEnabled()) {
92              // Give developer a clue in case error pages aren't configured
93              // properly.
94              LOG.trace("Action threw an exception and an error code will be returned.  Exception: " + e, e);
95          }
96          if (e instanceof CSRuntimeException) {
97              handleCSRuntimeException((CSRuntimeException) e);
98          } else {
99              sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
100         }
101     }
102 
103     /**
104      * This method transforms errno values into http status codes and sets them
105      * using the X-Fatwire-Status header.
106      * <p/>
107      * Only some errnos are handled by this base class.
108      * <p/>
109      * More info coming soon
110      * 
111      * @param e exception
112      */
113     protected final void handleCSRuntimeException(final CSRuntimeException e) {
114         switch (e.getErrno()) {
115             case HttpServletResponse.SC_BAD_REQUEST:
116             case ftErrors.badparams:
117                 sendError(HttpServletResponse.SC_BAD_REQUEST, e);
118                 break;
119             case HttpServletResponse.SC_NOT_FOUND:
120             case ftErrors.pagenotfound:
121                 sendError(HttpServletResponse.SC_NOT_FOUND, e);
122                 break;
123             case HttpServletResponse.SC_FORBIDDEN:
124             case ftErrors.noprivs:
125                 sendError(HttpServletResponse.SC_FORBIDDEN, e);
126                 break;
127             default:
128                 sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
129                 break;
130         }
131     }
132 
133 }