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