Initial commit
This commit is contained in:
commit
f12e359e2a
43
pom.xml
Normal file
43
pom.xml
Normal file
@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>de.stklcode.pubtrans</groupId>
|
||||
<artifactId>ura-connector</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.8.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.8.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.6.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
95
src/main/java/de/stklcode/pubtrans/ura/UraClient.java
Normal file
95
src/main/java/de/stklcode/pubtrans/ura/UraClient.java
Normal file
@ -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 <stefan@stklcode.de>
|
||||
*/
|
||||
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<Stop> listStops() {
|
||||
List<Stop> 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();
|
||||
}
|
||||
}
|
64
src/main/java/de/stklcode/pubtrans/ura/model/Stop.java
Normal file
64
src/main/java/de/stklcode/pubtrans/ura/model/Stop.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
15
src/test/java/de/stklcode/pubtrans/ura/UraClientTest.java
Normal file
15
src/test/java/de/stklcode/pubtrans/ura/UraClientTest.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user