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