/[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 1255 - (show annotations) (download)
Mon Apr 4 10:56:44 2011 UTC (13 years, 1 month ago) by torben
File size: 3204 byte(s)
Move database related classes to it's own package
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 if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) {
53
54 Statistics.getInstance().incrementStationLookupsLocation();
55
56 double latitude = Double.parseDouble( req.getParameter("latitude") );
57 double longitude = Double.parseDouble( req.getParameter("longitude") );
58 stations = stationDao.getByLocation(latitude, longitude);
59
60 } else if (req.getParameter("name") != null) {
61 Statistics.getInstance().incrementStationLookupsName();
62 String name = req.getParameter("name").trim();
63 stations = stationDao.getByName(name);
64
65 } else if (req.getParameter("list") != null) {
66 Statistics.getInstance().incrementStationLookupsFavorites();
67 String list = transformToIntList( req.getParameter("list"));
68 stations = stationDao.getByList(list);
69 } else if (req.getParameter("dump") != null) {
70 stations = stationDao.dumpAll();
71 }
72 return stations;
73 }
74
75
76 protected String formatStations(StationBean stations) throws ServletException {
77
78 Serializer serializer = new Persister();
79
80 StringWriter out = new StringWriter();
81
82 try {
83 serializer.write(stations, out);
84 } catch (Exception e) {
85 throw new ServletException(e);
86 }
87
88 return out.toString();
89 }
90
91 @Override
92 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
93
94
95 try {
96 StationBean stations = getStations(request);
97
98
99 if (stations != null){
100 String xml = formatStations(stations);
101
102 response.setContentType("text/xml");
103 response.getWriter().print(xml);
104 } else {
105 response.sendError(400, "not enough parameters");
106 }
107
108
109 } catch (Exception e) {
110 logger.log(Level.SEVERE, "Exception while finding stations", e);
111 response.sendError(500);
112 }
113 }
114
115 }

  ViewVC Help
Powered by ViewVC 1.1.20