1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.servlets;
35
36 import info.magnolia.cms.util.RequestFormUtil;
37
38 import java.io.PrintWriter;
39 import java.io.StringWriter;
40 import java.lang.reflect.InvocationTargetException;
41 import java.lang.reflect.Method;
42 import java.util.HashMap;
43 import java.util.Map;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48 import org.apache.commons.beanutils.BeanUtils;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52
53
54
55
56
57
58 public abstract class MVCServletHandlerImpl implements MVCServletHandler {
59
60 protected static final String VIEW_ERROR = "error";
61
62 protected static final String VIEW_SUCCESS = "success";
63
64
65
66
67 private static Logger log = LoggerFactory.getLogger(MVCServletHandlerImpl.class);
68
69 protected HttpServletRequest request;
70
71 protected HttpServletResponse response;
72
73 protected Throwable exception;
74
75 private String name;
76
77 private String command;
78
79 protected MVCServletHandlerImpl(String name, HttpServletRequest request, HttpServletResponse response) {
80 this.name = name;
81 this.setRequest(request);
82 this.setResponse(response);
83 }
84
85 public void init() {
86 populateFromRequest(this);
87 }
88
89 protected void populateFromRequest(Object bean) {
90 RequestFormUtil requestFormUtil = new RequestFormUtil(this.getRequest());
91 Map parameters = new HashMap();
92 parameters.putAll(requestFormUtil.getParameters());
93 parameters.putAll(requestFormUtil.getDocuments());
94
95 try {
96
97 BeanUtils.populate(bean, parameters);
98 }
99 catch (Exception e) {
100 log.error("can't set properties on the handler", e);
101 }
102 }
103
104
105
106
107 public String getName() {
108 return name;
109 }
110
111
112
113
114
115
116 public String execute(String command) {
117 String view = VIEW_ERROR;
118 Method method;
119
120 try {
121 method = this.getClass().getMethod(command, new Class[]{});
122
123 view = (String) method.invoke(this, new Object[]{});
124 }
125 catch (InvocationTargetException e) {
126 log.error("can't call command: " + command, e.getTargetException());
127 exception = e.getTargetException();
128 }
129 catch (Exception e) {
130 log.error("can't call command: " + command, e);
131 exception = e;
132 }
133
134 return view;
135 }
136
137
138
139
140 public void setRequest(HttpServletRequest request) {
141 this.request = request;
142 }
143
144
145
146
147 public HttpServletRequest getRequest() {
148 return request;
149 }
150
151
152
153
154 public void setResponse(HttpServletResponse response) {
155 this.response = response;
156 }
157
158
159
160
161 public HttpServletResponse getResponse() {
162 return response;
163 }
164
165
166
167
168 public String getCommand() {
169 return this.command;
170 }
171
172
173
174
175 public void setCommand(String command) {
176 this.command = command;
177 }
178
179
180
181
182
183 public Throwable getException() {
184 return this.exception;
185 }
186
187
188
189
190
191 public String getExceptionStackTrace() {
192 if (this.exception == null) {
193 return null;
194 }
195 StringWriter writer = new StringWriter();
196 this.exception.printStackTrace(new PrintWriter(writer));
197 return writer.toString();
198 }
199 }