mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-14 00:30:31 +00:00
AbstractHafasMobileProvider: Implement request body checksum.
This commit is contained in:
parent
f1a3f99ccc
commit
c410fcda65
1 changed files with 27 additions and 4 deletions
|
@ -43,7 +43,11 @@ import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import com.google.common.base.Charsets;
|
||||||
import com.google.common.base.Joiner;
|
import com.google.common.base.Joiner;
|
||||||
|
import com.google.common.hash.HashCode;
|
||||||
|
import com.google.common.hash.HashFunction;
|
||||||
|
import com.google.common.hash.Hashing;
|
||||||
|
|
||||||
import de.schildbach.pte.dto.Departure;
|
import de.schildbach.pte.dto.Departure;
|
||||||
import de.schildbach.pte.dto.Fare;
|
import de.schildbach.pte.dto.Fare;
|
||||||
|
@ -78,6 +82,10 @@ public abstract class AbstractHafasMobileProvider extends AbstractHafasProvider
|
||||||
public String apiAuthorization;
|
public String apiAuthorization;
|
||||||
@Nullable
|
@Nullable
|
||||||
public String apiClient;
|
public String apiClient;
|
||||||
|
@Nullable
|
||||||
|
public String requestChecksumSalt;
|
||||||
|
|
||||||
|
private static final HashFunction MD5 = Hashing.md5();
|
||||||
|
|
||||||
public AbstractHafasMobileProvider(final NetworkId network, final HttpUrl apiBase, final Product[] productsMap) {
|
public AbstractHafasMobileProvider(final NetworkId network, final HttpUrl apiBase, final Product[] productsMap) {
|
||||||
super(network, productsMap);
|
super(network, productsMap);
|
||||||
|
@ -100,6 +108,11 @@ public abstract class AbstractHafasMobileProvider extends AbstractHafasProvider
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected AbstractHafasMobileProvider setRequestChecksumSalt(final String requestChecksumSalt) {
|
||||||
|
this.requestChecksumSalt = requestChecksumSalt;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NearbyLocationsResult queryNearbyLocations(final EnumSet<LocationType> types, final Location location,
|
public NearbyLocationsResult queryNearbyLocations(final EnumSet<LocationType> types, final Location location,
|
||||||
final int maxDistance, final int maxLocations) throws IOException {
|
final int maxDistance, final int maxLocations) throws IOException {
|
||||||
|
@ -144,7 +157,7 @@ public abstract class AbstractHafasMobileProvider extends AbstractHafasProvider
|
||||||
+ "\"getPOIs\":" + getPOIs + "}", //
|
+ "\"getPOIs\":" + getPOIs + "}", //
|
||||||
false);
|
false);
|
||||||
|
|
||||||
final HttpUrl url = checkNotNull(mgateEndpoint);
|
final HttpUrl url = requestUrl(request);
|
||||||
final CharSequence page = httpClient.get(url, request, "application/json");
|
final CharSequence page = httpClient.get(url, request, "application/json");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -209,7 +222,7 @@ public abstract class AbstractHafasMobileProvider extends AbstractHafasProvider
|
||||||
+ getPasslist + "}",
|
+ getPasslist + "}",
|
||||||
false);
|
false);
|
||||||
|
|
||||||
final HttpUrl url = checkNotNull(mgateEndpoint);
|
final HttpUrl url = requestUrl(request);
|
||||||
final CharSequence page = httpClient.get(url, request, "application/json");
|
final CharSequence page = httpClient.get(url, request, "application/json");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -317,7 +330,7 @@ public abstract class AbstractHafasMobileProvider extends AbstractHafasProvider
|
||||||
+ ",\"meta\":false},\"maxLoc\":" + DEFAULT_MAX_LOCATIONS + "}}",
|
+ ",\"meta\":false},\"maxLoc\":" + DEFAULT_MAX_LOCATIONS + "}}",
|
||||||
false);
|
false);
|
||||||
|
|
||||||
final HttpUrl url = checkNotNull(mgateEndpoint);
|
final HttpUrl url = requestUrl(request);
|
||||||
final CharSequence page = httpClient.get(url, request, "application/json");
|
final CharSequence page = httpClient.get(url, request, "application/json");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -414,7 +427,7 @@ public abstract class AbstractHafasMobileProvider extends AbstractHafasProvider
|
||||||
+ "\"getPolyline\":false,\"getPasslist\":true,\"getIST\":false,\"getEco\":false,\"extChgTime\":-1}", //
|
+ "\"getPolyline\":false,\"getPasslist\":true,\"getIST\":false,\"getEco\":false,\"extChgTime\":-1}", //
|
||||||
false);
|
false);
|
||||||
|
|
||||||
final HttpUrl url = checkNotNull(mgateEndpoint);
|
final HttpUrl url = requestUrl(request);
|
||||||
final CharSequence page = httpClient.get(url, request, "application/json");
|
final CharSequence page = httpClient.get(url, request, "application/json");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -597,6 +610,16 @@ public abstract class AbstractHafasMobileProvider extends AbstractHafasProvider
|
||||||
+ "\"formatted\":" + formatted + "}";
|
+ "\"formatted\":" + formatted + "}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private HttpUrl requestUrl(final String body) {
|
||||||
|
final HttpUrl.Builder url = checkNotNull(mgateEndpoint).newBuilder();
|
||||||
|
if (requestChecksumSalt != null) {
|
||||||
|
final HashCode checksum = MD5.newHasher().putString(body, Charsets.UTF_8)
|
||||||
|
.putString(requestChecksumSalt, Charsets.UTF_8).hash();
|
||||||
|
url.addQueryParameter("checksum", checksum.toString());
|
||||||
|
}
|
||||||
|
return url.build();
|
||||||
|
}
|
||||||
|
|
||||||
private String jsonLocation(final Location location) {
|
private String jsonLocation(final Location location) {
|
||||||
if (location.type == LocationType.STATION && location.hasId())
|
if (location.type == LocationType.STATION && location.hasId())
|
||||||
return "{\"type\":\"S\",\"extId\":" + JSONObject.quote(location.id) + "}";
|
return "{\"type\":\"S\",\"extId\":" + JSONObject.quote(location.id) + "}";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue