summaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-server
diff options
context:
space:
mode:
authorYuanHu <yuan.hu1@zte.com.cn>2018-02-05 12:23:24 +0800
committerYuanHu <yuan.hu1@zte.com.cn>2018-02-05 12:23:24 +0800
commit3a8b80d2c524c0df5aa2032896a04a92ae06a0fc (patch)
treead0a27f37ca84f9cd566241da9a94faf8d8d56e3 /sdc-workflow-designer-server
parentcbf2a87d94e7fadfd82ba2c8c42568588dd3a56c (diff)
Importing Some Tool Class.
Importing tool class for common use. Issue-ID: SDC-981 Change-Id: Ic5c9f3f0c4944c55afb74d940d10f6b5024b6405 Signed-off-by: YuanHu <yuan.hu1@zte.com.cn>
Diffstat (limited to 'sdc-workflow-designer-server')
-rw-r--r--sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/FileCommonUtils.java466
-rw-r--r--sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/RestUtils.java30
-rw-r--r--sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/ToolUtils.java254
-rw-r--r--sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/entity/CommonErrorResponse.java35
4 files changed, 785 insertions, 0 deletions
diff --git a/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/FileCommonUtils.java b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/FileCommonUtils.java
new file mode 100644
index 00000000..6e65a96b
--- /dev/null
+++ b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/FileCommonUtils.java
@@ -0,0 +1,466 @@
+/**
+ * Copyright (c) 2017 ZTE Corporation.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the Apache License, Version 2.0
+ * and the Eclipse Public License v1.0 which both accompany this distribution,
+ * and are available at http://www.eclipse.org/legal/epl-v10.html
+ * and http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Contributors:
+ * ZTE - initial API and implementation and/or initial documentation
+ */
+
+package org.onap.sdc.workflowdesigner.utils;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.UnsupportedEncodingException;
+import java.io.Writer;
+import java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.text.DecimalFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * common utility class.
+ *
+ */
+public class FileCommonUtils {
+ private static final Logger logger = LoggerFactory.getLogger(FileCommonUtils.class);
+
+ private static final int KILO_BYTE = 1024; // 1K
+ private static final int MEGA_BYTE = 1024 * 1024; // 1M
+ private static final int GIGA_BYTE = 1024 * 1024 * 1024; // 1G
+
+ private static final int TRY_COUNT = 3;
+
+ /**
+ *
+ * @param srcAbsolutePath
+ * @param destAbsolutePath
+ */
+ public static void rename(String srcAbsolutePath, String destAbsolutePath) {
+ File dest = new File(destAbsolutePath);
+ new File(srcAbsolutePath).renameTo(dest);
+ }
+
+ /**
+ *
+ * @param filePath
+ * @return
+ */
+ public static String getFileName(String filePath) {
+ return new File(filePath).getName();
+ }
+
+ /**
+ *
+ * @param filePath
+ * @return
+ */
+ public static String getFilePath(String filePath) {
+ return new File(filePath).getParent();
+ }
+
+ /**
+ * @param ins
+ */
+ public static void closeInputStream(InputStream ins) {
+ if (ins != null) {
+ try {
+ ins.close();
+ } catch (IOException e) {
+ logger.info("Close InputStream failed.", e);
+ }
+ }
+ }
+
+ /**
+ *
+ * @param os
+ */
+ public static void closeOutputStream(OutputStream os) {
+ if (os != null) {
+ try {
+ os.close();
+ } catch (IOException e) {
+ logger.info("Close OutputStream failed.", e);
+ }
+ }
+ }
+
+ /**
+ * @param reader
+ */
+ public static void closeReader(Reader reader) {
+ if (reader != null) {
+ try {
+ reader.close();
+ } catch (IOException e) {
+ logger.info("Close Reader failed.", e);
+ }
+ }
+ }
+
+ /**
+ *
+ * @param writer
+ */
+ public static void closeWriter(Writer writer) {
+ if (writer != null) {
+ try {
+ writer.close();
+ } catch (IOException e) {
+ logger.info("Close Writer failed.", e);
+ }
+ }
+ }
+
+ /**
+ * @param sourecePath
+ * @param targetPath
+ * @throws IOException
+ */
+ public static void copy(Path sourecePath, Path targetPath) throws IOException {
+ if (!sourecePath.toFile().exists()) {
+ return;
+ }
+
+ if (Files.isDirectory(sourecePath)) {
+ List<Path> paths = list(sourecePath);
+ for (Path path : paths) {
+ copy(path, targetPath.resolve(path.getFileName()));
+ }
+
+ return;
+ }
+
+ if (!targetPath.getParent().toFile().exists()) {
+ targetPath.getParent().toFile().mkdirs();
+ }
+ Files.copy(sourecePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
+ }
+
+ /**
+ * create folder.
+ *
+ * @param path folder path to create
+ * @return boolean
+ */
+ public static boolean createDirectory(String path) {
+ File folder = new File(path);
+ int tryCount = 0;
+ while (tryCount < FileCommonUtils.TRY_COUNT) {
+ tryCount++;
+ if (!folder.exists() && !folder.mkdirs()) {
+ continue;
+ } else {
+ return true;
+ }
+ }
+
+ return folder.exists();
+ }
+
+ /**
+ * delete the file and file directory.
+ *
+ * @param file file
+ * @return boolean
+ */
+ public static void delete(File file) {
+ logger.info("delete file = {}", file);
+
+ if (file.isDirectory()) {
+ String[] children = file.list();
+ if (children != null) {
+ for (int i = 0; i < children.length; i++) {
+ delete(new File(file, children[i]));
+ }
+ }
+ }
+
+ deleteFile(file);
+ }
+
+ /**
+ *
+ * @param file
+ */
+ public static void deleteFile(File file) {
+ logger.info("deleteFile file = {}", file);
+
+ try {
+ FileUtils.forceDelete(file);
+ } catch (IOException e) {
+ logger.warn("deleteFile error. {}", file, e);
+ }
+ }
+
+ public static String formatFileSize(double fileLength, int fileUnit) {
+ DecimalFormat format = new DecimalFormat("#0.00");
+ return format.format(fileLength / fileUnit) + "M";
+ }
+
+ /**
+ * get file size.
+ *
+ * @param file file which to get the size
+ * @param fileUnit file unit
+ * @return String file size
+ */
+ public static String getFileSize(File file, int fileUnit) {
+ String fileSize = "";
+ DecimalFormat format = new DecimalFormat("#0.00");
+ if (file.exists()) {
+ fileSize = format.format((double) file.length() / fileUnit) + "M";
+ }
+ return fileSize;
+ }
+
+ /**
+ * get file size by content.
+ *
+ * @param contentRange content range
+ * @return String
+ */
+ public static String getFileSizeByContent(String contentRange) {
+ String size =
+ contentRange.substring(contentRange.indexOf("/") + 1, contentRange.length()).trim();
+ return formatFileSize(Double.parseDouble(size), MEGA_BYTE);
+ }
+
+ /**
+ * get the size format according file size.
+ *
+ * @param fileSize file size
+ * @return size format
+ */
+ public static String getFormatFileSize(long fileSize) {
+ if (fileSize >= GIGA_BYTE) {
+ return String.format("%.1f GB", (float) fileSize / (long) GIGA_BYTE);
+ } else if (fileSize >= MEGA_BYTE) {
+ float fi = (float) fileSize / (long) MEGA_BYTE;
+ return String.format(fi > 100 ? "%.0f MB" : "%.1f MB", fi);
+ } else if (fileSize >= KILO_BYTE) {
+ float fi = (float) fileSize / (long) KILO_BYTE;
+ return String.format(fi > 100 ? "%.0f KB" : "%.1f KB", fi);
+ } else {
+ return String.format("%d B", fileSize);
+ }
+ }
+
+ public static long getFileSize(String path) {
+ return getFileSize(new File(path));
+ }
+
+ /**
+ *
+ * @param file
+ * @return
+ */
+ public static long getFileSize(final File file) {
+ if (file.isFile()) {
+ return file.length();
+ }
+
+ final File[] children = file.listFiles();
+ long total = 0;
+ if (children != null) {
+ for (final File child : children) {
+ total += getFileSize(child);
+ }
+ }
+
+ return total;
+ }
+
+ /**
+ *
+ * @param path
+ * @return
+ * @throws IOException
+ */
+ public static List<Path> list(Path path) throws IOException {
+ if (!path.toFile().isDirectory()) {
+ return new ArrayList<>();
+ }
+
+ List<Path> list = new ArrayList<>();
+ DirectoryStream<Path> ds = Files.newDirectoryStream(path);
+ for (Path p : ds) {
+ list.add(p);
+ }
+ return list;
+ }
+
+ /**
+ * @param path
+ * @return
+ * @throws IOException
+ */
+ public static List<String> listFileName(Path path) throws IOException {
+ List<String> list = new ArrayList<>();
+ DirectoryStream<Path> ds = Files.newDirectoryStream(path);
+ for (Path p : ds) {
+ list.add(p.getFileName().toString());
+ }
+
+ return list;
+ }
+
+ /**
+ *
+ * @param srcPath
+ * @param destPath
+ * @throws IOException
+ */
+ public static void moveDirectory(String srcPath, String destPath) throws IOException {
+ File destDirectory = new File(destPath);
+ File srcDirectory = new File(srcPath);
+ FileUtils.deleteDirectory(destDirectory);
+ FileUtils.copyDirectory(srcDirectory, destDirectory);
+ FileUtils.deleteDirectory(srcDirectory);
+ }
+
+ /**
+ *
+ * @param ins
+ * @return
+ * @throws IOException
+ */
+ public static String[] readLines(InputStream ins) throws IOException {
+ InputStreamReader insReader = new InputStreamReader(ins);
+ BufferedReader reader = new BufferedReader(insReader);
+
+ List<String> lineList = new ArrayList<>();
+ String line;
+ try {
+ while ((line = reader.readLine()) != null) {
+ lineList.add(line);
+ }
+ } finally {
+ closeReader(reader);
+ closeReader(insReader);
+ }
+
+ return lineList.toArray(new String[0]);
+ }
+
+ /**
+ *
+ * @param ins
+ * @return
+ * @throws IOException
+ */
+ public static String readString(InputStream ins) throws IOException {
+ return IOUtils.toString(ins, "UTF-8");
+ }
+
+ /**
+ *
+ * @param filePath
+ * @return
+ * @throws IOException
+ */
+ public static String readString(String filePath) throws IOException {
+ InputStream ins = null;
+ try {
+ ins = Files.newInputStream(Paths.get(filePath));
+ return readString(ins);
+ } finally {
+ closeInputStream(ins);
+ }
+ }
+
+ /**
+ *
+ * @param ins
+ * @param path
+ * @param fileName
+ * @return
+ * @throws IOException
+ */
+ public static String saveFile(InputStream ins, String path, String fileName) throws IOException {
+ File tmpPath = new File(path);
+ if (!tmpPath.exists()) {
+ tmpPath.mkdirs();
+ }
+
+ File file = new File(path + File.separator + fileName);
+ OutputStream os = null;
+ try {
+ int read = 0;
+ byte[] bytes = new byte[1024];
+ os = new FileOutputStream(file, true);
+ while ((read = ins.read(bytes)) != -1) {
+ os.write(bytes, 0, read);
+ }
+ os.flush();
+ return file.getAbsolutePath();
+ } finally {
+ closeOutputStream(os);
+ }
+ }
+
+ /**
+ * @param s
+ * @throws IOException
+ * @throws UnsupportedEncodingException
+ */
+ public static void write(String s, String fileName)
+ throws UnsupportedEncodingException, IOException {
+ FileOutputStream out = null;
+ try {
+ out = new FileOutputStream(fileName);
+ out.write(s.getBytes("UTF-8"));
+ out.close();
+ } finally {
+ closeOutputStream(out);
+ }
+
+ }
+
+ /**
+ * @param ss
+ * @param fileName
+ * @throws IOException
+ * @throws UnsupportedEncodingException
+ */
+ public static void write(String[] ss, String fileName)
+ throws UnsupportedEncodingException, IOException {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < ss.length; i++) {
+ sb.append(ss[i]).append(System.lineSeparator());
+ }
+
+ write(sb.toString(), fileName);
+ }
+
+ /**
+ *
+ * @param rootPath
+ * @param more
+ * @return
+ */
+ public static String buildAbsolutePath(String rootPath, String... more) {
+ Path absolutePath = Paths.get(rootPath, more);
+ return absolutePath.toFile().getAbsolutePath();
+ }
+
+}
diff --git a/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/RestUtils.java b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/RestUtils.java
new file mode 100644
index 00000000..c54d0f07
--- /dev/null
+++ b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/RestUtils.java
@@ -0,0 +1,30 @@
+/**
+ * Copyright (c) 2017 ZTE Corporation.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the Apache License, Version 2.0
+ * and the Eclipse Public License v1.0 which both accompany this distribution,
+ * and are available at http://www.eclipse.org/legal/epl-v10.html
+ * and http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Contributors:
+ * ZTE - initial API and implementation and/or initial documentation
+ */
+
+package org.onap.sdc.workflowdesigner.utils;
+
+import javax.ws.rs.InternalServerErrorException;
+import javax.ws.rs.core.Response;
+
+import org.onap.sdc.workflowdesigner.utils.entity.CommonErrorResponse;
+
+/**
+ *
+ * @author 10090474
+ *
+ */
+public class RestUtils {
+ public static InternalServerErrorException newInternalServerErrorException(Exception e) {
+ return new InternalServerErrorException(Response.status(Response.Status.INTERNAL_SERVER_ERROR)
+ .entity(new CommonErrorResponse(e.getMessage())).build(), e);
+ }
+}
diff --git a/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/ToolUtils.java b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/ToolUtils.java
new file mode 100644
index 00000000..2359f714
--- /dev/null
+++ b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/ToolUtils.java
@@ -0,0 +1,254 @@
+/**
+ * Copyright (c) 2017 ZTE Corporation.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the Apache License, Version 2.0
+ * and the Eclipse Public License v1.0 which both accompany this distribution,
+ * and are available at http://www.eclipse.org/legal/epl-v10.html
+ * and http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Contributors:
+ * ZTE - initial API and implementation and/or initial documentation
+ */
+
+package org.onap.sdc.workflowdesigner.utils;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.UUID;
+
+import org.apache.commons.collections.MapUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * common utility class.
+ *
+ */
+public class ToolUtils {
+ static final Logger logger = LoggerFactory.getLogger(ToolUtils.class);
+
+ /**
+ * @param array
+ * @param t
+ * @param ts
+ * @return
+ */
+ public static <T> T[] addElement2Array(T[] array, T t, T[] ts) {
+ List<T> list = new ArrayList<>();
+ if (isNotEmpty(array)) {
+ list.addAll(Arrays.asList(array));
+ }
+ list.add(t);
+
+ return list.toArray(ts);
+ }
+
+ /**
+ *
+ * @param name
+ * @param v
+ * @return
+ */
+ public static <K, V> Entry<K, V> buildEntry(K name, V v) {
+ return buildMapObject(name, v).entrySet().iterator().next();
+ }
+
+ /**
+ * @param key
+ * @param v
+ * @return
+ */
+ public static <K, V> Map<K, V> buildMapObject(K key, V v) {
+ Map<K, V> map = new HashMap<>();
+ map.put(key, v);
+ return map;
+ }
+
+ /**
+ *
+ * @return
+ */
+ public static String generateUUID() {
+ return UUID.randomUUID().toString();
+ }
+
+ /**
+ *
+ * @param path
+ * @return
+ */
+ public static String getRuntimePath(String path) {
+ return Class.class.getClass().getResource("/").getPath() + path;
+ }
+
+ /**
+ *
+ * @param map
+ * @return
+ */
+ @SuppressWarnings("unchecked")
+ public static <K, V> Map<V, K> invertMap(Map<K, V> map) {
+ if (isEmpty(map)) {
+ return new HashMap<>();
+ }
+
+ return MapUtils.invertMap(map);
+ }
+
+ /**
+ *
+ * @param coll
+ * @return
+ */
+ public static boolean isEmpty(Collection<?> coll) {
+ return null == coll || coll.isEmpty();
+ }
+
+ /**
+ * @param map
+ * @return
+ */
+ public static <K, V> boolean isEmpty(Map<K, V> map) {
+ return map == null || map.isEmpty();
+ }
+
+ /**
+ * @param val
+ * @return
+ */
+ public static boolean isEmpty(String val) {
+ return val == null || val.trim().isEmpty();
+ }
+
+ /**
+ *
+ * @param t
+ * @return
+ */
+ public static <T> boolean isEmpty(T t) {
+ return t == null || t.toString().isEmpty();
+ }
+
+ /**
+ *
+ * @param array
+ * @return
+ */
+ public static <T> boolean isEmpty(T[] array) {
+ return array == null || array.length == 0;
+ }
+
+ /**
+ *
+ * @param x
+ * @param y
+ * @return
+ */
+ public static <T> boolean isEqual(final T x, final T y) {
+ return x == y || (x != null && y != null && x.equals(y));
+ }
+
+ /**
+ *
+ * @param coll
+ * @return
+ */
+ public static boolean isNotEmpty(Collection<?> coll) {
+ return null != coll && !coll.isEmpty();
+ }
+
+ /**
+ *
+ * @param map
+ * @return
+ */
+ public static <K, V> boolean isNotEmpty(Map<K, V> map) {
+ return map != null && !map.isEmpty();
+ }
+
+ /**
+ *
+ * @param val
+ * @return
+ */
+ public static boolean isNotEmpty(String val) {
+ return val != null && !val.trim().isEmpty();
+ }
+
+ /**
+ *
+ * @param t
+ * @return
+ */
+ public static <T> boolean isNotEmpty(T t) {
+ return t != null && !t.toString().isEmpty();
+ }
+
+ /**
+ *
+ * @param array
+ * @return
+ */
+ public static <T> boolean isNotEmpty(T[] array) {
+ return array != null && array.length > 0;
+ }
+
+ private static <K, V> Map<K, V> merge(Map<K, V> mapA, Map<K, V> mapB) {
+ Map<K, V> target = new HashMap<>();
+ if (!isEmpty(mapA)) {
+ target.putAll(mapA);
+ }
+ if (!isEmpty(mapB)) {
+ target.putAll(mapB);
+ }
+ return target;
+ }
+
+ /**
+ * @param mapA
+ * @param mapB
+ * @param override
+ * @return
+ */
+ public static <K, V> Map<K, V> merge(Map<K, V> mapA, Map<K, V> mapB, boolean override) {
+ if (override) {
+ return merge(mapA, mapB);
+ }
+
+ return merge(mapB, mapA);
+ }
+
+ /**
+ *
+ * @param keySet
+ * @param origalKey
+ * @return
+ */
+ public static String newNonRepetitiveKey(Set<String> keySet, String origalKey) {
+ return newNonRepetitiveKey(keySet, origalKey, 1);
+ }
+
+ /**
+ *
+ * @param keySet
+ * @param origalKey
+ * @param index
+ * @return
+ */
+ public static String newNonRepetitiveKey(Set<String> keySet, String origalKey, int index) {
+ String key = origalKey + index;
+ while (keySet.contains(key)) {
+ index++;
+ key = origalKey + index;
+ }
+
+ return key;
+ }
+
+}
diff --git a/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/entity/CommonErrorResponse.java b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/entity/CommonErrorResponse.java
new file mode 100644
index 00000000..59ffd602
--- /dev/null
+++ b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/entity/CommonErrorResponse.java
@@ -0,0 +1,35 @@
+/**
+ * Copyright (c) 2017 ZTE Corporation.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the Apache License, Version 2.0
+ * and the Eclipse Public License v1.0 which both accompany this distribution,
+ * and are available at http://www.eclipse.org/legal/epl-v10.html
+ * and http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Contributors:
+ * ZTE - initial API and implementation and/or initial documentation
+ */
+
+package org.onap.sdc.workflowdesigner.utils.entity;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class CommonErrorResponse {
+ private String code;
+ private String message;
+
+ public static Object failure(String message) {
+ return message;
+ }
+
+ public CommonErrorResponse(String message) {
+ super();
+ this.message = message;
+ }
+
+}