X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Fremote%2FServer.java;h=30c404ca223cabcab710b13671ae99fa32cdf0f5;hb=f06c81000632cfb5f525ca458f719338f55f9f66;hp=60f323ad15df89fd148cf06a0cce8fab125cf2aa;hpb=5ad0e17e7fea1c602cb2638a006424af9c7e33e8;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/remote/Server.java b/src/be/nikiroo/jvcard/remote/Server.java index 60f323a..30c404c 100644 --- a/src/be/nikiroo/jvcard/remote/Server.java +++ b/src/be/nikiroo/jvcard/remote/Server.java @@ -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; *

* * @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(); @@ -323,7 +324,7 @@ public class Server implements Runnable { switch (command) { case GET_CARD: { - s.sendBlock(doGetCard(name)); + sendCardBlock(s, name); break; } case POST_CARD: { @@ -405,10 +406,13 @@ 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: { @@ -516,9 +520,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(); @@ -602,20 +606,19 @@ public class Server implements Runnable { * @throws IOException * in case of error */ - private List doGetCard(String name) throws IOException { - List lines = new LinkedList(); - + 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 + data - lines.add(StringUtils.fromTime(card.getLastModified())); - lines.addAll(Vcard21Parser.toStrings(card)); + app.append(StringUtils.fromTime(card.getLastModified()) + "\r\n"); + Vcard21Parser.write(app, card); } - return lines; + app.close(); } /**