diff --git a/datastore/src/main/java/net/jami/datastore/dao/AbstractDao.java b/datastore/src/main/java/net/jami/datastore/dao/AbstractDao.java index 06968295ad130f6b35e5404b570e5292a0795dec..0470e8a488e3f3c5d479b09abcba436c82bc46df 100644 --- a/datastore/src/main/java/net/jami/datastore/dao/AbstractDao.java +++ b/datastore/src/main/java/net/jami/datastore/dao/AbstractDao.java @@ -86,7 +86,7 @@ public abstract class AbstractDao<T> { SQLConnection connection = DataStore.connectionPool.getConnection(); try{ PreparedStatement ps = DeleteStatementBuilder.buildStatement(tableName,delete,connection); - return ps.execute(); + return ps.executeUpdate() != 0; } catch (Exception e){ log.error("An error has occurred while trying to fetch a device: " + e.toString()); diff --git a/datastore/src/main/java/net/jami/datastore/dao/ContactDao.java b/datastore/src/main/java/net/jami/datastore/dao/ContactDao.java index ac39587e242fdca3ef2aa509b661ac9e4c9abb22..8d97878e0ad49021d149e45b936d57dab0d02e94 100644 --- a/datastore/src/main/java/net/jami/datastore/dao/ContactDao.java +++ b/datastore/src/main/java/net/jami/datastore/dao/ContactDao.java @@ -40,9 +40,9 @@ public class ContactDao extends AbstractDao<Contact> { this.setTClass(Contact.class); String createTable = "CREATE TABLE contacts (" + "owner varchar(255), " + - "contact varchar(255)," + + "uri varchar(255)," + "displayName varchar(255),"+ - "PRIMARY KEY (owner,contact))"; + "PRIMARY KEY (owner,uri))"; PreparedStatement ps = connection.getConnection().prepareStatement(createTable); ps.execute(); } @@ -56,6 +56,21 @@ public class ContactDao extends AbstractDao<Contact> { @Override public boolean storeObject(Contact object) { - return false; + SQLConnection connection = DataStore.connectionPool.getConnection(); + try{ + PreparedStatement ps = connection.getConnection().prepareStatement("INSERT INTO contacts " + + "(owner, uri, displayName) " + + "VALUES " + + "(?, ?, ?)"); + ps = object.getInsert(ps); + return ps.execute(); + } + catch (Exception e){ + log.error("An error has occurred while trying to store a user: " + e.toString()); + return false; + } + finally { + DataStore.connectionPool.returnConnection(connection); + } } } diff --git a/integration-test/contact-test.py b/integration-test/contact-test.py new file mode 100644 index 0000000000000000000000000000000000000000..6bf97644aa0e9e9960c34da5f0c8c523b8c79ba3 --- /dev/null +++ b/integration-test/contact-test.py @@ -0,0 +1,48 @@ +import json +import requests +import urllib.parse + +header = {} +header['Content-type'] = "application/json" + +response = requests.post('http://localhost:8080/api/auth/login',data={"username":"fsidokhine","password":"mes87hesm97daa"}) +if response.status_code == 200: + token = json.loads(response.text)['access_token'] + +header = {} +header['Bearer'] = token + + +#Create two contacts +data = {} +data['uri'] = "jami://71828737748484" +data['displayName'] = "Felix" +response = requests.put("http://localhost:8080/api/auth/contacts",headers=header,data=json.dumps(data)) +print(response.status_code) +print(response.text) + +data = {} +data['uri'] = "jami://71828737748485" +data['displayName'] = "Adrien" +response = requests.put("http://localhost:8080/api/auth/contacts",headers=header,data=json.dumps(data)) +print(response.status_code) +print(response.text) + +#Get both contacts + +response = requests.get("http://localhost:8080/api/auth/contacts",headers=header) +print(response.status_code) +print(response.text) + +#Delete a contact +params = {'uri':'jami://71828737748485'} +p = urllib.parse.urlencode(params,safe='') +response = requests.delete("http://localhost:8080/api/auth/contacts?" + p,headers=header) +print(response.status_code) +print(response.text) +# +##Get contact +# +response = requests.get("http://localhost:8080/api/auth/contacts",headers=header) +print(response.status_code) +print(response.text) \ No newline at end of file diff --git a/integration-test/install-server.py b/integration-test/install-server.py index 9d97dd5e45806479bef5c2896b45969f9ac6da60..c102383deab0568a39e36a5df8c6cfd6b5e297c2 100644 --- a/integration-test/install-server.py +++ b/integration-test/install-server.py @@ -131,4 +131,4 @@ print(response.text) response = requests.post("http://localhost:8080/api/update",headers=header) print(response.status_code) -print(response.text) +print(response.text) \ No newline at end of file diff --git a/jams-common/src/main/java/net/jami/jams/common/dao/DeleteStatementBuilder.java b/jams-common/src/main/java/net/jami/jams/common/dao/DeleteStatementBuilder.java index 8c3d05f6edd63e10845b5ea6e26975c3ab194a5c..46be13b6a5b031d20aff0b0d66ca9f6800a72e9d 100644 --- a/jams-common/src/main/java/net/jami/jams/common/dao/DeleteStatementBuilder.java +++ b/jams-common/src/main/java/net/jami/jams/common/dao/DeleteStatementBuilder.java @@ -43,7 +43,8 @@ public class DeleteStatementBuilder { .append(" ") .append("?") .append(" ") - .append(statementElement.getNextStatementRelation()); + .append(statementElement.getNextStatementRelation()) + .append(" "); } ps = connection.getConnection().prepareStatement(stringBuilder.toString()); int i = 1; diff --git a/jams-common/src/main/java/net/jami/jams/common/dao/SelectStatementBuilder.java b/jams-common/src/main/java/net/jami/jams/common/dao/SelectStatementBuilder.java index c361e884579819fca538de04fdcedd2c33db2396..fb80dea462edd8424084d87e789879667fd13757 100644 --- a/jams-common/src/main/java/net/jami/jams/common/dao/SelectStatementBuilder.java +++ b/jams-common/src/main/java/net/jami/jams/common/dao/SelectStatementBuilder.java @@ -44,7 +44,8 @@ public class SelectStatementBuilder { .append(" ") .append("?") .append(" ") - .append(statementElement.getNextStatementRelation()); + .append(statementElement.getNextStatementRelation()) + .append(" "); } ps = connection.getConnection().prepareStatement(stringBuilder.toString()); int i = 1; diff --git a/jams-server/src/main/java/net/jami/jams/server/core/TomcatLauncher.java b/jams-server/src/main/java/net/jami/jams/server/core/TomcatLauncher.java index 863b549c20bbedc4ea70d63d1633de73135e775a..a3378db4ae08059f70011dde0bd19c001d8d7c52 100644 --- a/jams-server/src/main/java/net/jami/jams/server/core/TomcatLauncher.java +++ b/jams-server/src/main/java/net/jami/jams/server/core/TomcatLauncher.java @@ -1,25 +1,26 @@ /* -* Copyright (C) 2020 by Savoir-faire Linux -* Authors: William Enright <william.enright@savoirfairelinux.com> -* Ndeye Anna Ndiaye <anna.ndiaye@savoirfairelinux.com> -* Johnny Flores <johnny.flores@savoirfairelinux.com> -* Mohammed Raza <mohammed.raza@savoirfairelinux.com> -* Felix Sidokhine <felix.sidokhine@savoirfairelinux.com> -* -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see <https://www.gnu.org/licenses/>. -*/ + * Copyright (C) 2020 by Savoir-faire Linux + * Authors: William Enright <william.enright@savoirfairelinux.com> + * Ndeye Anna Ndiaye <anna.ndiaye@savoirfairelinux.com> + * Johnny Flores <johnny.flores@savoirfairelinux.com> + * Mohammed Raza <mohammed.raza@savoirfairelinux.com> + * Felix Sidokhine <felix.sidokhine@savoirfairelinux.com> + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + package net.jami.jams.server.core; import lombok.extern.slf4j.Slf4j; @@ -38,7 +39,6 @@ import java.net.URI; import java.nio.file.Files; import java.nio.file.Paths; - //This class boots the tomcat server which provides the subsystem //for the API calls. @Slf4j @@ -54,18 +54,23 @@ public class TomcatLauncher { } public TomcatLauncher(int port, String certificateFile, String keyFile) { - if(Files.exists(Paths.get(System.getProperty("user.dir") + File.separator + "keystore.jks"))){ + if (!Files.exists(Paths.get(System.getProperty("user.dir") + File.separator + certificateFile)) || + !Files.exists(Paths.get(System.getProperty("user.dir") + File.separator + keyFile))) { + log.info("Could not find certificate or keyfile, starting in plain HTTP connector as fallback!"); + tomcat.getService().addConnector(TomcatConnectorFactory.getNoSSLConnector(port)); + this.startServer(); + return; + } + if (Files.exists(Paths.get(System.getProperty("user.dir") + File.separator + "keystore.jks"))) { log.info("Found a valid trust store, injecting into tomcat!"); tomcat.getService().addConnector(TomcatConnectorFactory.getSSLConnectorWithTrustStore(certificateFile, keyFile, port)); - } - else{ + } else { connector = TomcatConnectorFactory.getSSLConnectorWithoutTrustStore(certificateFile, keyFile, port); tomcat.getService().addConnector(connector); } this.startServer(); } - public void startServer() { String jarName = System.getProperty("user.dir") + File.separator + "jams-server.jar"; log.info("JAR Resource File = " + jarName); @@ -77,12 +82,11 @@ public class TomcatLauncher { if (jarName.contains(".jar")) { resources.addPreResources(new JarResourceSet(resources, "/WEB-INF/classes", jarName, "/net/jami/jams/server/servlets")); resources.addPreResources(new JarResourceSet(resources, "/", jarName, "/webapp")); - } - else { + } else { log.info("WARNING: You are running from your local filesystem, this makes sense only for developers!"); StringBuilder basePath = new StringBuilder(); String[] paths = System.getProperty("user.dir").split("/"); - for(int i=0; i < paths.length-1; i++){ + for (int i = 0; i < paths.length - 1; i++) { basePath.append("/").append(paths[i]); } basePath.append("/jams-server"); @@ -96,28 +100,27 @@ public class TomcatLauncher { errorPage.setErrorCode(404); errorPage.setLocation("/index"); context.addErrorPage(errorPage); - try{ + try { tomcat.start(); //Try to pop-up Web-UI - if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) + if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { Desktop.getDesktop().browse(new URI("https://localhost:8080")); - else + } else { log.info("There is no graphical interface on this system - please connect remotely!"); - + } } catch (Exception e) { log.error("Could not start web-server!"); } } - public void stopTomcat(){ + public void stopTomcat() { try { synchronized (tomcat) { tomcat.stop(); tomcat.destroy(); } - } - catch (Exception e){ - log.info("Failed to stop tomcat server with error {}",e.getMessage()); + } catch (Exception e) { + log.info("Failed to stop tomcat server with error {}", e.getMessage()); } } } diff --git a/jams-server/src/main/java/net/jami/jams/server/servlets/api/auth/contacts/ContactServlet.java b/jams-server/src/main/java/net/jami/jams/server/servlets/api/auth/contacts/ContactServlet.java index e43a40d46f0535fba9b314d60ca0092882189816..70c9075cab0f8ab132156b2f3b67233ae2c9da9e 100644 --- a/jams-server/src/main/java/net/jami/jams/server/servlets/api/auth/contacts/ContactServlet.java +++ b/jams-server/src/main/java/net/jami/jams/server/servlets/api/auth/contacts/ContactServlet.java @@ -54,7 +54,7 @@ public class ContactServlet extends HttpServlet { Contact contact = JsonIterator.deserialize(req.getInputStream().readAllBytes(),Contact.class); contact.setOwner(req.getAttribute("username").toString()); if(dataStore.getContactDao().storeObject(contact)) resp.setStatus(200); - else resp.sendError(500,"Could not store device!"); + else resp.sendError(500,"Could not store contact!"); } //TODO: Because deleting requires sending the URI in the body, didn't want to do this now.