commit f12e359e2a151bbd60eda7c8a0902ed1f02001bc Author: Stefan Kalscheuer Date: Sat Dec 24 16:52:29 2016 +0100 Initial commit diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7472fed --- /dev/null +++ b/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + de.stklcode.pubtrans + ura-connector + 1.0.0-SNAPSHOT + + + + com.fasterxml.jackson.core + jackson-core + 2.8.5 + + + com.fasterxml.jackson.core + jackson-databind + 2.8.5 + + + junit + junit + 4.12 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.0 + + 1.8 + 1.8 + + + + + diff --git a/src/main/java/de/stklcode/pubtrans/ura/UraClient.java b/src/main/java/de/stklcode/pubtrans/ura/UraClient.java new file mode 100644 index 0000000..605fa4c --- /dev/null +++ b/src/main/java/de/stklcode/pubtrans/ura/UraClient.java @@ -0,0 +1,95 @@ +package de.stklcode.pubtrans.ura; + +import com.fasterxml.jackson.databind.ObjectMapper; +import de.stklcode.pubtrans.ura.model.Stop; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +/** + * Client for URA based public transport API. + * + * @author Stefan Kalscheuer + */ +public class UraClient { + private static final String DEFAULT_INSTANT_URL = "/interfaces/ura/instant_V2"; + private static final String DEFAULT_STREAM_URL = "/interfaces/ura/stream_V2"; + + private static final String FILTER_LINE = "LineID"; + private static final String FILTER_STOP = "StopID"; + + private static final String REQUEST_STOP_ID = "StopID"; + private static final String REQUEST_STOP_NAME = "StopPointName"; + private static final String REQUEST_STOP_STATE = "StopPointState"; + private static final String REQUEST_STOP_INDICATOR = "StopPointIndicator"; + private static final String REQUEST_GEOLOCATION = "Latitude,Longitude"; + + private final String baseURL; + private final String instantURL; + private final String streamURL; + private final ObjectMapper mapper; + + /** + * Constructor with base URL and default API paths. + * + * @param baseURL the base URL (with protocol, without trailing slash) + */ + public UraClient(String baseURL) { + this(baseURL, DEFAULT_INSTANT_URL, DEFAULT_STREAM_URL); + } + + /** + * Constructor with base URL and custom API paths + * + * @param baseURL the base URL (including protocol) + * @param instantURL the path for instant requests + * @param streamURL the path for stream requests + */ + public UraClient(String baseURL, String instantURL, String streamURL) { + this.baseURL = baseURL; + this.instantURL = instantURL; + this.streamURL = streamURL; + this.mapper = new ObjectMapper(); + } + + /** + * List available stops. + * + * @return the list + */ + public List listStops() { + List stops = new ArrayList<>(); + try (InputStream is = requestInstant(REQUEST_STOP_NAME, REQUEST_STOP_ID, REQUEST_GEOLOCATION); + BufferedReader br = new BufferedReader(new InputStreamReader(is))) { + String line; + boolean first = false; + while ((line = br.readLine()) != null) { + if (!first) { + first = true; + continue; + } + stops.add(new Stop(mapper.readValue(line, List.class))); + } + } catch (IOException e) { + e.printStackTrace(); + } + return stops; + } + + /** + * Issue request to instant endpoint and return input stream. + * + * @param returnList fields to fetch + * @return Input stream of the URL + * @throws IOException on errors + */ + private InputStream requestInstant(String...returnList) throws IOException { + URL url = new URL(baseURL + instantURL + "?ReturnList=" + String.join(",", returnList)); + return url.openStream(); + } +} diff --git a/src/main/java/de/stklcode/pubtrans/ura/model/Stop.java b/src/main/java/de/stklcode/pubtrans/ura/model/Stop.java new file mode 100644 index 0000000..6daa46f --- /dev/null +++ b/src/main/java/de/stklcode/pubtrans/ura/model/Stop.java @@ -0,0 +1,64 @@ +package de.stklcode.pubtrans.ura.model; + +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonSetter; +import com.fasterxml.jackson.databind.annotation.JsonAppend; + +import java.util.List; +import java.util.StringJoiner; + +/** + * Created by stefan on 24.12.16. + */ +@JsonIgnoreProperties(ignoreUnknown = true) +public class Stop { + private final String id; + private final String name; + private final Double latitude; + private final Double longitude; + + public Stop(String id, String name, Double latitude, Double longitude) { + this.id = id; + this.name = name; + this.latitude = latitude; + this.longitude = longitude; + } + + public Stop(List raw) { + if (raw.get(1) instanceof String) + name = (String)raw.get(1); + else + throw new UnsupportedOperationException("Field 1 not of expected Type String"); + if (raw.get(2) instanceof String) + id = (String)raw.get(2); + else + throw new UnsupportedOperationException("Field 2 not of expected Type String"); + if (raw.get(3) instanceof Double) + latitude = (Double)raw.get(3); + else + throw new UnsupportedOperationException("Field 3 not of expected Type Double"); + if (raw.get(4) instanceof Double) + longitude = (Double)raw.get(4); + else + throw new UnsupportedOperationException("Field 4 not of expected Type Double"); + + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public Double getLatitude() { + return latitude; + } + + public Double getLongitude() { + return longitude; + } +} diff --git a/src/test/java/de/stklcode/pubtrans/ura/UraClientTest.java b/src/test/java/de/stklcode/pubtrans/ura/UraClientTest.java new file mode 100644 index 0000000..afec97c --- /dev/null +++ b/src/test/java/de/stklcode/pubtrans/ura/UraClientTest.java @@ -0,0 +1,15 @@ +package de.stklcode.pubtrans.ura; + +import org.junit.Test; + +/** + * Created by stefan on 24.12.16. + */ +public class UraClientTest { + private static final String BASE_ASEAG = "http://ivu.aseag.de"; + + @Test + public void listStopsTest() { + new UraClient(BASE_ASEAG).listStops(); + } +}