Commit acb0dfb1 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Added utils

Remved IParameter to simplify unmarshalling with jackson
Added tests in commons for types  
parent e1e33d95
/drip-commons/target/ /drip-commons/target/
/drip-api/target/ /drip-api/target/
/drip-planner/target/ /drip-planner/target/
\ No newline at end of file /drip-commons/nbproject/
\ No newline at end of file
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package nl.uva.sne.drip.api.rest; package nl.uva.sne.drip.api.rest;
import com.fasterxml.jackson.databind.ObjectMapper;
import nl.uva.sne.drip.commons.types.Parameter; import nl.uva.sne.drip.commons.types.Parameter;
import nl.uva.sne.drip.api.rpc.PlannerCaller; import nl.uva.sne.drip.api.rpc.PlannerCaller;
import java.io.IOException; import java.io.IOException;
...@@ -26,7 +27,6 @@ import java.util.List; ...@@ -26,7 +27,6 @@ import java.util.List;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import nl.uva.sne.drip.commons.types.IParameter;
import nl.uva.sne.drip.commons.types.Message; import nl.uva.sne.drip.commons.types.Message;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -56,81 +56,55 @@ public class UploadToscaController { ...@@ -56,81 +56,55 @@ public class UploadToscaController {
@RequestMapping(value = "/upload", method = RequestMethod.POST) @RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody public @ResponseBody
String toscaUpload(@RequestParam("file") MultipartFile file) { String toscaUpload(@RequestParam("file") MultipartFile file) {
PlannerCaller planner = null;
if (!file.isEmpty()) { if (!file.isEmpty()) {
try { try {
String originalFileName = file.getOriginalFilename(); String originalFileName = file.getOriginalFilename();
String name = System.currentTimeMillis() + "_" + originalFileName; String name = System.currentTimeMillis() + "_" + originalFileName;
// File targetToscaFile = new File(inputToscaFolderPath + File.separator + name);
// file.transferTo(targetToscaFile);
Message invokationMessage = new Message(); Message invokationMessage = new Message();
List parameters = new ArrayList(); List parameters = new ArrayList();
Parameter fileArgument = new Parameter(); Parameter fileArgument = new Parameter();
byte[] bytes = file.getBytes();//Files.readAllBytes(Paths.get(targetToscaFile.getAbsolutePath())); byte[] bytes = file.getBytes();
String charset = "UTF-8"; String charset = "UTF-8";
fileArgument.setValue(new String(bytes, charset)); fileArgument.setValue(new String(bytes, charset));
fileArgument.setEncoding(charset); fileArgument.setEncoding(charset);
fileArgument.setName(name); fileArgument.setName("input");
parameters.add(fileArgument); parameters.add(fileArgument);
fileArgument = new Parameter(); fileArgument = new Parameter();
bytes = Files.readAllBytes(Paths.get("/home/alogo/Downloads/DRIP/example_a.yml")); bytes = Files.readAllBytes(Paths.get("/home/alogo/Downloads/DRIP/example_a.yml"));
fileArgument.setValue(new String(bytes, charset)); fileArgument.setValue(new String(bytes, charset));
fileArgument.setEncoding(charset); fileArgument.setEncoding(charset);
fileArgument.setName("example_a.yml"); fileArgument.setName("example");
parameters.add(fileArgument); parameters.add(fileArgument);
invokationMessage.setParameters(parameters); invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate(new Date(System.currentTimeMillis())); invokationMessage.setCreationDate((System.currentTimeMillis()));
PlannerCaller planner = new PlannerCaller(messageBrokerHost); planner = new PlannerCaller(messageBrokerHost);
String returned = planner.plan(invokationMessage); String returned = planner.plan(invokationMessage);
ObjectMapper mapper = new ObjectMapper();
Message request = mapper.readValue(returned, Message.class);
System.err.println(returned);
System.err.println(request.getCreationDate());
planner.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !"; return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (IOException | IllegalStateException | TimeoutException | InterruptedException ex) { } catch (IOException | IllegalStateException | TimeoutException | InterruptedException ex) {
Logger.getLogger(UploadToscaController.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(UploadToscaController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (planner != null) {
try {
planner.close();
} catch (IOException | TimeoutException ex) {
Logger.getLogger(UploadToscaController.class.getName()).log(Level.WARNING, null, ex);
}
}
} }
} }
return "Upload failed. 'file' was empty."; return "Upload failed. 'file' was empty.";
} }
@RequestMapping(value = "/args", method = RequestMethod.GET)
public Message args() {
try {
Message r = new Message();
List<IParameter> args = new ArrayList();
IParameter intParam = new Parameter();
intParam.setValue("1");
args.add(intParam);
Parameter strParam = new Parameter();
strParam.setValue("string");
args.add(strParam);
IParameter targetToscaFile = new Parameter();
byte[] bytes = Files.readAllBytes(Paths.get("/home/alogo/Downloads/planner_output_all.yml"));
targetToscaFile.setValue(new String(bytes, "UTF-8"));
targetToscaFile.setName("planner_output_all.yml");
targetToscaFile.setEncoding("UTF-8");
args.add(targetToscaFile);
IParameter file = new Parameter();
file.setName("Dockerfile");
file.setURL("https://github.com/QCAPI-DRIP/DRIP-integradation/releases/download/valpha/Dockerfile");
args.add(file);
r.setParameters(args);
r.setCreationDate(new Date(System.currentTimeMillis()));
// ObjectMapper mapper = new ObjectMapper();
// String jsonInString = mapper.writeValueAsString(r);
// System.err.println(jsonInString);
return r;
} catch (IOException ex) {
Logger.getLogger(UploadToscaController.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
} }
...@@ -15,6 +15,33 @@ ...@@ -15,6 +15,33 @@
<version>2.8.0</version> <version>2.8.0</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
......
...@@ -23,13 +23,15 @@ import java.util.List; ...@@ -23,13 +23,15 @@ import java.util.List;
* @author S. Koulouzis * @author S. Koulouzis
*/ */
public interface IMessage { public interface IMessage {
public Date getCreationDate();
public void setCreationDate(Date creationDate); public static final String CREATION_DATE = "creationDate";
public void setParameters(List<IParameter> params); public Long getCreationDate();
public void setCreationDate(Long creationDate);
public void setParameters(List<Parameter> params);
public List getParameters(); public List getParameters();
} }
...@@ -19,6 +19,7 @@ import com.fasterxml.jackson.annotation.JsonFormat; ...@@ -19,6 +19,7 @@ import com.fasterxml.jackson.annotation.JsonFormat;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
//import javax.persistence.Column; //import javax.persistence.Column;
//import javax.persistence.Entity; //import javax.persistence.Entity;
//import javax.persistence.GeneratedValue; //import javax.persistence.GeneratedValue;
...@@ -36,30 +37,29 @@ import java.util.List; ...@@ -36,30 +37,29 @@ import java.util.List;
//@Entity //@Entity
public class Message implements IMessage, Serializable { public class Message implements IMessage, Serializable {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.S") // @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.S")
// @Temporal(TemporalType.DATE) // @Temporal(TemporalType.DATE)
private Date creationDate; private Long creationDate;
private List<Parameter> parameters;
// @Column
private List<IParameter> parameters;
@Override @Override
public Date getCreationDate() { public Long getCreationDate() {
return this.creationDate; return this.creationDate;
} }
@Override @Override
public void setParameters(List<IParameter> args) { public void setParameters(List<Parameter> parameters) {
this.parameters = args; this.parameters = parameters;
} }
@Override @Override
public List getParameters() { public List<Parameter> getParameters() {
return this.parameters; return this.parameters;
} }
@Override @Override
public void setCreationDate(Date creationDate) { public void setCreationDate(Long creationDate) {
this.creationDate = creationDate; this.creationDate = creationDate;
} }
......
...@@ -21,49 +21,55 @@ import java.io.Serializable; ...@@ -21,49 +21,55 @@ import java.io.Serializable;
* *
* @author S. Koulouzis. * @author S. Koulouzis.
*/ */
public class Parameter implements IParameter, Serializable { public class Parameter implements Serializable {
private String url; private String url;
private String encoding; private String encoding;
private String value; private String value;
private String name; private String name;
@Override public static final String NAME = "name";
public static final String URL = "url";
public static final String VALUE = "value";
public static final String ENCODING = "encoding";
// @Override
public String getURL() { public String getURL() {
return this.url; return this.url;
} }
@Override // @Override
public void setURL(String url) { public void setURL(String url) {
this.url = url; this.url = url;
} }
@Override // @Override
public String getEncoding() { public String getEncoding() {
return this.encoding; return this.encoding;
} }
@Override // @Override
public void setEncoding(String encoding) { public void setEncoding(String encoding) {
this.encoding = encoding; this.encoding = encoding;
} }
@Override // @Override
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
@Override // @Override
public String getName() { public String getName() {
return this.name; return this.name;
} }
//
// @Override
@Override
public String getValue() { public String getValue() {
return this.value; return this.value;
} }
@Override // @Override
public void setValue(String value) { public void setValue(String value) {
this.value = value; this.value = value;
} }
......
...@@ -13,33 +13,44 @@ ...@@ -13,33 +13,44 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package nl.uva.sne.drip.commons.types; package nl.uva.sne.drip.commons.utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/** /**
* *
* @author S. Koulouzis * @author S. Koulouzis
*/ */
public interface IParameter { public class FileHash {
public static final String NAME = "name"; /**
public static final String URL = "url"; * Code from: http://www.mkyong.com/java/java-sha-hashing-example/
public static final String VALUE = "value"; *
public static final String ENCODING = "encoding"; * @param filePath
* @return
public void setName(String name); * @throws NoSuchAlgorithmException
* @throws FileNotFoundException
public String getName(); * @throws IOException
*/
public String getValue(); public static String getSHA256(String filePath) throws NoSuchAlgorithmException, FileNotFoundException, IOException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
public void setValue(String value); FileInputStream fis = new FileInputStream(filePath);
public String getURL(); byte[] dataBytes = new byte[1024];
public void setURL(String url); int nread;
while ((nread = fis.read(dataBytes)) != -1) {
public String getEncoding(); md.update(dataBytes, 0, nread);
}
public void setEncoding(String encoding); byte[] mdbytes = md.digest();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
} }
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.commons.types;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import nl.uva.sne.drip.commons.utils.FileHash;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
*
* @author alogo
*/
public class TypesJUnitTest {
private static File tempFile1;
private static String tempFile1Hash;
private static Message message1;
private static String fileParamContentsName;
private static File tempFile2;
private static String tempFile2Hash;
private static List<File> files;
@BeforeClass
public static void setUpClass() throws IOException, NoSuchAlgorithmException {
files = new ArrayList<>();
initTmpRandomFiles();
initMessages();
}
@AfterClass
public static void tearDownClass() {
if (tempFile1 != null) {
tempFile1.delete();
}
}
private static void initTmpRandomFiles() throws IOException, NoSuchAlgorithmException {
tempFile1 = File.createTempFile("temp-", Long.toString(System.nanoTime()));
try (PrintWriter writer = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(tempFile1), "UTF-8")), false)) {
Random random = new Random();
String sep = "";
for (int i = 0; i < 100; i++) {
int number = random.nextInt(1000) + 1;
writer.print(sep);
writer.print(number / 1e3);
sep = " ";
writer.println();
}
writer.println();
}
tempFile1Hash = FileHash.getSHA256(tempFile1.getAbsolutePath());
files.add(tempFile1);
tempFile2 = File.createTempFile("temp-", Long.toString(System.nanoTime()));
try (PrintWriter writer = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(tempFile2), "UTF-8")), false)) {
Random random = new Random();
String sep = "";
for (int i = 0; i < 100; i++) {
int number = random.nextInt(1000) + 1;
writer.print(sep);
writer.print(number / 1e3);
sep = " ";
writer.println();
}
writer.println();
}
tempFile2Hash = FileHash.getSHA256(tempFile2.getAbsolutePath());
files.add(tempFile2);
}
private static void initMessages() throws IOException {
message1 = new Message();
message1.setCreationDate((System.currentTimeMillis()));
List<Parameter> parameters = new ArrayList();
Parameter numParam = new Parameter();
String numParamName = "input";
numParam.setName(numParamName);
numParam.setValue("33000");
parameters.add(numParam);
Parameter fileParamContents = new Parameter();
fileParamContentsName = "someInputFile";
fileParamContents.setName(fileParamContentsName);
byte[] bytes = Files.readAllBytes(Paths.get(tempFile1.getAbsolutePath()));
String charset = "UTF-8";
fileParamContents.setValue(new String(bytes, charset));
fileParamContents.setEncoding(charset);
parameters.add(fileParamContents);
Parameter fileParamRef = new Parameter();
fileParamRef.setName("theNameOfTheParamater");
fileParamRef.setURL("http://www.gutenberg.org/cache/epub/3160/pg3160.txt");
parameters.add(fileParamRef);
message1.setParameters(parameters);
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void testMarshallUnmarshalJackson() throws IOException, NoSuchAlgorithmException {
File fileParam = null;
try {
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(message1);
// System.err.println(jsonInString);
assertTrue("JSON should contain " + IMessage.CREATION_DATE, jsonInString.contains(IMessage.CREATION_DATE));
assertTrue("JSON should contain parameters", jsonInString.contains("parameters"));
// System.err.println(jsonInString);
Message request = mapper.readValue(jsonInString, Message.class);
List<Parameter> params = request.getParameters();
assertEquals(message1.getParameters().size(), params.size());
fileParam = new File(fileParamContentsName);
for (Parameter param : params) {
if (param.getName().equals(fileParamContentsName)) {
String value = param.getValue();
try (PrintWriter out = new PrintWriter(fileParam)) {
out.print(value);
}
break;
}
}
assertNotNull(fileParam);
assertEquals(tempFile1.length(), fileParam.length());
String fileParamHash = FileHash.getSHA256(fileParam.getAbsolutePath());
assertEquals(tempFile1Hash, fileParamHash);
} finally {
if (fileParam != null) {
fileParam.delete();
}
}
}
@Test
public void testMarshallUnmarshalSimpleJson() throws IOException, NoSuchAlgorithmException, JSONException {
File fileParam = null;
try {
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(message1);
JSONObject jo = new JSONObject(jsonInString);
JSONArray parameters = jo.getJSONArray("parameters");
fileParam = new File(fileParamContentsName);
for (int i = 0; i < parameters.length(); i++) {
JSONObject param = (JSONObject) parameters.get(i);
String name = (String) param.get(Parameter.NAME);
if (name.equals(fileParamContentsName)) {
String value = (String) param.get(Parameter.VALUE);
try (PrintWriter out = new PrintWriter(fileParam)) {
out.print(value);
}
break;
}
}
assertNotNull(fileParam);
assertEquals(tempFile1.length(), fileParam.length());
String fileParamHash = FileHash.getSHA256(fileParam.getAbsolutePath());
assertEquals(tempFile1Hash, fileParamHash);
} finally {
if (fileParam != null) {
fileParam.delete();
}
}
}
@Test
public void testJacksonMarshal() throws UnsupportedEncodingException, IOException {
Message responseMessage = new Message();
List parameters = new ArrayList();
String charset = "UTF-8";
for (File f : files) {
Parameter fileParam = new Parameter();
byte[] bytes = Files.readAllBytes(Paths.get(f.getAbsolutePath()));
fileParam.setValue(new String(bytes, charset));
fileParam.setEncoding(charset);
fileParam.setName(f.getName());
parameters.add(fileParam);
}
responseMessage.setParameters(parameters);
responseMessage.setCreationDate((System.currentTimeMillis()));
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(responseMessage);
}
@Test
public void testsimpleJsonMarshal() throws JSONException, IOException {
JSONObject jo = new JSONObject();
jo.put("creationDate", (System.currentTimeMillis()));
List parameters = new ArrayList();
String charset = "UTF-8";
for (File f : files) {
Map<String, String> fileArguments = new HashMap<>();
fileArguments.put("encoding", charset);
fileArguments.put("name", f.getName());
byte[] bytes = Files.readAllBytes(Paths.get(f.getAbsolutePath()));
fileArguments.put("value", new String(bytes, charset));
parameters.add(fileArguments);
}
jo.put("parameters", parameters);
ObjectMapper mapper = new ObjectMapper();
Message request = mapper.readValue(jo.toString(), Message.class);
Date cDate = new Date(request.getCreationDate());
}
}
...@@ -65,18 +65,24 @@ public class Consumer extends DefaultConsumer { ...@@ -65,18 +65,24 @@ public class Consumer extends DefaultConsumer {
try { try {
String message = new String(body, "UTF-8"); String message = new String(body, "UTF-8");
File[] inputFiles; File[] inputFiles;
File tempDir = new File(System.getProperty("java.io.tmpdir") + File.separator + this.getClass().getSimpleName() + "-" + Long.toString(System.nanoTime()));
if (!(tempDir.mkdirs())) {
throw new FileNotFoundException("Could not create output directory: " + tempDir.getAbsolutePath());
}
inputFiles = jacksonUnmarshalExample(message); inputFiles = jacksonUnmarshalExample(message);
panner.plan(inputFiles[0].getAbsolutePath(), inputFiles[1].getAbsolutePath(), "/tmp/out"); panner.plan(inputFiles[0].getAbsolutePath(), inputFiles[1].getAbsolutePath(), tempDir.getAbsolutePath());
inputFiles = simpleJsonUnmarshalExample(message); inputFiles = simpleJsonUnmarshalExample(message);
List<File> files = panner.plan(inputFiles[0].getAbsolutePath(), inputFiles[1].getAbsolutePath(), "/tmp/out");
List<File> files = panner.plan(inputFiles[0].getAbsolutePath(), inputFiles[1].getAbsolutePath(), tempDir.getAbsolutePath());
response = jacksonMarshalExample(files); response = jacksonMarshalExample(files);
System.err.println(response); System.err.println(response);
response = simpleJsonMarshalExample(files); response = simpleJsonMarshalExample(files);
System.err.println(response); System.err.println(response);
} catch (RuntimeException | JSONException ex) { } catch (JSONException | FileNotFoundException ex) {
response = ex.getMessage();
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
} finally { } finally {
channel.basicPublish("", properties.getReplyTo(), replyProps, response.getBytes("UTF-8")); channel.basicPublish("", properties.getReplyTo(), replyProps, response.getBytes("UTF-8"));
...@@ -89,50 +95,48 @@ public class Consumer extends DefaultConsumer { ...@@ -89,50 +95,48 @@ public class Consumer extends DefaultConsumer {
File[] files = new File[2]; File[] files = new File[2];
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
Message request = mapper.readValue(message, Message.class); Message request = mapper.readValue(message, Message.class);
//We know that in parameters 0-1 we should have files List<Parameter> params = request.getParameters();
for (int i = 0; i < 2; i++) { File inputFile = File.createTempFile("input-", Long.toString(System.nanoTime()));
Map<String, String> fileArg = (java.util.LinkedHashMap) request.getParameters().get(i); File exampleFile = File.createTempFile("example-", Long.toString(System.nanoTime()));
for (Parameter param : params) {
String fileName = fileArg.get(Parameter.NAME); if (param.getName().equals("input")) {
//If not null should be able to use it to download file try (PrintWriter out = new PrintWriter(inputFile)) {
String url = fileArg.get(Parameter.URL); out.print(param.getValue());
if (url != null) { }
//download files[0] = inputFile;
} }
if (param.getName().equals("example")) {
String encoding = fileArg.get(Parameter.ENCODING); try (PrintWriter out = new PrintWriter(exampleFile)) {
String value = fileArg.get(Parameter.VALUE); out.print(param.getValue());
try (PrintWriter out = new PrintWriter(fileName)) { }
out.print(value); files[1] = exampleFile;
} }
files[i] = new File(fileName);
} }
return files; return files;
} }
private File[] simpleJsonUnmarshalExample(String message) throws JSONException, FileNotFoundException { private File[] simpleJsonUnmarshalExample(String message) throws JSONException, FileNotFoundException, IOException {
File[] files = new File[2]; File[] files = new File[2];
JSONObject jo = new JSONObject(message); JSONObject jo = new JSONObject(message);
JSONArray args = jo.getJSONArray("parameters"); JSONArray parameters = jo.getJSONArray("parameters");
//We know that in parameters 0-2 we should have files File inputFile = File.createTempFile("input-", Long.toString(System.nanoTime()));
for (int i = 0; i < 2; i++) { File exampleFile = File.createTempFile("example-", Long.toString(System.nanoTime()));
JSONObject arg = (JSONObject) args.get(i); for (int i = 0; i < parameters.length(); i++) {
String fileName = (String) arg.get(Parameter.NAME); JSONObject param = (JSONObject) parameters.get(i);
//If not null should be able to use it to download file String name = (String) param.get(Parameter.NAME);
if (name.equals("input")) {
if (!arg.isNull("url")) { try (PrintWriter out = new PrintWriter(inputFile)) {
String url = (String) arg.get("url"); out.print(param.get(Parameter.VALUE));
//download }
files[0] = inputFile;
} }
//Otherwise get contents as string if (name.equals("example")) {
String value = (String) arg.get(Parameter.VALUE); try (PrintWriter out = new PrintWriter(exampleFile)) {
String encoding = (String) arg.get(Parameter.ENCODING); out.print(param.get(Parameter.VALUE));
}
try (PrintWriter out = new PrintWriter(fileName)) { files[1] = exampleFile;
out.print(value);
} }
files[i] = new File(fileName);
} }
return files; return files;
} }
...@@ -149,10 +153,9 @@ public class Consumer extends DefaultConsumer { ...@@ -149,10 +153,9 @@ public class Consumer extends DefaultConsumer {
fileParam.setEncoding(charset); fileParam.setEncoding(charset);
fileParam.setName(f.getName()); fileParam.setName(f.getName());
parameters.add(fileParam); parameters.add(fileParam);
} }
responseMessage.setParameters(parameters); responseMessage.setParameters(parameters);
responseMessage.setCreationDate(new Date(System.currentTimeMillis())); responseMessage.setCreationDate((System.currentTimeMillis()));
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(responseMessage); return mapper.writeValueAsString(responseMessage);
...@@ -160,7 +163,7 @@ public class Consumer extends DefaultConsumer { ...@@ -160,7 +163,7 @@ public class Consumer extends DefaultConsumer {
private String simpleJsonMarshalExample(List<File> files) throws JSONException, IOException { private String simpleJsonMarshalExample(List<File> files) throws JSONException, IOException {
JSONObject jo = new JSONObject(); JSONObject jo = new JSONObject();
jo.put("creationDate", new Date(System.currentTimeMillis())); jo.put("creationDate", (System.currentTimeMillis()));
List parameters = new ArrayList(); List parameters = new ArrayList();
String charset = "UTF-8"; String charset = "UTF-8";
for (File f : files) { for (File f : files) {
......
...@@ -111,6 +111,7 @@ public class Planner { ...@@ -111,6 +111,7 @@ public class Planner {
outputFiles.add(new File(outfPath)); outputFiles.add(new File(outfPath));
outputFiles.add(new File(allFilePath)); outputFiles.add(new File(allFilePath));
return outputFiles; return outputFiles;
} }
private String generateVM(String block, String nodeName, String dockerName, String privateAddress, String role) { private String generateVM(String block, String nodeName, String dockerName, String privateAddress, String role) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment