/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java
ViewVC logotype

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2081 - (show annotations) (download)
Sat Nov 23 12:08:25 2013 UTC (10 years, 5 months ago) by torben
File size: 5586 byte(s)
Import cleanup
1 package dk.thoerup.traininfoservice.banedk;
2
3 import java.io.IOException;
4 import java.io.StringWriter;
5 import java.util.logging.Level;
6 import java.util.logging.Logger;
7
8 import javax.servlet.ServletException;
9 import javax.servlet.annotation.WebServlet;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.xml.transform.Templates;
14 import javax.xml.transform.TransformerFactory;
15
16 import org.simpleframework.xml.Serializer;
17 import org.simpleframework.xml.core.Persister;
18
19 import dk.thoerup.android.traininfo.common.DepartureBean;
20 import dk.thoerup.circuitbreaker.CircuitBreakerException;
21 import dk.thoerup.traininfoservice.Statistics;
22 import dk.thoerup.traininfoservice.TraininfoSettings;
23 import dk.thoerup.traininfoservice.banedk.DepartureFetcher.FetchTrainType;
24 import dk.thoerup.traininfoservice.db.StationDAO;
25
26 /**
27 * Servlet implementation class DepartureServlet
28 */
29 @WebServlet(urlPatterns={"/DepartureServlet"})
30 public class DepartureServlet extends HttpServlet {
31 private static final long serialVersionUID = 1L;
32
33 Logger logger = Logger.getLogger( DepartureServlet.class.getName() );
34
35 DepartureFetcher fetcher;
36
37
38
39 TransformerFactory xslTransFact;
40 Templates xslTemplate;
41
42
43 @Override
44 public void init() throws ServletException {
45 super.init();
46
47
48 TraininfoSettings settings = (TraininfoSettings) getServletContext().getAttribute("settings");
49 fetcher = new DepartureFetcher(settings);
50
51 /*
52 xslTransFact = TransformerFactory.newInstance();
53
54 String xslPath = getServletContext().getRealPath("/departures.xsl");
55 Source stylesheet = new StreamSource( new File(xslPath) );
56
57 try {
58 xslTemplate = xslTransFact.newTemplates(stylesheet);
59 } catch (Exception e) {
60 throw new ServletException(e);
61 }*/
62 }
63
64 @Override
65 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
66 if (req.getParameter("station") == null || req.getParameter("station").equals("") ) {
67 resp.sendError(400, "not enough parameters (station)");
68 return;
69 }
70 if (req.getParameter("format") == null || req.getParameter("format").equals("")) {
71 resp.sendError(400, "not enough parameters (format)");
72 return;
73 }
74
75 boolean arrival = false;
76 try {
77 arrival = Integer.parseInt( req.getParameter("arrival")) != 0;
78 } catch (Exception e) {}
79
80 Statistics.getInstance().incrementDepartureLookups();
81
82 int station = Integer.parseInt( req.getParameter("station") );
83 String format = req.getParameter("format");
84
85 DepartureBean beans;
86
87 DepartureFetcher.FetchTrainType type = FetchTrainType.BOTH;
88 if ( req.getParameter("type") != null) {
89 type = FetchTrainType.valueOf( req.getParameter("type") );
90 }
91
92
93 try {
94 beans = fetcher.cachedLookupDepartures(station, arrival, type);
95 beans.arrival = arrival;
96 } catch (java.io.IOException ioe) {
97 logger.warning("Read failed, station="+station + ". " + ioe.getMessage() );
98 Statistics.getInstance().incrementDepartureErrors();
99 resp.sendError(500, "backend didnt answer");
100 return;
101 } catch (CircuitBreakerException cbe) {
102 logger.warning("Circuitbreaker - failing fast, station=" +station);
103 Statistics.getInstance().incrementDepartureErrors();
104 beans = generateErrorBean(1);
105 } catch (StationDAO.NostationException nse) {
106 logger.log(Level.WARNING, "Station not in Database, station=" +station);
107 Statistics.getInstance().incrementDepartureErrors();
108 resp.sendError(400, "invalid station ID");
109 return;
110 } catch (Exception e) {
111 logger.log(Level.WARNING, "Unknown exception, station=" +station, e);
112 Statistics.getInstance().incrementDepartureErrors();
113 resp.sendError(500);
114 return;
115 }
116
117 resp.setDateHeader("Expires", 0);
118 resp.setHeader("Cache-Control", "no-cache, must-revalidate");
119
120 if (format.equalsIgnoreCase("xml")) {
121 resp.setContentType("text/xml");
122 resp.getWriter().print( formatXml(beans) );
123 } else if (format.equalsIgnoreCase("html")) {
124
125 String advStr = req.getParameter("advanced");
126 boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false;
127
128 req.setAttribute("advanced", advanced);
129 req.setAttribute("departurebeans", beans);
130 req.setAttribute("stationID", station );
131 getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp);
132 /*resp.setContentType("text/html");
133 resp.getWriter().print( xmlToHtml(formatXml(beans)) );*/
134 } else {
135 resp.sendError(400, "Unknown format");
136 }
137 }
138
139 protected DepartureBean generateErrorBean(int code) {
140 DepartureBean bean = new DepartureBean();
141 bean.stationName="";
142 bean.errorCode = code;
143 return bean;
144 }
145
146 protected String formatXml(DepartureBean beans) throws ServletException{
147
148 Serializer serializer = new Persister();
149
150 StringWriter out = new StringWriter();
151
152 try {
153 serializer.write(beans, out);
154 } catch (Exception e) {
155 throw new ServletException(e);
156 }
157
158 return out.toString();
159 }
160 /*
161 protected String xmlToHtml(String input) throws ServletException {
162
163 try {
164 Transformer trans = xslTemplate.newTransformer();
165
166
167 Source xml = new StreamSource( new StringReader(input));
168 StringWriter out = new StringWriter();
169 trans.transform(xml, new StreamResult(out));
170
171 return out.toString();
172 } catch (Exception e) {
173 throw new ServletException(e);
174 }
175 }*/
176
177 }

  ViewVC Help
Powered by ViewVC 1.1.20