diff options
author | Jim Hahn <jrh3@att.com> | 2021-04-07 16:23:10 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2021-04-07 16:27:18 -0400 |
commit | 04623fef95f2b164d79ad48de2ef3708d02eda80 (patch) | |
tree | 4405b33881ee796091eb42f03ea617dfbdcb40ed /utils/src/main | |
parent | 8ebb34d51adc48d94c2ceb5a32d3341b076b8207 (diff) |
Modify StandardCoder to handle empty source
Currently, if the source string or source file is empty, the
StandardCoder just returns null. Modified it to throw an exception in
those cases. However, if the string reference is null, it will still
return null.
Issue-ID: POLICY-3172
Change-Id: Ie7d5e1b0328a24d94a75af8d18a341faebbe5d5f
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'utils/src/main')
-rw-r--r-- | utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java index 7f5e3b85..3b4f3c23 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java @@ -1,8 +1,8 @@ -/* +/*- * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T 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. @@ -40,6 +40,7 @@ import java.io.Writer; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.gson.DoubleConverter; import org.onap.policy.common.gson.GsonMessageBodyHandler; @@ -341,8 +342,17 @@ public class StandardCoder implements Coder { * @param json json string to be decoded * @param clazz class of object to be decoded * @return the object represented by the given json string + * @throws CoderException if the source is empty */ - protected <T> T fromJson(String json, Class<T> clazz) { + protected <T> T fromJson(String json, Class<T> clazz) throws CoderException { + if (json == null) { + return null; + } + + if (StringUtils.isBlank(json)) { + throw new CoderException("source string is empty"); + } + return convertFromDouble(clazz, gson.fromJson(json, clazz)); } @@ -352,9 +362,16 @@ public class StandardCoder implements Coder { * @param source source from which to read the json string to be decoded * @param clazz class of object to be decoded * @return the object represented by the given json string + * @throws CoderException if the source is empty */ - protected <T> T fromJson(Reader source, Class<T> clazz) { - return convertFromDouble(clazz, gson.fromJson(source, clazz)); + protected <T> T fromJson(Reader source, Class<T> clazz) throws CoderException { + T object = gson.fromJson(source, clazz); + + if (object == null) { + throw new CoderException("source is empty"); + } + + return convertFromDouble(clazz, object); } /** |