Initial
[frank.git] / prod / net / jaekl / frank / octranspo / Server.java
1 package net.jaekl.frank.octranspo;
2
3 import java.io.BufferedReader;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.io.IOException;
7 import java.util.ArrayList;
8
9 import net.jaekl.frank.FrankException;
10 import net.jaekl.qd.QDException;
11 import net.jaekl.qd.http.RequestBroker;
12
13 import org.apache.http.NameValuePair;
14 import org.apache.http.message.BasicNameValuePair;
15
16 public class Server
17 {
18         static final String API_KEY = "apiKey";
19         static final String APP_ID = "appID";
20         static final String GATEWAY_URL = "https://api.octranspo1.com/v1.2/";
21         static final String ROUTE_NO = "routeNo";
22         static final String STOP_NO = "stopNo";
23         static final String GET_ROUTE_SUMMARY_FOR_STOP = "GetRouteSummaryForStop";
24         static final String GET_NEXT_TRIPS_FOR_STOP = "GetNextTripsForStop";
25         static final String GET_NEXT_TRIPS_FOR_STOP_ALL_ROUTES = "GetNextTripsForStopAllRoutes";
26
27         RequestBroker m_broker;
28         ArrayList<NameValuePair> m_baseParams;
29
30         // Constructor
31         // appID   The appID for our application
32         // is      InputStream from which to read the apiKey
33         public Server(String appID, InputStream is) throws IOException {
34                 assert (null != appID);
35                 assert (null != is);
36
37                 BufferedReader br = new BufferedReader(new InputStreamReader(is));
38                 String apiKey = br.readLine();
39
40                 m_baseParams = new ArrayList<NameValuePair>();
41                 m_baseParams.add(new BasicNameValuePair(APP_ID, appID));
42                 m_baseParams.add(new BasicNameValuePair(API_KEY, apiKey));
43
44                 m_broker = new RequestBroker(GATEWAY_URL, m_baseParams);
45         }
46
47         public StopInfo getRouteSummaryForStop(int stopNo) 
48                         throws FrankException
49         {
50                 try {
51                         ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
52                         params.add(new BasicNameValuePair(STOP_NO, Integer.valueOf(stopNo).toString()));
53                         return (StopInfo) m_broker.submitAndParse(GET_ROUTE_SUMMARY_FOR_STOP, params, RouteSummary.class);
54                 }
55                 catch (QDException e) {
56                         throw new FrankException(e);
57                 }
58         }
59
60         public StopInfo getNextTripsForStop(int stopNo, int routeNo) 
61                         throws FrankException
62         {
63                 try {
64                         ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
65                         params.add(new BasicNameValuePair(STOP_NO, Integer.valueOf(stopNo).toString()));
66                         params.add(new BasicNameValuePair(ROUTE_NO, Integer.valueOf(routeNo).toString()));
67                         return (StopInfo) m_broker.submitAndParse(GET_NEXT_TRIPS_FOR_STOP, params, NextTrips.class);
68                 }
69                 catch (QDException e) {
70                         throw new FrankException(e);
71                 }
72         }
73
74         public StopInfo getNextTripsForStopAllRoutes(int stopNo) 
75                         throws FrankException
76         {
77                 try {
78                         ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
79                         params.add(new BasicNameValuePair(STOP_NO, Integer.valueOf(stopNo).toString()));
80                         return (StopInfo) m_broker.submitAndParse(GET_NEXT_TRIPS_FOR_STOP_ALL_ROUTES, params, RouteSummary.class);
81                 }
82                 catch (QDException e) {
83                         throw new FrankException(e);
84                 }
85         }
86 }
87