Provide the rest api of saving workflow data. 27/30627/1
authorYuanHu <yuan.hu1@zte.com.cn>
Wed, 7 Feb 2018 08:46:51 +0000 (16:46 +0800)
committerYuanHu <yuan.hu1@zte.com.cn>
Wed, 7 Feb 2018 08:46:51 +0000 (16:46 +0800)
Save temporary files to local.

Issue-ID: SDC-1004

Change-Id: I09f52cdf4ff39e103b7f75041e070c4148b23e07
Signed-off-by: YuanHu <yuan.hu1@zte.com.cn>
sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/resources/WorkflowModelerResource.java
sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/FileCommonConstants.java [new file with mode: 0644]
sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/FileCommonUtils.java
sdc-workflow-designer-server/src/test/java/org/onap/sdc/workflowdesigner/utils/FileCommonUtilsTest.java [new file with mode: 0644]

index 8b3d723..2ad4aea 100644 (file)
@@ -13,7 +13,6 @@
 package org.onap.sdc.workflowdesigner.resources;
 
 import java.io.IOException;
-import java.io.StringBufferInputStream;
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
@@ -93,8 +92,8 @@ public class WorkflowModelerResource {
       @ApiParam(value = "Model Content", required = true) String json) {
     String filePath = "model.json";
     try {
-      FileCommonUtils.saveFile(new StringBufferInputStream(json), "", filePath);
-      return Response.status(Response.Status.OK).entity(id).build();
+      FileCommonUtils.write(filePath, json);
+      return Response.status(Response.Status.OK).entity(json).build();
     } catch (IOException e) {
       logger.error("getServiceTemplateById failed.", e);
       throw RestUtils.newInternalServerErrorException(e);
diff --git a/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/FileCommonConstants.java b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/FileCommonConstants.java
new file mode 100644 (file)
index 0000000..b2c7490
--- /dev/null
@@ -0,0 +1,21 @@
+/**
+ * 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;
+
+/**
+ * 
+ */
+public interface FileCommonConstants {
+  String DEFAULT_CHARSET_NAME = "UTF-8";
+
+}
index 6e65a96..bdea66e 100644 (file)
@@ -20,7 +20,6 @@ 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;
@@ -407,7 +406,7 @@ public class FileCommonUtils {
     try {
       int read = 0;
       byte[] bytes = new byte[1024];
-      os = new FileOutputStream(file, true);
+      os = new FileOutputStream(file, false);
       while ((read = ins.read(bytes)) != -1) {
         os.write(bytes, 0, read);
       }
@@ -419,37 +418,102 @@ public class FileCommonUtils {
   }
 
   /**
+   * 
+   * @param path
+   * @param fileName
+   * @param content
+   * @throws IOException
+   */
+  public static void writetoAbsoluteFile(String path, String fileName, String content)
+      throws IOException {
+    writetoAbsoluteFile(path, fileName, content, FileCommonConstants.DEFAULT_CHARSET_NAME);
+  }
+
+  /**
+   * 
+   * @param path
+   * @param fileName
+   * @param content
+   * @param charsetName
+   * @throws IOException
+   */
+  public static void writetoAbsoluteFile(String path, String fileName, String content,
+      String charsetName) throws IOException {
+    write(path, fileName, content, charsetName);
+  }
+
+  /**
+   * 
+   * @param fileName
    * @param s
    * @throws IOException
-   * @throws UnsupportedEncodingException
    */
-  public static void write(String s, String fileName)
-      throws UnsupportedEncodingException, IOException {
+  public static void write(String fileName, String s) throws IOException {
+    write(".", fileName, s, FileCommonConstants.DEFAULT_CHARSET_NAME);
+
+  }
+
+  /**
+   * 
+   * @param path
+   * @param fileName
+   * @param s
+   * @param charsetName
+   * @throws IOException
+   */
+  public static void write(String path, String fileName, String s, String charsetName)
+      throws IOException {
+    File tmpPath = new File(path);
+    if (!tmpPath.exists()) {
+      tmpPath.mkdirs();
+    }
+
+    String absolutePath = path + File.separator + fileName;
     FileOutputStream out = null;
     try {
-      out = new FileOutputStream(fileName);
-      out.write(s.getBytes("UTF-8"));
+      out = new FileOutputStream(absolutePath);
+      out.write(s.getBytes(charsetName));
       out.close();
     } finally {
       closeOutputStream(out);
     }
+  }
 
+  /**
+   * 
+   * @param fileName
+   * @param s
+   * @param charsetName
+   * @throws IOException
+   */
+  public static void write(String fileName, String s, String charsetName) throws IOException {
+    write(".", fileName, s, charsetName);
   }
 
   /**
+   * 
+   * @param fileName
    * @param ss
+   * @throws IOException
+   */
+  public static void write(String fileName, String[] ss) throws IOException {
+    write(fileName, ss, FileCommonConstants.DEFAULT_CHARSET_NAME);
+  }
+
+  /**
+   * 
    * @param fileName
+   * @param ss
+   * @param charsetName
    * @throws IOException
-   * @throws UnsupportedEncodingException
    */
-  public static void write(String[] ss, String fileName)
-      throws UnsupportedEncodingException, IOException {
+  public static void write(String fileName, String[] ss, String charsetName) throws IOException {
     StringBuilder sb = new StringBuilder();
     for (int i = 0; i < ss.length; i++) {
       sb.append(ss[i]).append(System.lineSeparator());
     }
 
-    write(sb.toString(), fileName);
+    write(sb.toString(), fileName, charsetName);
   }
 
   /**
diff --git a/sdc-workflow-designer-server/src/test/java/org/onap/sdc/workflowdesigner/utils/FileCommonUtilsTest.java b/sdc-workflow-designer-server/src/test/java/org/onap/sdc/workflowdesigner/utils/FileCommonUtilsTest.java
new file mode 100644 (file)
index 0000000..b8d4319
--- /dev/null
@@ -0,0 +1,75 @@
+/**
+ * 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 static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class FileCommonUtilsTest {
+
+  /**
+   * @throws java.lang.Exception
+   */
+  @Before
+  public void setUp() throws Exception {}
+
+  /**
+   * @throws java.lang.Exception
+   */
+  @After
+  public void tearDown() throws Exception {}
+
+  /**
+   * Test method for
+   * {@link org.onap.sdc.workflowdesigner.utils.FileCommonUtils#write(String, String)} .
+   */
+  @Test
+  public final void writeStringString() {
+    String fileName = "test.json";
+    String content = "{\"aaa\": \"节点\"}";
+
+    try {
+      FileCommonUtils.write(fileName, content);
+      String s = FileCommonUtils.readString(fileName);
+      assertEquals(s, content);
+    } catch (IOException e) {
+    }
+  }
+
+  /**
+   * Test method for
+   * {@link org.onap.sdc.workflowdesigner.utils.FileCommonUtils#writetoAbsoluteFile(String, String, String)}
+   * .
+   */
+  @Test
+  public final void saveFileStringStringString() {
+    String fileName = "test1.json";
+    String content = "{\"aaa\": \"节点\"}";
+
+    try {
+      FileCommonUtils.writetoAbsoluteFile(".", fileName, content);
+      String s = FileCommonUtils.readString(fileName);
+      assertEquals(s, content);
+    } catch (IOException e) {
+    }
+  }
+
+}