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 private static Logger log = LoggerFactory.getLogger(MVCServletHandlerImpl.class);
65
66 protected HttpServletRequest request;
67
68 protected HttpServletResponse response;
69
70 protected Throwable exception;
71
72 private String name;
73
74 private String command;
75
76 protected MVCServletHandlerImpl(String name, HttpServletRequest request, HttpServletResponse response) {
77 this.name = name;
78 this.setRequest(request);
79 this.setResponse(response);
80 }
81
82 @Override
83 public void init() {
84 populateFromRequest(this);
85 }
86
87 protected void populateFromRequest(Object bean) {
88 RequestFormUtil requestFormUtil = new RequestFormUtil(this.getRequest());
89 Map parameters = new HashMap();
90 parameters.putAll(requestFormUtil.getParameters());
91 parameters.putAll(requestFormUtil.getDocuments());
92
93 try {
94
95 BeanUtils.populate(bean, parameters);
96 }
97 catch (Exception e) {
98 log.error("can't set properties on the handler", e);
99 }
100 }
101
102
103
104
105 @Override
106 public String getName() {
107 return name;
108 }
109
110
111
112
113 @Override
114 public String execute(String command) {
115 String view = VIEW_ERROR;
116 Method method;
117
118 try {
119 method = this.getClass().getMethod(command, new Class[]{});
120
121 view = (String) method.invoke(this, new Object[]{});
122 }
123 catch (InvocationTargetException e) {
124 log.error("can't call command: " + command, e.getTargetException());
125 exception = e.getTargetException();
126 }
127 catch (Exception e) {
128 log.error("can't call command: " + command, e);
129 exception = e;
130 }
131
132 return view;
133 }
134
135
136
137
138 public void setRequest(HttpServletRequest request) {
139 this.request = request;
140 }
141
142
143
144
145 public HttpServletRequest getRequest() {
146 return request;
147 }
148
149
150
151
152 public void setResponse(HttpServletResponse response) {
153 this.response = response;
154 }
155
156
157
158
159 public HttpServletResponse getResponse() {
160 return response;
161 }
162
163
164
165
166 @Override
167 public String getCommand() {
168 return this.command;
169 }
170
171
172
173
174 public void setCommand(String command) {
175 this.command = command;
176 }
177
178
179
180
181
182 public Throwable getException() {
183 return this.exception;
184 }
185
186
187
188
189 public String getExceptionStackTrace() {
190 if (this.exception == null) {
191 return null;
192 }
193 StringWriter writer = new StringWriter();
194 this.exception.printStackTrace(new PrintWriter(writer));
195 return writer.toString();
196 }
197 }