diff options
5 files changed, 176 insertions, 1 deletions
diff --git a/gson/pom.xml b/gson/pom.xml index d02ede1f..7d7797a2 100644 --- a/gson/pom.xml +++ b/gson/pom.xml @@ -47,6 +47,11 @@ <artifactId>gson</artifactId> </dependency> <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <scope>provided</scope> + </dependency> + <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <scope>test</scope> diff --git a/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java b/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java index fc8e23ef..9dad6db8 100644 --- a/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java +++ b/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java @@ -21,6 +21,7 @@ package org.onap.policy.common.gson; import com.google.gson.Gson; + import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -36,6 +37,9 @@ import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.ext.MessageBodyReader; import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; + +import lombok.Getter; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,7 +56,8 @@ public class GsonMessageBodyHandler implements MessageBodyReader<Object>, Messag /** * Object to be used to serialize and de-serialize. */ - private Gson gson; + @Getter + private final Gson gson; /** * Constructs the object, using a plain Gson object. diff --git a/lombok.config b/lombok.config new file mode 100644 index 00000000..df71bb6a --- /dev/null +++ b/lombok.config @@ -0,0 +1,2 @@ +config.stopBubbling = true +lombok.addLombokGeneratedAnnotation = true diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java b/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java new file mode 100644 index 00000000..5aeacf26 --- /dev/null +++ b/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java @@ -0,0 +1,110 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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_END========================================================= + */ + +package org.onap.policy.common.utils.resources; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.file.Files; + +/** + * The Class TextFileUtils is class that provides useful functions for handling text files. Functions to read and write + * text files to strings and strings are provided. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public abstract class TextFileUtils { + private static final String UTF_8 = "UTF-8"; + private static final int READER_CHAR_BUFFER_SIZE_4096 = 4096; + + private TextFileUtils() { + // This class cannot be initialized + } + + /** + * Method to return the contents of a text file as a string. + * + * @param textFilePath The path to the file as a string + * @return A string containing the contents of the file + * @throws IOException on errors reading text from the file + */ + public static String getTextFileAsString(final String textFilePath) throws IOException { + final File textFile = new File(textFilePath); + return new String(Files.readAllBytes(textFile.toPath()), UTF_8); + } + + /** + * Method to write contents of a string to a text file. + * + * @param outString The string to write + * @param textFilePath The path to the file as a string + * @throws IOException on errors reading text from the file + */ + public static void putStringAsTextFile(final String outString, final String textFilePath) throws IOException { + final File textFile = new File(textFilePath); + putStringAsFile(outString, textFile); + } + + /** + * Method to write contents of a string to a text file. + * + * @param outString The string to write + * @param textFile The file to write the string to + * @throws IOException on errors reading text from the file + */ + public static void putStringAsFile(final String outString, final File textFile) throws IOException { + Files.write(textFile.toPath(), outString.getBytes(UTF_8)); + } + + /** + * Method to return the contents of a text steam as a string. + * + * @param textStream The stream + * @return A string containing the output of the stream as text + * @throws IOException on errors reading text from the file + */ + public static String getStreamAsString(final InputStream textStream) throws IOException { + return getReaderAsString(new InputStreamReader(textStream, UTF_8)); + } + + /** + * Method to return the contents of a reader steam as a string. This closes the reader after use + * + * @param textReader The reader + * @return A string containing the output of the reader as text + * @throws IOException on errors reading text from the file + */ + public static String getReaderAsString(final Reader textReader) throws IOException { + final StringBuilder builder = new StringBuilder(); + int charsRead = -1; + final char[] chars = new char[READER_CHAR_BUFFER_SIZE_4096]; + do { + charsRead = textReader.read(chars); + if (charsRead > 0) { + builder.append(chars, 0, charsRead); + } + } + while (charsRead > 0); + return builder.toString(); + } +} diff --git a/utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java new file mode 100644 index 00000000..7f246ab2 --- /dev/null +++ b/utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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_END========================================================= + */ + +package org.onap.policy.common.utils.resources; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +import org.junit.Test; + +/** + * Test text file utilities. + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class TextFileUtilsTest { + + private static final String FILE_CONTENT = "This is the contents of a text file"; + + @Test + public void test() throws IOException { + final File tempTextFile = File.createTempFile("Test", "txt"); + + TextFileUtils.putStringAsTextFile(FILE_CONTENT, tempTextFile.getAbsolutePath()); + + final String textFileString0 = TextFileUtils.getTextFileAsString(tempTextFile.getAbsolutePath()); + assertEquals(FILE_CONTENT, textFileString0); + + final FileInputStream fis = new FileInputStream(tempTextFile); + final String textFileString1 = TextFileUtils.getStreamAsString(fis); + assertEquals(textFileString0, textFileString1); + + } +} |