Rev 1312 | Rev 1502 | 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 com.owlike.genson.TransformationException; |
4 | import jomm.dao.impl.AbstractDao; |
||
5 | import org.apache.log4j.Logger; |
||
6 | import org.json.JSONArray; |
||
7 | import org.json.JSONException; |
||
8 | import org.json.JSONObject; |
||
9 | import pt.estgp.estgweb.domain.UserSession; |
||
10 | import pt.estgp.estgweb.services.courses.CoursesService; |
||
11 | import pt.estgp.estgweb.services.courses.xsd.Curso; |
||
12 | import pt.estgp.estgweb.web.UserSessionProxy; |
||
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.JAXBException; |
||
20 | import javax.xml.bind.Marshaller; |
||
21 | import javax.xml.bind.Unmarshaller; |
||
1312 | jmachado | 22 | import java.io.IOException; |
1500 | jmachado | 23 | import java.io.PrintWriter; |
24 | import java.io.StringReader; |
||
1312 | jmachado | 25 | |
26 | /** |
||
27 | * Created by jorgemachado on 02/02/16. |
||
28 | */ |
||
29 | public class JsonHandler extends HttpServlet |
||
30 | { |
||
1500 | jmachado | 31 | 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); |
32 | |||
1312 | jmachado | 33 | /** Handles the HTTP <code>GET</code> method. |
34 | * @param request servlet request |
||
35 | * @param response servlet response |
||
36 | */ |
||
37 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
||
1500 | jmachado | 38 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
39 | processRequestSafe(request, response); |
||
1312 | jmachado | 40 | } |
41 | |||
42 | /** Handles the HTTP <code>POST</code> method. |
||
43 | * @param request servlet request |
||
44 | * @param response servlet response |
||
45 | */ |
||
46 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
||
47 | throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
1500 | jmachado | 48 | processRequestSafe(request, response); |
1312 | jmachado | 49 | } |
50 | |||
1500 | jmachado | 51 | |
52 | private void processRequestSafe(HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
53 | response.setContentType("application/json"); |
||
54 | try { |
||
55 | processRequest(request, response); |
||
56 | } catch (TransformationException e) |
||
57 | { |
||
58 | response.getWriter().print(e); |
||
59 | response.getWriter().print("See log for details:"); |
||
60 | logger.error(e,e); |
||
61 | } |
||
62 | catch (JAXBException e) |
||
63 | { |
||
64 | response.getWriter().print(e); |
||
65 | response.getWriter().print("See log for details:"); |
||
66 | logger.error(e,e); |
||
67 | } |
||
68 | catch (JSONException e) |
||
69 | { |
||
70 | response.getWriter().print(e); |
||
71 | response.getWriter().print("See log for details:"); |
||
72 | logger.error(e, e); |
||
73 | } |
||
1312 | jmachado | 74 | } |
75 | |||
1500 | jmachado | 76 | private void processRequest(HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JAXBException, JSONException { |
1312 | jmachado | 77 | |
1500 | jmachado | 78 | |
79 | 1.5.0/docs/api/java/lang/String.html">String service = request.getParameter("service"); |
||
80 | if(service == null) |
||
81 | { |
||
82 | printServices(response.getWriter()); |
||
83 | } |
||
84 | else if(service.equals("listCourses")) |
||
85 | { |
||
86 | 1.5.0/docs/api/java/lang/String.html">String school = request.getParameter("school"); |
||
87 | 1.5.0/docs/api/java/lang/String.html">String type = request.getParameter("type"); |
||
88 | |||
89 | AbstractDao.getCurrentSession().beginTransaction(); |
||
90 | JSONObject obj = new CoursesService().getActiveCoursesForJsonApi(school,type); |
||
91 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
92 | response.getWriter().write(obj.toString()); |
||
93 | } |
||
94 | else if(service.equals("getCourse") && request.getParameter("code") != null) |
||
95 | { |
||
96 | 1.5.0/docs/api/java/lang/String.html">String code = request.getParameter("code"); |
||
97 | |||
98 | |||
99 | AbstractDao.getCurrentSession().beginTransaction(); |
||
100 | JSONObject obj = new CoursesService().getCourseDetailForJsonApi(code); |
||
101 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
102 | response.getWriter().write(obj.toString()); |
||
103 | } |
||
104 | else if(service.equals("getStudiesPlanXml") && request.getParameter("code") != null) |
||
105 | { |
||
106 | 1.5.0/docs/api/java/lang/String.html">String code = request.getParameter("code"); |
||
107 | response.setContentType("application/xml"); |
||
108 | response.setCharacterEncoding("UTF-8"); |
||
109 | AbstractDao.getCurrentSession().beginTransaction(); |
||
110 | 1.5.0/docs/api/java/lang/String.html">String xml = new CoursesService().getCourseStudiesPlanXml(code); |
||
111 | JAXBContext jc = JAXBContext.newInstance(Curso.class); |
||
112 | Unmarshaller unmarshaller = jc.createUnmarshaller(); |
||
113 | Curso curso = (Curso) unmarshaller.unmarshal(new 1.5.0/docs/api/java/io/StringReader.html">StringReader(xml)); |
||
114 | Marshaller marshaller = jc.createMarshaller(); |
||
115 | marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); |
||
116 | marshaller.marshal(curso, response.getOutputStream()); |
||
117 | //SETTING XML in COURSE STUDIES PLAN |
||
118 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
119 | |||
120 | } |
||
121 | else if(service.equals("syncSystemPlans") && request.getParameter("systemUrl") != null && request.getParameter("setActive") != null) |
||
122 | { |
||
123 | UserSession sess = UserSessionProxy.loadUserSessionFromRequest(request); |
||
124 | response.setContentType("application/xml"); |
||
125 | if(sess.getUser() != null && sess.getUser().isSuperuserOrAdmin()) |
||
126 | { |
||
127 | 1.5.0/docs/api/java/lang/String.html">String systemUrl = request.getParameter("systemUrl"); |
||
128 | boolean setActive = 1.5.0/docs/api/java/lang/Boolean.html">Boolean.parseBoolean(request.getParameter("setActive")); |
||
129 | response.setContentType("application/xml"); |
||
130 | AbstractDao.getCurrentSession().beginTransaction(); |
||
131 | 1.5.0/docs/api/java/lang/String.html">String log = new CoursesService().sincronizeCoursesStudiesPlans(systemUrl,setActive); |
||
132 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
133 | response.getWriter().write("<response><status>OK</status><log>" + log + "</log></response>"); |
||
134 | } |
||
135 | else |
||
136 | { |
||
137 | response.getWriter().write("<error>Permission Denied</error>"); |
||
138 | } |
||
139 | } |
||
140 | else |
||
141 | { |
||
142 | printServices(response.getWriter()); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | 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 |
||
147 | { |
||
148 | JSONObject obj = new JSONObject(); |
||
149 | |||
150 | JSONArray availableServices = new JSONArray(); |
||
151 | |||
152 | JSONObject service2 = new JSONObject(); |
||
153 | service2.put("name","getCourse"); |
||
154 | service2.put("parameterCode","code"); |
||
155 | service2.put("parameterCodeType","integer"); |
||
156 | service2.put("exampleCall1","/wsjson/api?service=getCourse&code=9119"); |
||
157 | |||
158 | JSONObject service = new JSONObject(); |
||
159 | service.put("name","listCourses"); |
||
160 | service.put("parameterType","type"); |
||
161 | service.put("parameterTypeEnumValues","Licenciaturas,Mestrados,PosGraduacoes,CET,CTeSP or none for all"); |
||
162 | service.put("parameterSchool","school"); |
||
163 | service.put("parameterSchoolEnumValues","ESTG,ESECS,ESS,ESAE or none for all"); |
||
164 | service.put("exampleCall1","/wsjson/api?service=listCourses"); |
||
165 | service.put("exampleCall2","/wsjson/api?service=listCourses&type=Licenciaturas&school=ESTG"); |
||
166 | |||
167 | availableServices.put(service); |
||
168 | availableServices.put(service2); |
||
169 | JSONObject response = new JSONObject(); |
||
170 | obj.put("response",response); |
||
171 | response.put("status","invalid service"); |
||
172 | response.put("availableServices",availableServices); |
||
173 | out.write(obj.toString()); |
||
174 | } |
||
175 | |||
1312 | jmachado | 176 | } |