root / trunk / src / java / org / lidar / LogServlet.java @ 12
History | View | Annotate | Download (3.76 KB)
1 |
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 |
* Log Servlet
|
18 |
* @author Andrej Cimpersek
|
19 |
*/
|
20 |
public class LogServlet extends HttpServlet { |
21 |
|
22 |
/**
|
23 |
* 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 |
throws ServletException, IOException { |
31 |
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 |
if (request.getParameter("limit") != null) { |
38 |
limit = Integer.parseInt(request.getParameter("limit")); |
39 |
} |
40 |
if (request.getParameter("start") != null) { |
41 |
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 |
JSONResponse resp = new JSONResponse(true, total, (ArrayList) query.list()); |
53 |
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 |
|
65 |
private boolean success = true; |
66 |
private int total; |
67 |
private ArrayList rows; |
68 |
|
69 |
public JSONResponse(boolean success, int total, ArrayList rows) { |
70 |
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 |
/**
|
78 |
* 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 |
throws ServletException, IOException { |
87 |
processRequest(request, response); |
88 |
} |
89 |
|
90 |
/**
|
91 |
* 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 |
throws ServletException, IOException { |
100 |
processRequest(request, response); |
101 |
} |
102 |
|
103 |
/**
|
104 |
* 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 |
} |