[SDC] rebase 1710 code
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / util / GeneralUtility.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.common.util;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.regex.Pattern;
27
28 import org.apache.commons.codec.binary.Base64;
29 import org.apache.commons.io.FileUtils;
30 import org.openecomp.sdc.common.api.Constants;
31
32 public class GeneralUtility {
33
34         public static boolean generateTextFile(String fileName, String fileData) {
35                 boolean isSuccessfull = true;
36                 try {
37                         FileUtils.writeStringToFile(new File(fileName), fileData);
38                 } catch (IOException e) {
39                         isSuccessfull = false;
40                 }
41                 return isSuccessfull;
42         }
43         
44         /**
45          * Use with care, usage is not advised!!!
46          * The method only checks if String does not contain special characters + length divided by 4 with no remainder.
47          * The methods contained in other common libraries do the same.
48          */
49         public static boolean isBase64Encoded(byte[] data) {
50                 return Base64.isBase64(data);
51         }
52         
53         /**
54          *Use with care, usage is not advised!!!
55          * The method only checks if String does not contain special characters + length divided by 4 with no remainder.
56          * The methods contained in other common libraries do the same.
57          */
58         public static boolean isBase64Encoded(String str) {
59                 boolean isEncoded = false;
60                 try {
61                         // checks if the string was properly padded to the
62                         isEncoded = ((str.length() % 4 == 0) && (Pattern.matches("\\A[a-zA-Z0-9/+]+={0,2}\\z", str)));
63                         if (isEncoded) {
64                                 // If no exception is caught, then it is possibly a base64
65                                 // encoded string
66                                 byte[] data = Base64.decodeBase64(str);
67                         }
68
69                 } catch (Exception e) {
70                         // If exception is caught, then it is not a base64 encoded string
71                         isEncoded = false;
72                 }
73                 return isEncoded;
74         }
75
76         /**
77          * Checks whether the passed string exceeds a limit of number of characters.
78          * 
79          * @param str
80          * @param limit
81          * @return the result of comparison, or false if str is null.
82          */
83         public static boolean isExceedingLimit(String str, int limit) {
84                 if (str == null) {
85                         return false;
86                 }
87                 return str.length() > limit;
88         }
89
90         /**
91          * Checks the passed string list whether the cumulative length of strings and delimiters between them exceeds a limit of number of characters. For example for list ("one","two","three") with delimiter "," the length of list is calculated
92          * 3+1+3+1+5=13
93          *
94          * @param strList
95          * @param limit
96          * @param delimiterLength
97          *            - 0 if there is no delimeter.
98          * @return the result of comparison, or false if strList is null.
99          */
100         public static boolean isExceedingLimit(List<String> strList, int limit, int delimiterLength) {
101                 if (strList == null || strList.isEmpty()) {
102                         return false;
103                 }
104                 int sum = 0;
105                 int size = strList.size();
106                 for (int i = 0; i < size - 1; i++) {
107                         String str = strList.get(i);
108                         if (str != null) {
109                                 sum += str.length();
110                         }
111                         sum += delimiterLength;
112                 }
113                 String str = strList.get(size - 1);
114                 if (str != null) {
115                         sum += str.length();
116                 }
117                 return sum > limit;
118         }
119
120         /**
121          * Return the extension as the substring from the last dot. For input "kuku.txt", "txt" will be returned. If no dot is found or input is null, empty string is returned.
122          * 
123          * @param fileName
124          * @return extension
125          */
126         public static String getFilenameExtension(String fileName) {
127                 String res = Constants.EMPTY_STRING;
128                 if (fileName != null) {
129                         int indexOf = fileName.lastIndexOf('.');
130                         if (indexOf != -1 && indexOf < (fileName.length() - 1)) {
131                                 res = fileName.substring(indexOf + 1);
132                         }
133                 }
134                 return res;
135         }
136
137         public static String calculateMD5ByByteArray(byte[] payload) {
138                 String decodedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(payload);
139                 byte[] encodeMd5 = Base64.encodeBase64(decodedMd5.getBytes());
140                 return new String(encodeMd5);
141
142         }
143
144         /**
145          * 
146          * @param data
147          * @return
148          */
149         public static String calculateMD5ByString(String data) {
150                 String calculatedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data);
151
152                 // encode base-64 result
153                 byte[] encodeBase64 = Base64.encodeBase64(calculatedMd5.getBytes());
154                 return new String(encodeBase64);
155         }
156         
157         
158         /**
159          * 
160          * @param String
161          * @return String is null or Empty
162          */
163         public static boolean isEmptyString(String str) {
164         return str == null || str.trim().isEmpty();
165     }
166 }