Rev 1577 | Rev 1732 | 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; |
||
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 | /** |
||
131 | * RESPONSE IS A JSON OBJECT LIKE THIS: |
||
132 | * { |
||
133 | * "service" : "ok" OR "fail" if has errors, |
||
134 | * "response" : { userResponseJson }, |
||
135 | * "messages" : |
||
136 | * [ |
||
137 | * "MESSAGE 1", |
||
138 | * "MESSAGE 2", |
||
139 | * ], |
||
140 | * "errors" : |
||
141 | * [ |
||
142 | * "ERROR 1", |
||
143 | * "ERROR 2", |
||
144 | * ] |
||
145 | * } |
||
146 | * @param object |
||
147 | * @param request |
||
148 | * @param response |
||
149 | * @throws JSONException |
||
150 | */ |
||
151 | private void prepareResponse(JSONObject object,HttpServletRequest request,HttpServletResponse response) |
||
152 | throws JSONException |
||
153 | { |
||
154 | ActionMessages messages = super.getMessages(request); |
||
155 | ActionMessages errors = super.getErrors(request); |
||
156 | JSONObject responseObject = new JSONObject(); |
||
157 | if(object.has("service-simple-response")) |
||
158 | { |
||
159 | responseObject.put("service",object.getString("service-simple-response")); |
||
160 | responseObject.put("response",object.getString("service-simple-response")); |
||
161 | |||
162 | } |
||
163 | else |
||
164 | { |
||
165 | 1.5.0/docs/api/java/lang/String.html">String service = errors == null || errors.isEmpty() ? "ok" : "fail"; |
||
166 | responseObject.put("service",service); |
||
167 | responseObject.put("response",object); |
||
168 | } |
||
169 | |||
170 | |||
171 | |||
172 | |||
173 | |||
174 | if(messages != null) |
||
175 | { |
||
176 | JSONArray messagesJson = new JSONArray(); |
||
177 | responseObject.put("messages",messagesJson); |
||
178 | 1.5.0/docs/api/java/util/Iterator.html">Iterator reports = messages.get(); |
||
179 | while (reports.hasNext()) |
||
180 | { |
||
181 | ActionMessage report = (ActionMessage) reports.next(); |
||
1577 | jmachado | 182 | if(report instanceof StringMessage) |
183 | { |
||
184 | 1.5.0/docs/api/java/lang/String.html">String messageTranslated = ((StringMessage)report).toString(); |
||
185 | if(messageTranslated != null) |
||
186 | messagesJson.put(messageTranslated); |
||
187 | } |
||
188 | else |
||
189 | { |
||
190 | 1.5.0/docs/api/java/lang/String.html">String messageTranslated = MessageResources.getMessage(request,report.getKey(),report.getValues()); |
||
191 | if(messageTranslated != null) |
||
192 | messagesJson.put(messageTranslated); |
||
193 | } |
||
194 | |||
1576 | jmachado | 195 | } |
196 | } |
||
197 | if(errors != null) |
||
198 | { |
||
199 | JSONArray errorsJson = new JSONArray(); |
||
200 | responseObject.put("errors",errorsJson); |
||
201 | 1.5.0/docs/api/java/util/Iterator.html">Iterator reports = errors.get(); |
||
202 | while (reports.hasNext()) |
||
203 | { |
||
204 | ActionMessage report = (ActionMessage) reports.next(); |
||
1577 | jmachado | 205 | if(report instanceof StringMessage) |
206 | { |
||
207 | 1.5.0/docs/api/java/lang/String.html">String messageTranslated = ((StringMessage)report).toString(); |
||
208 | if(messageTranslated != null) |
||
209 | errorsJson.put(messageTranslated); |
||
210 | } |
||
211 | else |
||
212 | { |
||
213 | 1.5.0/docs/api/java/lang/String.html">String messageTranslated = MessageResources.getMessage(request,report.getKey(),report.getValues()); |
||
214 | if(messageTranslated != null) |
||
215 | errorsJson.put(messageTranslated); |
||
216 | } |
||
1576 | jmachado | 217 | } |
218 | } |
||
219 | request.setAttribute(WIDGET_REPONSE_KEY, responseObject); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * RESPONSE IS A JSON OBJECT LIKE THIS: |
||
224 | * { |
||
225 | * "service" : "error" |
||
226 | * "exception" : "string with exception.toString()" |
||
227 | * } |
||
228 | * @param e |
||
229 | * @param request |
||
230 | * @param response |
||
231 | * @throws JSONException |
||
232 | */ |
||
1704 | jmachado | 233 | public static void prepareError(1.5.0/docs/api/java/lang/Throwable.html">Throwable e,HttpServletRequest request,HttpServletResponse response) throws JSONException |
1576 | jmachado | 234 | { |
235 | logger.error(e,e); |
||
1577 | jmachado | 236 | //request.setAttribute(WIDGET_REPONSE_KEY,e.toString()); |
1576 | jmachado | 237 | JSONObject errorObject = new JSONObject(); |
238 | errorObject.put("service","error"); |
||
239 | errorObject.put("exception",e.toString()); |
||
240 | request.setAttribute(WIDGET_REPONSE_KEY,errorObject); |
||
241 | } |
||
242 | |||
243 | } |