Rev 1814 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1312 | jmachado | 1 | package pt.estgp.estgweb.web.json; |
2 | |||
1500 | jmachado | 3 | import org.apache.log4j.Logger; |
4 | import org.json.JSONArray; |
||
5 | import org.json.JSONException; |
||
6 | import org.json.JSONObject; |
||
7 | import pt.estgp.estgweb.domain.UserSession; |
||
8 | import pt.estgp.estgweb.services.courses.xsd.Curso; |
||
9 | import pt.estgp.estgweb.web.UserSessionProxy; |
||
1502 | jmachado | 10 | import pt.estgp.estgweb.web.utils.RequestUtils; |
11 | import pt.utl.ist.berserk.logic.serviceManager.IServiceManager; |
||
12 | import pt.utl.ist.berserk.logic.serviceManager.ServiceManager; |
||
1500 | jmachado | 13 | |
1312 | jmachado | 14 | import javax.servlet.ServletException; |
15 | import javax.servlet.http.HttpServlet; |
||
16 | import javax.servlet.http.HttpServletRequest; |
||
17 | import javax.servlet.http.HttpServletResponse; |
||
1500 | jmachado | 18 | import javax.xml.bind.JAXBContext; |
19 | import javax.xml.bind.Marshaller; |
||
20 | import javax.xml.bind.Unmarshaller; |
||
1312 | jmachado | 21 | import java.io.IOException; |
1500 | jmachado | 22 | import java.io.PrintWriter; |
23 | import java.io.StringReader; |
||
1312 | jmachado | 24 | |
25 | /** |
||
26 | * Created by jorgemachado on 02/02/16. |
||
27 | */ |
||
28 | public class JsonHandler extends HttpServlet |
||
29 | { |
||
1500 | jmachado | 30 | 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(JsonHandler.class); |
31 | |||
1312 | jmachado | 32 | /** Handles the HTTP <code>GET</code> method. |
33 | * @param request servlet request |
||
34 | * @param response servlet response |
||
35 | */ |
||
36 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
||
1500 | jmachado | 37 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
38 | processRequestSafe(request, response); |
||
1312 | jmachado | 39 | } |
40 | |||
41 | /** Handles the HTTP <code>POST</code> method. |
||
42 | * @param request servlet request |
||
43 | * @param response servlet response |
||
44 | */ |
||
45 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
||
46 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
1500 | jmachado | 47 | processRequestSafe(request, response); |
1312 | jmachado | 48 | } |
49 | |||
1500 | jmachado | 50 | |
51 | private void processRequestSafe(HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
52 | response.setContentType("application/json"); |
||
53 | try { |
||
54 | processRequest(request, response); |
||
1776 | jmachado | 55 | |
56 | }catch (JSONException e) |
||
1500 | jmachado | 57 | { |
58 | response.getWriter().print(e); |
||
59 | response.getWriter().print("See log for details:"); |
||
60 | logger.error(e, e); |
||
1502 | jmachado | 61 | } catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable throwable) { |
62 | response.getWriter().print(throwable); |
||
63 | response.getWriter().print("See log for details:"); |
||
64 | logger.error(throwable, throwable); |
||
1500 | jmachado | 65 | } |
1312 | jmachado | 66 | } |
67 | |||
1502 | jmachado | 68 | private void processRequest(HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable { |
1312 | jmachado | 69 | |
1502 | jmachado | 70 | IServiceManager sm = ServiceManager.getInstance(); |
1500 | jmachado | 71 | |
72 | 1.5.0/docs/api/java/lang/String.html">String service = request.getParameter("service"); |
||
73 | if(service == null) |
||
74 | { |
||
75 | printServices(response.getWriter()); |
||
76 | } |
||
77 | else if(service.equals("listCourses")) |
||
78 | { |
||
79 | 1.5.0/docs/api/java/lang/String.html">String school = request.getParameter("school"); |
||
80 | 1.5.0/docs/api/java/lang/String.html">String type = request.getParameter("type"); |
||
1502 | jmachado | 81 | JSONObject obj = (JSONObject) sm.execute(RequestUtils.getRequester(request, response), "GetActiveCoursesForJsonApi", new 5+0%2Fdocs%2Fapi+Object">Object[]{school, type}); |
1500 | jmachado | 82 | response.getWriter().write(obj.toString()); |
83 | } |
||
84 | else if(service.equals("getCourse") && request.getParameter("code") != null) |
||
85 | { |
||
86 | 1.5.0/docs/api/java/lang/String.html">String code = request.getParameter("code"); |
||
1502 | jmachado | 87 | JSONObject obj = (JSONObject) sm.execute(RequestUtils.getRequester(request, response), "GetCourseDetailForJsonApi", new 5+0%2Fdocs%2Fapi+Object">Object[]{code}); |
1500 | jmachado | 88 | response.getWriter().write(obj.toString()); |
89 | } |
||
90 | else if(service.equals("getStudiesPlanXml") && request.getParameter("code") != null) |
||
91 | { |
||
92 | 1.5.0/docs/api/java/lang/String.html">String code = request.getParameter("code"); |
||
1502 | jmachado | 93 | 1.5.0/docs/api/java/lang/String.html">String renew = request.getParameter("renew"); |
1500 | jmachado | 94 | response.setContentType("application/xml"); |
1502 | jmachado | 95 | response.setCharacterEncoding("UTF-8"); |
96 | 1.5.0/docs/api/java/lang/String.html">String xml = (1.5.0/docs/api/java/lang/String.html">String) sm.execute(RequestUtils.getRequester(request, response), "GetCourseStudiesPlanXml", new 5+0%2Fdocs%2Fapi+Object">Object[]{code,renew}); |
||
97 | |||
1500 | jmachado | 98 | JAXBContext jc = JAXBContext.newInstance(Curso.class); |
99 | Unmarshaller unmarshaller = jc.createUnmarshaller(); |
||
100 | Curso curso = (Curso) unmarshaller.unmarshal(new 1.5.0/docs/api/java/io/StringReader.html">StringReader(xml)); |
||
101 | Marshaller marshaller = jc.createMarshaller(); |
||
102 | marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); |
||
103 | marshaller.marshal(curso, response.getOutputStream()); |
||
104 | //SETTING XML in COURSE STUDIES PLAN |
||
105 | } |
||
106 | else if(service.equals("syncSystemPlans") && request.getParameter("systemUrl") != null && request.getParameter("setActive") != null) |
||
107 | { |
||
108 | UserSession sess = UserSessionProxy.loadUserSessionFromRequest(request); |
||
109 | response.setContentType("application/xml"); |
||
110 | if(sess.getUser() != null && sess.getUser().isSuperuserOrAdmin()) |
||
111 | { |
||
112 | 1.5.0/docs/api/java/lang/String.html">String systemUrl = request.getParameter("systemUrl"); |
||
113 | boolean setActive = 1.5.0/docs/api/java/lang/Boolean.html">Boolean.parseBoolean(request.getParameter("setActive")); |
||
114 | response.setContentType("application/xml"); |
||
1502 | jmachado | 115 | 1.5.0/docs/api/java/lang/String.html">String log = (1.5.0/docs/api/java/lang/String.html">String) sm.execute(RequestUtils.getRequester(request, response), "SincronizeCoursesStudiesPlans", new 5+0%2Fdocs%2Fapi+Object">Object[]{systemUrl,setActive}); |
1500 | jmachado | 116 | response.getWriter().write("<response><status>OK</status><log>" + log + "</log></response>"); |
117 | } |
||
118 | else |
||
119 | { |
||
120 | response.getWriter().write("<error>Permission Denied</error>"); |
||
121 | } |
||
122 | } |
||
123 | else |
||
124 | { |
||
125 | printServices(response.getWriter()); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | public void printServices(1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter out) throws JSONException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
130 | { |
||
131 | JSONObject obj = new JSONObject(); |
||
132 | |||
133 | JSONArray availableServices = new JSONArray(); |
||
134 | |||
1830 | jmachado | 135 | |
136 | |||
1500 | jmachado | 137 | JSONObject service2 = new JSONObject(); |
138 | service2.put("name","getCourse"); |
||
139 | service2.put("parameterCode","code"); |
||
140 | service2.put("parameterCodeType","integer"); |
||
141 | service2.put("exampleCall1","/wsjson/api?service=getCourse&code=9119"); |
||
142 | |||
143 | JSONObject service = new JSONObject(); |
||
144 | service.put("name","listCourses"); |
||
145 | service.put("parameterType","type"); |
||
146 | service.put("parameterTypeEnumValues","Licenciaturas,Mestrados,PosGraduacoes,CET,CTeSP or none for all"); |
||
147 | service.put("parameterSchool","school"); |
||
148 | service.put("parameterSchoolEnumValues","ESTG,ESECS,ESS,ESAE or none for all"); |
||
149 | service.put("exampleCall1","/wsjson/api?service=listCourses"); |
||
150 | service.put("exampleCall2","/wsjson/api?service=listCourses&type=Licenciaturas&school=ESTG"); |
||
151 | |||
1814 | jmachado | 152 | |
1500 | jmachado | 153 | availableServices.put(service); |
154 | availableServices.put(service2); |
||
1830 | jmachado | 155 | |
1500 | jmachado | 156 | JSONObject response = new JSONObject(); |
157 | obj.put("response",response); |
||
158 | response.put("status","invalid service"); |
||
159 | response.put("availableServices",availableServices); |
||
160 | out.write(obj.toString()); |
||
161 | } |
||
162 | |||
1312 | jmachado | 163 | } |