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 |
} else if (req.getParameter("dump") != null) { |
68 |
stations = stationDao.dumpAll(); |
69 |
} |
70 |
return stations; |
71 |
} |
72 |
|
73 |
|
74 |
protected String formatStations(StationBean stations) throws ServletException { |
75 |
|
76 |
Serializer serializer = new Persister(); |
77 |
|
78 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
79 |
|
80 |
try { |
81 |
serializer.write(stations, out); |
82 |
} catch (Exception e) { |
83 |
throw new ServletException(e); |
84 |
} |
85 |
|
86 |
return out.toString(); |
87 |
} |
88 |
|
89 |
@Override |
90 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
91 |
|
92 |
|
93 |
try { |
94 |
StationBean stations = getStations(request); |
95 |
|
96 |
|
97 |
if (stations != null){ |
98 |
String xml = formatStations(stations); |
99 |
|
100 |
response.setContentType("text/xml"); |
101 |
response.getWriter().print(xml); |
102 |
} else { |
103 |
response.sendError(400, "not enough parameters"); |
104 |
} |
105 |
|
106 |
|
107 |
} catch (Exception e) { |
108 |
logger.log(Level.SEVERE, "Exception while finding stations", e); |
109 |
response.sendError(500); |
110 |
} |
111 |
} |
112 |
|
113 |
} |