2 * ============LICENSE_START=======================================================
3 * DCAEGEN2-SERVICES-SDK
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer;
26 import java.util.Optional;
27 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient;
28 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
29 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.DMaaPAbstractReactiveHttpClient;
30 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.DMaaPClientServiceUtils;
31 import org.onap.dcaegen2.services.sdk.rest.services.model.DmaapModel;
32 import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder;
33 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
34 import org.onap.dcaegen2.services.sdk.rest.services.uri.URI.URIBuilder;
35 import reactor.core.publisher.Mono;
39 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
41 public class DMaaPPublisherReactiveHttpClient extends DMaaPAbstractReactiveHttpClient {
43 private final DmaapPublisherConfiguration dmaapPublisherConfiguration;
44 private final JsonBodyBuilder jsonBodyBuilder;
45 private final CloudHttpClient cloudHttpClient;
48 * Constructor DMaaPPublisherReactiveHttpClient.
50 * @param dmaapPublisherConfiguration - DMaaP producer configuration object
51 * @param cloudHttpClient - cloudHttpClient sending http requests
53 DMaaPPublisherReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration,
54 CloudHttpClient cloudHttpClient, JsonBodyBuilder jsonBodyBuilder) {
55 this.dmaapPublisherConfiguration = dmaapPublisherConfiguration;
56 this.cloudHttpClient = cloudHttpClient;
57 this.jsonBodyBuilder = jsonBodyBuilder;
61 * Function for calling DMaaP HTTP producer - post request to DMaaP.
63 * @param dmaapModel - object which will be sent to DMaaP
64 * @return status code of operation
67 public Mono<Integer> getDMaaPProducerResponse(DmaapModel dmaapModel,
68 Optional<RequestDiagnosticContext> requestDiagnosticContextOptional) {
69 return Mono.defer(() -> {
70 Map<String, String> headers = DMaaPClientServiceUtils.getHeaders(dmaapPublisherConfiguration.dmaapContentType());
71 if (requestDiagnosticContextOptional.isPresent()) {
73 .post(getUri().toString(), requestDiagnosticContextOptional.get(), headers, jsonBodyBuilder,
76 return cloudHttpClient
77 .post(getUri().toString(), getRequestDiagnosticContext(), headers, jsonBodyBuilder, dmaapModel);
84 new URIBuilder().scheme(dmaapPublisherConfiguration.dmaapProtocol())
85 .host(dmaapPublisherConfiguration.dmaapHostName()).port(dmaapPublisherConfiguration.dmaapPortNumber())
86 .path(createRequestPath())
90 private String createRequestPath() {
91 return new StringBuilder().append(SLASH).append(dmaapPublisherConfiguration.dmaapTopicName()).toString();