Add more warnings source to 1.6) and fix warnings
[jvcard.git] / src / be / nikiroo / jvcard / remote / Server.java
index 567df69c83f12b735d33a2c330ff6b4a96a657f3..6cba0c33cbb09047ed09f4004f88266e9ee4193d 100644 (file)
@@ -11,15 +11,16 @@ 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.remote.SimpleSocket.BlockAppendable;
+import be.nikiroo.jvcard.resources.RemoteBundle;
+import be.nikiroo.jvcard.resources.RemotingOption;
+import be.nikiroo.utils.StringUtils;
 
 /**
  * This class implements a small server that can listen for requests to
@@ -33,7 +34,7 @@ import be.nikiroo.jvcard.resources.StringUtils;
  * </p>
  * 
  * @author niki
- *
+ * 
  */
 public class Server implements Runnable {
        private ServerSocket ss;
@@ -58,9 +59,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();
 
@@ -130,8 +131,9 @@ public class Server implements Runnable {
                                                try {
                                                        ss.open(false);
 
-                                                       while (processCmd(ss))
-                                                               ;
+                                                       while (processCmd(ss)) {
+                                                               // nothing to do: process the command
+                                                       }
 
                                                } catch (IOException e) {
                                                        e.printStackTrace();
@@ -201,7 +203,7 @@ public class Server implements Runnable {
                        break;
                }
                case VERSION: {
-                       s.sendCommand(Command.VERSION);
+                       s.sendLine("" + SimpleSocket.CURRENT_VERSION);
                        break;
                }
                case TIME: {
@@ -236,8 +238,9 @@ public class Server implements Runnable {
                                        try {
                                                s.sendLine(StringUtils.fromTime(file.lastModified()));
 
-                                               while (processLockedCmd(s, name))
-                                                       ;
+                                               while (processLockedCmd(s, name)) {
+                                                       // nothing to do: process the command
+                                               }
                                        } catch (InvalidParameterException e) {
                                                System.err
                                                                .println("Unsupported command received from a client connection, closing it: "
@@ -259,8 +262,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 +278,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;
                }
@@ -321,7 +326,7 @@ public class Server implements Runnable {
 
                switch (command) {
                case GET_CARD: {
-                       s.sendBlock(doGetCard(name));
+                       sendCardBlock(s, name);
                        break;
                }
                case POST_CARD: {
@@ -338,8 +343,9 @@ public class Server implements Runnable {
                        } else {
                                Card card = new Card(vcf, Format.VCard21);
                                try {
-                                       while (processContactCmd(s, card))
-                                               ;
+                                       while (processContactCmd(s, card)) {
+                                               // nothing to do: process the command
+                                       }
                                        card.save();
                                        s.sendLine(StringUtils.fromTime(card.getLastModified()));
                                } catch (InvalidParameterException e) {
@@ -364,7 +370,8 @@ public class Server implements Runnable {
                        break;
                }
                default: {
-                       throw new InvalidParameterException("command invalid here");
+                       throw new InvalidParameterException("command invalid here: "
+                                       + command);
                }
                }
 
@@ -402,23 +409,26 @@ public class Server implements Runnable {
                switch (command) {
                case GET_CONTACT: {
                        Contact contact = card.getById(cmd.getParam());
-                       if (contact != null)
-                               s.sendBlock(Vcard21Parser.toStrings(contact, -1));
-                       else
+                       if (contact != null) {
+                               BlockAppendable app = s.createBlockAppendable();
+                               Vcard21Parser.write(app, contact, -1);
+                               app.close();
+                       } else {
                                s.sendBlock();
+                       }
                        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: {
@@ -428,8 +438,9 @@ public class Server implements Runnable {
                                throw new InvalidParameterException(
                                                "Cannot find contact to modify for UID: " + uid);
                        }
-                       while (processDataCmd(s, contact))
-                               ;
+                       while (processDataCmd(s, contact)) {
+                               // nothing to do: process the command
+                       }
                        break;
                }
                case DELETE_CONTACT: {
@@ -450,13 +461,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 +484,8 @@ public class Server implements Runnable {
                        break;
                }
                default: {
-                       throw new InvalidParameterException("command invalid here");
+                       throw new InvalidParameterException("command invalid here: "
+                                       + command);
                }
                }
 
@@ -505,9 +524,9 @@ public class Server implements Runnable {
                case GET_DATA: {
                        for (Data data : contact) {
                                if (data.getName().equals(cmd.getParam())) {
-                                       for (String line : Vcard21Parser.toStrings(data)) {
-                                               s.send(line);
-                                       }
+                                       BlockAppendable app = s.createBlockAppendable();
+                                       Vcard21Parser.write(app, data);
+                                       // note: we do NOT close 'app', since it would send an EOB
                                }
                        }
                        s.sendBlock();
@@ -517,7 +536,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 +552,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 +568,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 +576,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 +591,8 @@ public class Server implements Runnable {
                        break;
                }
                default: {
-                       throw new InvalidParameterException("command invalid here");
+                       throw new InvalidParameterException("command invalid here: "
+                                       + command);
                }
                }
 
@@ -585,20 +610,19 @@ public class Server implements Runnable {
         * @throws IOException
         *             in case of error
         */
-       private List<String> doGetCard(String name) throws IOException {
-               List<String> lines = new LinkedList<String>();
-
+       private void sendCardBlock(SimpleSocket s, String name) throws IOException {
                File vcf = getFile(name);
+               BlockAppendable app = s.createBlockAppendable();
 
                if (vcf != null && vcf.exists()) {
                        Card card = new Card(vcf, Format.VCard21);
 
-                       // timestamp:
-                       lines.add(StringUtils.fromTime(card.getLastModified()));
-                       lines.addAll(Vcard21Parser.toStrings(card));
+                       // timestamp + data
+                       app.append(StringUtils.fromTime(card.getLastModified()) + "\r\n");
+                       Vcard21Parser.write(app, card);
                }
 
-               return lines;
+               app.close();
        }
 
        /**