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