Fix the ssl config
[clamp.git] / src / main / java / org / onap / clamp / clds / util / ResourceFileUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.util;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.Scanner;
29
30 /**
31  * Utility methods supporting resources accesses.
32  */
33 public final class ResourceFileUtils {
34
35     /**
36      * getResourceAsStram supports the "file:" prefix as they use URL.
37      * So here we want to eliminate classpath: prefix, so that this class can get
38      * files from jar resource or file system.
39      */
40
41     private static final String CLASSPATH_PREFIX = "classpath:";
42
43     /**
44      * Private constructor to avoid creating instances of util class.
45      */
46     private ResourceFileUtils() {
47     }
48
49     /**
50      * Method to access a file from the jar resource folder or file system.
51      * Give the prefix "classpath:" so that it accesses the jar resource folder (default case)
52      * or the prefix "file:" so that it accesses the file system.
53      *
54      * @param fileName The path of the resource (no prefix it will be a classpath access,
55      *                 "classpath:/myfilename" or "file:/myfilename")
56      * @return The file as inputStream
57      */
58     public static InputStream getResourceAsStream(String fileName) {
59         InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(
60                 fileName.startsWith(CLASSPATH_PREFIX) ? fileName.replaceFirst(CLASSPATH_PREFIX, "") : fileName);
61         if (is == null) {
62             throw new IllegalArgumentException("Unable to find resource: " + fileName);
63         }
64         return is;
65     }
66
67     /**
68      * Method to access a resource file as a string.
69      * Give the prefix "classpath:" so that it accesses the jar resource folder (default case)
70      * or the prefix "file:" so that it accesses the file system.
71      *
72      * @param fileName The path of the resource (no prefix it will be a classpath access,
73      *                 "classpath:/myfilename" or "file:/myfilename")
74      * @return The file as String
75      * @throws IOException In case of failure to find the file.
76      */
77     public static String getResourceAsString(String fileName) throws IOException {
78         try (InputStream is = getResourceAsStream(fileName)) {
79             return streamToString(is);
80         }
81     }
82
83     private static String streamToString(InputStream inputStream) {
84         try (Scanner scanner = new Scanner(inputStream)) {
85             Scanner delimitedScanner = scanner.useDelimiter("\\A");
86             return delimitedScanner.hasNext() ? delimitedScanner.next() : "";
87         }
88     }
89 }