/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/LocateStations.java
ViewVC logotype

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/LocateStations.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1557 - (show annotations) (download)
Fri Jul 8 12:47:02 2011 UTC (12 years, 10 months ago) by torben
File size: 3389 byte(s)
add support for dummy lookups - needed for statistics when the stationlist goes clientside
1 package dk.thoerup.traininfoservice;
2
3
4 import java.io.IOException;
5 import java.io.StringWriter;
6 import java.sql.SQLException;
7 import java.util.logging.Level;
8 import java.util.logging.Logger;
9
10 import javax.servlet.ServletException;
11 import javax.servlet.annotation.WebServlet;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16 import org.simpleframework.xml.Serializer;
17 import org.simpleframework.xml.core.Persister;
18
19 import dk.thoerup.android.traininfo.common.StationBean;
20 import dk.thoerup.traininfoservice.db.StationDAO;
21
22 /**
23 * Servlet implementation class LocateStations
24 */
25 @WebServlet(urlPatterns={"/LocateStations"})
26 public class LocateStations extends HttpServlet {
27 private static final long serialVersionUID = 1L;
28
29 Logger logger = Logger.getLogger( LocateStations.class.toString() );
30
31 StationDAO stationDao = new StationDAO();
32
33
34 protected String transformToIntList(String input) {
35 String strings[] = input.split(",");
36
37 StringBuffer sb = new StringBuffer();
38 sb.append("(");
39 for (int i = 0; i<strings.length; i++) {
40 if (i>0) {
41 sb.append(",");
42 }
43 sb.append( Integer.parseInt(strings[i])); //by doing the integer conversion we ensure that it really is a integer
44 }
45 sb.append(")");
46 return sb.toString();
47 }
48
49
50 protected StationBean getStations(HttpServletRequest req) throws SQLException {
51 StationBean stations = null;
52
53 boolean dummy = req.getParameter("dummy") != null;
54 if (dummy == true)
55 stations = new StationBean();
56
57 if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) {
58
59 Statistics.getInstance().incrementStationLookupsLocation();
60
61 double latitude = Double.parseDouble( req.getParameter("latitude") );
62 double longitude = Double.parseDouble( req.getParameter("longitude") );
63 if (dummy == false)
64 stations = stationDao.getByLocation(latitude, longitude);
65
66 } else if (req.getParameter("name") != null) {
67 Statistics.getInstance().incrementStationLookupsName();
68 String name = req.getParameter("name").trim();
69 if (dummy == false)
70 stations = stationDao.getByName(name);
71
72 } else if (req.getParameter("list") != null) {
73 Statistics.getInstance().incrementStationLookupsFavorites();
74 String list = transformToIntList( req.getParameter("list"));
75 if (dummy == false)
76 stations = stationDao.getByList(list);
77 } else if (req.getParameter("dump") != null) {
78 stations = stationDao.dumpAll();
79 }
80 return stations;
81 }
82
83
84 protected String formatStations(StationBean stations) throws ServletException {
85
86 Serializer serializer = new Persister();
87
88 StringWriter out = new StringWriter();
89
90 try {
91 serializer.write(stations, out);
92 } catch (Exception e) {
93 throw new ServletException(e);
94 }
95
96 return out.toString();
97 }
98
99 @Override
100 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
101
102
103 try {
104 StationBean stations = getStations(request);
105
106
107 if (stations != null){
108 String xml = formatStations(stations);
109
110 response.setContentType("text/xml");
111 response.getWriter().print(xml);
112 } else {
113 response.sendError(400, "not enough parameters");
114 }
115
116
117 } catch (Exception e) {
118 logger.log(Level.SEVERE, "Exception while finding stations", e);
119 response.sendError(500);
120 }
121 }
122
123 }

  ViewVC Help
Powered by ViewVC 1.1.20