Rev 1863 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1576 | jmachado | 1 | package pt.estgp.estgweb.web.controllers.utils; |
2 | |||
3 | import jomm.utils.MessageResources; |
||
4 | import org.apache.log4j.Logger; |
||
5 | import org.apache.struts.action.*; |
||
6 | import org.json.JSONArray; |
||
7 | import org.json.JSONException; |
||
8 | import org.json.JSONObject; |
||
9 | import pt.estgp.estgweb.web.controllers.ApplicationDispatchController; |
||
1732 | jmachado | 10 | import pt.utl.ist.berserk.logic.serviceManager.exceptions.FilterChainFailedException; |
1576 | jmachado | 11 | |
12 | import javax.servlet.ServletException; |
||
13 | import javax.servlet.http.HttpServletRequest; |
||
14 | import javax.servlet.http.HttpServletResponse; |
||
15 | import java.lang.reflect.InvocationTargetException; |
||
16 | import java.lang.reflect.Method; |
||
17 | import java.util.Iterator; |
||
18 | |||
19 | /** |
||
20 | * Created by jorgemachado on 27/12/16. |
||
21 | * |
||
22 | * Classes extending this Controller must implment the following API in each service |
||
23 | * and must define the following service names in parameter : |
||
24 | * |
||
25 | * ->"serviceJson"<- |
||
26 | * |
||
27 | * when dispatch = executeService |
||
28 | * JSONObject serviceName(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Throwable; |
||
29 | * |
||
30 | * |
||
31 | * when dispatch = executeServiceOkFail |
||
32 | * Boolean serviceName(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Throwable; |
||
33 | * |
||
34 | */ |
||
35 | public abstract class AbstractWidgetAjaxController extends ApplicationDispatchController |
||
36 | { |
||
37 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(AbstractWidgetAjaxController.class); |
||
38 | |||
39 | public static final 1.5.0/docs/api/java/lang/String.html">String WIDGET_REPONSE_KEY = "pt.estgp.estgweb.web.controllers.utils.AbstractWidgetAjaxController"; |
||
40 | |||
41 | |||
42 | public ActionForward executeService( |
||
43 | ActionMapping mapping, |
||
44 | ActionForm form, |
||
45 | HttpServletRequest request, |
||
46 | HttpServletResponse response) |
||
47 | throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException |
||
48 | { |
||
49 | try |
||
50 | { |
||
51 | 1.5.0/docs/api/java/lang/String.html">String serviceJson = request.getParameter("serviceJson"); |
||
1732 | jmachado | 52 | logger.info("Calling abstract widget service: " + serviceJson); |
1576 | jmachado | 53 | |
54 | 1.5.0/docs/api/java/lang/reflect/Method.html">Method serviceChild = this.getClass().getMethod(serviceJson,new 1.5.0/docs/api/java/lang/Class.html">Class[]{ActionForm.class,HttpServletRequest.class,HttpServletResponse.class}); |
||
55 | |||
56 | JSONObject responseJson = (JSONObject) serviceChild.invoke(this, form, request, response); |
||
57 | |||
58 | prepareResponse(responseJson,request,response); |
||
59 | |||
60 | return mapping.findForward("JSON_WidgetAjax_RESPONSE"); |
||
61 | } |
||
62 | catch(1.5.0/docs/api/java/lang/reflect/InvocationTargetException.html">InvocationTargetException e) |
||
63 | { |
||
1732 | jmachado | 64 | |
65 | 1.5.0/docs/api/java/lang/Throwable.html">Throwable cause = e; |
||
66 | if(e.getTargetException() != null) |
||
67 | { |
||
68 | cause = e.getTargetException(); |
||
69 | if(e.getTargetException().getCause() != null) |
||
70 | cause = e.getTargetException().getCause(); |
||
71 | } |
||
72 | prepareError(cause,request,response); |
||
1576 | jmachado | 73 | return mapping.findForward("JSON_WidgetAjax_RESPONSE_ERROR"); |
74 | } |
||
75 | catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
||
76 | { |
||
1732 | jmachado | 77 | if(e instanceof FilterChainFailedException) |
78 | { |
||
79 | logger.error(e,e); |
||
80 | } |
||
81 | else |
||
82 | { |
||
83 | prepareError(e,request,response); |
||
84 | } |
||
1576 | jmachado | 85 | return mapping.findForward("JSON_WidgetAjax_RESPONSE_ERROR"); |
86 | } |
||
87 | } |
||
88 | |||
89 | public ActionForward executeSimpleServiceOkFail( |
||
90 | ActionMapping mapping, |
||
91 | ActionForm form, |
||
92 | HttpServletRequest request, |
||
93 | HttpServletResponse response) |
||
94 | throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException |
||
95 | { |
||
96 | try |
||
97 | { |
||
98 | 1.5.0/docs/api/java/lang/String.html">String serviceJson = request.getParameter("serviceJson"); |
||
1732 | jmachado | 99 | logger.info("Calling abstract widget service: " + serviceJson); |
1576 | jmachado | 100 | 1.5.0/docs/api/java/lang/reflect/Method.html">Method serviceChild = this.getClass().getMethod(serviceJson,new 1.5.0/docs/api/java/lang/Class.html">Class[]{ActionForm.class,HttpServletRequest.class,HttpServletResponse.class}); |
101 | |||
102 | 1.5.0/docs/api/java/lang/Boolean.html">Boolean responseBoolean = (1.5.0/docs/api/java/lang/Boolean.html">Boolean) serviceChild.invoke(this, form, request, response); |
||
103 | |||
104 | if(responseBoolean) |
||
105 | { |
||
106 | /** |
||
107 | * RESPONSE IS A JSON OBJECT LIKE THIS: |
||
108 | * { |
||
109 | * "service" : "ok" |
||
110 | * "messages" : "...." |
||
111 | * } |
||
112 | */ |
||
113 | JSONObject jsonObject = new JSONObject(); |
||
114 | jsonObject.put("service-simple-response","ok"); |
||
115 | prepareResponse(jsonObject, request, response); |
||
116 | return mapping.findForward("JSON_WidgetAjax_RESPONSE"); |
||
117 | } |
||
118 | else |
||
119 | { |
||
120 | /** |
||
121 | * RESPONSE IS A JSON OBJECT LIKE THIS: |
||
122 | * { |
||
123 | * "service" : "fail", |
||
124 | * "errors" : "...." |
||
125 | * } |
||
126 | */ |
||
127 | JSONObject jsonObject = new JSONObject(); |
||
128 | jsonObject.put("service-simple-response","fail"); |
||
129 | prepareResponse(jsonObject, request, response); |
||
130 | return mapping.findForward("JSON_WidgetAjax_RESPONSE"); |
||
131 | } |
||
132 | |||
133 | } |
||
134 | catch(1.5.0/docs/api/java/lang/reflect/InvocationTargetException.html">InvocationTargetException e) |
||
135 | { |
||
1732 | jmachado | 136 | 1.5.0/docs/api/java/lang/Throwable.html">Throwable cause = e.getCause(); |
137 | if(e.getTargetException() != null) |
||
138 | { |
||
139 | cause = e.getTargetException(); |
||
140 | if(e.getTargetException().getCause() != null) |
||
141 | cause = e.getTargetException().getCause(); |
||
142 | } |
||
143 | prepareError(cause,request,response); |
||
1576 | jmachado | 144 | return mapping.findForward("JSON_WidgetAjax_RESPONSE_ERROR"); |
145 | } |
||
146 | catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
||
147 | { |
||
1732 | jmachado | 148 | if(e instanceof FilterChainFailedException) |
149 | { |
||
150 | logger.error(e,e); |
||
151 | } |
||
152 | else |
||
153 | { |
||
154 | prepareError(e,request,response); |
||
155 | } |
||
1576 | jmachado | 156 | return mapping.findForward("JSON_WidgetAjax_RESPONSE_ERROR"); |
157 | } |
||
158 | |||
159 | } |
||
160 | |||
161 | |||
162 | /** |
||
163 | * RESPONSE IS A JSON OBJECT LIKE THIS: |
||
164 | * { |
||
165 | * "service" : "ok" OR "fail" if has errors, |
||
166 | * "response" : { userResponseJson }, |
||
167 | * "messages" : |
||
168 | * [ |
||
169 | * "MESSAGE 1", |
||
170 | * "MESSAGE 2", |
||
171 | * ], |
||
172 | * "errors" : |
||
173 | * [ |
||
174 | * "ERROR 1", |
||
175 | * "ERROR 2", |
||
176 | * ] |
||
177 | * } |
||
178 | * @param object |
||
179 | * @param request |
||
180 | * @param response |
||
181 | * @throws JSONException |
||
182 | */ |
||
183 | private void prepareResponse(JSONObject object,HttpServletRequest request,HttpServletResponse response) |
||
184 | throws JSONException |
||
185 | { |
||
186 | ActionMessages messages = super.getMessages(request); |
||
187 | ActionMessages errors = super.getErrors(request); |
||
188 | JSONObject responseObject = new JSONObject(); |
||
189 | if(object.has("service-simple-response")) |
||
190 | { |
||
191 | responseObject.put("service",object.getString("service-simple-response")); |
||
192 | responseObject.put("response",object.getString("service-simple-response")); |
||
193 | |||
194 | } |
||
195 | else |
||
196 | { |
||
197 | 1.5.0/docs/api/java/lang/String.html">String service = errors == null || errors.isEmpty() ? "ok" : "fail"; |
||
198 | responseObject.put("service",service); |
||
199 | responseObject.put("response",object); |
||
200 | } |
||
201 | |||
202 | |||
203 | |||
204 | |||
205 | |||
206 | if(messages != null) |
||
207 | { |
||
208 | JSONArray messagesJson = new JSONArray(); |
||
209 | responseObject.put("messages",messagesJson); |
||
210 | 1.5.0/docs/api/java/util/Iterator.html">Iterator reports = messages.get(); |
||
211 | while (reports.hasNext()) |
||
212 | { |
||
213 | ActionMessage report = (ActionMessage) reports.next(); |
||
1577 | jmachado | 214 | if(report instanceof StringMessage) |
215 | { |
||
216 | 1.5.0/docs/api/java/lang/String.html">String messageTranslated = ((StringMessage)report).toString(); |
||
217 | if(messageTranslated != null) |
||
218 | messagesJson.put(messageTranslated); |
||
219 | } |
||
220 | else |
||
221 | { |
||
222 | 1.5.0/docs/api/java/lang/String.html">String messageTranslated = MessageResources.getMessage(request,report.getKey(),report.getValues()); |
||
223 | if(messageTranslated != null) |
||
224 | messagesJson.put(messageTranslated); |
||
225 | } |
||
226 | |||
1576 | jmachado | 227 | } |
228 | } |
||
229 | if(errors != null) |
||
230 | { |
||
231 | JSONArray errorsJson = new JSONArray(); |
||
232 | responseObject.put("errors",errorsJson); |
||
233 | 1.5.0/docs/api/java/util/Iterator.html">Iterator reports = errors.get(); |
||
234 | while (reports.hasNext()) |
||
235 | { |
||
236 | ActionMessage report = (ActionMessage) reports.next(); |
||
1577 | jmachado | 237 | if(report instanceof StringMessage) |
238 | { |
||
239 | 1.5.0/docs/api/java/lang/String.html">String messageTranslated = ((StringMessage)report).toString(); |
||
240 | if(messageTranslated != null) |
||
241 | errorsJson.put(messageTranslated); |
||
242 | } |
||
243 | else |
||
244 | { |
||
245 | 1.5.0/docs/api/java/lang/String.html">String messageTranslated = MessageResources.getMessage(request,report.getKey(),report.getValues()); |
||
246 | if(messageTranslated != null) |
||
247 | errorsJson.put(messageTranslated); |
||
248 | } |
||
1576 | jmachado | 249 | } |
250 | } |
||
251 | request.setAttribute(WIDGET_REPONSE_KEY, responseObject); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * RESPONSE IS A JSON OBJECT LIKE THIS: |
||
256 | * { |
||
257 | * "service" : "error" |
||
258 | * "exception" : "string with exception.toString()" |
||
259 | * } |
||
260 | * @param e |
||
261 | * @param request |
||
262 | * @param response |
||
263 | * @throws JSONException |
||
264 | */ |
||
1704 | jmachado | 265 | public static void prepareError(1.5.0/docs/api/java/lang/Throwable.html">Throwable e,HttpServletRequest request,HttpServletResponse response) throws JSONException |
1576 | jmachado | 266 | { |
267 | logger.error(e,e); |
||
1577 | jmachado | 268 | //request.setAttribute(WIDGET_REPONSE_KEY,e.toString()); |
1576 | jmachado | 269 | JSONObject errorObject = new JSONObject(); |
270 | errorObject.put("service","error"); |
||
271 | errorObject.put("exception",e.toString()); |
||
272 | request.setAttribute(WIDGET_REPONSE_KEY,errorObject); |
||
273 | } |
||
274 | } |