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 |
|
21 |
/** |
22 |
* Servlet implementation class LocateStations |
23 |
*/ |
24 |
@WebServlet(urlPatterns={"/LocateStations"}) |
25 |
public class LocateStations extends HttpServlet { |
26 |
private static final long serialVersionUID = 1L; |
27 |
|
28 |
Logger logger = Logger.getLogger( LocateStations.class.toString() ); |
29 |
|
30 |
StationDAO stationDao = new StationDAO(); |
31 |
|
32 |
|
33 |
protected String transformToIntList(String input) { |
34 |
String strings[] = input.split(","); |
35 |
|
36 |
StringBuffer sb = new StringBuffer(); |
37 |
sb.append("("); |
38 |
for (int i = 0; i<strings.length; i++) { |
39 |
if (i>0) { |
40 |
sb.append(","); |
41 |
} |
42 |
sb.append( Integer.parseInt(strings[i])); //by doing the integer conversion we ensure that it really is a integer |
43 |
} |
44 |
sb.append(")"); |
45 |
return sb.toString(); |
46 |
} |
47 |
|
48 |
|
49 |
protected StationBean getStations(HttpServletRequest req) throws SQLException { |
50 |
StationBean stations = null; |
51 |
if (req.getParameter("latitude") != null && req.getParameter("latitude") != null) { |
52 |
|
53 |
Statistics.getInstance().incrementStationLookupsLocation(); |
54 |
|
55 |
double latitude = Double.parseDouble( req.getParameter("latitude") ); |
56 |
double longitude = Double.parseDouble( req.getParameter("longitude") ); |
57 |
stations = stationDao.getByLocation(latitude, longitude); |
58 |
|
59 |
} else if (req.getParameter("name") != null) { |
60 |
Statistics.getInstance().incrementStationLookupsName(); |
61 |
String name = req.getParameter("name").trim(); |
62 |
stations = stationDao.getByName(name); |
63 |
|
64 |
} else if (req.getParameter("list") != null) { |
65 |
Statistics.getInstance().incrementStationLookupsFavorites(); |
66 |
String list = transformToIntList( req.getParameter("list")); |
67 |
stations = stationDao.getByList(list); |
68 |
} else if (req.getParameter("dump") != null) { |
69 |
stations = stationDao.dumpAll(); |
70 |
} |
71 |
return stations; |
72 |
} |
73 |
|
74 |
|
75 |
protected String formatStations(StationBean stations) throws ServletException { |
76 |
|
77 |
Serializer serializer = new Persister(); |
78 |
|
79 |
StringWriter out = new StringWriter(); |
80 |
|
81 |
try { |
82 |
serializer.write(stations, out); |
83 |
} catch (Exception e) { |
84 |
throw new ServletException(e); |
85 |
} |
86 |
|
87 |
return out.toString(); |
88 |
} |
89 |
|
90 |
@Override |
91 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
92 |
|
93 |
|
94 |
try { |
95 |
StationBean stations = getStations(request); |
96 |
|
97 |
|
98 |
if (stations != null){ |
99 |
String xml = formatStations(stations); |
100 |
|
101 |
response.setContentType("text/xml"); |
102 |
response.getWriter().print(xml); |
103 |
} else { |
104 |
response.sendError(400, "not enough parameters"); |
105 |
} |
106 |
|
107 |
|
108 |
} catch (Exception e) { |
109 |
logger.log(Level.SEVERE, "Exception while finding stations", e); |
110 |
response.sendError(500); |
111 |
} |
112 |
} |
113 |
|
114 |
} |