diff options
author | Joanna Jeremicz <joanna.jeremicz@nokia.com> | 2019-10-02 07:15:16 +0200 |
---|---|---|
committer | Joanna Jeremicz <joanna.jeremicz@nokia.com> | 2019-10-11 14:30:13 +0200 |
commit | 19bb7116409b4eaa546b48b7d7389e4de04e4f03 (patch) | |
tree | 7fdcd4f631506196416157fa607003bf654ad24b /rest-services/http-client | |
parent | 6f66c52a858038d9915251dcdd9fab5e2551394b (diff) |
Restructure aai-client
and get rid of common-dependency module
Issue-ID: DCAEGEN2-1792
Change-Id: Ifcc69a917b2aec02615df93d10979d6c38a25ff2
Signed-off-by: Joanna Jeremicz <joanna.jeremicz@nokia.com>
Diffstat (limited to 'rest-services/http-client')
7 files changed, 494 insertions, 6 deletions
diff --git a/rest-services/http-client/pom.xml b/rest-services/http-client/pom.xml index dd5be34e..d3b8f124 100644 --- a/rest-services/http-client/pom.xml +++ b/rest-services/http-client/pom.xml @@ -28,7 +28,7 @@ <parent> <groupId>org.onap.dcaegen2.services.sdk</groupId> <artifactId>dcaegen2-services-sdk-rest-services</artifactId> - <version>1.3.1-SNAPSHOT</version> + <version>1.3.2-SNAPSHOT</version> </parent> <groupId>org.onap.dcaegen2.services.sdk.rest.services</groupId> @@ -45,11 +45,6 @@ <version>${project.version}</version> </dependency> <dependency> - <groupId>org.onap.dcaegen2.services.sdk.rest.services</groupId> - <artifactId>common-dependency</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> <groupId>io.projectreactor.netty</groupId> <artifactId>reactor-netty</artifactId> </dependency> @@ -62,6 +57,10 @@ <artifactId>value</artifactId> </dependency> <dependency> + <groupId>org.immutables</groupId> + <artifactId>gson</artifactId> + </dependency> + <dependency> <groupId>org.jetbrains</groupId> <artifactId>annotations</artifactId> </dependency> diff --git a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/URI.java b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/URI.java new file mode 100644 index 00000000..6b74a82a --- /dev/null +++ b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/URI.java @@ -0,0 +1,164 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2019 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + * + */ + +package org.onap.dcaegen2.services.sdk.rest.services.uri; + +public final class URI { + private String scheme; + private String host; + private int port; + private String path; + private String fragment; + private String authority; + private String userInfo; + private String query; + private String schemeSpecificPart; + private String string; + + private URI() { + } + + public static final class URIBuilder { + private String scheme; + private String host; + private int port; + private String path; + private String fragment; + private String authority; + private String userInfo; + private String query; + private String schemeSpecificPart; + + public URIBuilder scheme(String scheme) { + this.scheme = scheme; + return this; + } + + public URIBuilder host(String host) { + this.host = host; + return this; + } + + public URIBuilder port(int port) { + this.port = port; + return this; + } + + public URIBuilder path(String path) { + this.path = path; + return this; + } + + public URIBuilder fragment(String fragment) { + this.fragment = fragment; + return this; + } + + public URIBuilder authority(String authority) { + this.authority = authority; + return this; + } + + public URIBuilder userInfo(String userInfo) { + this.userInfo = userInfo; + return this; + } + + public URIBuilder query(String query) { + this.query = query; + return this; + } + + public URIBuilder schemeSpecificPart(String schemeSpecificPart) { + this.schemeSpecificPart = schemeSpecificPart; + return this; + } + + public URI build() { + URI uri = new URI(); + uri.scheme = this.scheme; + uri.host = this.host; + uri.port = this.port; + uri.path = this.path; + uri.fragment = this.fragment; + uri.authority = this.authority; + uri.userInfo = this.userInfo; + uri.query = this.query; + uri.schemeSpecificPart = this.schemeSpecificPart; + return uri; + } + } + + @Override + public String toString() { + defineString(); + return string; + } + + private void defineString() { + if (string != null) return; + + StringBuffer sb = new StringBuffer(); + if (scheme != null) { + sb.append(scheme); + sb.append(':'); + } + if (isOpaque()) { + sb.append(schemeSpecificPart); + } else { + if (host != null) { + sb.append("//"); + if (userInfo != null) { + sb.append(userInfo); + sb.append('@'); + } + boolean needBrackets = ((host.indexOf(':') >= 0) + && !host.startsWith("[") + && !host.endsWith("]")); + if (needBrackets) sb.append('['); + sb.append(host); + if (needBrackets) sb.append(']'); + if (port != -1) { + sb.append(':'); + sb.append(port); + } + } else if (authority != null) { + sb.append("//"); + sb.append(authority); + } + if (path != null) + sb.append(path); + if (query != null) { + sb.append('?'); + sb.append(query); + } + } + if (fragment != null) { + sb.append('#'); + sb.append(fragment); + } + string = sb.toString(); + } + + private boolean isOpaque() { + return path == null; + } +}
\ No newline at end of file diff --git a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/GlobalDiagnosticContext.java b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/GlobalDiagnosticContext.java new file mode 100644 index 00000000..db4a0fd6 --- /dev/null +++ b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/GlobalDiagnosticContext.java @@ -0,0 +1,71 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019 Nokia. All rights reserved. + * ========================================================= + * 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. + * ============LICENSE_END===================================== + */ + +package org.onap.dcaegen2.services.sdk.rest.services.model.logging; + +import io.vavr.collection.HashMap; +import io.vavr.collection.Map; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.UUID; +import org.immutables.value.Value; +import org.jetbrains.annotations.Nullable; +import org.slf4j.MDC; + +/** + * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> + * @since 1.1.2 + */ +@Value.Immutable(singleton = true) +public interface GlobalDiagnosticContext { + + @Value.Default + default String instanceId() { + return UUID.randomUUID().toString(); + } + + @Value.Default + default String serverFqdn() { + try { + return InetAddress.getLocalHost().toString(); + } catch (UnknownHostException ex) { + return InetAddress.getLoopbackAddress().toString(); + } + } + + @Value.Default + default String serviceName() { + return System.getenv().getOrDefault("HOSTNAME", "unknown_service"); + } + + @Value.Derived + default Map<String, String> asMap() { + return HashMap.of( + MdcVariables.INSTANCE_ID, instanceId(), + MdcVariables.SERVER_FQDN, serverFqdn(), + MdcVariables.SERVICE_NAME, serviceName()); + } + + static GlobalDiagnosticContext instance() { + return ImmutableGlobalDiagnosticContext.of(); + } +} + + diff --git a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/MdcVariables.java b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/MdcVariables.java new file mode 100644 index 00000000..652e3541 --- /dev/null +++ b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/MdcVariables.java @@ -0,0 +1,65 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.sdk.rest.services.model.logging; + +import org.slf4j.MDC; + +import java.util.Map; + +public final class MdcVariables { + + @Deprecated + public static final String X_ONAP_REQUEST_ID = "X-ONAP-RequestID"; + @Deprecated + public static final String X_INVOCATION_ID = "X-InvocationID"; + + public static final String INSTANCE_UUID = "InstanceUUID"; + public static final String RESPONSE_CODE = "ResponseCode"; + public static final String REQUEST_ID = "RequestID"; + public static final String CLIENT_NAME = "PartnerName"; + public static final String CLIENT_IP = "ClientIPAddress"; + public static final String INVOCATION_ID = "InvocationID"; + public static final String INVOCATION_TIMESTAMP = "InvokeTimestamp"; + public static final String STATUS_CODE = "StatusCode"; + public static final String INSTANCE_ID = "InstanceID"; + public static final String SERVER_FQDN = "ServerFQDN"; + public static final String SERVICE_NAME = "ServiceName"; + + private static final String HTTP_HEADER_PREFIX = "X-"; + + private MdcVariables() { + } + + public static String httpHeader(String mdcName) { + return HTTP_HEADER_PREFIX + mdcName; + } + + /** + * @deprecated use {@link RequestDiagnosticContext#withSlf4jMdc(Runnable)}. + * @param mdcContextMap + */ + @Deprecated + public static void setMdcContextMap(Map<String, String> mdcContextMap) { + if (mdcContextMap != null) { + MDC.setContextMap(mdcContextMap); + } + } +} diff --git a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/RequestDiagnosticContext.java b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/RequestDiagnosticContext.java new file mode 100644 index 00000000..0c4a4b1f --- /dev/null +++ b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/RequestDiagnosticContext.java @@ -0,0 +1,108 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019 Nokia. All rights reserved. + * ========================================================= + * 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. + * ============LICENSE_END===================================== + */ + +package org.onap.dcaegen2.services.sdk.rest.services.model.logging; + +import io.vavr.collection.HashMap; +import io.vavr.collection.Map; +import java.util.UUID; +import org.immutables.value.Value; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.slf4j.MDC; + +/** + * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> + * @since 1.1.2 + */ +@Value.Immutable +public interface RequestDiagnosticContext { + + UUID requestId(); + + @Nullable UUID invocationId(); + + @Value.Default + default GlobalDiagnosticContext global() { + return GlobalDiagnosticContext.instance(); + } + + @Value.Derived + default Map<String, String> remoteCallHttpHeaders() { + java.util.Map<String, String> result = new java.util.HashMap<>(); + + result.put(MdcVariables.httpHeader(MdcVariables.REQUEST_ID), requestId().toString()); + + if (invocationId() != null) { + result.put(MdcVariables.httpHeader(MdcVariables.INVOCATION_ID), invocationId().toString()); + } + + return HashMap.ofAll(result); + } + + @Value.Derived + default Map<String, String> asMap() { + java.util.Map<String, String> result = new java.util.HashMap<>(); + + if (requestId() != null) { + result.put(MdcVariables.REQUEST_ID, requestId().toString()); + } + + if (invocationId() != null) { + result.put(MdcVariables.INVOCATION_ID, invocationId().toString()); + } + + return global().asMap().merge(HashMap.ofAll(result)); + } + + default void withSlf4jMdc(Runnable runnable) { + withSlf4jMdc(true, runnable); + } + + default void withSlf4jMdc(boolean loglevelEnabled, Runnable runnable) { + if (loglevelEnabled) { + final java.util.Map<String, String> ctxBefore = MDC.getCopyOfContextMap(); + try { + MDC.setContextMap(asMap().toJavaMap()); + runnable.run(); + } finally { + if (ctxBefore == null) { + MDC.clear(); + } else { + MDC.setContextMap(ctxBefore); + } + } + } + } + + default @NotNull RequestDiagnosticContext withNewInvocationId() { + return ImmutableRequestDiagnosticContext.copyOf(this) + .withInvocationId(UUID.randomUUID()); + } + + static ImmutableRequestDiagnosticContext create() { + return ImmutableRequestDiagnosticContext.builder() + .requestId(UUID.randomUUID()) + .invocationId(UUID.randomUUID()) + .build(); + } +} + + diff --git a/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/URITest.java b/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/URITest.java new file mode 100644 index 00000000..b4a59638 --- /dev/null +++ b/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/URITest.java @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2019 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + * + */ + +package org.onap.dcaegen2.services.sdk.rest.services.uri; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class URITest { + + @Test + void buildProperUri() { + String expectedValue = "http://user@localhost:8080path?query#fragment"; + URI uri = new URI.URIBuilder().scheme("http") + .host("localhost") + .port(8080) + .path("path") + .fragment("fragment") + .authority("authority") + .userInfo("user") + .query("query") + .build(); + + assertEquals(expectedValue, uri.toString()); + } +}
\ No newline at end of file diff --git a/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/MdcVariablesTest.java b/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/MdcVariablesTest.java new file mode 100644 index 00000000..bb0cc6cf --- /dev/null +++ b/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/logging/MdcVariablesTest.java @@ -0,0 +1,36 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.sdk.rest.services.model.logging; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class MdcVariablesTest { + + @Test + void shouldReturnProperHttpHeader() { + String expectedValue = "X-header"; + String returnedValue = MdcVariables.httpHeader("header"); + + assertEquals(expectedValue, returnedValue); + } +}
\ No newline at end of file |