Commit 5000c2b1 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Added converters

Reaplce dots in yml text (mongo doesn't like them)
Rename controller 
Added notes for dockers 
parent 5f58dcce
#-------------------------Dockers--------------------------------------------
docker run -d --hostname my-rabbit --name some-rabbit rabbitmq:3-management
docker run --name mongo-inst -p 127.0.0.1:27017:27017 -d mongo:3
......@@ -30,7 +30,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
})
@EnableWebMvc
public class AppConfig {
public class Config {
@Bean(name = "multipartResolver")
public CommonsMultipartResolver createMultipartResolver() {
......@@ -39,7 +39,6 @@ public class AppConfig {
// resolver.setMaxInMemorySize(0);
//resolver.setMaxUploadSize(0);
//resolver.setMaxUploadSizePerFile(0);
return resolver;
}
......
/*
* 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.api.conf;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
/**
*
* @author S. Koulouzis
*/
@Configuration
@EnableMongoRepositories(basePackages = "nl.uva.sne.drip.api")
@PropertySources({
@PropertySource(value = "classpath:drip.properties", ignoreResourceNotFound = true),
@PropertySource(value = "file:etc/drip.properties", ignoreResourceNotFound = true)
})
public class MongoConfig extends AbstractMongoConfiguration {
@Value("${db.name}")
private String dbName;
@Value("${db.username}")
private String dbUsername;
@Value("${db.password}")
private String dbPass;
// @Autowired
// private MongoDbFactory mongoFactory;
// @Autowired
// private MongoMappingContext mongoMappingContext;
@Override
protected String getDatabaseName() {
return dbName;
}
@Override
public Mongo mongo() throws Exception {
return new MongoClient("127.0.0.1", 27017);
}
@Override
protected String getMappingBasePackage() {
return "nl.uva.sne.drip";
}
// @Bean
// public MappingMongoConverter mongoConverter() throws Exception {
// DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoFactory);
// MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
// mongoConverter.setMapKeyDotReplacement('\uff0E');
// mongoConverter.afterPropertiesSet();
// return mongoConverter;
// }
}
......@@ -11,8 +11,6 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package nl.uva.sne.drip.api.conf;
import javax.servlet.ServletContext;
......@@ -27,8 +25,11 @@ public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.register(Config.class);
ctx.register(MongoConfig.class);
ctx.setServletContext(servletContext);
Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
dynamic.addMapping("/");
dynamic.setLoadOnStartup(1);
......
/*
* 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.api.rest;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
*
* @author S. Koulouzis
*/
@RestController
@RequestMapping("/rest")
@Component
public class ProvisionController {
@Value("${message.broker.host}")
private String messageBrokerHost;
@Autowired
private ToscaRepository dao;
@RequestMapping(value = "/t", method = RequestMethod.GET)
public ToscaRepresentation toscaUpload() throws JSONException {
return dao.findAll().get(0);
}
}
......@@ -22,11 +22,18 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.utils.Converter;
import static nl.uva.sne.drip.commons.utils.Converter.ymlString2Map;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -35,75 +42,129 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import static nl.uva.sne.drip.commons.utils.Converter.ymlString2Map;
import org.springframework.web.bind.annotation.PathVariable;
/**
*
* @author S. Koulouzis
*/
@RestController
@RequestMapping("/rest")
@RequestMapping("/rest/tosca")
@Component
public class UploadToscaController {
@Value("${input.tosca.folder.path}")
private String inputToscaFolderPath;
public class ToscaController {
@Value("${message.broker.host}")
private String messageBrokerHost;
// For some reson only Autowired decelred here work
// @Autowired
// private UploadService upload;
@Autowired
private ToscaRepository dao;
// curl -X POST -F "file=@DRIP/input.yaml" localhost:8080/drip-api/rest/upload
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody
String toscaUpload(@RequestParam("file") MultipartFile file) {
String toscaUpload(@RequestParam("file") MultipartFile file) throws JSONException {
PlannerCaller planner = null;
if (!file.isEmpty()) {
try {
String originalFileName = file.getOriginalFilename();
String name = System.currentTimeMillis() + "_" + originalFileName;
Message invokationMessage = new Message();
List parameters = new ArrayList();
Parameter fileArgument = new Parameter();
byte[] bytes = file.getBytes();
String charset = "UTF-8";
fileArgument.setValue(new String(bytes, charset));
fileArgument.setEncoding(charset);
fileArgument.setName("input");
parameters.add(fileArgument);
fileArgument = new Parameter();
bytes = Files.readAllBytes(Paths.get("/home/alogo/Downloads/DRIP/example_a.yml"));
fileArgument.setValue(new String(bytes, charset));
fileArgument.setEncoding(charset);
fileArgument.setName("example");
parameters.add(fileArgument);
String str = new String(bytes, "UTF-8");
str = str.replaceAll("\\.", "\uff0E");
invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate((System.currentTimeMillis()));
Map<String, Object> map = Converter.ymlString2Map(str);
ToscaRepresentation t = new ToscaRepresentation();
t.setKvMap(map);
dao.save(t);
// String ymlStr = Converter.map2YmlString(map);
// ymlStr = ymlStr.replaceAll("\\uff0E", "\\.");
// map = Converter.ymlString2Map(ymlStr);
// t.setKvMap(map);
planner = new PlannerCaller(messageBrokerHost);
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());
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (IOException | IllegalStateException | TimeoutException | InterruptedException ex) {
Logger.getLogger(UploadToscaController.class.getName()).log(Level.SEVERE, null, ex);
// ToscaRepresentation t2 = dao.findOne(id);
// map = t2.getKvMap();
// String ymlStr = Converter.map2YmlString(map);
// ymlStr = ymlStr.replaceAll("\\uff0E", "\\.");
// System.err.println(ymlStr);
// Message invokationMessage = new Message();
//
// List parameters = new ArrayList();
// Parameter fileArgument = new Parameter();
//
// String charset = "UTF-8";
// fileArgument.setValue(new String(bytes, charset));
// fileArgument.setEncoding(charset);
// fileArgument.setName("input");
// parameters.add(fileArgument);
//
// fileArgument = new Parameter();
// bytes = Files.readAllBytes(Paths.get("/home/alogo/Downloads/DRIP/example_a.yml"));
// fileArgument.setValue(new String(bytes, charset));
// fileArgument.setEncoding(charset);
// fileArgument.setName("example");
// parameters.add(fileArgument);
//
// invokationMessage.setParameters(parameters);
// invokationMessage.setCreationDate((System.currentTimeMillis()));
//
// planner = new PlannerCaller(messageBrokerHost);
// 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());
return t.getId();//"You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (IOException | IllegalStateException ex) {
Logger.getLogger(ToscaController.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);
Logger.getLogger(ToscaController.class.getName()).log(Level.WARNING, null, ex);
}
}
}
}
return "Upload failed. 'file' was empty.";
return null;
}
// curl http://localhost:8080/drip-api/rest/tosca/589e1160d9925f9dc127e882/?fromat=yaml
@RequestMapping(value = "/{id}", method = RequestMethod.GET, params = {"fromat"})
public @ResponseBody
String getTosca(@PathVariable("id") String id, @RequestParam(value = "fromat") String fromat) throws JSONException {
Map<String, Object> map = dao.findOne(id).getKvMap();
if (fromat != null && fromat.equals("yml")) {
String ymlStr = Converter.map2YmlString(map);
ymlStr = ymlStr.replaceAll("\\uff0E", "\\.");
return ymlStr;
}
if (fromat != null && fromat.equals("json")) {
String jsonStr = Converter.map2JsonString(map);
jsonStr = jsonStr.replaceAll("\\uff0E", "\\.");
return jsonStr;
}
String ymlStr = Converter.map2YmlString(map);
ymlStr = ymlStr.replaceAll("\\uff0E", "\\.");
return ymlStr;
}
// http://localhost:8080/drip-api/rest/tosca/ids
@RequestMapping(value = "/ids")
public @ResponseBody
List<String> getIds() throws JSONException {
List<ToscaRepresentation> all = dao.findAll();
List<String> ids = new ArrayList<>();
for (ToscaRepresentation tr : all) {
ids.add(tr.getId());
}
return ids;
}
}
/*
* 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.api.rest;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
*
* @author S. Koulouzis
*/
public interface ToscaRepository extends MongoRepository<ToscaRepresentation, String> {
}
/*
* 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.api.rest;
import java.util.Map;
import javax.persistence.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
*
* @author S. Koulouzis
*/
@Document
public class ToscaRepresentation {
@Id
private String id;
private Map<String, Object> kvMap;
public final String getId() {
return id;
}
public final void setId(final String id) {
this.id = id;
}
/**
* @return the kvMap
*/
public Map<String, Object> getKvMap() {
return kvMap;
}
/**
* @param kvMap the kvMap to set
*/
public void setKvMap(Map<String, Object> kvMap) {
this.kvMap = kvMap;
}
}
/*
* 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.api.rest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* @author S. Koulouzis
*/
@Service
public class UploadService {
@Autowired
private ToscaRepository dao;
public List<ToscaRepresentation> findAll() {
Iterable<ToscaRepresentation> all = dao.findAll();
List<ToscaRepresentation> list = new ArrayList();
Iterator<ToscaRepresentation> it = all.iterator();
while (it.hasNext()) {
list.add(it.next());
}
return list;
}
public ToscaRepresentation findOneById(String id) {
return dao.findOne(id);
}
public ToscaRepository getDao() {
return dao;
}
}
input.tosca.folder.path=/tmp/
message.broker.host=172.17.0.2
db.name=drip
db.username=drip-user
db.password=drip-pass
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Welcom to the DRIP API</h1>
</body>
</html>
<body>
<div class="container">
<h1>This is secured!</h1>
<p>
Hello <b><c:out value="${pageContext.request.remoteUser}"/></b>
</p>
<c:url var="logoutUrl" value="/logout"/>
<form class="form-inline" action="${logoutUrl}" method="post">
<input type="submit" value="Log out" />
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
</div>
</body>
......@@ -15,14 +15,20 @@
*/
package nl.uva.sne.drip.commons.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.events.Event;
/**
*
......@@ -30,17 +36,73 @@ import org.yaml.snakeyaml.Yaml;
*/
public class Converter {
public static String yml2Json(String yamlString) {
public static String ymlString2Json(String yamlString) {
JSONObject jsonObject = new JSONObject(ymlString2Map(yamlString));
return jsonObject.toString();
}
public static Map<String, Object> ymlString2Map(String yamlString) {
Yaml yaml = new Yaml();
return (Map<String, Object>) yaml.load(yamlString);
}
public static Map<String, Object> ymlString2Map(InputStream in) {
Yaml yaml = new Yaml();
Map<String, Object> map = (Map<String, Object>) yaml.load(yamlString);
Map<String, Object> map = (Map<String, Object>) yaml.load(in);
return map;
}
public static String map2YmlString(Map<String, Object> map) throws JSONException {
JSONObject jsonObject = new JSONObject(map);
return json2Yml2(jsonObject.toString());
}
public static String map2JsonString(Map<String, Object> map) {
JSONObject jsonObject = new JSONObject(map);
return jsonObject.toString();
}
public static Map<String, Object> jsonString2Map(String jsonString) throws JSONException {
JSONObject jsonObject = new JSONObject(jsonString);
return jsonObject2Map(jsonObject);
}
public static Map<String, Object> jsonObject2Map(JSONObject object) throws JSONException {
Map<String, Object> map = new HashMap();
Iterator<String> keysItr = object.keys();
while (keysItr.hasNext()) {
String key = keysItr.next();
Object value = object.get(key);
if (value instanceof JSONArray) {
value = jsonArray2List((JSONArray) value);
} else if (value instanceof JSONObject) {
value = jsonObject2Map((JSONObject) value);
}
map.put(key, value);
}
return map;
}
public static List<Object> jsonArray2List(JSONArray array) throws JSONException {
List<Object> list = new ArrayList();
for (int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if (value instanceof JSONArray) {
value = jsonArray2List((JSONArray) value);
} else if (value instanceof JSONObject) {
value = jsonObject2Map((JSONObject) value);
}
list.add(value);
}
return list;
}
public static String json2Yml2(String jsonString) throws JSONException {
Yaml yaml = new Yaml();
Map<String, Object> map = (Map<String, Object>) yaml.load(jsonString);
return yaml.dump(map);
return yaml.dump(ymlString2Map(jsonString));
}
}
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