1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
|
import java.io.ByteArrayOutputStream; |
4 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.util.List; |
|
5 |
import java.util.logging.Level; |
import java.util.logging.Level; |
6 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
7 |
|
|
8 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
9 |
|
import javax.servlet.annotation.WebServlet; |
10 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
11 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
12 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
|
import javax.xml.parsers.DocumentBuilder; |
|
13 |
import javax.xml.parsers.DocumentBuilderFactory; |
import javax.xml.parsers.DocumentBuilderFactory; |
|
import javax.xml.transform.OutputKeys; |
|
|
import javax.xml.transform.Transformer; |
|
14 |
import javax.xml.transform.TransformerFactory; |
import javax.xml.transform.TransformerFactory; |
|
import javax.xml.transform.dom.DOMSource; |
|
|
import javax.xml.transform.stream.StreamResult; |
|
15 |
|
|
16 |
import org.w3c.dom.DOMImplementation; |
import org.simpleframework.xml.Serializer; |
17 |
import org.w3c.dom.Document; |
import org.simpleframework.xml.core.Persister; |
|
import org.w3c.dom.Element; |
|
18 |
|
|
19 |
|
import dk.thoerup.android.traininfo.common.DepartureBean; |
20 |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
|
import dk.thoerup.traininfoservice.StationDAO; |
|
21 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
22 |
|
|
23 |
/** |
/** |
24 |
* Servlet implementation class DepartureServlet |
* Servlet implementation class DepartureServlet |
25 |
*/ |
*/ |
26 |
|
@WebServlet(urlPatterns={"/DepartureServlet"}) |
27 |
public class DepartureServlet extends HttpServlet { |
public class DepartureServlet extends HttpServlet { |
28 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
29 |
|
|
38 |
public void init() throws ServletException { |
public void init() throws ServletException { |
39 |
super.init(); |
super.init(); |
40 |
|
|
41 |
boolean useTempSite = Boolean.parseBoolean( getServletContext().getInitParameter("usetempsite") ); |
boolean useAzureSite = Boolean.parseBoolean( getServletContext().getInitParameter("useazuresite") ); |
42 |
int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); |
int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); |
43 |
logger.info( "DepartureServlet, use temp site=" + useTempSite + ", cache=" + cacheTimeout); |
int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") ); |
44 |
fetcher = new DepartureFetcher(useTempSite, cacheTimeout); |
logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); |
45 |
|
fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout); |
46 |
} |
} |
47 |
|
|
48 |
@Override |
@Override |
62 |
int station = Integer.parseInt( req.getParameter("station") ); |
int station = Integer.parseInt( req.getParameter("station") ); |
63 |
String format = req.getParameter("format"); |
String format = req.getParameter("format"); |
64 |
|
|
65 |
List<DepartureBean> beans; |
DepartureBean beans; |
|
|
|
|
String stationName = StationDAO.getStationName(station); |
|
66 |
|
|
67 |
try { |
try { |
68 |
beans = fetcher.cachedLookupDepartures(station, arrival); |
beans = fetcher.cachedLookupDepartures(station, arrival); |
88 |
|
|
89 |
if (format.equalsIgnoreCase("xml")) { |
if (format.equalsIgnoreCase("xml")) { |
90 |
resp.setContentType("text/xml"); |
resp.setContentType("text/xml"); |
91 |
resp.getWriter().print( formatXml(beans, stationName) ); |
resp.getWriter().print( formatXml(beans, arrival) ); |
92 |
} else if (format.equalsIgnoreCase("html")) { |
} else if (format.equalsIgnoreCase("html")) { |
93 |
|
|
94 |
String advStr = req.getParameter("advanced"); |
String advStr = req.getParameter("advanced"); |
95 |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
96 |
|
|
97 |
req.setAttribute("advanced", advanced); |
req.setAttribute("advanced", advanced); |
|
req.setAttribute("stationname", stationName ); |
|
98 |
req.setAttribute("departurebeans", beans); |
req.setAttribute("departurebeans", beans); |
99 |
req.setAttribute("stationID", station ); |
req.setAttribute("stationID", station ); |
100 |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
101 |
} else { |
} else { |
102 |
throw new ServletException("Unknown format"); |
resp.sendError(400, "Unknown format"); |
103 |
} |
} |
104 |
|
|
105 |
} |
} |
106 |
|
|
107 |
protected String formatXml(List<DepartureBean> beans, String stationName) throws ServletException{ |
protected String formatXml(DepartureBean beans, boolean arrival) throws ServletException{ |
108 |
String xml = ""; |
|
109 |
try { |
Serializer serializer = new Persister(); |
|
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); |
|
|
DOMImplementation impl = builder.getDOMImplementation(); |
|
|
|
|
|
|
|
|
Document doc = impl.createDocument(null,null,null); |
|
|
Element root = doc.createElement("departureinfo"); |
|
|
root.setAttribute("station", stationName); |
|
|
for (DepartureBean departure : beans) { |
|
|
Element train = doc.createElement("train"); |
|
|
|
|
|
Element time = doc.createElement("time"); |
|
|
time.setTextContent( departure.getTime() ); |
|
|
train.appendChild(time); |
|
|
|
|
|
Element updated = doc.createElement("updated"); |
|
|
updated.setTextContent( String.valueOf(departure.getUpdated()) ); |
|
|
train.appendChild(updated); |
|
|
|
|
|
Element trainNumber = doc.createElement("trainnumber"); |
|
|
trainNumber.setTextContent( departure.getTrainNumber() ); |
|
|
train.appendChild(trainNumber); |
|
|
|
|
|
Element destination = doc.createElement("destination"); |
|
|
destination.setTextContent( departure.getDestination()); |
|
|
train.appendChild(destination); |
|
|
|
|
|
Element origin = doc.createElement("origin"); |
|
|
origin.setTextContent( departure.getOrigin() ); |
|
|
train.appendChild(origin); |
|
|
|
|
|
Element location= doc.createElement("location"); |
|
|
location.setTextContent( departure.getLocation() ); |
|
|
train.appendChild(location); |
|
|
|
|
|
Element status = doc.createElement("status"); |
|
|
status.setTextContent( departure.getStatus() ); |
|
|
train.appendChild(status); |
|
|
|
|
|
Element note = doc.createElement("note"); |
|
|
note.setTextContent( departure.getNote() ); |
|
|
train.appendChild(note); |
|
|
|
|
|
Element type = doc.createElement("type"); |
|
|
type.setTextContent( departure.getType() ); |
|
|
train.appendChild(type); |
|
|
|
|
|
root.appendChild(train); |
|
|
} |
|
|
|
|
|
doc.appendChild(root); |
|
|
|
|
|
|
|
|
DOMSource domSource = new DOMSource(doc); |
|
|
|
|
|
Transformer transformer = transformerFactory.newTransformer(); |
|
|
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
|
|
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); |
|
|
transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); |
|
|
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1"); |
|
|
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
|
|
java.io.StringWriter sw = new java.io.StringWriter(); |
|
|
StreamResult sr = new StreamResult(sw); |
|
|
transformer.transform(domSource, sr); |
|
|
xml = sw.toString(); |
|
110 |
|
|
111 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
112 |
|
|
113 |
|
try { |
114 |
|
serializer.write(beans, out); |
115 |
} catch (Exception e) { |
} catch (Exception e) { |
116 |
throw new ServletException(e); |
throw new ServletException(e); |
117 |
} |
} |
118 |
return xml; |
|
119 |
|
return out.toString(); |
120 |
} |
} |
121 |
|
|
122 |
} |
} |