From 37f438ba0239e4c6dff9f74e2bb935a16bbfa7ab Mon Sep 17 00:00:00 2001
From: Felix Sidokhine <felix.sidokhine@savoirfairelinux.com>
Date: Tue, 4 Aug 2020 10:42:42 +0300
Subject: [PATCH] fixes for bugs and build scripts

Change-Id: I0ae8cb75a3428d098299f95b250c7cedccb9e653
---
 jams-react-client/build_deploy_jams_client.sh |  26 ---
 jams-server/build-ui.sh                       |  10 ++
 jams-server/pom.xml                           |  18 ++
 .../jami/jams/server/core/TomcatLauncher.java |   2 +-
 .../api/admin/contacts/ContactServlet.java    | 169 ++++++++++++++++++
 5 files changed, 198 insertions(+), 27 deletions(-)
 delete mode 100755 jams-react-client/build_deploy_jams_client.sh
 create mode 100755 jams-server/build-ui.sh
 create mode 100644 jams-server/src/main/java/net/jami/jams/server/servlets/api/admin/contacts/ContactServlet.java

diff --git a/jams-react-client/build_deploy_jams_client.sh b/jams-react-client/build_deploy_jams_client.sh
deleted file mode 100755
index 56b8f594..00000000
--- a/jams-react-client/build_deploy_jams_client.sh
+++ /dev/null
@@ -1,26 +0,0 @@
-BUILD_DIRECTORY=build/
-WEBAPP=../jams-server/src/main/resources/webapp/
-JAMS_CLIENT=jams-client/
-JAMS=jams
-
-sudo npm run build
-
-rm -rf $WEBAPP*
-cp -r $BUILD_DIRECTORY* $WEBAPP
-
-sed -i 's/material-dashboard-react\///g' $WEBAPP"index.html"
-
-
-#cd ../$JAMS
-#rm -rf jams
-#rm derby.log
-#rm oauth.key
-#rm oauth.pub
-#rm config.json
-
-cd ..
-#mvn clean package -DskipTests
-mvn package
-
-cd $JAMS
-java -jar jams-launcher.jar 8080 server.pem server.key
diff --git a/jams-server/build-ui.sh b/jams-server/build-ui.sh
new file mode 100755
index 00000000..594748d8
--- /dev/null
+++ b/jams-server/build-ui.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+#Build the UI from the react js folder.
+cd ../jams-react-client || exit
+npm run build
+#Now the UI is built we can exit and copy resource
+mkdir -p ../jams-server/src/main/resources/webapp
+sed -i 's/material-dashboard-react\///g' build/index.html
+rm -rf ../jams-server/src/main/resources/webapp/*
+mv build/* ../jams-server/src/main/resources/webapp
+rm -rf build
diff --git a/jams-server/pom.xml b/jams-server/pom.xml
index 1734c709..6556e2c5 100644
--- a/jams-server/pom.xml
+++ b/jams-server/pom.xml
@@ -217,6 +217,24 @@
                     </execution>
                 </executions>
             </plugin>
+            <plugin>
+                <artifactId>exec-maven-plugin</artifactId>
+                <groupId>org.codehaus.mojo</groupId>
+                <version>${maven.exec.version}</version>
+                <executions>
+                    <execution>
+                        <id>Build UI</id>
+                        <phase>prepare-package</phase>
+                        <goals>
+                            <goal>exec</goal>
+                        </goals>
+                        <configuration>
+                            <executable>bash</executable>
+                            <commandlineArgs>build-ui.sh</commandlineArgs>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
         </plugins>
     </build>
 
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 92d94db2..458ba08e 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
@@ -57,7 +57,7 @@ public class TomcatLauncher {
 
     //Swap connectors fix.
     public void swapConnectors(){
-        if(getTomcat().getConnector().findSslHostConfigs() == null || getTomcat().getConnector().findSslHostConfigs().length < 1){
+        if(getTomcat().getConnector().findSslHostConfigs() != null && getTomcat().getConnector().findSslHostConfigs().length > 0){
             getTomcat().getConnector().findSslHostConfigs()[0].setTruststoreFile(System.getProperty("user.dir") + File.separator + "keystore.jks");
             getTomcat().getConnector().findSslHostConfigs()[0].setTruststorePassword("changeit");
             getTomcat().getConnector().findSslHostConfigs()[0].setCertificateVerification("optional");
diff --git a/jams-server/src/main/java/net/jami/jams/server/servlets/api/admin/contacts/ContactServlet.java b/jams-server/src/main/java/net/jami/jams/server/servlets/api/admin/contacts/ContactServlet.java
new file mode 100644
index 00000000..57738bca
--- /dev/null
+++ b/jams-server/src/main/java/net/jami/jams/server/servlets/api/admin/contacts/ContactServlet.java
@@ -0,0 +1,169 @@
+/*
+* 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.servlets.api.admin.contacts;
+
+import com.jsoniter.JsonIterator;
+import com.jsoniter.output.JsonStream;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.annotation.WebServlet;
+import jakarta.servlet.http.HttpServlet;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import net.jami.jams.common.annotations.ScopedServletMethod;
+import net.jami.jams.common.dao.StatementElement;
+import net.jami.jams.common.dao.StatementList;
+import net.jami.jams.common.objects.contacts.Contact;
+import net.jami.jams.common.objects.user.AccessLevel;
+import net.jami.jams.common.serialization.tomcat.TomcatCustomErrorHandler;
+import net.jami.jams.common.utils.ContactMerger;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static net.jami.jams.server.Server.dataStore;
+
+@WebServlet("/api/admin/contacts")
+public class ContactServlet extends HttpServlet {
+
+
+    /**
+     * @apiVersion 1.0.0
+     * @api {get} /api/auth/contacts View contacts
+     * @apiName getContact
+     * @apiGroup Contacts
+     *
+     * @apiSuccess (200) {body} Contact[]  List of contacts for the user
+     * @apiSuccessExample {json} Success-Response:
+     *     [{
+     *       "uri": "jami://7e3ab29383",
+     *       "added": 18272662662
+     *     },
+     *     {
+     *        "uri": "jami://7e3ab29383",
+     *        "removed": 12387873
+     *      },
+     *     ]
+     */
+    @Override
+    @ScopedServletMethod(securityGroups = {AccessLevel.ADMIN})
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        StatementList statementList = new StatementList();
+        statementList.addStatement(new StatementElement("owner","=",req.getParameter("username").toString(),""));
+        List<Contact> contactList = dataStore.getContactDao().getObjects(statementList);
+        resp.getOutputStream().write(JsonStream.serialize(contactList).getBytes());
+    }
+
+
+    /**
+     * @apiVersion 1.0.0
+     * @api {put} /api/auth/contacts Add a contact
+     * @apiName putContact
+     * @apiGroup Contacts
+     *
+     * @apiParam {body} Contact JSON representation of the contact object
+     * @apiParamExample {json} Request-Example:
+     *      {
+     *        "uri": "jami://7e3ab29383"
+     *      }
+     *
+     * @apiSuccess (200) {null} null successfully added contact
+     * @apiError (500) {null} null contact could not be successfully added
+     */
+    @Override
+    @ScopedServletMethod(securityGroups = {AccessLevel.ADMIN})
+    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        Contact contact = JsonIterator.deserialize(req.getInputStream().readAllBytes(),Contact.class);
+        //TODO: Replace with mergetool.
+        contact.setTimestamp(System.currentTimeMillis());
+        contact.setStatus('A');
+        contact.setOwner(req.getAttribute("username").toString());
+        StatementList statementList = new StatementList();
+        statementList.addStatement(new StatementElement("owner","=",req.getParameter("username").toString(),""));
+        List<Contact> localList = dataStore.getContactDao().getObjects(statementList);
+        List<Contact> remoteList = new ArrayList<>();
+        remoteList.add(contact);
+        List<Contact> result = ContactMerger.mergeContacts(localList,remoteList);
+        if(dataStore.getContactDao().storeContactList(result)) resp.setStatus(200);
+        else TomcatCustomErrorHandler.sendCustomError(resp,500,"could not store a contact due to server-side error");
+    }
+
+    /**
+     * @apiVersion 1.0.0
+     * @api {delete} /api/auth/contacts Delete a contact
+     * @apiName deleteContact
+     * @apiGroup Contacts
+     *
+     * @apiParam {query} uri uri of the contact to delete
+     *
+     * @apiSuccess (200) {null} null successfully deleted contact
+     * @apiError (500) {null} null contact could not be successfully deleted
+     */
+    @Override
+    @ScopedServletMethod(securityGroups = {AccessLevel.ADMIN})
+    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        StatementList statementList = new StatementList();
+        statementList.addStatement(new StatementElement("owner","=",req.getParameter("username").toString(),"AND"));
+        statementList.addStatement(new StatementElement("uri","=",req.getParameter("uri"),""));
+        List<Contact> remoteList = dataStore.getContactDao().getObjects(statementList);
+        remoteList.get(0).setStatus('D');
+        remoteList.get(0).setTimestamp(System.currentTimeMillis());
+        statementList = new StatementList();
+        statementList.addStatement(new StatementElement("owner","=",req.getParameter("username").toString(),"AND"));
+        List<Contact> localList = dataStore.getContactDao().getObjects(statementList);
+        List<Contact> result = ContactMerger.mergeContacts(localList,remoteList);
+        if(dataStore.getContactDao().storeContactList(result)) resp.setStatus(200);
+        else TomcatCustomErrorHandler.sendCustomError(resp,500,"could not delete a contact due to server-side error");
+    }
+
+
+    /**
+     * @apiVersion 1.0.0
+     * @api {post} /api/auth/contacts Add a contact
+     * @apiName putContact
+     * @apiGroup Contacts
+     *
+     * @apiParam {body} Contact JSON representation of the contact object
+     * @apiParamExample {json} Request-Example:
+     *[
+     * {"uri":"tcp://def@local","added":1594742298377},
+     * {"uri":"tcp://abc@19293.com","removed":1594742298377}
+     * ]
+     *
+     * @apiSuccess (200) {json} Contact[] successfully added contact
+     * @apiError (500) {null} null contact could not be successfully added
+     */
+    @Override
+    @ScopedServletMethod(securityGroups = {AccessLevel.ADMIN})
+    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        StatementList statementList = new StatementList();
+        statementList.addStatement(new StatementElement("owner","=",req.getParameter("username").toString(),""));
+        List<Contact> localList = dataStore.getContactDao().getObjects(statementList);
+        List<Contact> remoteList = Arrays.asList(JsonIterator.deserialize(req.getInputStream().readAllBytes(),Contact[].class));
+        remoteList.forEach(contact -> contact.setOwner(req.getParameter("username").toString()));
+        List<Contact> result = ContactMerger.mergeContacts(localList,remoteList);
+        if(!dataStore.getContactDao().storeContactList(result)) TomcatCustomErrorHandler.sendCustomError(resp,500,"Could not store contacts!");
+        else resp.getOutputStream().write(JsonStream.serialize(result).getBytes());
+    }
+}
-- 
GitLab