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