diff options
author | Jim Hahn <jrh3@att.com> | 2020-05-29 16:32:57 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-05-29 16:51:29 -0400 |
commit | 45e3d3ae1cc5a4c29526da4f4cb6096e1b6d7d4f (patch) | |
tree | 02b5f1fa7f150ef86846d37143b008e292ce9eaa /models-interactions/model-actors/actorServiceProvider/src | |
parent | 6d5459569af6a9c70abf5a8bbb54917decd37b38 (diff) |
Use "coder" to serialize Actor requests
Modified the Actors to use the "coder" to serialize requests
instead of defaulting to the HttpClient serialization provider.
Decided to just pretty-print the requests since that can be used for
both logging and transmission, which avoids serializing the request twice.
Issue-ID: POLICY-2601
Change-Id: I190ed19dd852a1aa66156b358cbc97c3b121af1f
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'models-interactions/model-actors/actorServiceProvider/src')
4 files changed, 39 insertions, 31 deletions
diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperation.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperation.java index 8285a5635..2a460667a 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperation.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperation.java @@ -166,18 +166,8 @@ public abstract class BidirectionalTopicOperation<Q, S> extends OperationPartial * @param request request to be published */ protected void publishRequest(Q request) { - String json; - try { - if (request instanceof String) { - json = request.toString(); - } else { - json = makeCoder().encode(request); - } - } catch (CoderException e) { - throw new IllegalArgumentException("cannot encode request", e); - } - - logMessage(EventType.OUT, topicHandler.getSinkTopicCommInfrastructure(), topicHandler.getSinkTopic(), request); + String json = prettyPrint(request); + logMessage(EventType.OUT, topicHandler.getSinkTopicCommInfrastructure(), topicHandler.getSinkTopic(), json); if (!topicHandler.send(json)) { throw new IllegalStateException("nothing published"); diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperation.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperation.java index 1acc1ff65..766468c07 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperation.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperation.java @@ -177,7 +177,7 @@ public abstract class HttpOperation<T> extends OperationPartial { logger.info("{}.{}: response received for {}", params.getActor(), params.getOperation(), params.getRequestId()); - String strResponse = HttpClient.getBody(rawResponse, String.class); + String strResponse = rawResponse.readEntity(String.class); logMessage(EventType.IN, CommInfrastructure.REST, url, strResponse); diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java index 4aa723614..1df318799 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java @@ -989,30 +989,24 @@ public abstract class OperationPartial implements Operation { } /** - * Logs a response. If the response is not of type, String, then it attempts to + * Logs a message. If the message is not of type, String, then it attempts to * pretty-print it into JSON before logging. * * @param direction IN or OUT * @param infra communication infrastructure on which it was published * @param source source name (e.g., the URL or Topic name) - * @param response response to be logged + * @param message message to be logged * @return the JSON text that was logged */ - public <T> String logMessage(EventType direction, CommInfrastructure infra, String source, T response) { + public <T> String logMessage(EventType direction, CommInfrastructure infra, String source, T message) { String json; try { - if (response == null) { - json = null; - } else if (response instanceof String) { - json = response.toString(); - } else { - json = makeCoder().encode(response, true); - } + json = prettyPrint(message); - } catch (CoderException e) { + } catch (IllegalArgumentException e) { String type = (direction == EventType.IN ? "response" : "request"); logger.warn("cannot pretty-print {}", type, e); - json = response.toString(); + json = message.toString(); } logger.info("[{}|{}|{}|]{}{}", direction, infra, source, NetLoggerUtil.SYSTEM_LS, json); @@ -1020,6 +1014,28 @@ public abstract class OperationPartial implements Operation { return json; } + /** + * Converts a message to a "pretty-printed" String using the operation's normal + * serialization provider (i.e., it's <i>coder</i>). + * + * @param message response to be logged + * @return the JSON text that was logged + * @throws IllegalArgumentException if the message cannot be converted + */ + public <T> String prettyPrint(T message) { + if (message == null) { + return null; + } else if (message instanceof String) { + return message.toString(); + } else { + try { + return makeCoder().encode(message, true); + } catch (CoderException e) { + throw new IllegalArgumentException("cannot encode message", e); + } + } + } + // these may be overridden by subclasses or junit tests /** diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperationTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperationTest.java index eb1aa880f..9ac88f488 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperationTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperationTest.java @@ -532,14 +532,15 @@ public class HttpOperationTest { MyRequest request = new MyRequest(); - Entity<MyRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON); - Map<String, Object> headers = makeHeaders(); headers.put("Accept", MediaType.APPLICATION_JSON); String url = getUrl(); - logMessage(EventType.OUT, CommInfrastructure.REST, url, request); + String strRequest = prettyPrint(request); + logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest); + + Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON); // @formatter:off return handleResponse(outcome, url, @@ -558,14 +559,15 @@ public class HttpOperationTest { MyRequest request = new MyRequest(); - Entity<MyRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON); - Map<String, Object> headers = makeHeaders(); headers.put("Accept", MediaType.APPLICATION_JSON); String url = getUrl(); - logMessage(EventType.OUT, CommInfrastructure.REST, url, request); + String strRequest = prettyPrint(request); + logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest); + + Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON); // @formatter:off return handleResponse(outcome, url, |