--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2010/06/16 09:58:15 861 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2010/09/16 13:32:10 1060 @@ -1,33 +1,28 @@ package dk.thoerup.traininfoservice.banedk; +import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import org.w3c.dom.DOMImplementation; -import org.w3c.dom.Document; -import org.w3c.dom.Element; +import org.simpleframework.xml.Serializer; +import org.simpleframework.xml.core.Persister; import dk.thoerup.circuitbreaker.CircuitBreakerException; -import dk.thoerup.traininfoservice.StationDAO; import dk.thoerup.traininfoservice.Statistics; /** * Servlet implementation class DepartureServlet */ +@WebServlet(urlPatterns={"/DepartureServlet"}) public class DepartureServlet extends HttpServlet { private static final long serialVersionUID = 1L; @@ -42,10 +37,11 @@ public void init() throws ServletException { super.init(); - boolean useTempSite = Boolean.parseBoolean( getServletContext().getInitParameter("usetempsite") ); + boolean useAzureSite = Boolean.parseBoolean( getServletContext().getInitParameter("useazuresite") ); int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); - logger.info( "DepartureServlet, use temp site=" + useTempSite + ", cache=" + cacheTimeout); - fetcher = new DepartureFetcher(useTempSite, cacheTimeout); + int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") ); + logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); + fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout); } @Override @@ -65,9 +61,7 @@ int station = Integer.parseInt( req.getParameter("station") ); String format = req.getParameter("format"); - List beans; - - String stationName = StationDAO.getStationName(station); + DepartureBean beans; try { beans = fetcher.cachedLookupDepartures(station, arrival); @@ -93,96 +87,35 @@ if (format.equalsIgnoreCase("xml")) { resp.setContentType("text/xml"); - resp.getWriter().print( formatXml(beans, stationName) ); + resp.getWriter().print( formatXml(beans, arrival) ); } else if (format.equalsIgnoreCase("html")) { String advStr = req.getParameter("advanced"); boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; req.setAttribute("advanced", advanced); - req.setAttribute("stationname", stationName ); req.setAttribute("departurebeans", beans); req.setAttribute("stationID", station ); getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); } else { - throw new ServletException("Unknown format"); + resp.sendError(400, "Unknown format"); } } - protected String formatXml(List beans, String stationName) throws ServletException{ - String xml = ""; - try { - 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(); + protected String formatXml(DepartureBean beans, boolean arrival) throws ServletException{ + + Serializer serializer = new Persister(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try { + serializer.write(beans, out); } catch (Exception e) { throw new ServletException(e); } - return xml; + + return out.toString(); } }