Skip to content

Commit c069622

Browse files
Merge pull request #5035 from ibi-group/cleanup-gtfs-rt
Use HttpHeaders class for GTFS-RT, cleanup
2 parents 2f3b103 + d5b1fca commit c069622

28 files changed

+201
-374
lines changed

docs/UpdaterConfig.md

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ The information is downloaded in a single HTTP request and polled regularly.
4444
| frequencySec | `integer` | How often the URL should be fetched. | *Optional* | `60` | 1.5 |
4545
| fuzzyTripMatching | `boolean` | Whether to match trips fuzzily. | *Optional* | `false` | 1.5 |
4646
| url | `string` | URL to fetch the GTFS-RT feed from. | *Required* | | 1.5 |
47-
| [headers](#u_0_headers) | `map of string` | Extra headers to add to the HTTP request fetching the data. | *Optional* | | 2.3 |
47+
| [headers](#u_0_headers) | `map of string` | HTTP headers to add to the request. Any header key, value can be inserted. | *Optional* | | 2.3 |
4848

4949

5050
##### Parameter details
@@ -54,7 +54,7 @@ The information is downloaded in a single HTTP request and polled regularly.
5454
**Since version:** `2.3`**Type:** `map of string`**Cardinality:** `Optional`
5555
**Path:** /updaters/[0]
5656

57-
Extra headers to add to the HTTP request fetching the data.
57+
HTTP headers to add to the request. Any header key, value can be inserted.
5858

5959

6060

@@ -89,15 +89,15 @@ The information is downloaded in a single HTTP request and polled regularly.
8989
<!-- stop-time-updater BEGIN -->
9090
<!-- NOTE! This section is auto-generated. Do not change, change doc in code instead. -->
9191

92-
| Config Parameter | Type | Summary | Req./Opt. | Default Value | Since |
93-
|-----------------------------------------------------------------------|:---------------:|-------------------------------------------------------------|:----------:|----------------------|:-----:|
94-
| type = "stop-time-updater" | `enum` | The type of the updater. | *Required* | | 1.5 |
95-
| [backwardsDelayPropagationType](#u__5__backwardsDelayPropagationType) | `enum` | How backwards propagation should be handled. | *Optional* | `"required-no-data"` | 2.2 |
96-
| feedId | `string` | Which feed the updates apply to. | *Optional* | | 1.5 |
97-
| frequencySec | `integer` | How often the data should be downloaded in seconds. | *Optional* | `60` | 1.5 |
98-
| fuzzyTripMatching | `boolean` | If the trips should be matched fuzzily. | *Optional* | `false` | 1.5 |
99-
| url | `string` | The URL of the GTFS-RT resource. | *Required* | | 1.5 |
100-
| [headers](#u__5__headers) | `map of string` | Extra headers to add to the HTTP request fetching the data. | *Optional* | | 2.3 |
92+
| Config Parameter | Type | Summary | Req./Opt. | Default Value | Since |
93+
|-----------------------------------------------------------------------|:---------------:|----------------------------------------------------------------------------|:----------:|----------------------|:-----:|
94+
| type = "stop-time-updater" | `enum` | The type of the updater. | *Required* | | 1.5 |
95+
| [backwardsDelayPropagationType](#u__5__backwardsDelayPropagationType) | `enum` | How backwards propagation should be handled. | *Optional* | `"required-no-data"` | 2.2 |
96+
| feedId | `string` | Which feed the updates apply to. | *Optional* | | 1.5 |
97+
| frequencySec | `integer` | How often the data should be downloaded in seconds. | *Optional* | `60` | 1.5 |
98+
| fuzzyTripMatching | `boolean` | If the trips should be matched fuzzily. | *Optional* | `false` | 1.5 |
99+
| [url](#u__5__url) | `string` | The URL of the GTFS-RT resource. | *Required* | | 1.5 |
100+
| [headers](#u__5__headers) | `map of string` | HTTP headers to add to the request. Any header key, value can be inserted. | *Optional* | | 2.3 |
101101

102102

103103
##### Parameter details
@@ -124,12 +124,21 @@ How backwards propagation should be handled.
124124
The updated times are exposed through APIs.
125125

126126

127+
<h4 id="u__5__url">url</h4>
128+
129+
**Since version:** `1.5`**Type:** `string`**Cardinality:** `Required`
130+
**Path:** /updaters/[5]
131+
132+
The URL of the GTFS-RT resource.
133+
134+
`file:` URLs are also supported if you want to read a file from the local disk.
135+
127136
<h4 id="u__5__headers">headers</h4>
128137

129138
**Since version:** `2.3`**Type:** `map of string`**Cardinality:** `Optional`
130139
**Path:** /updaters/[5]
131140

132-
Extra headers to add to the HTTP request fetching the data.
141+
HTTP headers to add to the request. Any header key, value can be inserted.
133142

134143

135144

@@ -214,13 +223,13 @@ The information is downloaded in a single HTTP request and polled regularly.
214223
<!-- vehicle-positions BEGIN -->
215224
<!-- NOTE! This section is auto-generated. Do not change, change doc in code instead. -->
216225

217-
| Config Parameter | Type | Summary | Req./Opt. | Default Value | Since |
218-
|----------------------------|:---------------:|---------------------------------------------------------------------------|:----------:|---------------|:-----:|
219-
| type = "vehicle-positions" | `enum` | The type of the updater. | *Required* | | 1.5 |
220-
| feedId | `string` | Feed ID to which the update should be applied. | *Required* | | 2.2 |
221-
| frequencySec | `integer` | How often the positions should be updated. | *Optional* | `60` | 2.2 |
222-
| url | `uri` | The URL of GTFS-RT protobuf HTTP resource to download the positions from. | *Required* | | 2.2 |
223-
| [headers](#u__6__headers) | `map of string` | Extra headers to add to the HTTP request fetching the data. | *Optional* | | 2.3 |
226+
| Config Parameter | Type | Summary | Req./Opt. | Default Value | Since |
227+
|----------------------------|:---------------:|----------------------------------------------------------------------------|:----------:|---------------|:-----:|
228+
| type = "vehicle-positions" | `enum` | The type of the updater. | *Required* | | 1.5 |
229+
| feedId | `string` | Feed ID to which the update should be applied. | *Required* | | 2.2 |
230+
| frequencySec | `integer` | How often the positions should be updated. | *Optional* | `60` | 2.2 |
231+
| url | `uri` | The URL of GTFS-RT protobuf HTTP resource to download the positions from. | *Required* | | 2.2 |
232+
| [headers](#u__6__headers) | `map of string` | HTTP headers to add to the request. Any header key, value can be inserted. | *Optional* | | 2.3 |
224233

225234

226235
##### Parameter details
@@ -230,7 +239,7 @@ The information is downloaded in a single HTTP request and polled regularly.
230239
**Since version:** `2.3`**Type:** `map of string`**Cardinality:** `Optional`
231240
**Path:** /updaters/[6]
232241

233-
Extra headers to add to the HTTP request fetching the data.
242+
HTTP headers to add to the request. Any header key, value can be inserted.
234243

235244

236245

src/ext/java/org/opentripplanner/ext/siri/updater/SiriETHttpTripUpdateSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public Siri getUpdates() {
6767
url,
6868
etServiceRequest,
6969
timeout,
70-
requestHeaders.headers()
70+
requestHeaders.asMap()
7171
);
7272
if (is != null) {
7373
// Decode message

src/ext/java/org/opentripplanner/ext/siri/updater/SiriSXUpdater.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private Siri getUpdates() throws IOException {
138138
url,
139139
sxServiceRequest,
140140
timeout,
141-
requestHeaders.headers()
141+
requestHeaders.asMap()
142142
);
143143

144144
fetching = System.currentTimeMillis() - t1;

src/ext/java/org/opentripplanner/ext/siri/updater/azure/SiriAzureETUpdater.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ protected void initializeData(String url, Consumer<ServiceBusReceivedMessageCont
9393
HttpHeaders rh = HttpHeaders.of().acceptApplicationXML().build();
9494

9595
final long t1 = System.currentTimeMillis();
96-
final InputStream data = HttpUtils.getData(uri, Duration.ofMillis(timeout), rh.headers());
96+
final InputStream data = HttpUtils.getData(uri, Duration.ofMillis(timeout), rh.asMap());
9797
final long t2 = System.currentTimeMillis();
9898

9999
if (data == null) {

src/ext/java/org/opentripplanner/ext/siri/updater/azure/SiriAzureSXUpdater.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ protected void initializeData(String url, Consumer<ServiceBusReceivedMessageCont
9393
// Maybe put this in the config?
9494
HttpHeaders rh = HttpHeaders.of().acceptApplicationXML().build();
9595

96-
final InputStream data = HttpUtils.getData(uri, Duration.ofMillis(timeout), rh.headers());
96+
final InputStream data = HttpUtils.getData(uri, Duration.ofMillis(timeout), rh.asMap());
9797
final long t2 = System.currentTimeMillis();
9898

9999
if (data == null) {

src/main/java/org/opentripplanner/standalone/config/routerconfig/updaters/GtfsRealtimeAlertsUpdaterConfig.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ public static GtfsRealtimeAlertsUpdaterParameters create(String configRef, NodeA
2828
.summary("Whether to match trips fuzzily.")
2929
.asBoolean(false),
3030
c.of("frequencySec").since(V1_5).summary("How often the URL should be fetched.").asInt(60),
31-
c
32-
.of("headers")
33-
.since(V2_3)
34-
.summary("Extra headers to add to the HTTP request fetching the data.")
35-
.asStringMap()
31+
HttpHeadersConfig.headers(c, V2_3)
3632
);
3733
}
3834
}

src/main/java/org/opentripplanner/standalone/config/routerconfig/updaters/PollingTripUpdaterConfig.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,23 @@
44
import static org.opentripplanner.standalone.config.framework.json.OtpVersion.V2_2;
55
import static org.opentripplanner.standalone.config.framework.json.OtpVersion.V2_3;
66

7-
import org.opentripplanner.framework.application.OtpAppException;
87
import org.opentripplanner.standalone.config.framework.json.NodeAdapter;
98
import org.opentripplanner.updater.trip.BackwardsDelayPropagationType;
109
import org.opentripplanner.updater.trip.PollingTripUpdaterParameters;
1110

1211
public class PollingTripUpdaterConfig {
1312

1413
public static PollingTripUpdaterParameters create(String configRef, NodeAdapter c) {
15-
String file = null;
16-
String url = null;
14+
var url = c
15+
.of("url")
16+
.since(V1_5)
17+
.summary("The URL of the GTFS-RT resource.")
18+
.description(
19+
"`file:` URLs are also supported if you want to read a file from the local disk."
20+
)
21+
.asString();
1722

18-
if (c.exist("file")) {
19-
file = c.of("file").since(V1_5).summary("The path of the GTFS-RT file.").asString();
20-
} else if (c.exist("url")) {
21-
url = c.of("url").since(V1_5).summary("The URL of the GTFS-RT resource.").asString();
22-
} else {
23-
throw new OtpAppException(
24-
"Need either 'url' or 'file' properties to configure " +
25-
configRef +
26-
" but received: " +
27-
c.asText()
28-
);
29-
}
30-
31-
var headers = c
32-
.of("headers")
33-
.since(V2_3)
34-
.summary("Extra headers to add to the HTTP request fetching the data.")
35-
.asStringMap();
23+
var headers = HttpHeadersConfig.headers(c, V2_3);
3624

3725
return new PollingTripUpdaterParameters(
3826
configRef,
@@ -69,7 +57,6 @@ public static PollingTripUpdaterParameters create(String configRef, NodeAdapter
6957
.asEnum(BackwardsDelayPropagationType.REQUIRED_NO_DATA),
7058
c.of("feedId").since(V1_5).summary("Which feed the updates apply to.").asString(null),
7159
url,
72-
file,
7360
headers
7461
);
7562
}

src/main/java/org/opentripplanner/standalone/config/routerconfig/updaters/VehiclePositionsUpdaterConfig.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ public static VehiclePositionsUpdaterParameters create(String updaterRef, NodeAd
2424
.since(V2_2)
2525
.summary("The URL of GTFS-RT protobuf HTTP resource to download the positions from.")
2626
.asUri();
27-
var headers = c
28-
.of("headers")
29-
.since(V2_3)
30-
.summary("Extra headers to add to the HTTP request fetching the data.")
31-
.asStringMap();
27+
var headers = HttpHeadersConfig.headers(c, V2_3);
3228
return new VehiclePositionsUpdaterParameters(updaterRef, feedId, url, frequencySec, headers);
3329
}
3430
}

src/main/java/org/opentripplanner/updater/alert/GtfsRealtimeAlertsUpdater.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,30 @@
33
import com.google.transit.realtime.GtfsRealtime.FeedMessage;
44
import java.io.InputStream;
55
import java.net.URI;
6-
import java.util.Map;
7-
import org.opentripplanner.framework.collection.MapUtils;
86
import org.opentripplanner.framework.io.HttpUtils;
97
import org.opentripplanner.framework.tostring.ToStringBuilder;
108
import org.opentripplanner.routing.impl.TransitAlertServiceImpl;
119
import org.opentripplanner.routing.services.TransitAlertService;
1210
import org.opentripplanner.transit.service.DefaultTransitService;
1311
import org.opentripplanner.transit.service.TransitModel;
1412
import org.opentripplanner.updater.GtfsRealtimeFuzzyTripMatcher;
13+
import org.opentripplanner.updater.spi.HttpHeaders;
1514
import org.opentripplanner.updater.spi.PollingGraphUpdater;
1615
import org.opentripplanner.updater.spi.WriteToGraphCallback;
1716
import org.slf4j.Logger;
1817
import org.slf4j.LoggerFactory;
1918

2019
/**
2120
* GTFS-RT alerts updater
22-
* <p>
23-
* Usage example:
24-
*
25-
* <pre>
26-
* myalert.type = real-time-alerts
27-
* myalert.frequencySec = 60
28-
* myalert.url = http://host.tld/path
29-
* myalert.earlyStartSec = 3600
30-
* myalert.feedId = TA
31-
* </pre>
3221
*/
3322
public class GtfsRealtimeAlertsUpdater extends PollingGraphUpdater implements TransitAlertProvider {
3423

3524
private static final Logger LOG = LoggerFactory.getLogger(GtfsRealtimeAlertsUpdater.class);
36-
public static final Map<String, String> DEFAULT_HEADERS = Map.of(
37-
"Accept",
38-
"application/x-google-protobuf, application/x-protobuf, application/protobuf, application/octet-stream, */*"
39-
);
4025

4126
private final String url;
4227
private final AlertsUpdateHandler updateHandler;
4328
private final TransitAlertService transitAlertService;
44-
private final Map<String, String> headers;
29+
private final HttpHeaders headers;
4530
private WriteToGraphCallback saveResultOnGraph;
4631
private Long lastTimestamp = Long.MIN_VALUE;
4732

@@ -51,7 +36,7 @@ public GtfsRealtimeAlertsUpdater(
5136
) {
5237
super(config);
5338
this.url = config.url();
54-
this.headers = MapUtils.combine(config.headers(), DEFAULT_HEADERS);
39+
this.headers = HttpHeaders.of().acceptProtobuf().add(config.headers()).build();
5540
TransitAlertService transitAlertService = new TransitAlertServiceImpl(transitModel);
5641

5742
var fuzzyTripMatcher = config.fuzzyTripMatching()
@@ -90,7 +75,7 @@ public String toString() {
9075
@Override
9176
protected void runPolling() {
9277
try {
93-
InputStream data = HttpUtils.getData(URI.create(url), this.headers);
78+
InputStream data = HttpUtils.getData(URI.create(url), this.headers.asMap());
9479
if (data == null) {
9580
throw new RuntimeException("Failed to get data from url " + url);
9681
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.opentripplanner.updater.alert;
22

33
import java.util.Map;
4+
import org.opentripplanner.updater.spi.HttpHeaders;
45
import org.opentripplanner.updater.spi.PollingGraphUpdaterParameters;
56

67
public record GtfsRealtimeAlertsUpdaterParameters(
@@ -10,10 +11,6 @@ public record GtfsRealtimeAlertsUpdaterParameters(
1011
int earlyStartSec,
1112
boolean fuzzyTripMatching,
1213
int frequencySec,
13-
Map<String, String> headers
14+
HttpHeaders headers
1415
)
15-
implements PollingGraphUpdaterParameters {
16-
public GtfsRealtimeAlertsUpdaterParameters {
17-
headers = Map.copyOf(headers);
18-
}
19-
}
16+
implements PollingGraphUpdaterParameters {}

0 commit comments

Comments
 (0)