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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1191 by torben, Fri Nov 12 18:38:30 2010 UTC revision 1373 by torben, Sat Apr 23 08:01:57 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice.banedk;  package dk.thoerup.traininfoservice.banedk;
2    
 import java.io.File;  
3  import java.io.IOException;  import java.io.IOException;
 import java.io.StringReader;  
4  import java.io.StringWriter;  import java.io.StringWriter;
5  import java.util.logging.Level;  import java.util.logging.Level;
6  import java.util.logging.Logger;  import java.util.logging.Logger;
# Line 12  import javax.servlet.annotation.WebServl Line 10  import javax.servlet.annotation.WebServl
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.transform.Source;  
13  import javax.xml.transform.Templates;  import javax.xml.transform.Templates;
 import javax.xml.transform.Transformer;  
14  import javax.xml.transform.TransformerFactory;  import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.stream.StreamResult;  
 import javax.xml.transform.stream.StreamSource;  
15    
16  import org.simpleframework.xml.Serializer;  import org.simpleframework.xml.Serializer;
17  import org.simpleframework.xml.core.Persister;  import org.simpleframework.xml.core.Persister;
18    
19  import dk.thoerup.android.traininfo.common.DepartureBean;  import dk.thoerup.android.traininfo.common.DepartureBean;
20    import dk.thoerup.android.traininfo.common.StationBean;
21  import dk.thoerup.circuitbreaker.CircuitBreakerException;  import dk.thoerup.circuitbreaker.CircuitBreakerException;
 import dk.thoerup.traininfoservice.StationDAO;  
22  import dk.thoerup.traininfoservice.Statistics;  import dk.thoerup.traininfoservice.Statistics;
23    import dk.thoerup.traininfoservice.TraininfoSettings;
24    import dk.thoerup.traininfoservice.banedk.DepartureFetcher.FetchTrainType;
25    import dk.thoerup.traininfoservice.db.StationDAO;
26    
27  /**  /**
28   * Servlet implementation class DepartureServlet   * Servlet implementation class DepartureServlet
# Line 48  public class DepartureServlet extends Ht Line 45  public class DepartureServlet extends Ht
45          public void init() throws ServletException {          public void init() throws ServletException {
46                  super.init();                  super.init();
47    
48                  boolean useAzureSite =  Boolean.parseBoolean( getServletContext().getInitParameter("useazuresite") );  
49                  int cacheTimeout =  Integer.parseInt( getServletContext().getInitParameter("cache_timeout") );                  TraininfoSettings settings = (TraininfoSettings) getServletContext().getAttribute("settings");
50                  int replyTimeout =  Integer.parseInt( getServletContext().getInitParameter("reply_timeout") );                  fetcher = new DepartureFetcher(settings);
                 logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout);  
                 fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout);  
51                                    
52                    /*
53                  xslTransFact = TransformerFactory.newInstance();                  xslTransFact = TransformerFactory.newInstance();
54                                    
55                  String xslPath = getServletContext().getRealPath("/departures.xsl");                  String xslPath = getServletContext().getRealPath("/departures.xsl");
# Line 63  public class DepartureServlet extends Ht Line 59  public class DepartureServlet extends Ht
59                          xslTemplate = xslTransFact.newTemplates(stylesheet);                          xslTemplate = xslTransFact.newTemplates(stylesheet);
60                  } catch (Exception e) {                  } catch (Exception e) {
61                          throw new ServletException(e);                          throw new ServletException(e);
62                  }                  }*/
63          }          }
64    
65          @Override          @Override
# Line 87  public class DepartureServlet extends Ht Line 83  public class DepartureServlet extends Ht
83                  int station = Integer.parseInt( req.getParameter("station") );                  int station = Integer.parseInt( req.getParameter("station") );
84                  String format = req.getParameter("format");                  String format = req.getParameter("format");
85    
86                  DepartureBean beans;                              DepartureBean beans;
87                    
88                    DepartureFetcher.FetchTrainType type = FetchTrainType.BOTH;
89                    if ( req.getParameter("type") != null) {
90                            type = FetchTrainType.valueOf( req.getParameter("type") );
91                    }
92    
93                    
94                  try {                  try {
95                          beans = fetcher.cachedLookupDepartures(station, arrival);                          beans = fetcher.cachedLookupDepartures(station, arrival, type);
96                          beans.arrival = arrival;                          beans.arrival = arrival;
97                  } catch (java.io.IOException ioe) {                  } catch (java.io.IOException ioe) {
98                          logger.warning("Read failed, station="+station + ". " + ioe.getMessage() );                          logger.warning("Read failed, station="+station + ". " + ioe.getMessage() );
99                          Statistics.getInstance().incrementDepartureErrors();                          Statistics.getInstance().incrementDepartureErrors();
100                          resp.sendError(500);                          beans = generateErrorBean(1);
                         return;  
101                  } catch (CircuitBreakerException cbe) {                  } catch (CircuitBreakerException cbe) {
102                          logger.warning("Circuitbreaker - failing fast, station=" +station);                          logger.warning("Circuitbreaker - failing fast, station=" +station);
103                          Statistics.getInstance().incrementDepartureErrors();                          Statistics.getInstance().incrementDepartureErrors();
104                          resp.sendError(500);                          beans = generateErrorBean(1);
                         return;  
105                  } catch (StationDAO.NostationException nse) {                  } catch (StationDAO.NostationException nse) {
106                          logger.log(Level.WARNING, "Station not in Database, station=" +station);                          logger.log(Level.WARNING, "Station not in Database, station=" +station);
107                          Statistics.getInstance().incrementDepartureErrors();                          Statistics.getInstance().incrementDepartureErrors();
# Line 121  public class DepartureServlet extends Ht Line 121  public class DepartureServlet extends Ht
121                          resp.setContentType("text/xml");                          resp.setContentType("text/xml");
122                          resp.getWriter().print( formatXml(beans) );                          resp.getWriter().print( formatXml(beans) );
123                  } else if (format.equalsIgnoreCase("html")) {                  } else if (format.equalsIgnoreCase("html")) {
124                          /*                          
125                          String advStr = req.getParameter("advanced");                          String advStr = req.getParameter("advanced");
126                          boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false;                          boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false;
127                                                    
128                          req.setAttribute("advanced", advanced);                          req.setAttribute("advanced", advanced);
129                          req.setAttribute("departurebeans", beans);                          req.setAttribute("departurebeans", beans);
130                          req.setAttribute("stationID", station );                          req.setAttribute("stationID", station );
131                          getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp);*/                          getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp);
132                          resp.setContentType("text/html");                          /*resp.setContentType("text/html");
133                          resp.getWriter().print(  xmlToHtml(formatXml(beans))  );                          resp.getWriter().print(  xmlToHtml(formatXml(beans))  );*/
134                  } else {                  } else {
135                          resp.sendError(400, "Unknown format");                                            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{          protected String formatXml(DepartureBean beans) throws ServletException{
# Line 151  public class DepartureServlet extends Ht Line 157  public class DepartureServlet extends Ht
157                                    
158                  return out.toString();                  return out.toString();
159          }          }
160                    /*
161          protected String xmlToHtml(String input) throws ServletException {          protected String xmlToHtml(String input) throws ServletException {
162    
163                  try {                                                    try {                                  
# Line 166  public class DepartureServlet extends Ht Line 172  public class DepartureServlet extends Ht
172                  } catch (Exception e) {                  } catch (Exception e) {
173                          throw new ServletException(e);                          throw new ServletException(e);
174                  }                  }
175          }          }*/
176    
177  }  }

Legend:
Removed from v.1191  
changed lines
  Added in v.1373

  ViewVC Help
Powered by ViewVC 1.1.20