1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29
30
31
32
33
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
45 RenderUtils.recordBaseCompositionalDependencies(ics);
46
47
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
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
70 action.handleRequest(ics);
71 LOG.trace("Request handling complete");
72 }
73
74
75
76
77 protected abstract ActionNameResolver getActionNameResolver();
78
79
80
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
93
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
105
106
107
108
109
110
111
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 }