Resources system rewrite + new "--save-config DIR" option
[jvcard.git] / src / be / nikiroo / jvcard / remote / Server.java
index 567df69c83f12b735d33a2c330ff6b4a96a657f3..962f2d00133069eee26205f97e23205373b86e44 100644 (file)
@@ -11,15 +11,15 @@ import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.ResourceBundle;
 
 import be.nikiroo.jvcard.Card;
 import be.nikiroo.jvcard.Contact;
 import be.nikiroo.jvcard.Data;
 import be.nikiroo.jvcard.parsers.Format;
 import be.nikiroo.jvcard.parsers.Vcard21Parser;
-import be.nikiroo.jvcard.resources.Bundles;
 import be.nikiroo.jvcard.resources.StringUtils;
+import be.nikiroo.jvcard.resources.bundles.RemoteBundle;
+import be.nikiroo.jvcard.resources.enums.RemotingOption;
 
 /**
  * This class implements a small server that can listen for requests to
@@ -58,9 +58,9 @@ public class Server implements Runnable {
         */
        public Server(int port) throws IOException {
                this.port = port;
-               ResourceBundle bundle = Bundles.getBundle("remote");
+               RemoteBundle bundle = new RemoteBundle();
                try {
-                       String dir = bundle.getString("SERVER_DATA_PATH");
+                       String dir = bundle.getString(RemotingOption.SERVER_DATA_PATH);
                        dataDir = new File(dir);
                        dataDir.mkdir();
 
@@ -201,7 +201,7 @@ public class Server implements Runnable {
                        break;
                }
                case VERSION: {
-                       s.sendCommand(Command.VERSION);
+                       s.sendLine("" + SimpleSocket.CURRENT_VERSION);
                        break;
                }
                case TIME: {
@@ -259,8 +259,10 @@ public class Server implements Runnable {
                }
                case LIST_CARD: {
                        for (File file : dataDir.listFiles()) {
-                               if (cmd.getParam() == null || cmd.getParam().length() == 0
-                                               || file.getName().contains(cmd.getParam())) {
+                               if (cmd.getParam() == null
+                                               || cmd.getParam().length() == 0
+                                               || file.getName().toLowerCase()
+                                                               .contains(cmd.getParam().toLowerCase())) {
                                        s.send(StringUtils.fromTime(file.lastModified()) + " "
                                                        + file.getName());
                                }
@@ -273,8 +275,8 @@ public class Server implements Runnable {
                        s.send("The following commands are available:");
                        s.send("- TIME: get the server time");
                        s.send("- HELP: this help screen");
-                       s.send("- LIST: list the available cards on this server");
-                       s.send("- VERSION/GET/PUT/POST/DELETE/STOP: TODO");
+                       s.send("- LIST_CARD: list the available cards on this server");
+                       s.send("- VERSION/GET_*/PUT_*/POST_*/DELETE_*/STOP: TODO");
                        s.sendBlock();
                        break;
                }
@@ -364,7 +366,8 @@ public class Server implements Runnable {
                        break;
                }
                default: {
-                       throw new InvalidParameterException("command invalid here");
+                       throw new InvalidParameterException("command invalid here: "
+                                       + command);
                }
                }
 
@@ -409,16 +412,16 @@ public class Server implements Runnable {
                        break;
                }
                case POST_CONTACT: {
-                       String uid = cmd.getParam();
-                       Contact contact = card.getById(uid);
-                       if (contact != null)
-                               contact.delete();
                        List<Contact> list = Vcard21Parser.parseContact(s.receiveBlock());
                        if (list.size() > 0) {
-                               contact = list.get(0);
-                               contact.getPreferredData("UID").setValue(uid);
-                               card.add(contact);
+                               Contact newContact = list.get(0);
+                               String uid = newContact.getPreferredDataValue("UID");
+                               Contact oldContact = card.getById(uid);
+                               if (oldContact != null)
+                                       oldContact.delete();
+                               card.add(newContact);
                        }
+
                        break;
                }
                case PUT_CONTACT: {
@@ -450,13 +453,20 @@ public class Server implements Runnable {
                        if (contact == null) {
                                s.sendBlock();
                        } else {
-                               s.sendLine(contact.getContentState());
+                               s.sendLine(contact.getContentState(true));
                        }
                        break;
                }
                case LIST_CONTACT: {
                        for (Contact contact : card) {
-                               s.send(contact.getContentState() + " " + contact.getId());
+                               if (cmd.getParam() == null
+                                               || cmd.getParam().length() == 0
+                                               || (contact.getPreferredDataValue("FN") + contact
+                                                               .getPreferredDataValue("N")).toLowerCase()
+                                                               .contains(cmd.getParam().toLowerCase())) {
+                                       s.send(contact.getContentState(true) + " "
+                                                       + contact.getId());
+                               }
                        }
                        s.sendBlock();
                        break;
@@ -466,7 +476,8 @@ public class Server implements Runnable {
                        break;
                }
                default: {
-                       throw new InvalidParameterException("command invalid here");
+                       throw new InvalidParameterException("command invalid here: "
+                                       + command);
                }
                }
 
@@ -517,7 +528,7 @@ public class Server implements Runnable {
                        String cstate = cmd.getParam();
                        Data data = null;
                        for (Data d : contact) {
-                               if (cstate.equals(d.getContentState()))
+                               if (cstate.equals(d.getContentState(true)))
                                        data = d;
                        }
 
@@ -533,7 +544,7 @@ public class Server implements Runnable {
                        String cstate = cmd.getParam();
                        Data data = null;
                        for (Data d : contact) {
-                               if (cstate.equals(d.getContentState()))
+                               if (cstate.equals(d.getContentState(true)))
                                        data = d;
                        }
 
@@ -549,7 +560,7 @@ public class Server implements Runnable {
                case HASH_DATA: {
                        for (Data data : contact) {
                                if (data.getId().equals(cmd.getParam())) {
-                                       s.send(data.getContentState());
+                                       s.send(data.getContentState(true));
                                }
                        }
                        s.sendBlock();
@@ -557,7 +568,12 @@ public class Server implements Runnable {
                }
                case LIST_DATA: {
                        for (Data data : contact) {
-                               s.send(data.getContentState() + " " + data.getName());
+                               if (cmd.getParam() == null
+                                               || cmd.getParam().length() == 0
+                                               || data.getName().toLowerCase()
+                                                               .contains(cmd.getParam().toLowerCase())) {
+                                       s.send(data.getContentState(true) + " " + data.getName());
+                               }
                        }
                        s.sendBlock();
                        break;
@@ -567,7 +583,8 @@ public class Server implements Runnable {
                        break;
                }
                default: {
-                       throw new InvalidParameterException("command invalid here");
+                       throw new InvalidParameterException("command invalid here: "
+                                       + command);
                }
                }
 
@@ -593,7 +610,7 @@ public class Server implements Runnable {
                if (vcf != null && vcf.exists()) {
                        Card card = new Card(vcf, Format.VCard21);
 
-                       // timestamp:
+                       // timestamp + data
                        lines.add(StringUtils.fromTime(card.getLastModified()));
                        lines.addAll(Vcard21Parser.toStrings(card));
                }