
org.opentripplanner.ext.vehicleparking.hslpark.HslFacilitiesDownloader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of otp Show documentation
Show all versions of otp Show documentation
The OpenTripPlanner multimodal journey planning system
The newest version!
package org.opentripplanner.ext.vehicleparking.hslpark;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import org.opentripplanner.framework.io.OtpHttpClient;
import org.opentripplanner.framework.io.OtpHttpClientException;
import org.opentripplanner.framework.io.OtpHttpClientFactory;
import org.opentripplanner.routing.vehicle_parking.VehicleParking;
import org.opentripplanner.routing.vehicle_parking.VehicleParkingGroup;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HslFacilitiesDownloader {
private static final Logger LOG = LoggerFactory.getLogger(HslFacilitiesDownloader.class);
private static final ObjectMapper mapper = new ObjectMapper();
private final String jsonParsePath;
private final BiFunction, VehicleParking> facilitiesParser;
private final String url;
private final OtpHttpClient otpHttpClient;
public HslFacilitiesDownloader(
String url,
String jsonParsePath,
BiFunction, VehicleParking> facilitiesParser,
OtpHttpClientFactory otpHttpClientFactory
) {
this.url = url;
this.jsonParsePath = jsonParsePath;
this.facilitiesParser = facilitiesParser;
this.otpHttpClient = otpHttpClientFactory.create(LOG);
}
public List downloadFacilities(
Map hubForPark
) {
if (url == null) {
LOG.warn("Cannot download updates, because url is null!");
return null;
}
try {
return otpHttpClient.getAndMap(
URI.create(url),
Map.of(),
is -> {
try {
return parseJSON(is, hubForPark);
} catch (IllegalArgumentException e) {
LOG.warn("Error parsing facilities from {}", url, e);
} catch (JsonProcessingException e) {
LOG.warn("Error parsing facilities from {} (bad JSON of some sort)", url, e);
} catch (IOException e) {
LOG.warn("Error reading facilities from {}", url, e);
}
return null;
}
);
} catch (OtpHttpClientException e) {
LOG.warn("Failed to get data from url {}", url);
return null;
}
}
private static String convertStreamToString(java.io.InputStream is) {
try (java.util.Scanner scanner = new java.util.Scanner(is).useDelimiter("\\A")) {
return scanner.hasNext() ? scanner.next() : "";
}
}
private List parseJSON(
InputStream dataStream,
Map hubForPark
) throws IllegalArgumentException, IOException {
ArrayList out = new ArrayList<>();
String facilitiesString = convertStreamToString(dataStream);
JsonNode rootNode = mapper.readTree(facilitiesString);
if (!jsonParsePath.isEmpty()) {
String delimiter = "/";
String[] parseElement = jsonParsePath.split(delimiter);
for (String s : parseElement) {
rootNode = rootNode.path(s);
}
if (rootNode.isMissingNode()) {
throw new IllegalArgumentException("Could not find jSON elements " + jsonParsePath);
}
}
for (JsonNode node : rootNode) {
if (node == null) {
continue;
}
VehicleParking parsedElement = facilitiesParser.apply(node, hubForPark);
if (parsedElement != null) {
out.add(parsedElement);
}
}
return out;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy