root / trunk / src / java / org / lidar / LogServlet.java
History | View | Annotate | Download (3.76 KB)
1 | 9 | andrej.cim | package org.lidar; |
---|---|---|---|
2 | |||
3 | import com.google.gson.Gson; |
||
4 | import java.io.IOException; |
||
5 | import java.io.PrintWriter; |
||
6 | import java.util.ArrayList; |
||
7 | import javax.servlet.ServletException; |
||
8 | import javax.servlet.http.HttpServlet; |
||
9 | import javax.servlet.http.HttpServletRequest; |
||
10 | import javax.servlet.http.HttpServletResponse; |
||
11 | import org.hibernate.Query; |
||
12 | import org.hibernate.Session; |
||
13 | import org.hibernate.Transaction; |
||
14 | import org.lidar.HibernateUtil; |
||
15 | |||
16 | /**
|
||
17 | 11 | andrej.cim | * Log Servlet
|
18 | 9 | andrej.cim | * @author Andrej Cimpersek
|
19 | */
|
||
20 | public class LogServlet extends HttpServlet { |
||
21 | 11 | andrej.cim | |
22 | /**
|
||
23 | 9 | andrej.cim | * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
|
24 | * @param request servlet request
|
||
25 | * @param response servlet response
|
||
26 | * @throws ServletException if a servlet-specific error occurs
|
||
27 | * @throws IOException if an I/O error occurs
|
||
28 | */
|
||
29 | protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
||
30 | 11 | andrej.cim | throws ServletException, IOException { |
31 | 9 | andrej.cim | response.setContentType("application/json;charset=UTF-8");
|
32 | PrintWriter out = response.getWriter();
|
||
33 | Gson gson = new Gson();
|
||
34 | try {
|
||
35 | int start = 0; |
||
36 | int limit = 15; |
||
37 | 11 | andrej.cim | if (request.getParameter("limit") != null) { |
38 | 9 | andrej.cim | limit = Integer.parseInt(request.getParameter("limit")); |
39 | } |
||
40 | 11 | andrej.cim | if (request.getParameter("start") != null) { |
41 | 9 | andrej.cim | start = Integer.parseInt(request.getParameter("start")); |
42 | } |
||
43 | |||
44 | Session session = HibernateUtil.getSessionFactory().getCurrentSession(); |
||
45 | Transaction tx = session.beginTransaction(); |
||
46 | |||
47 | // FIX: use COUNT instead
|
||
48 | int total = session.createQuery("from Log as log").list().size(); |
||
49 | Query query = session.createQuery("from Log as log order by log.date desc"); |
||
50 | query.setFirstResult(start); |
||
51 | query.setMaxResults(limit); |
||
52 | 11 | andrej.cim | JSONResponse resp = new JSONResponse(true, total, (ArrayList) query.list()); |
53 | 9 | andrej.cim | out.write(gson.toJson(resp)); |
54 | tx.commit(); |
||
55 | } catch (Exception e) { |
||
56 | response.setContentType("text/plain;charset=UTF-8");
|
||
57 | out.write(e.getMessage()); |
||
58 | } finally {
|
||
59 | out.close(); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | class JSONResponse { |
||
64 | 11 | andrej.cim | |
65 | 9 | andrej.cim | private boolean success = true; |
66 | private int total; |
||
67 | private ArrayList rows; |
||
68 | |||
69 | 11 | andrej.cim | public JSONResponse(boolean success, int total, ArrayList rows) { |
70 | 9 | andrej.cim | this.success = success;
|
71 | this.total = total;
|
||
72 | this.rows = rows;
|
||
73 | } |
||
74 | } |
||
75 | |||
76 | // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
|
||
77 | 11 | andrej.cim | /**
|
78 | 9 | andrej.cim | * Handles the HTTP <code>GET</code> method.
|
79 | * @param request servlet request
|
||
80 | * @param response servlet response
|
||
81 | * @throws ServletException if a servlet-specific error occurs
|
||
82 | * @throws IOException if an I/O error occurs
|
||
83 | */
|
||
84 | @Override
|
||
85 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
||
86 | 11 | andrej.cim | throws ServletException, IOException { |
87 | 9 | andrej.cim | processRequest(request, response); |
88 | 11 | andrej.cim | } |
89 | 9 | andrej.cim | |
90 | 11 | andrej.cim | /**
|
91 | 9 | andrej.cim | * Handles the HTTP <code>POST</code> method.
|
92 | * @param request servlet request
|
||
93 | * @param response servlet response
|
||
94 | * @throws ServletException if a servlet-specific error occurs
|
||
95 | * @throws IOException if an I/O error occurs
|
||
96 | */
|
||
97 | @Override
|
||
98 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
||
99 | 11 | andrej.cim | throws ServletException, IOException { |
100 | 9 | andrej.cim | processRequest(request, response); |
101 | } |
||
102 | |||
103 | 11 | andrej.cim | /**
|
104 | 9 | andrej.cim | * Returns a short description of the servlet.
|
105 | * @return a String containing servlet description
|
||
106 | */
|
||
107 | @Override
|
||
108 | public String getServletInfo() { |
||
109 | return "Short description"; |
||
110 | }// </editor-fold>
|
||
111 | } |