Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataHub Connector #350

Open
wants to merge 7 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,14 @@ project(":here-naksha-storage-http") {
implementation(commons_lang3)

testImplementation(mockito)
testImplementation("io.rest-assured:rest-assured:5.5.0")
}
tasks.withType<Test> {
if (System.getenv("runConnectorIntegrationTests")?.toBoolean() != true) {
exclude("**/integration/**")
}
}
setOverallCoverage(0.0) // only increasing allowed!

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ protected void init() {}
queryParamsMap.put(EAST, east);
queryParamsMap.put(SOUTH, south);
queryParamsMap.put(LIMIT, limit);
queryParamsMap.put(CLIP_GEO, clip);
if (propSearchOp != null) {
queryParamsMap.put(PROPERTY_SEARCH_OP, propSearchOp);
}
Expand Down Expand Up @@ -275,6 +276,7 @@ protected void init() {}
queryParamsMap.put(LIMIT, limit);
queryParamsMap.put(TILE_TYPE, tileType);
queryParamsMap.put(TILE_ID, tileId);
queryParamsMap.put(CLIP_GEO, clip);
if (propSearchOp != null) {
queryParamsMap.put(PROPERTY_SEARCH_OP, propSearchOp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.here.naksha.lib.core.models.geojson.declaration.ILonLat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.prep.PreparedGeometry;
import org.locationtech.jts.geom.prep.PreparedGeometryFactory;
Expand Down Expand Up @@ -380,7 +381,7 @@ public static long quadKeyToTile(String quadKey) throws NullPointerException, Il
*
* @param quadKey the quaddKey that represents
*/
public static WebMercatorTile forQuadkey(String quadKey) throws IllegalArgumentException {
public static @NotNull WebMercatorTile forQuadkey(String quadKey) throws IllegalArgumentException {
if (!QUADKEY_REGEXP.matcher(quadKey).matches()) {
throw new IllegalArgumentException("Invalid quadkey.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public long remaining(@NotNull TimeUnit timeUnit) {
return remaining;
}

@JsonView(ExcludeFromHash.class)
@JsonProperty("streamId")
private String streamId;

@JsonView(ExcludeFromHash.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.io.ByteStreams;
import com.here.naksha.lib.core.models.Typed;
import com.here.naksha.lib.core.util.Hasher;
import com.here.naksha.lib.core.util.json.JsonObject;
import com.here.naksha.lib.core.util.json.JsonSerializable;
import java.io.BufferedInputStream;
Expand Down Expand Up @@ -139,18 +138,6 @@ public static int compareVersions(String versionA, String versionB) {
return 0;
}

/**
* Returns the hash of the event as a base64 string.
*/
@JsonIgnore
public String getHash() {
try {
return Hasher.getHash(getCacheString());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

@SuppressWarnings("WeakerAccess")
@JsonIgnore
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
@JsonTypeName(value = "PropertyQuery")
public class PropertyQuery {

public PropertyQuery() {}

public PropertyQuery(@NotNull String key, @NotNull QueryOperation op) {}
public PropertyQuery(@NotNull String key, @NotNull QueryOperation op) {
this.key = key;
this.operation = op;
}

/** The property key as JSON path. */
private String key;
Expand Down Expand Up @@ -93,4 +94,14 @@ public void setValues(@NotNull List<@Nullable Object> values) {
this.values = values;
return this;
}

public enum QueryOperation {
EQUALS,
NOT_EQUALS,
LESS_THAN,
GREATER_THAN,
LESS_THAN_OR_EQUALS,
GREATER_THAN_OR_EQUALS,
CONTAINS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
*/
package com.here.naksha.lib.core.models.payload.events.feature;

import static com.here.naksha.lib.core.models.payload.events.feature.GetFeaturesByTileResponseType.GEO_JSON;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonTypeName;
import org.jetbrains.annotations.NotNull;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeName(value = "GetFeaturesByTileEvent")
Expand All @@ -34,7 +31,6 @@ public final class GetFeaturesByTileEvent extends GetFeaturesByBBoxEvent {
private int y;
private String quadkey;
private int margin;
private @NotNull GetFeaturesByTileResponseType responseType = GEO_JSON;
private boolean hereTileFlag;

public int getLevel() {
Expand Down Expand Up @@ -114,17 +110,4 @@ public GetFeaturesByTileEvent withMargin(int margin) {
setMargin(margin);
return this;
}

public @NotNull GetFeaturesByTileResponseType getResponseType() {
return responseType;
}

public void setResponseType(@NotNull GetFeaturesByTileResponseType responseType) {
this.responseType = responseType;
}

public GetFeaturesByTileEvent withResponseType(GetFeaturesByTileResponseType responseType) {
setResponseType(responseType);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,4 @@ public void getFeaturesByTileEventTest() throws Exception {
assertEquals(-77.080078125D, event.getBbox().getWest(), 0);
}

@Test
public void checkHash() throws Exception {
GetFeaturesByTileEvent event1 =
JsonSerializable.deserialize(LazyParsedFeatureCollectionTest.class.getResourceAsStream(
"/com/here/xyz/test/GetFeaturesByTileEvent.json"));
GetFeaturesByTileEvent event2 =
JsonSerializable.deserialize(LazyParsedFeatureCollectionTest.class.getResourceAsStream(
"/com/here/xyz/test/GetFeaturesByTileEvent2.json"));
GetFeaturesByTileEvent event3 =
JsonSerializable.deserialize(LazyParsedFeatureCollectionTest.class.getResourceAsStream(
"/com/here/xyz/test/GetFeaturesByTileEvent3.json"));

assertEquals(event1.getHash(), event2.getHash());
assertNotEquals(event1.getHash(), event3.getHash());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2017-2024 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package com.here.naksha.storage.http;

public enum HttpInterface {
ffwAdapter,
dataHubConnector
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public class HttpStorage implements IStorage {
private static final Logger log = LoggerFactory.getLogger(HttpStorage.class);

private final RequestSender requestSender;
private final HttpStorageProperties properties;

public HttpStorage(@NotNull Storage storage) {
HttpStorageProperties properties = HttpStorage.getProperties(storage);
properties = HttpStorage.getProperties(storage);
requestSender = RequestSenderCache.getInstance()
.getSenderWith(new KeyProperties(
storage.getId(),
Expand All @@ -53,7 +54,7 @@ public HttpStorage(@NotNull Storage storage) {

@Override
public @NotNull IReadSession newReadSession(@Nullable NakshaContext context, boolean useMaster) {
return new HttpStorageReadSession(context, useMaster, requestSender);
return new HttpStorageReadSession(context, useMaster, requestSender, properties.getProtocol());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class HttpStorageProperties extends XyzProperties {
private static final String SOCKET_TIMEOUT = "socketTimeout";
private static final String HEADERS = "headers";

private static final String HTTP_INTERFACE = "httpInterface";
private static final HttpInterface DEFAULT_XYZ_PROTOCOL = HttpInterface.ffwAdapter;

@JsonProperty(URL)
private @NotNull String url;

Expand All @@ -56,16 +59,29 @@ public class HttpStorageProperties extends XyzProperties {
@JsonProperty(HEADERS)
private @NotNull Map<String, String> headers;

@JsonProperty(HTTP_INTERFACE)
private @NotNull HttpInterface httpInterface;

@JsonCreator
public HttpStorageProperties(
@JsonProperty(value = URL, required = true) @NotNull String url,
@JsonProperty(CONNECTION_TIMEOUT) @Nullable Long connectTimeout,
@JsonProperty(SOCKET_TIMEOUT) @Nullable Long socketTimeout,
@JsonProperty(HEADERS) @Nullable Map<String, String> headers) {
@JsonProperty(HEADERS) @Nullable Map<String, String> headers,
@JsonProperty(HTTP_INTERFACE) @Nullable HttpInterface httpInterface) {
this.url = url;
this.connectTimeout = connectTimeout == null ? DEF_CONNECTION_TIMEOUT_SEC : connectTimeout;
this.socketTimeout = socketTimeout == null ? DEF_SOCKET_TIMEOUT_SEC : socketTimeout;
this.headers = headers == null ? DEFAULT_HEADERS : headers;
this.httpInterface = httpInterface == null ? DEFAULT_XYZ_PROTOCOL : httpInterface;
}

public HttpStorageProperties(
@JsonProperty(value = URL, required = true) @NotNull String url,
@JsonProperty(CONNECTION_TIMEOUT) @Nullable Long connectTimeout,
@JsonProperty(SOCKET_TIMEOUT) @Nullable Long socketTimeout,
@JsonProperty(HEADERS) @Nullable Map<String, String> headers) {
this(url, connectTimeout, socketTimeout, headers, DEFAULT_XYZ_PROTOCOL);
}

/**
Expand All @@ -86,4 +102,8 @@ public HttpStorageProperties(
public @NotNull Map<String, String> getHeaders() {
return headers;
}

public @NotNull HttpInterface getProtocol() {
return httpInterface;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.here.naksha.lib.core.models.XyzError;
import com.here.naksha.lib.core.models.storage.*;
import com.here.naksha.lib.core.storage.IReadSession;
import com.here.naksha.storage.http.connector.ConnectorInterfaceReadExecute;
import com.here.naksha.storage.http.ffw.FfwInterfaceReadExecute;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.NotImplementedException;
import org.jetbrains.annotations.NotNull;
Expand All @@ -41,10 +43,18 @@ public final class HttpStorageReadSession implements IReadSession {
@NotNull
private final RequestSender requestSender;

HttpStorageReadSession(@Nullable NakshaContext context, boolean useMaster, @NotNull RequestSender requestSender) {
@NotNull
private final HttpInterface httpInterface;

HttpStorageReadSession(
@Nullable NakshaContext context,
boolean useMaster,
@NotNull RequestSender requestSender,
@NotNull HttpInterface httpInterface) {
this.context = context == null ? NakshaContext.currentContext() : context;
this.useMaster = useMaster;
this.requestSender = requestSender;
this.httpInterface = httpInterface;
}

@Override
Expand Down Expand Up @@ -90,7 +100,13 @@ public void setLockTimeout(long timeout, @NotNull TimeUnit timeUnit) {
@Override
public @NotNull Result execute(@NotNull ReadRequest<?> readRequest) {
try {
return HttpStorageReadExecute.execute(context, (ReadFeaturesProxyWrapper) readRequest, requestSender);
return switch (httpInterface) {
case ffwAdapter -> FfwInterfaceReadExecute.execute(
context, (ReadFeaturesProxyWrapper) readRequest, requestSender);
case dataHubConnector -> ConnectorInterfaceReadExecute.execute(
context, (ReadFeaturesProxyWrapper) readRequest, requestSender);
};

} catch (Exception e) {
log.warn("We got exception while executing Read request.", e);
return new ErrorResult(XyzError.EXCEPTION, e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import com.here.naksha.lib.core.models.Typed;
import com.here.naksha.lib.core.models.XyzError;
import com.here.naksha.lib.core.models.geojson.implementation.XyzFeature;
import com.here.naksha.lib.core.models.payload.responses.ErrorResponse;
import com.here.naksha.lib.core.models.storage.*;
import com.here.naksha.lib.core.util.json.JsonSerializable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.HttpURLConnection;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
Expand All @@ -41,22 +43,28 @@
/**
* Builds a {@link Result} from {@link HttpResponse}
*/
class PrepareResult {
public class PrepareResult {

static Result prepareResult(List<XyzFeature> featureList) {
public static Result prepareResult(List<XyzFeature> featureList) {
return createHttpResultFromFeatureList(featureList);
}

static <T extends Typed> Result prepareResult(
public static <T extends Typed> Result prepareResult(
HttpResponse<byte[]> httpResponse,
Class<T> httpResponseType,
Function<T, List<XyzFeature>> typedResponseToFeatureList) {

XyzError error = mapHttpStatusToErrorOrNull(httpResponse.statusCode());
if (error != null) return new ErrorResult(error, "Response http status code: " + httpResponse.statusCode());

T resultFeatures = JsonSerializable.deserialize(prepareBody(httpResponse), httpResponseType);
return prepareResult(typedResponseToFeatureList.apply(resultFeatures));
String preapredBody = prepareBody(httpResponse);
try {
T resultFeatures = JsonSerializable.deserialize(preapredBody, httpResponseType);
return prepareResult(typedResponseToFeatureList.apply(resultFeatures));
} catch (UncheckedIOException e) {
ErrorResponse errorResponse = JsonSerializable.deserialize(preapredBody, ErrorResponse.class);
return new ErrorResult(errorResponse.getError(), "Error response : " + errorResponse.getErrorMessage());
}
}

private static String prepareBody(HttpResponse<byte[]> response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public RequestSender(@NotNull RequestSender.KeyProperties keyProps) {
* @param endpoint does not contain host:port part, starts with "/".
* @param addHeaders headers to be added to the ones defines {@link KeyProperties#defaultHeaders}.
*/
HttpResponse<byte[]> sendRequest(@NotNull String endpoint, @Nullable Map<String, String> addHeaders) {
public HttpResponse<byte[]> sendRequest(@NotNull String endpoint, @Nullable Map<String, String> addHeaders) {
return sendRequest(endpoint, true, addHeaders, null, null);
}

HttpResponse<byte[]> sendRequest(
public HttpResponse<byte[]> sendRequest(
@NotNull String endpoint,
boolean keepDefHeaders,
@Nullable Map<String, String> headers,
Expand Down
Loading
Loading