+ break;
+ case DELETE_CARD:
+ // TODO
+ System.err
+ .println("Unsupported command received from a client connection, closing it: "
+ + verb);
+ clientContinue = false;
+ break;
+ case LIST:
+ for (File file : dataDir.listFiles()) {
+ if (cmd.getParam() == null || cmd.getParam().length() == 0
+ || file.getName().contains(cmd.getParam())) {
+ s.send(StringUtils.fromTime(file.lastModified()) + " "
+ + file.getName());
+ }
+ }
+ s.sendBlock();
+ break;
+ case HELP:
+ // TODO: i18n
+ 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.sendBlock();
+ break;
+ default:
+ System.err
+ .println("Unsupported command received from a client connection, closing it: "
+ + verb);
+ clientContinue = false;
+ break;
+ }
+
+ return clientContinue;
+ }
+
+ /**
+ * Process a *_CONTACT subcommand.
+ *
+ * @param s
+ * the {@link SimpleSocket} to process
+ * @param card
+ * the target {@link Card}
+ *
+ * @return TRUE if the client is ready for another command, FALSE when the
+ * client is done
+ *
+ * @throws IOException
+ * in case of IO error
+ *
+ * @throw InvalidParameterException in case of invalid subcommand
+ */
+ private boolean processContactCmd(SimpleSocket s, Card card)
+ throws IOException {
+ Command cmd = s.receiveCommand();
+ Command.Verb verb = cmd.getVerb();
+
+ if (verb == null)
+ return false;
+
+ boolean clientContinue = true;
+
+ System.out.println(s + " -> " + verb);
+
+ switch (verb) {
+ case GET_CONTACT: {
+ Contact contact = card.getById(cmd.getParam());
+ if (contact != null)
+ s.sendBlock(Vcard21Parser.toStrings(contact, -1));
+ 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);
+ }
+ break;
+ }
+ case PUT_CONTACT: {
+ String uid = cmd.getParam();
+ Contact contact = card.getById(uid);
+ if (contact == null) {
+ throw new InvalidParameterException(
+ "Cannot find contact to modify for UID: " + uid);
+ }
+ while (processDataCmd(s, contact))
+ ;
+ break;
+ }
+ case DELETE_CONTACT: {
+ String uid = cmd.getParam();
+ Contact contact = card.getById(uid);
+ if (contact == null) {
+ throw new InvalidParameterException(
+ "Cannot find contact to delete for UID: " + uid);
+ }
+
+ contact.delete();
+ break;
+ }
+ case PUT_CARD: {
+ clientContinue = false;
+ break;
+ }
+ default: {
+ throw new InvalidParameterException("command invalid here");
+ }