aboutsummaryrefslogtreecommitdiffstats
path: root/vnfmarket-be/vnf-sdk-marketplace/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'vnfmarket-be/vnf-sdk-marketplace/src/main')
-rw-r--r--vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/JsonUtil.java47
-rw-r--r--vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/ToolUtil.java510
2 files changed, 219 insertions, 338 deletions
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/JsonUtil.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/JsonUtil.java
index eb471907..1a47522c 100644
--- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/JsonUtil.java
+++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/JsonUtil.java
@@ -16,12 +16,10 @@
package org.onap.vnfsdk.marketplace.common;
-import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
-import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -48,7 +46,7 @@ public final class JsonUtil {
public static String toJson(Object obj) {
try {
return new ObjectMapper().writeValueAsString(obj);
- } catch(IOException ex) {
+ } catch (IOException ex) {
LOGGER.error("Parser to json error.", ex);
throw new IllegalArgumentException("Parser obj to json error, obj = " + obj, ex);
}
@@ -67,52 +65,11 @@ public final class JsonUtil {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper.readValue(jsonStr, objClass);
- } catch(IOException ex) {
+ } catch (IOException ex) {
LOGGER.error("Parser to object error.", ex);
throw new IllegalArgumentException(
"Parser json to object error, json = " + jsonStr + ", expect class = " + objClass, ex);
}
}
- /**
- * Convert JSON to object.<br>
- *
- * @param jsonStr The JSON to be converted
- * @param typeRef The object type
- * @return The typeRef object
- * @since GSO 0.5
- */
- public static <T> T fromJson(String jsonStr, TypeReference<T> typeRef) {
- try {
- ObjectMapper mapper = new ObjectMapper();
- mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
- return mapper.readValue(jsonStr, typeRef);
- } catch(IOException ex) {
- LOGGER.error("Parser to object by type reference error.", ex);
- throw new IllegalArgumentException(
- "Parser json to object error, json = " + jsonStr + ", expect type = " + typeRef.getType(), ex);
- }
- }
-
- /**
- * Turn a json file in to a java object. <br>
- *
- * @param file the json file need to change.
- * @param objClass the java class json string represent.
- * @return the java object parsed from json string.
- * @since GSO 0.5
- */
- public static <T> T fromJson(File file, Class<T> objClass) {
- try {
-
- ObjectMapper mapper = new ObjectMapper();
-
- mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
- return mapper.readValue(file, objClass);
- } catch(IOException ex) {
- LOGGER.error("Parser to object error.", ex);
- throw new IllegalArgumentException(
- "Parser json to object error, file = " + file.getName() + ", expect class = " + objClass, ex);
- }
- }
}
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/ToolUtil.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/ToolUtil.java
index 777a39d7..55b02f7a 100644
--- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/ToolUtil.java
+++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/ToolUtil.java
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.onap.vnfsdk.marketplace.common;
import java.io.File;
@@ -30,329 +31,252 @@ import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
-
-
/**
* common utility class.
- *
*/
public class ToolUtil {
- private static final Logger LOG = LoggerFactory.getLogger(ToolUtil.class);
- public static final String CATALOGUE_CSAR_DIR_NAME = "csar";
+ private static final Logger LOG = LoggerFactory.getLogger(ToolUtil.class);
+
+ public static final String CATALOGUE_CSAR_DIR_NAME = "csar";
- public static final String CATALOGUE_IMAGE_DIR_NAME = "image";
-
- public static final String CSAR_EXTENSION = ".csar";
+ public static final String CATALOGUE_IMAGE_DIR_NAME = "image";
- public static final int FILE_PERCENT = 1024 * 1024; // 1M
+ public static final String CSAR_EXTENSION = ".csar";
+
+ public static final int FILE_PERCENT = 1024 * 1024; // 1M
+
+ private ToolUtil() {
+ }
+
+ public static boolean isEmptyString(String val) {
+ return val == null || "".equals(val);
+ }
- private ToolUtil() {
- }
+ public static boolean isTrimedEmptyString(String val) {
+ return val == null || "".equals(val.trim());
+ }
- public static boolean isEmptyString(String val) {
- return val == null || "".equals(val);
- }
+ public static boolean isTrimedEmptyArray(String[] val) {
+ return val == null || val.length == 0;
+ }
- public static boolean isTrimedEmptyString(String val) {
- return val == null || "".equals(val.trim());
- }
+ public static boolean isEmptyCollection(Collection<?> coll) {
+ return null == coll || coll.isEmpty();
+ }
- public static boolean isTrimedEmptyArray(String[] val) {
- return val == null || val.length == 0;
- }
+ /**
+ * store chunk file to local temp directory.
+ *
+ * @param dirName directory name
+ * @param fileName file name
+ * @param uploadedInputStream upload input stream
+ * @return String
+ * @throws IOException e
+ */
+ public static String storeChunkFileInLocal(String dirName, String fileName, InputStream uploadedInputStream)
+ throws IOException {
+ File tmpDir = new File(dirName);
+ LOG.info("tmpdir = " + File.separator + dirName);
+ if(!tmpDir.exists()) {
+ tmpDir.mkdirs();
+ }
+ File file = new File(tmpDir + File.separator + fileName);
+
+ try (OutputStream os = new FileOutputStream(file, true);) {
+ int read = 0;
+ byte[] bytes = new byte[1024];
+ while((read = uploadedInputStream.read(bytes)) != -1) {
+ os.write(bytes, 0, read);
+ }
+ os.flush();
+ return file.getAbsolutePath();
+ }
+ }
- /**
- * trimed string.
- *
- * @param val string array to trim
- * @return String[]
- */
- public static String[] trimedStringArray(String[] val) {
- if (isTrimedEmptyArray(val)) {
- return val;
+ /**
+ * get temp dirctory when upload package.
+ *
+ * @param dirName temp directory name
+ * @param fileName package name
+ * @return String
+ */
+ public static String getTempDir(String dirName, String fileName) {
+ return Thread.currentThread().getContextClassLoader().getResource("/").getPath() + dirName + File.separator
+ + fileName.replace(CSAR_EXTENSION, "");
}
- String[] rets = new String[val.length];
- for (int i = 0; i < val.length; i++) {
- rets[i] = val[i].trim();
+ public static String getUnzipDir(String dirName) {
+ File tmpDir = new File(File.separator + dirName);
+ return tmpDir.getAbsolutePath().replace(CSAR_EXTENSION, "");
}
- return rets;
- }
-
- public static boolean isEmptyCollection(Collection<?> coll) {
- return null == coll || coll.isEmpty();
- }
-
- /**
- * store chunk file to local temp directory.
- *
- * @param dirName directory name
- * @param fileName file name
- * @param uploadedInputStream upload input stream
- * @return String
- * @throws IOException e
- */
- public static String storeChunkFileInLocal(String dirName, String fileName,
- InputStream uploadedInputStream) throws IOException {
- File tmpDir = new File(dirName);
- LOG.info("tmpdir = " + File.separator + dirName);
- if (!tmpDir.exists()) {
- tmpDir.mkdirs();
+
+ /**
+ * delete file.
+ *
+ * @param dirName the directory of file
+ * @param fileName file name
+ * @return boolean
+ */
+ public static boolean deleteFile(String dirName, String fileName) {
+ File tmpDir = new File(File.separator + dirName);
+ if(!tmpDir.exists()) {
+ return true;
+ }
+ File file = new File(tmpDir.getAbsolutePath() + File.separator + fileName);
+ if(file.exists()) {
+ return file.delete();
+ }
+ return true;
}
- File file = new File(tmpDir + File.separator + fileName);
-
- try (
- OutputStream os = new FileOutputStream(file, true);
- )
- {
- int read = 0;
- byte[] bytes = new byte[1024];
- while ((read = uploadedInputStream.read(bytes)) != -1) {
- os.write(bytes, 0, read);
- }
- os.flush();
- return file.getAbsolutePath();
+
+ public static String getCatalogueCsarPath() {
+ return File.separator + CATALOGUE_CSAR_DIR_NAME;
}
- }
-
- /**
- * get temp dirctory when upload package.
- *
- * @param dirName temp directory name
- * @param fileName package name
- * @return String
- */
- public static String getTempDir(String dirName, String fileName) {
- return Thread.currentThread().getContextClassLoader().getResource("/").getPath() + dirName + File.separator
- + fileName.replace(CSAR_EXTENSION, "");
- }
-
- public static String getUnzipDir(String dirName) {
- File tmpDir = new File(File.separator + dirName);
- return tmpDir.getAbsolutePath().replace(CSAR_EXTENSION, "");
- }
-
- /**
- * delete file.
- *
- * @param dirName the directory of file
- * @param fileName file name
- * @return boolean
- */
- public static boolean deleteFile(String dirName, String fileName) {
- File tmpDir = new File(File.separator + dirName);
- if (!tmpDir.exists()) {
- return true;
+
+ public static String getCatalogueImagePath() {
+ return File.separator + CATALOGUE_IMAGE_DIR_NAME;
}
- File file = new File(tmpDir.getAbsolutePath() + File.separator + fileName);
- if (file.exists()) {
- return file.delete();
+
+ /**
+ * 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;
}
- return true;
- }
-
- public static String getCatalogueCsarPath() {
- return File.separator + CATALOGUE_CSAR_DIR_NAME;
- }
-
- public static String getCatalogueImagePath() {
- return File.separator + CATALOGUE_IMAGE_DIR_NAME;
- }
-
- /**
- * 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";
+
+ public static String formatFileSize(double fileLength, int fileUnit) {
+ DecimalFormat format = new DecimalFormat("#0.00");
+ return format.format(fileLength / fileUnit) + "M";
}
- return fileSize;
- }
-
- public static String formatFileSize(double fileLength, int fileUnit) {
- DecimalFormat format = new DecimalFormat("#0.00");
- return format.format(fileLength / fileUnit) + "M";
- }
-
- /**
- * 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), FILE_PERCENT);
- }
-
- /**
- * fix package format.
- *
- * @param csarId package ID
- * @return String
- */
- public static String formatCsar(String csarId) {
- String result = csarId;
- if (csarId.indexOf(CSAR_EXTENSION) < 0) {
- result += CSAR_EXTENSION;
+
+ /**
+ * fix package format.
+ *
+ * @param csarId package ID
+ * @return String
+ */
+ public static String formatCsar(String csarId) {
+ String result = csarId;
+ if(csarId.indexOf(CSAR_EXTENSION) < 0) {
+ result += CSAR_EXTENSION;
+ }
+ return result;
}
- return result;
- }
-
-
- /**
- * delete the file and file directory.
- *
- * @param dir file
- * @return boolean
- */
- public static boolean deleteDir(File dir) {
- if (dir.isDirectory()) {
- String[] children = dir.list();
- for (int i = 0; i < children.length; i++) {
- boolean success = deleteDir(new File(dir, children[i]));
- if (!success) {
- return false;
+
+ /**
+ * judge the file's format is yaml or not.
+ *
+ * @param file file to judge
+ * @return boolean
+ */
+ public static boolean isYamlFile(File file) {
+ if(!file.isDirectory() && file.getName().indexOf(".yaml") != -1) {
+ return true;
}
- }
+ return false;
}
- return dir.delete();
- }
-
- /**
- * judge the file's format is yaml or not.
- *
- * @param file file to judge
- * @return boolean
- */
- public static boolean isYamlFile(File file) {
- if (!file.isDirectory() && file.getName().indexOf(".yaml") != -1) {
- return true;
+
+ /**
+ * remove the csar suffix.
+ *
+ * @param csarName package name
+ * @return String
+ */
+ public static String removeCsarSuffix(String csarName) {
+ return csarName.replaceAll(CSAR_EXTENSION, "");
}
- return false;
- }
-
- /**
- * remove the csar suffix.
- *
- * @param csarName package name
- * @return String
- */
- public static String removeCsarSuffix(String csarName) {
- return csarName.replaceAll(CSAR_EXTENSION, "");
- }
-
- /**
- * add the csar fuffix.
- *
- * @param csarName package name
- * @return String
- */
- public static String addCsarSuffix(String csarName) {
- if (csarName.indexOf(CSAR_EXTENSION) == -1) {
- return csarName + CSAR_EXTENSION;
+
+ /**
+ * add the csar fuffix.
+ *
+ * @param csarName package name
+ * @return String
+ */
+ public static String addCsarSuffix(String csarName) {
+ if(csarName.indexOf(CSAR_EXTENSION) == -1) {
+ return csarName + CSAR_EXTENSION;
+ }
+ return csarName;
}
- return csarName;
- }
-
- /**
- * process file name.
- *
- * @param fileName file's name
- * @return String
- */
- public static String processFileName(String fileName) {
- int index = fileName.indexOf(".zip");
- if (index == -1) {
- return fileName;
+
+ /**
+ * process file name.
+ *
+ * @param fileName file's name
+ * @return String
+ */
+ public static String processFileName(String fileName) {
+ int index = fileName.indexOf(".zip");
+ if(index == -1) {
+ return fileName;
+ }
+
+ return addCsarSuffix(fileName.replaceAll(".zip", ""));
}
- return addCsarSuffix(fileName.replaceAll(".zip", ""));
- }
-
- /**
- * exchange object to string.
- *
- * @param obj object
- * @return String
- */
- public static String objectToString(Object obj) {
- if (obj == null) {
- return "";
+ /**
+ * exchange object to string.
+ *
+ * @param obj object
+ * @return String
+ */
+ public static String objectToString(Object obj) {
+ if(obj == null) {
+ return "";
+ }
+ Gson gson = new Gson();
+
+ return gson.toJson(obj);
}
- Gson gson = new Gson();
-
- return gson.toJson(obj);
- }
-
- public static String generateId() {
- return UUID.randomUUID().toString();
- }
-
- /**
- * get the size format according file size.
- *
- * @param fileSize file size
- * @return size format
- */
- public static String getFormatFileSize(long fileSize) {
- long kb = 1024;
- long mb = kb * 1024;
- long gb = mb * 1024;
-
- if (fileSize >= gb) {
- return String.format("%.1f GB", (float) fileSize / gb);
- } else if (fileSize >= mb) {
- float fi = (float) fileSize / mb;
- return String.format(fi > 100 ? "%.0f MB" : "%.1f MB", fi);
- } else if (fileSize >= kb) {
- float fi = (float) fileSize / kb;
- return String.format(fi > 100 ? "%.0f KB" : "%.1f KB", fi);
- } else {
- return String.format("%d B", fileSize);
+
+ public static String generateId() {
+ return UUID.randomUUID().toString();
}
- }
-
- /**
- * get gson from json.
- * @param jsonString json string
- * @param templateClass template class
- * @return Template
- */
- public static <T> T fromJson(String jsonString, Class<T> templateClass) {
- Gson gson = new Gson();
- return gson.fromJson(jsonString, templateClass);
- }
-
- /**
- * gson to json.
- * @param template class name
- * @return String
- */
- public static <T> String toJson(T template) {
- Gson gson = new Gson();
- return gson.toJson(template);
- }
-
- /**
- * @param value
- * @return
- */
- public static String getAsString(JsonElement value) {
- if (value.isJsonPrimitive()) {
- return value.getAsString();
+
+ /**
+ * get the size format according file size.
+ *
+ * @param fileSize file size
+ * @return size format
+ */
+ public static String getFormatFileSize(long fileSize) {
+ long kb = 1024;
+ long mb = kb * 1024;
+ long gb = mb * 1024;
+
+ if(fileSize >= gb) {
+ return String.format("%.1f GB", (float)fileSize / gb);
+ } else if(fileSize >= mb) {
+ float fi = (float)fileSize / mb;
+ return String.format(fi > 100 ? "%.0f MB" : "%.1f MB", fi);
+ } else if(fileSize >= kb) {
+ float fi = (float)fileSize / kb;
+ return String.format(fi > 100 ? "%.0f KB" : "%.1f KB", fi);
+ } else {
+ return String.format("%d B", fileSize);
+ }
}
- return value.toString();
- }
+ /**
+ * get gson from json.
+ *
+ * @param jsonString json string
+ * @param templateClass template class
+ * @return Template
+ */
+ public static <T> T fromJson(String jsonString, Class<T> templateClass) {
+ Gson gson = new Gson();
+ return gson.fromJson(jsonString, templateClass);
+ }
}
-