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.DeprecationUtil;
37 import info.magnolia.cms.util.RequestFormUtil;
38
39 import java.io.PrintWriter;
40 import java.io.StringWriter;
41 import java.lang.reflect.InvocationTargetException;
42 import java.lang.reflect.Method;
43 import java.util.HashMap;
44 import java.util.Map;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
49 import org.apache.commons.beanutils.BeanUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53
54
55
56
57
58 @Deprecated
59 public abstract class MVCServletHandlerImpl implements MVCServletHandler {
60
61 protected static final String VIEW_ERROR = "error";
62
63 protected static final String VIEW_SUCCESS = "success";
64
65 private static Logger log = LoggerFactory.getLogger(MVCServletHandlerImpl.class);
66
67 protected HttpServletRequest request;
68
69 protected HttpServletResponse response;
70
71 protected Throwable exception;
72
73 private String name;
74
75 private String command;
76
77 protected MVCServletHandlerImpl(String name, HttpServletRequest request, HttpServletResponse response) {
78 this.name = name;
79 this.setRequest(request);
80 this.setResponse(response);
81 }
82
83 @Override
84 public void init() {
85 DeprecationUtil.isDeprecated();
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 } catch (Exception e) {
99 log.error("can't set properties on the handler", e);
100 }
101 }
102
103
104
105
106 @Override
107 public String getName() {
108 return name;
109 }
110
111
112
113
114 @Override
115 public String execute(String command) {
116 String view = VIEW_ERROR;
117 Method method;
118
119 try {
120 method = this.getClass().getMethod(command);
121
122 view = (String) method.invoke(this);
123 } catch (InvocationTargetException e) {
124 log.error("can't call command: {}", command, e.getTargetException());
125 exception = e.getTargetException();
126 } catch (Exception e) {
127 log.error("can't call command: {}", command, e);
128 exception = e;
129 }
130
131 return view;
132 }
133
134
135
136
137 public void setRequest(HttpServletRequest request) {
138 this.request = request;
139 }
140
141
142
143
144 public HttpServletRequest getRequest() {
145 return request;
146 }
147
148
149
150
151 public void setResponse(HttpServletResponse response) {
152 this.response = response;
153 }
154
155
156
157
158 public HttpServletResponse getResponse() {
159 return response;
160 }
161
162
163
164
165 @Override
166 public String getCommand() {
167 return this.command;
168 }
169
170
171
172
173 public void setCommand(String command) {
174 this.command = command;
175 }
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 }