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.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
30
31
32
33
34
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
46 RenderUtils.recordBaseCompositionalDependencies(ics);
47
48
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
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
71 action.handleRequest(ics);
72 LOG.trace("Request handling complete");
73 }
74
75
76
77
78 protected abstract ActionNameResolver getActionNameResolver();
79
80
81
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
94
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
106
107
108
109
110
111
112
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 }