2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.common.util;
23 import org.apache.commons.codec.binary.Base64;
24 import org.apache.commons.io.FileUtils;
25 import org.openecomp.sdc.common.api.Constants;
28 import java.io.IOException;
29 import java.util.List;
30 import java.util.regex.Pattern;
32 public class GeneralUtility {
34 public static boolean generateTextFile(String fileName, String fileData) {
35 boolean isSuccessfull = true;
37 FileUtils.writeStringToFile(new File(fileName), fileData);
38 } catch (IOException e) {
39 isSuccessfull = false;
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.
49 public static boolean isBase64Encoded(byte[] data) {
50 return Base64.isBase64(data);
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.
58 public static boolean isBase64Encoded(String str) {
59 boolean isEncoded = false;
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)));
64 // If no exception is caught, then it is possibly a base64
66 byte[] data = Base64.decodeBase64(str);
69 } catch (Exception e) {
70 // If exception is caught, then it is not a base64 encoded string
77 * Checks whether the passed string exceeds a limit of number of characters.
81 * @return the result of comparison, or false if str is null.
83 public static boolean isExceedingLimit(String str, int limit) {
87 return str.length() > limit;
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
96 * @param delimiterLength
97 * - 0 if there is no delimeter.
98 * @return the result of comparison, or false if strList is null.
100 public static boolean isExceedingLimit(List<String> strList, int limit, int delimiterLength) {
101 if (strList == null || strList.isEmpty()) {
105 int size = strList.size();
106 for (int i = 0; i < size - 1; i++) {
107 String str = strList.get(i);
111 sum += delimiterLength;
113 String str = strList.get(size - 1);
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.
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);
137 public static String calculateMD5Base64EncodedByByteArray(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);
149 public static String calculateMD5Base64EncodedByString(String data) {
150 String calculatedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data);
152 // encode base-64 result
153 byte[] encodeBase64 = Base64.encodeBase64(calculatedMd5.getBytes());
154 return new String(encodeBase64);
161 * @return String is null or Empty
163 public static boolean isEmptyString(String str) {
164 return str == null || str.trim().isEmpty();