1 package be
.nikiroo
.jvcard
.remote
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.BufferedWriter
;
6 import java
.io
.FileInputStream
;
7 import java
.io
.FileNotFoundException
;
8 import java
.io
.FileOutputStream
;
9 import java
.io
.IOException
;
10 import java
.io
.InputStreamReader
;
11 import java
.io
.OutputStreamWriter
;
12 import java
.net
.Socket
;
13 import java
.net
.UnknownHostException
;
14 import java
.security
.InvalidParameterException
;
15 import java
.util
.HashMap
;
16 import java
.util
.LinkedList
;
17 import java
.util
.List
;
19 import java
.util
.MissingResourceException
;
20 import java
.util
.ResourceBundle
;
22 import be
.nikiroo
.jvcard
.Card
;
23 import be
.nikiroo
.jvcard
.Contact
;
24 import be
.nikiroo
.jvcard
.Data
;
25 import be
.nikiroo
.jvcard
.launcher
.CardResult
;
26 import be
.nikiroo
.jvcard
.launcher
.CardResult
.MergeCallback
;
27 import be
.nikiroo
.jvcard
.parsers
.Format
;
28 import be
.nikiroo
.jvcard
.parsers
.Vcard21Parser
;
29 import be
.nikiroo
.jvcard
.resources
.Bundles
;
30 import be
.nikiroo
.jvcard
.resources
.StringUtils
;
33 * This class will synchronise {@link Card}s between a local instance an a
34 * remote jVCard server.
40 /** The time in ms after which we declare that 2 timestamps are different */
41 static private final int GRACE_TIME
= 2001;
43 /** Directory where to store local cache of remote {@link Card}s. */
44 static private File cacheDir
;
47 * Directory where to store cache of remote {@link Card}s without
48 * modifications since the last synchronisation.
50 static private File cacheDirOrig
;
51 /** Directory where to store timestamps for files in cacheDirOrig */
52 static private File cacheDirOrigTS
;
54 static private boolean autoSync
;
58 /** Resource name on the remote server. */
62 * Create a new {@link Sync} object, ready to operate for the given resource
63 * on the given server.
66 * Note that the format used is the standard "host:port_number/file", with
67 * an optional <tt>jvcard://</tt> prefix.
71 * E.g.: <tt>jvcard://localhost:4444/family.vcf</tt>
75 * the server and port to contact, optionally prefixed with
78 * @throws InvalidParameterException
79 * if the remote configuration file <tt>remote.properties</tt>
80 * cannot be accessed or if the cache directory cannot be used
82 public Sync(String url
) {
83 if (cacheDir
== null) {
88 url
= url
.replace("jvcard://", "");
89 int indexSl
= url
.indexOf('/');
90 this.name
= url
.substring(indexSl
+ 1);
91 url
= url
.substring(0, indexSl
);
92 this.host
= url
.split("\\:")[0];
93 this.port
= Integer
.parseInt(url
.split("\\:")[1]);
94 } catch (Exception e
) {
95 throw new InvalidParameterException(
96 "the given parameter was not a valid HOST:PORT value: "
102 * Create a new {@link Sync} object, ready to operate on the given server.
106 * the server to contact
110 * the resource name to synchronise to
112 public Sync(String host
, int port
, String name
) {
119 * Check if the remote server already know about this resource.
121 * @return TRUE if it is possible to contact the remote server and that this
122 * server has the resource available
124 public boolean isAvailable() {
126 SimpleSocket s
= new SimpleSocket(new Socket(host
, port
),
127 "check avail client");
129 s
.sendCommand(Command
.LIST_CARD
);
130 List
<String
> timestampedFiles
= s
.receiveBlock();
133 for (String timestampedFile
: timestampedFiles
) {
134 String file
= timestampedFile
.substring(StringUtils
.fromTime(0)
136 if (file
.equals(name
)) {
140 } catch (Exception e
) {
147 * Synchronise the current resource if needed, then return the locally
148 * cached version of said resource.
151 * A synchronisation is deemed necessary if one of the following is true:
153 * <li><tt>force</tt> is TRUE</li>
154 * <li><tt>CLIENT_AUTO_SYNC</tt> is TRUE in the configuration file</li>
155 * <li>the {@link Card} exists locally but not on the remote server</li>
156 * <li>the {@link Card} exists on the remote server but not locally</li>
161 * force the synchronisation to occur
163 * the {@link MergeCallback} to call in case of conflict
165 * @return the synchronised (or not) {@link Card}
167 * @throws UnknownHostException
168 * in case of server name resolution failure
169 * @throws IOException
170 * in case of IO error
172 public CardResult
sync(boolean force
, MergeCallback callback
)
173 throws UnknownHostException
, IOException
{
174 long tsOriginal
= getLastModified();
176 Card local
= new Card(getCache(cacheDir
), Format
.VCard21
);
178 // do NOT update unless we are in autoSync or forced mode or we don't
179 // have the file on cache
180 if (!autoSync
&& !force
&& tsOriginal
!= -1) {
181 return new CardResult(local
, true, false, false);
184 SimpleSocket s
= new SimpleSocket(new Socket(host
, port
), "sync client");
186 // get the server time stamp
188 boolean serverChanges
= false;
191 s
.sendCommand(Command
.LIST_CARD
);
192 List
<String
> timestampedFiles
= s
.receiveBlock();
194 for (String timestampedFile
: timestampedFiles
) {
195 String file
= timestampedFile
.substring(StringUtils
.fromTime(0)
197 if (file
.equals(name
)) {
198 tsServer
= StringUtils
.toTime(timestampedFile
.substring(0,
199 StringUtils
.fromTime(0).length()));
205 // - file not present neither in cache nor on server
206 // - remote < previous
207 if ((tsServer
== -1 && tsOriginal
== -1)
208 || (tsServer
!= -1 && tsOriginal
!= -1 && ((tsOriginal
- tsServer
) > GRACE_TIME
))) {
209 throw new IOException(
210 "The timestamps between server and client are invalid");
214 serverChanges
= (tsServer
- tsOriginal
) > GRACE_TIME
;
215 boolean localChanges
= false;
216 Card original
= null;
217 if (tsOriginal
!= -1) {
218 original
= new Card(getCache(cacheDirOrig
), Format
.VCard21
);
219 localChanges
= !local
.isEquals(original
, true);
222 Command action
= null;
224 // Sync to server if:
226 action
= Command
.PUT_CARD
;
229 // Sync from server if:
231 action
= Command
.HASH_CONTACT
;
234 // Sync from/to server if
235 if (serverChanges
&& localChanges
) {
236 action
= Command
.HELP
;
239 // POST the whole file if:
240 if (tsServer
== -1) {
241 action
= Command
.POST_CARD
;
244 // GET the whole file if:
245 if (tsOriginal
== -1) {
246 action
= Command
.GET_CARD
;
249 System
.err
.println("remote: " + (tsServer
/ 1000) % 1000 + " ("
251 System
.err
.println("previous: " + (tsOriginal
/ 1000) % 1000 + " ("
253 System
.err
.println("local changes: " + localChanges
);
254 System
.err
.println("server changes: " + serverChanges
);
255 System
.err
.println("choosen action: " + action
);
257 if (action
!= null) {
258 s
.sendCommand(Command
.SELECT
, name
);
259 if (tsServer
!= StringUtils
.toTime(s
.receiveLine())) {
260 System
.err
.println("DEBUG: it changed. retry.");
261 s
.sendCommand(Command
.SELECT
);
263 return sync(force
, callback
);
268 s
.sendCommand(Command
.GET_CARD
);
269 List
<String
> data
= s
.receiveBlock();
270 setLastModified(data
.remove(0));
271 local
.replaceListContent(Vcard21Parser
.parseContact(data
));
275 local
.saveAs(getCache(cacheDirOrig
), Format
.VCard21
);
279 s
.sendCommand(Command
.POST_CARD
);
280 s
.sendBlock(Vcard21Parser
.toStrings(local
));
281 local
.saveAs(getCache(cacheDirOrig
), Format
.VCard21
);
282 setLastModified(s
.receiveLine());
286 String serverLastModifTime
= updateToServer(s
, original
,
289 local
.saveAs(getCache(cacheDirOrig
), Format
.VCard21
);
291 setLastModified(serverLastModifTime
);
295 String serverLastModifTime
= updateFromServer(s
, local
);
298 local
.saveAs(getCache(cacheDirOrig
), Format
.VCard21
);
300 setLastModified(serverLastModifTime
);
304 // note: we are holding the server here, so it could throw
305 // us away if we take too long
307 // TODO: check if those files are deleted
308 File mergeF
= File
.createTempFile("contact-merge", ".vcf");
310 .createTempFile("contact-server", ".vcf");
311 original
.saveAs(serverF
, Format
.VCard21
);
313 Card server
= new Card(serverF
, Format
.VCard21
);
314 updateFromServer(s
, server
);
317 server
.saveAs(mergeF
, Format
.VCard21
);
318 Card merge
= new Card(mergeF
, Format
.VCard21
);
319 List
<Contact
> added
= new LinkedList
<Contact
>();
320 List
<Contact
> removed
= new LinkedList
<Contact
>();
321 original
.compare(local
, added
, removed
, removed
, added
);
322 for (Contact c
: removed
)
323 merge
.getById(c
.getId()).delete();
324 for (Contact c
: added
)
325 merge
.add(Vcard21Parser
.clone(c
));
330 if (callback
== null) {
331 throw new IOException(
332 "Conflicting changes detected and merge operation not allowed");
335 merge
= callback
.merge(original
, local
, server
, merge
);
337 throw new IOException(
338 "Conflicting changes detected and merge operation cancelled");
341 // TODO: something like:
342 // String serverLastModifTime = updateToServer(s, original,
344 // ...but without starting with original since it is not
346 s
.sendCommand(Command
.POST_CARD
);
347 s
.sendBlock(Vcard21Parser
.toStrings(merge
));
348 String serverLastModifTime
= s
.receiveLine();
351 merge
.saveAs(getCache(cacheDir
), Format
.VCard21
);
352 merge
.saveAs(getCache(cacheDirOrig
), Format
.VCard21
);
354 setLastModified(serverLastModifTime
);
365 s
.sendCommand(Command
.SELECT
);
367 } catch (IOException e
) {
368 return new CardResult(e
);
369 } catch (Exception e
) {
370 return new CardResult(new IOException(e
));
375 return new CardResult(local
, true, true, serverChanges
);
379 * Will update the currently selected {@link Card} on the remote server to
380 * be in the same state as <tt>local</tt>, assuming the server is currently
381 * in <tt>original</tt> state.
384 * the {@link SimpleSocket} to work on, which <b>MUST</b> be in
387 * the original {@link Card} as it was before the client made
390 * the {@link Card} to which state we want the server in
392 * @return the last modified time from the remote server (which is basically
395 * @throws IOException
396 * in case of IO error
398 private String
updateToServer(SimpleSocket s
, Card original
, Card local
)
400 List
<Contact
> added
= new LinkedList
<Contact
>();
401 List
<Contact
> removed
= new LinkedList
<Contact
>();
402 List
<Contact
> from
= new LinkedList
<Contact
>();
403 List
<Contact
> to
= new LinkedList
<Contact
>();
404 original
.compare(local
, added
, removed
, from
, to
);
406 s
.sendCommand(Command
.PUT_CARD
);
408 for (Contact c
: removed
) {
409 s
.sendCommand(Command
.DELETE_CONTACT
, c
.getId());
411 for (Contact c
: added
) {
412 s
.sendCommand(Command
.POST_CONTACT
, c
.getId());
413 s
.sendBlock(Vcard21Parser
.toStrings(c
, -1));
415 if (from
.size() > 0) {
416 for (int index
= 0; index
< from
.size(); index
++) {
417 Contact f
= from
.get(index
);
418 Contact t
= to
.get(index
);
420 List
<Data
> subadded
= new LinkedList
<Data
>();
421 List
<Data
> subremoved
= new LinkedList
<Data
>();
422 f
.compare(t
, subadded
, subremoved
, subremoved
, subadded
);
423 s
.sendCommand(Command
.PUT_CONTACT
, f
.getId());
424 for (Data d
: subremoved
) {
425 s
.sendCommand(Command
.DELETE_DATA
, d
.getContentState(true));
427 for (Data d
: subadded
) {
428 s
.sendCommand(Command
.POST_DATA
, d
.getContentState(true));
429 s
.sendBlock(Vcard21Parser
.toStrings(d
));
431 s
.sendCommand(Command
.PUT_CONTACT
);
435 s
.sendCommand(Command
.PUT_CARD
);
437 return s
.receiveLine();
441 * Will update the given {@link Card} object (not {@link File}) to the
442 * currently selected {@link Card} on the remote server.
445 * the {@link SimpleSocket} to work on, which <b>MUST</b> be in
448 * the {@link Card} to update
450 * @return the last modified time from the remote server
452 * @throws IOException
453 * in case of IO error
455 private String
updateFromServer(SimpleSocket s
, Card local
)
457 s
.sendCommand(Command
.PUT_CARD
);
459 s
.sendCommand(Command
.LIST_CONTACT
);
460 Map
<String
, String
> remote
= new HashMap
<String
, String
>();
461 for (String line
: s
.receiveBlock()) {
462 int indexSp
= line
.indexOf(" ");
463 String hash
= line
.substring(0, indexSp
);
464 String uid
= line
.substring(indexSp
+ 1);
466 remote
.put(uid
, hash
);
469 List
<Contact
> deleted
= new LinkedList
<Contact
>();
470 List
<Contact
> changed
= new LinkedList
<Contact
>();
471 List
<String
> added
= new LinkedList
<String
>();
473 for (Contact c
: local
) {
474 String hash
= remote
.get(c
.getId());
477 } else if (!hash
.equals(c
.getContentState(true))) {
482 for (String uid
: remote
.keySet()) {
483 if (local
.getById(uid
) == null)
489 for (Contact c
: deleted
) {
493 for (String uid
: added
) {
494 s
.sendCommand(Command
.GET_CONTACT
, uid
);
495 for (Contact cc
: Vcard21Parser
.parseContact(s
.receiveBlock())) {
500 for (Contact c
: changed
) {
502 s
.sendCommand(Command
.GET_CONTACT
, c
.getId());
503 for (Contact cc
: Vcard21Parser
.parseContact(s
.receiveBlock())) {
508 s
.sendCommand(Command
.PUT_CARD
);
510 return s
.receiveLine();
514 * Return the requested cache for the current resource.
519 * @return the cached {@link File}
521 private File
getCache(File dir
) {
522 return new File(dir
.getPath() + File
.separator
+ name
);
526 * Return the cached {@link File} corresponding to the current resource.
528 * @return the cached {@link File}
530 public File
getCache() {
531 return new File(cacheDir
.getPath() + File
.separator
+ name
);
535 * Get the last modified date of the current resource's original cached
536 * file, that is, the time the server reported as the "last modified time"
537 * when this resource was transfered.
539 * @return the last modified time from the server back when this resource
542 public long getLastModified() {
544 BufferedReader in
= new BufferedReader(new InputStreamReader(
545 new FileInputStream(cacheDirOrigTS
.getPath()
546 + File
.separator
+ name
)));
547 String line
= in
.readLine();
550 return StringUtils
.toTime(line
);
551 } catch (FileNotFoundException e
) {
553 } catch (Exception e
) {
559 * Set the last modified date of the current resource's original cached
560 * file, that is, the time the server reported as the "last modified time"
561 * when this resource was transfered.
564 * the last modified time from the server back when this resource
567 public void setLastModified(String time
) {
569 BufferedWriter out
= new BufferedWriter(new OutputStreamWriter(
570 new FileOutputStream(cacheDirOrigTS
.getPath()
571 + File
.separator
+ name
)));
575 } catch (FileNotFoundException e
) {
577 } catch (IOException e
) {
583 * Configure the synchronisation mechanism (cache, auto update...).
585 * @throws InvalidParameterException
586 * if the remote configuration file <tt>remote.properties</tt>
587 * cannot be accessed or if the cache directory cannot be used
589 static private void config() {
591 ResourceBundle bundle
= Bundles
.getBundle("remote");
594 dir
= bundle
.getString("CLIENT_CACHE_DIR").trim();
596 cacheDir
= new File(dir
+ File
.separator
+ "current");
598 cacheDirOrig
= new File(dir
+ File
.separator
+ "original");
599 cacheDirOrig
.mkdir();
600 cacheDirOrigTS
= new File(dir
+ File
.separator
+ "timestamps");
601 cacheDirOrigTS
.mkdir();
603 if (!cacheDir
.exists() || !cacheDirOrig
.exists()) {
604 throw new IOException("Cannot open or create cache store at: "
608 String autoStr
= bundle
.getString("CLIENT_AUTO_SYNC");
609 if (autoStr
!= null && autoStr
.trim().equalsIgnoreCase("true")) {
613 } catch (MissingResourceException e
) {
614 throw new InvalidParameterException(
615 "Cannot access remote.properties configuration file");
616 } catch (Exception e
) {
617 throw new InvalidParameterException(
618 "Cannot open or create cache store at: " + dir
);