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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1080 - (show annotations) (download)
Mon Sep 20 20:11:55 2010 UTC (13 years, 7 months ago) by torben
File size: 4838 byte(s)
Add a copy with partial support for google app engine
1 package dk.thoerup.traininfoservice.banedk;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.File;
6 import java.io.IOException;
7 import java.util.logging.Level;
8 import java.util.logging.Logger;
9
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14
15 import javax.xml.transform.Source;
16 import javax.xml.transform.Transformer;
17 import javax.xml.transform.TransformerFactory;
18 import javax.xml.transform.stream.StreamResult;
19 import javax.xml.transform.stream.StreamSource;
20
21 import org.simpleframework.xml.Serializer;
22 import org.simpleframework.xml.core.Persister;
23
24 import dk.thoerup.android.traininfo.common.DepartureBean;
25 import dk.thoerup.circuitbreaker.CircuitBreakerException;
26 import dk.thoerup.traininfoservice.Statistics;
27
28 /**
29 * Servlet implementation class DepartureServlet
30 */
31
32 public class DepartureServlet extends HttpServlet {
33 private static final long serialVersionUID = 1L;
34
35 Logger logger = Logger.getLogger( DepartureServlet.class.getName() );
36
37 DepartureFetcher fetcher;
38
39
40
41 String xslPath;
42
43
44 @Override
45 public void init() throws ServletException {
46 super.init();
47
48 boolean useAzureSite = Boolean.parseBoolean( getServletContext().getInitParameter("useazuresite") );
49 int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") );
50 int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") );
51 logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout);
52 fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout);
53
54 xslPath = getServletContext().getRealPath("/departures.xsl");
55 }
56
57 @Override
58 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
59 if (req.getParameter("station") == null) {
60 resp.sendError(400, "not enough parameters");
61 return;
62 }
63
64 boolean arrival = false;
65 try {
66 arrival = Integer.parseInt( req.getParameter("arrival")) != 0;
67 } catch (Exception e) {}
68
69 Statistics.getInstance().incrementDepartureLookups();
70
71 int station = Integer.parseInt( req.getParameter("station") );
72 String format = req.getParameter("format");
73
74 DepartureBean beans;
75
76 try {
77 beans = fetcher.cachedLookupDepartures(station, arrival);
78 beans.arrival = arrival;
79 } catch (java.io.IOException ioe) {
80 logger.warning("Read failed, station="+station + ". " + ioe.getMessage() );
81 Statistics.getInstance().incrementDepartureErrors();
82 resp.sendError(500);
83 return;
84 } catch (CircuitBreakerException cbe) {
85 logger.warning("Circuitbreaker - failing fast");
86 Statistics.getInstance().incrementDepartureErrors();
87 resp.sendError(500);
88 return;
89 } catch (Exception e) {
90 logger.log(Level.WARNING, "Unknown exception, station=" +station, e);
91 Statistics.getInstance().incrementDepartureErrors();
92 resp.sendError(500);
93 return;
94 }
95
96 resp.setDateHeader("Expires", 0);
97 resp.setHeader("Cache-Control", "no-cache, must-revalidate");
98
99 if (format.equalsIgnoreCase("xml")) {
100 resp.setContentType("text/xml");
101 resp.getWriter().print( formatXml(beans) );
102 } else if (format.equalsIgnoreCase("html")) {
103 /*
104 String advStr = req.getParameter("advanced");
105 boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false;
106
107 req.setAttribute("advanced", advanced);
108 req.setAttribute("departurebeans", beans);
109 req.setAttribute("stationID", station );
110 getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp);*/
111 resp.setContentType("text/html");
112 resp.getWriter().print( xmlToHtml(formatXml(beans)) );
113 } else {
114 resp.sendError(400, "Unknown format");
115 }
116
117 }
118
119 protected String formatXml(DepartureBean beans) throws ServletException{
120
121 Serializer serializer = new Persister();
122
123 ByteArrayOutputStream out = new ByteArrayOutputStream();
124
125 try {
126 serializer.write(beans, out);
127 } catch (Exception e) {
128 throw new ServletException(e);
129 }
130
131 return out.toString();
132 }
133
134 protected String xmlToHtml(String input) {
135
136 try {
137 Source stylesheet = new StreamSource( new File(xslPath) );
138
139
140 TransformerFactory transFact = TransformerFactory.newInstance();
141
142 Transformer trans = transFact.newTransformer(stylesheet);
143
144 Source xml = new StreamSource( new ByteArrayInputStream(input.getBytes() ));
145 ByteArrayOutputStream out = new ByteArrayOutputStream();
146 trans.transform(xml, new StreamResult(out));
147
148 return out.toString();
149 } catch (Exception e) {
150 return e.toString();
151 }
152 }
153
154 }

  ViewVC Help
Powered by ViewVC 1.1.20