Commit 6943d938 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Added user key controller

Added Cloud conf controller
Added json examples 
Added user script controller
parent 1b947c6c
{
"key": "ssh-rsa AAAAB3NzaC1yc2EAAWBqs75i849MytgwgQcRYMcsXIki0yeYTKABH6JqoiyFBHtYlyh/EV1t6cujb9LyNR1ArgFP4J5EN4fPbtwKYvxecd0LojSPxl4wjQlfrHyg6iKUYB7hVzGqACMvgYZHrtHPfrdEmOGPplPVPpoaX2j+u0BZ0yYhrWMKjzyYZKa68yy5N18+Gq+1p83HfUDwIU9wWaUYdgEvDujqF6b8p3z6LDx9Ob+RanSMZSt+b8eZRcd+F2Oy/gieJEJ8kc152VIOv8UY1xB3hVEwVnSRGgrAsa+9PChfF6efXUGWiKf8KBlWgBOYsSTsOY4ks9zkXMnbcTdC+o7xspOkyIcWjv u@host\n",
"name": "id_rsa.pub"
}
/*
* 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.dao;
import nl.uva.sne.drip.commons.types.UserPublicKey;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
*
* @author S. Koulouzis
*/
public interface UserKeyDao extends MongoRepository<UserPublicKey, 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.dao;
import nl.uva.sne.drip.commons.types.UserScript;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
*
* @author S. Koulouzis
*/
public interface UserScriptDao extends MongoRepository<UserScript, String> {
}
......@@ -15,28 +15,15 @@
*/
package nl.uva.sne.drip.api.rest;
import nl.uva.sne.drip.api.rpc.PlannerCaller;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
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.CloudCredentials;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
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;
import nl.uva.sne.drip.commons.types.LoginKey;
import nl.uva.sne.drip.api.dao.CloudCredentialsDao;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -50,52 +37,27 @@ import org.springframework.web.bind.annotation.PathVariable;
public class CloudConfigurationController {
@Autowired
private CloudCredentialsDao dao;
//
// @RequestMapping(value = "/upload", method = RequestMethod.POST)
// public @ResponseBody
// String loginKeyUpload(@RequestParam("file") MultipartFile file) throws JSONException {
// if (!file.isEmpty()) {
// try {
//
// String originalFileName = file.getOriginalFilename();
// String name = System.currentTimeMillis() + "_" + originalFileName;
// byte[] bytes = file.getBytes();
// String key = new String(bytes, "UTF-8");
// CloudCredentials kr = new CloudCredentials();
//
// return null;
// } catch (IOException | IllegalStateException ex) {
// Logger.getLogger(CloudConfigurationController.class.getName()).log(Level.SEVERE, null, ex);
// } finally {
// if (planner != null) {
// try {
// planner.close();
// } catch (IOException | TimeoutException ex) {
// Logger.getLogger(CloudConfigurationController.class.getName()).log(Level.WARNING, null, ex);
// }
// }
// }
// }
// return null;
// }
private CloudCredentialsDao cloudCredentialsDao;
// curl -H "Content-Type: application/json" -X POST -d '{"key":"my_secret_password","keyIdAlias":"geni","logineKys":[{"attributes":null,"key":"-----BEGINRSAPUBLICKEY-----\nMIIBCgKCAQEA+xGZ/wcz9ugFpP07Nspo6U17l0YhFiFpxxU4pTk3Lifz9R3zsIsu\nERwta7+fWIfxOo208ett/jhskiVodSEt3QBGh4XBipyWopKwZ93HHaDVZAALi/2A\n+xTBtWdEo7XGUujKDvC2/aZKukfjpOiUI8AhLAfjmlcD/UZ1QPh0mHsglRNCmpCw\nmwSXA9VNmhz+PiB+Dml4WWnKW/VHo2ujTXxq7+efMU4H2fny3Se3KYOsFPFGZ1TN\nQSYlFuShWrHPtiLmUdPoP6CV2mML1tk+l7DIIqXrQhLUKDACeM5roMx0kLhUWB8P\n+0uj1CNlNN4JRZlC7xFfqiMbFRU9Z4N6YwIDAQAB\n-----ENDRSAPUBLICKEY-----","type":"PUBLIC"},{"attributes":null,"key":"-----BEGINRSAPRIVATEKEY-----\nMIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp\nwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5\n1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh\n3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2\npIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX\nGukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il\nAkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF\nL0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k\nX6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl\nU9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ\n37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0=\n-----ENDRSAPRIVATEKEY-----","type":"PRIVATE"}],"cloudProviderName":"exogeni"}' http://localhost:8080/drip-api/rest/configuration
// curl -H "Content-Type: application/json" -X POST -d '{"key":"AKISAKISAKIS","keyIdAlias":"6J76J76J76J76J76J76J7","logineKys":[{"attributes":{"domain_name":"California"},"type":"PUBLIC","key":"-----BEGINRSAPRIVATEKEY-----\nMIIEpQIBAAKCAQEA3Tz2mr7SZiAMfQyuvBjM9Oi..Z1BjP5CE/Wm/Rr500P\nRK+Lh9x5eJPo5CAZ3/ANBE0sTK0ZsDGMak2m1g7..3VHqIxFTz0Ta1d+NAj\nwnLe4nOb7/eEJbDPkk05ShhBrJGBKKxb8n104o/..PdzbFMIyNjJzBM2o5y\n5A13wiLitEO7nco2WfyYkQzaxCw0AwzlkVHiIyC..71pSzkv6sv+4IDMbT/\nXpCo8L6wTarzrywnQsh+etLD6FtTjYbbrvZ8RQM..Hg2qxraAV++HNBYmNW\ns0duEdjUbJK+ZarypXI9TtnS4o1Ckj7POfljiQI..IBAFyidxtqRQyv5KrD\nkbJ+q+rsJxQlaipn2M4lGuQJEfIxELFDyd3XpxP..Un/82NZNXlPmRIopXs\n2T91jiLZEUKQw+n73j26adTbteuEaPGSrTZxBLR..yssO0wWomUyILqVeti\n6AkL0NJAuKcucHGqWVgUIa4g1haE0ilcm6dWUDo..fd+PpzdCJf1s4NdUWK\nYV2GJcutGQb+jqT5DTUqAgST7N8M28rwjK6nVMI..BUpP0xpPnuYDyPOw6x\n4hBt8DZQYyduzIXBXRBKNiNdv8fum68/5klHxp6..4HRkMUL958UVeljUsT\nBFQlO9UCgYEA/VqzXVzlz8K36VSTMPEhB5zBATV..PRiXtYK1YpYV4/jSUj\nvvT4hP8uoYNC+BlEMi98LtnxZIh0V4rqHDsScAq..VyeSLH0loKMZgpwFEm\nbEIDnEOD0nKrfT/9K9sPYgvB43wsLEtUujaYw3W..Liy0WKmB8CgYEA34xn\n1QlOOhHBn9Z8qYjoDYhvcj+a89tD9eMPhesfQFw..rsfGcXIonFmWdVygbe\n6Doihc+GIYIq/QP4jgMksE1ADvczJSke92ZfE2i..fitBpQERNJO0BlabfP\nALs5NssKNmLkWS2U2BHCbv4DzDXwiQB37KPOL1c..kBHfF2/htIs20d1UVL\n+PK+aXKwguI6bxLGZ3of0UH+mGsSl0mkp7kYZCm..OTQtfeRqP8rDSC7DgA\nkHc5ajYqh04AzNFaxjRo+M3IGICUaOdKnXd0Fda..QwfoaX4QlRTgLqb7AN\nZTzM9WbmnYoXrx17kZlT3lsCgYEAm757XI3WJVj..WoLj1+v48WyoxZpcai\nuv9bT4Cj+lXRS+gdKHK+SH7J3x2CRHVS+WH/SVC..DxuybvebDoT0TkKiCj\nBWQaGzCaJqZa+POHK0klvS+9ln0/6k539p95tfX..X4TCzbVG6+gJiX0ysz\nYfehn5MCgYEAkMiKuWHCsVyCab3RUf6XA9gd3qY..fCTIGtS1tR5PgFIV+G\nengiVoWc/hkj8SBHZz1n1xLN7KDf8ySU06MDggB..hJ+gXJKy+gf3mF5Kmj\nDtkpjGHQzPF6vOe907y5NQLvVFGXUq/FIJZxB8k..fJdHEm2M4=\n-----ENDRSAPRIVATEKEY-----"},{"attributes":{"domain_name":"Virginia"},"type":"PUBLIC","key":"-----BEGINRSAPRIVATEKEY-----\nMIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp\nwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5\n1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh\n3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2\npIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX\nGukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il\nAkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF\nL0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k\nX6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl\nU9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ\n37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0=\n-----ENDRSAPRIVATEKEY-----"}],"cloudProviderName":"ec2"}'
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody
String postConf(CloudCredentials cc) throws JSONException {
dao.save(cc);
String postConf(CloudCredentials cc) {
cloudCredentialsDao.save(cc);
return cc.getId();
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public CloudCredentials get(@PathVariable("id") String id) throws JSONException, IOException {
return dao.findOne(id);
@RequestMapping(value = "/cloud/{id}", method = RequestMethod.GET)
public CloudCredentials get(@PathVariable("id") String id) {
return cloudCredentialsDao.findOne(id);
}
@RequestMapping(value = "/ids")
@RequestMapping(value = "/cloud/ids")
public @ResponseBody
List<String> getIds() {
List<CloudCredentials> all = dao.findAll();
List<CloudCredentials> all = cloudCredentialsDao.findAll();
List<String> ids = new ArrayList<>();
for (CloudCredentials tr : all) {
ids.add(tr.getId());
......
......@@ -17,12 +17,8 @@ package nl.uva.sne.drip.api.rest;
import nl.uva.sne.drip.commons.types.ToscaRepresentation;
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;
......@@ -30,7 +26,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -45,10 +40,8 @@ import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
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;
import nl.uva.sne.drip.api.dao.ToscaDao;
/**
......@@ -67,7 +60,7 @@ public class PlannerController {
@RequestMapping(value = "/plan/{tosca_id}", method = RequestMethod.POST)
public @ResponseBody
String plann(@PathVariable("tosca_id") String toscaId) throws JSONException, IOException, TimeoutException {
String plann(@PathVariable("tosca_id") String toscaId) {
PlannerCaller planner = null;
try {
ToscaRepresentation t2 = dao.findOne(toscaId);
......@@ -106,9 +99,15 @@ public class PlannerController {
return returned;
} catch (UnsupportedEncodingException | TimeoutException | InterruptedException ex) {
Logger.getLogger(PlannerController.class.getName()).log(Level.SEVERE, null, ex);
} catch (JSONException | IOException ex) {
Logger.getLogger(PlannerController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (planner != null) {
planner.close();
try {
planner.close();
} catch (IOException | TimeoutException ex) {
Logger.getLogger(PlannerController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return null;
......
......@@ -27,7 +27,6 @@ import java.util.logging.Logger;
import nl.uva.sne.drip.commons.utils.Converter;
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;
......@@ -47,16 +46,15 @@ import nl.uva.sne.drip.api.dao.ToscaDao;
@Component
public class ToscaController {
@Value("${message.broker.host}")
private String messageBrokerHost;
// @Value("${message.broker.host}")
// private String messageBrokerHost;
@Autowired
private ToscaDao dao;
// curl -X POST -F "file=@DRIP/input.yaml" localhost:8080/drip-api/rest/upload
// 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) throws JSONException {
String toscaUpload(@RequestParam("file") MultipartFile file) {
PlannerCaller planner = null;
if (!file.isEmpty()) {
try {
......@@ -92,21 +90,26 @@ public class ToscaController {
// curl http://localhost:8080/drip-api/rest/tosca/589e1160d9925f9dc127e882/?fromat=yaml
@RequestMapping(value = "/{id}", method = RequestMethod.GET, params = {"fromat"})
public @ResponseBody
String get(@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 get(@PathVariable("id") String id, @RequestParam(value = "fromat") String fromat) {
try {
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;
} catch (JSONException ex) {
Logger.getLogger(ToscaController.class.getName()).log(Level.SEVERE, null, ex);
}
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;
return null;
}
// http://localhost:8080/drip-api/rest/tosca/ids
......
/*
* Copyright 2017 S. Koulouzis.
*
* 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 nl.uva.sne.drip.commons.types.UserPublicKey;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
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;
import nl.uva.sne.drip.api.dao.UserKeyDao;
import org.springframework.web.bind.annotation.PathVariable;
/**
*
* @author S. Koulouzis
*/
@RestController
@RequestMapping("/rest/user_key")
@Component
public class UserPublicKeysController {
@Autowired
private UserKeyDao dao;
// curl -v -X POST -F "file=@.ssh/id_dsa.pub" localhost:8080/drip-api/rest/user_key/upload
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody
String uploadUserPublicKeys(@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
try {
String originalFileName = file.getOriginalFilename();
String name = System.currentTimeMillis() + "_" + originalFileName;
byte[] bytes = file.getBytes();
String key = new String(bytes, "UTF-8");
UserPublicKey upk = new UserPublicKey();
upk.setKey(key);
upk.setName(name);
dao.save(upk);
return upk.getId();
} catch (IOException | IllegalStateException ex) {
Logger.getLogger(UserPublicKeysController.class.getName()).log(Level.SEVERE, null, ex);
}
}
return null;
}
// curl -H "Content-Type: application/json" -X POST -d '{"key":"ssh-rsa AAAAB3NzaDWBqs75i849MytgwgQcRYMcsXIki0yeYTKABH6JqoiyFBHtYlyh/EV1t6cujb9LyNP4J5EN4fPbtwKYvxecd0LojSPxl4wjQlfrHyg6iKUYB7hVzGqACMvgYZHrtHPfrdEmOGPplPVPpoaX2j+u0BZ0yYhrWMKjzyYZKa68yy5N18+Gq+1p83HfUDwIU9wWaUYdgEvDujqF6b8p3z6LDx9Ob+RanSMZSt+b8eZRcd+F2Oy/gieJEJ8kc152VIOv8UY1xB3hVEwVnSRGgrAsa+9PChfF6efXUGWiKf8KBlWgBOYsSTsOY4ks9zkXMnbcTdC+o7xspOkyIcWjv us@u\n","name":"id_rsa.pub"}' localhost:8080/drip-api/rest/user_key/
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody
String postConf(UserPublicKey uk) throws JSONException {
String name = System.currentTimeMillis() + "_" + uk.getName();
uk.setName(name);
dao.save(uk);
return uk.getId();
}
//curl localhost:8080/drip-api/rest/user_key/58a20be263d4a5898835676e
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public UserPublicKey get(@PathVariable("id") String id) {
return dao.findOne(id);
}
// localhost:8080/drip-api/rest/user_key/ids
@RequestMapping(value = "/ids")
public @ResponseBody
List<String> getIds() {
List<UserPublicKey> all = dao.findAll();
List<String> ids = new ArrayList<>();
for (UserPublicKey tr : all) {
ids.add(tr.getId());
}
return ids;
}
}
/*
* Copyright 2017 S. Koulouzis.
*
* 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 nl.uva.sne.drip.commons.types.UserPublicKey;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
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;
import nl.uva.sne.drip.api.dao.UserScriptDao;
import nl.uva.sne.drip.commons.types.UserScript;
import org.springframework.web.bind.annotation.PathVariable;
/**
*
* @author S. Koulouzis
*/
@RestController
@RequestMapping("/rest/user_script")
@Component
public class UserScriptController {
@Autowired
private UserScriptDao dao;
// curl -v -X POST -F "file=@.ssh/id_dsa.pub" localhost:8080/drip-api/rest/user_script/upload
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody
String uploadUserScript(@RequestParam("file") MultipartFile file) throws JSONException {
if (!file.isEmpty()) {
try {
String originalFileName = file.getOriginalFilename();
String name = System.currentTimeMillis() + "_" + originalFileName;
byte[] bytes = file.getBytes();
String conents = new String(bytes, "UTF-8");
UserScript us = new UserScript();
us.setContents(conents);
us.setName(name);
dao.save(us);
return us.getId();
} catch (IOException | IllegalStateException ex) {
Logger.getLogger(UserScriptController.class.getName()).log(Level.SEVERE, null, ex);
}
}
return null;
}
// curl -H "Content-Type: application/json" -X POST -d '{"key":"ssh-rsa AAAAB3NzaDWBqs75i849MytgwgQcRYMcsXIki0yeYTKABH6JqoiyFBHtYlyh/EV1t6cujb9LyNP4J5EN4fPbtwKYvxecd0LojSPxl4wjQlfrHyg6iKUYB7hVzGqACMvgYZHrtHPfrdEmOGPplPVPpoaX2j+u0BZ0yYhrWMKjzyYZKa68yy5N18+Gq+1p83HfUDwIU9wWaUYdgEvDujqF6b8p3z6LDx9Ob+RanSMZSt+b8eZRcd+F2Oy/gieJEJ8kc152VIOv8UY1xB3hVEwVnSRGgrAsa+9PChfF6efXUGWiKf8KBlWgBOYsSTsOY4ks9zkXMnbcTdC+o7xspOkyIcWjv us@u\n","name":"id_rsa.pub"}' localhost:8080/drip-api/rest/user_key/
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody
String postConf(UserScript us) {
String name = System.currentTimeMillis() + "_" + us.getName();
us.setName(name);
dao.save(us);
return us.getId();
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public UserScript get(@PathVariable("id") String id) {
return dao.findOne(id);
}
@RequestMapping(value = "/ids")
public @ResponseBody
List<String> getIds() {
List<UserScript> all = dao.findAll();
List<String> ids = new ArrayList<>();
for (UserScript us : all) {
ids.add(us.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.commons.types;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
*
* @author S. Koulouzis
*/
@Document
public class UserPublicKey {
@Id
private String id;
private String key;
private String name;
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the key
*/
public String getKey() {
return key;
}
/**
* @param key the key to set
*/
public void setKey(String key) {
this.key = key;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
/*
* 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 org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
*
* @author S. Koulouzis
*/
@Document
public class UserScript {
@Id
private String id;
private String contents;
private String name;
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the contents
*/
public String getContents() {
return contents;
}
/**
* @param contents the contents to set
*/
public void setContents(String contents) {
this.contents = contents;
}
}
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