Resources system rewrite + new "--save-config DIR" option
[jvcard.git] / src / be / nikiroo / jvcard / remote / Server.java
CommitLineData
a046fa49
NR
1package be.nikiroo.jvcard.remote;
2
3import java.io.File;
4import java.io.IOException;
5import java.net.ServerSocket;
6import java.net.Socket;
7import java.net.UnknownHostException;
0b6140e4 8import java.security.InvalidParameterException;
a046fa49 9import java.util.Date;
4298276a 10import java.util.HashMap;
a046fa49
NR
11import java.util.LinkedList;
12import java.util.List;
4298276a 13import java.util.Map;
a046fa49
NR
14
15import be.nikiroo.jvcard.Card;
0b6140e4
NR
16import be.nikiroo.jvcard.Contact;
17import be.nikiroo.jvcard.Data;
a046fa49 18import be.nikiroo.jvcard.parsers.Format;
0b6140e4 19import be.nikiroo.jvcard.parsers.Vcard21Parser;
7da41ecd 20import be.nikiroo.jvcard.resources.StringUtils;
e119a1c1
NR
21import be.nikiroo.jvcard.resources.bundles.RemoteBundle;
22import be.nikiroo.jvcard.resources.enums.RemotingOption;
a046fa49
NR
23
24/**
25 * This class implements a small server that can listen for requests to
26 * synchronise, get and put {@link Card}s.
27 *
28 * <p>
29 * It is <b>NOT</b> secured in any way (it even is nice enough to give you a
30 * help message when you connect in raw mode via nc on how to use it), so do
31 * <b>NOT</b> enable such a server to be accessible from internet. This is not
32 * safe. Use a ssh/openssl tunnel or similar.
33 * </p>
34 *
35 * @author niki
36 *
37 */
38public class Server implements Runnable {
39 private ServerSocket ss;
40 private int port;
41 private boolean stop;
42 private File dataDir;
43
44 private Object clientsLock = new Object();
45 private List<SimpleSocket> clients = new LinkedList<SimpleSocket>();
46
0b6140e4 47 private Object updateLock = new Object();
4298276a 48 private Map<File, Integer> updates = new HashMap<File, Integer>();
a046fa49 49
a046fa49 50 /**
7da41ecd 51 * Create a new jVCard server on the given port.
a046fa49
NR
52 *
53 * @param port
54 * the port to run on
55 *
56 * @throws IOException
57 * in case of IO error
58 */
59 public Server(int port) throws IOException {
60 this.port = port;
e119a1c1 61 RemoteBundle bundle = new RemoteBundle();
a046fa49 62 try {
e119a1c1 63 String dir = bundle.getString(RemotingOption.SERVER_DATA_PATH);
a046fa49
NR
64 dataDir = new File(dir);
65 dataDir.mkdir();
66
67 if (!dataDir.exists()) {
68 throw new IOException("Cannot open or create data store at: "
69 + dataDir);
70 }
71 } catch (Exception e) {
72 e.printStackTrace();
73 throw new IOException("Cannot open or create data store at: "
74 + dataDir, e);
75 }
76
77 ss = new ServerSocket(port);
78 }
79
80 /**
81 * Stop the server. It may take some time before returning, but will only
82 * return when the server is actually stopped.
83 */
84 public void stop() {
85 stop = true;
86 try {
87 SimpleSocket c = new SimpleSocket(new Socket((String) null, port),
88 "special STOP client");
89 c.open(true);
845fb1d7 90 c.sendCommand(Command.STOP);
a046fa49
NR
91 c.close();
92 } catch (UnknownHostException e) {
93 e.printStackTrace();
94 } catch (IOException e) {
95 e.printStackTrace();
96 }
97
98 if (clients.size() > 0) {
99 try {
100 Thread.sleep(100);
101 } catch (InterruptedException e) {
102 }
103
104 if (clients.size() > 0) {
105 synchronized (clientsLock) {
106 for (SimpleSocket s : clients) {
107 System.err
108 .println("Forcefully closing client connection");
109 s.close();
110 }
111
112 clients.clear();
113 }
114 }
115 }
116 }
117
118 @Override
119 public void run() {
120 while (!stop) {
121 try {
122 final Socket s = ss.accept();
123 // TODO: thread pool?
124 new Thread(new Runnable() {
125 @Override
126 public void run() {
0b6140e4
NR
127 SimpleSocket ss = new SimpleSocket(s, "[request]");
128
129 addClient(ss);
130 try {
131 ss.open(false);
132
133 while (processCmd(ss))
134 ;
135
136 } catch (IOException e) {
137 e.printStackTrace();
138 } finally {
139 ss.close();
140 }
141 removeClient(ss);
a046fa49
NR
142 }
143 }).start();
144 } catch (IOException ioe) {
145 ioe.printStackTrace();
146 }
147 }
148 }
149
150 /**
151 * Add a client to the current count.
152 *
153 * @return the client index number
154 */
155 private void addClient(SimpleSocket s) {
156 synchronized (clientsLock) {
157 clients.add(s);
158 }
159 }
160
161 /**
162 * Remove a client from the current count.
163 *
164 * @param index
165 * the client index number
166 */
167 private void removeClient(SimpleSocket s) {
168 synchronized (clientsLock) {
169 clients.remove(s);
170 }
171 }
172
173 /**
4298276a 174 * Process a first-level command.
a046fa49
NR
175 *
176 * @param s
0b6140e4
NR
177 * the {@link SimpleSocket} from which to get the command to
178 * process
179 *
180 * @return TRUE if the client is ready for another command, FALSE when the
181 * client exited
182 *
183 * @throws IOException
184 * in case of IO error
a046fa49 185 */
0b6140e4 186 private boolean processCmd(SimpleSocket s) throws IOException {
845fb1d7
NR
187 CommandInstance cmd = s.receiveCommand();
188 Command command = cmd.getCommand();
0b6140e4 189
845fb1d7 190 if (command == null)
0b6140e4
NR
191 return false;
192
193 boolean clientContinue = true;
194
845fb1d7 195 System.out.println(s + " -> " + command
4298276a 196 + (cmd.getParam() == null ? "" : " " + cmd.getParam()));
0b6140e4 197
845fb1d7 198 switch (command) {
4298276a 199 case STOP: {
0b6140e4
NR
200 clientContinue = false;
201 break;
4298276a
NR
202 }
203 case VERSION: {
e4444b0b 204 s.sendLine("" + SimpleSocket.CURRENT_VERSION);
0b6140e4 205 break;
4298276a
NR
206 }
207 case TIME: {
0b6140e4
NR
208 s.sendLine(StringUtils.fromTime(new Date().getTime()));
209 break;
4298276a
NR
210 }
211 case SELECT: {
212 String name = cmd.getParam();
213 File file = new File(dataDir.getAbsolutePath() + File.separator
214 + name);
215 if (name == null || name.length() == 0 || !file.exists()) {
216 System.err
217 .println("SELECT: resource not found, closing connection: "
218 + name);
219 clientContinue = false;
220 } else {
221 synchronized (updateLock) {
222 for (File f : updates.keySet()) {
223 if (f.getCanonicalPath()
224 .equals(file.getCanonicalPath())) {
225 file = f;
226 break;
227 }
228 }
229
230 if (!updates.containsKey(file))
231 updates.put(file, 0);
232 updates.put(file, updates.get(file) + 1);
233 }
234
235 synchronized (file) {
0b6140e4 236 try {
4298276a
NR
237 s.sendLine(StringUtils.fromTime(file.lastModified()));
238
239 while (processLockedCmd(s, name))
0b6140e4 240 ;
0b6140e4
NR
241 } catch (InvalidParameterException e) {
242 System.err
243 .println("Unsupported command received from a client connection, closing it: "
845fb1d7 244 + command + " (" + e.getMessage() + ")");
0b6140e4
NR
245 clientContinue = false;
246 }
a046fa49 247 }
4298276a
NR
248
249 synchronized (updateLock) {
250 int num = updates.get(file) - 1;
251 if (num == 0) {
252 updates.remove(file);
253 } else {
254 updates.put(file, num);
255 }
256 }
a046fa49 257 }
0b6140e4 258 break;
4298276a 259 }
845fb1d7 260 case LIST_CARD: {
0b6140e4 261 for (File file : dataDir.listFiles()) {
e4444b0b
NR
262 if (cmd.getParam() == null
263 || cmd.getParam().length() == 0
264 || file.getName().toLowerCase()
265 .contains(cmd.getParam().toLowerCase())) {
0b6140e4
NR
266 s.send(StringUtils.fromTime(file.lastModified()) + " "
267 + file.getName());
268 }
269 }
270 s.sendBlock();
271 break;
4298276a
NR
272 }
273 case HELP: {
0b6140e4
NR
274 // TODO: i18n
275 s.send("The following commands are available:");
276 s.send("- TIME: get the server time");
277 s.send("- HELP: this help screen");
e4444b0b
NR
278 s.send("- LIST_CARD: list the available cards on this server");
279 s.send("- VERSION/GET_*/PUT_*/POST_*/DELETE_*/STOP: TODO");
0b6140e4
NR
280 s.sendBlock();
281 break;
4298276a
NR
282 }
283 default: {
284 System.err
285 .println("Unsupported command received from a client connection, closing it: "
845fb1d7 286 + command);
4298276a
NR
287 clientContinue = false;
288 break;
289 }
290 }
291
292 return clientContinue;
293 }
294
295 /**
296 * Process a subcommand while protected for resource <tt>name</tt>.
297 *
298 * @param s
299 * the {@link SimpleSocket} to process
300 *
301 * @param name
302 * the resource that is protected (and to target)
303 *
304 * @return TRUE if the client is ready for another command, FALSE when the
305 * client is done
306 *
307 * @throws IOException
308 * in case of IO error
309 *
310 * @throw InvalidParameterException in case of invalid subcommand
311 */
312 private boolean processLockedCmd(SimpleSocket s, String name)
313 throws IOException {
845fb1d7
NR
314 CommandInstance cmd = s.receiveCommand();
315 Command command = cmd.getCommand();
4298276a 316
845fb1d7 317 if (command == null)
4298276a
NR
318 return false;
319
320 boolean clientContinue = true;
321
845fb1d7 322 System.out.println(s + " -> " + command);
4298276a 323
845fb1d7 324 switch (command) {
4298276a
NR
325 case GET_CARD: {
326 s.sendBlock(doGetCard(name));
327 break;
328 }
329 case POST_CARD: {
330 s.sendLine(doPostCard(name, s.receiveBlock()));
331 break;
332 }
333 case PUT_CARD: {
334 File vcf = getFile(name);
335 if (vcf == null) {
336 System.err
337 .println("Fail to update a card, file not available: "
338 + name);
339 clientContinue = false;
340 } else {
341 Card card = new Card(vcf, Format.VCard21);
342 try {
343 while (processContactCmd(s, card))
344 ;
345 card.save();
845fb1d7 346 s.sendLine(StringUtils.fromTime(card.getLastModified()));
4298276a
NR
347 } catch (InvalidParameterException e) {
348 System.err
349 .println("Unsupported command received from a client connection, closing it: "
845fb1d7 350 + command + " (" + e.getMessage() + ")");
4298276a
NR
351 clientContinue = false;
352 }
353 }
354 break;
355 }
356 case DELETE_CARD: {
357 // TODO
0b6140e4
NR
358 System.err
359 .println("Unsupported command received from a client connection, closing it: "
845fb1d7 360 + command);
0b6140e4
NR
361 clientContinue = false;
362 break;
363 }
4298276a
NR
364 case SELECT: {
365 clientContinue = false;
366 break;
367 }
368 default: {
e4444b0b
NR
369 throw new InvalidParameterException("command invalid here: "
370 + command);
4298276a
NR
371 }
372 }
0b6140e4
NR
373
374 return clientContinue;
375 }
376
377 /**
378 * Process a *_CONTACT subcommand.
379 *
380 * @param s
381 * the {@link SimpleSocket} to process
382 * @param card
383 * the target {@link Card}
384 *
385 * @return TRUE if the client is ready for another command, FALSE when the
386 * client is done
387 *
388 * @throws IOException
389 * in case of IO error
390 *
391 * @throw InvalidParameterException in case of invalid subcommand
392 */
393 private boolean processContactCmd(SimpleSocket s, Card card)
394 throws IOException {
845fb1d7
NR
395 CommandInstance cmd = s.receiveCommand();
396 Command command = cmd.getCommand();
0b6140e4 397
845fb1d7 398 if (command == null)
0b6140e4
NR
399 return false;
400
401 boolean clientContinue = true;
402
845fb1d7 403 System.out.println(s + " -> " + command);
0b6140e4 404
845fb1d7 405 switch (command) {
0b6140e4
NR
406 case GET_CONTACT: {
407 Contact contact = card.getById(cmd.getParam());
408 if (contact != null)
409 s.sendBlock(Vcard21Parser.toStrings(contact, -1));
410 else
411 s.sendBlock();
412 break;
413 }
414 case POST_CONTACT: {
0b6140e4
NR
415 List<Contact> list = Vcard21Parser.parseContact(s.receiveBlock());
416 if (list.size() > 0) {
e4444b0b
NR
417 Contact newContact = list.get(0);
418 String uid = newContact.getPreferredDataValue("UID");
419 Contact oldContact = card.getById(uid);
420 if (oldContact != null)
421 oldContact.delete();
422 card.add(newContact);
0b6140e4 423 }
e4444b0b 424
0b6140e4
NR
425 break;
426 }
427 case PUT_CONTACT: {
428 String uid = cmd.getParam();
429 Contact contact = card.getById(uid);
430 if (contact == null) {
431 throw new InvalidParameterException(
432 "Cannot find contact to modify for UID: " + uid);
433 }
434 while (processDataCmd(s, contact))
435 ;
436 break;
437 }
438 case DELETE_CONTACT: {
439 String uid = cmd.getParam();
440 Contact contact = card.getById(uid);
441 if (contact == null) {
442 throw new InvalidParameterException(
443 "Cannot find contact to delete for UID: " + uid);
444 }
445
446 contact.delete();
447 break;
448 }
845fb1d7
NR
449 case HASH_CONTACT: {
450 String uid = cmd.getParam();
451 Contact contact = card.getById(uid);
452
453 if (contact == null) {
454 s.sendBlock();
455 } else {
5ad0e17e 456 s.sendLine(contact.getContentState(true));
845fb1d7
NR
457 }
458 break;
459 }
460 case LIST_CONTACT: {
461 for (Contact contact : card) {
e4444b0b
NR
462 if (cmd.getParam() == null
463 || cmd.getParam().length() == 0
464 || (contact.getPreferredDataValue("FN") + contact
465 .getPreferredDataValue("N")).toLowerCase()
466 .contains(cmd.getParam().toLowerCase())) {
5ad0e17e
NR
467 s.send(contact.getContentState(true) + " "
468 + contact.getId());
e4444b0b 469 }
845fb1d7
NR
470 }
471 s.sendBlock();
472 break;
473 }
0b6140e4
NR
474 case PUT_CARD: {
475 clientContinue = false;
476 break;
477 }
478 default: {
e4444b0b
NR
479 throw new InvalidParameterException("command invalid here: "
480 + command);
0b6140e4 481 }
a046fa49
NR
482 }
483
0b6140e4
NR
484 return clientContinue;
485 }
486
487 /**
488 * Process a *_DATA subcommand.
489 *
490 * @param s
491 * the {@link SimpleSocket} to process
492 * @param card
493 * the target {@link Contact}
494 *
495 * @return TRUE if the client is ready for another command, FALSE when the
496 * client is done
497 *
498 * @throws IOException
499 * in case of IO error
500 *
501 * @throw InvalidParameterException in case of invalid subcommand
502 */
503 private boolean processDataCmd(SimpleSocket s, Contact contact)
504 throws IOException {
845fb1d7
NR
505 CommandInstance cmd = s.receiveCommand();
506 Command command = cmd.getCommand();
0b6140e4 507
845fb1d7 508 if (command == null)
0b6140e4
NR
509 return false;
510
511 boolean clientContinue = true;
512
845fb1d7 513 System.out.println(s + " -> " + command);
0b6140e4 514
845fb1d7 515 switch (command) {
0b6140e4 516 case GET_DATA: {
845fb1d7
NR
517 for (Data data : contact) {
518 if (data.getName().equals(cmd.getParam())) {
519 for (String line : Vcard21Parser.toStrings(data)) {
520 s.send(line);
521 }
522 }
523 }
524 s.sendBlock();
0b6140e4
NR
525 break;
526 }
527 case POST_DATA: {
528 String cstate = cmd.getParam();
529 Data data = null;
530 for (Data d : contact) {
5ad0e17e 531 if (cstate.equals(d.getContentState(true)))
0b6140e4
NR
532 data = d;
533 }
534
535 if (data != null)
536 data.delete();
537 List<Data> list = Vcard21Parser.parseData(s.receiveBlock());
538 if (list.size() > 0) {
539 contact.add(list.get(0));
540 }
541 break;
542 }
543 case DELETE_DATA: {
544 String cstate = cmd.getParam();
545 Data data = null;
546 for (Data d : contact) {
5ad0e17e 547 if (cstate.equals(d.getContentState(true)))
0b6140e4
NR
548 data = d;
549 }
550
551 if (data == null) {
552 throw new InvalidParameterException(
553 "Cannot find data to delete for content state: "
554 + cstate);
555 }
556
557 contact.delete();
558 break;
559 }
845fb1d7
NR
560 case HASH_DATA: {
561 for (Data data : contact) {
562 if (data.getId().equals(cmd.getParam())) {
5ad0e17e 563 s.send(data.getContentState(true));
845fb1d7
NR
564 }
565 }
566 s.sendBlock();
567 break;
568 }
569 case LIST_DATA: {
570 for (Data data : contact) {
e4444b0b
NR
571 if (cmd.getParam() == null
572 || cmd.getParam().length() == 0
573 || data.getName().toLowerCase()
574 .contains(cmd.getParam().toLowerCase())) {
5ad0e17e 575 s.send(data.getContentState(true) + " " + data.getName());
e4444b0b 576 }
845fb1d7
NR
577 }
578 s.sendBlock();
579 break;
580 }
0b6140e4
NR
581 case PUT_CONTACT: {
582 clientContinue = false;
583 break;
584 }
585 default: {
e4444b0b
NR
586 throw new InvalidParameterException("command invalid here: "
587 + command);
0b6140e4
NR
588 }
589 }
590
591 return clientContinue;
a046fa49
NR
592 }
593
594 /**
595 * Return the serialised {@link Card} (with timestamp).
596 *
597 * @param name
598 * the resource name to load
599 *
600 * @return the serialised data
601 *
602 * @throws IOException
603 * in case of error
604 */
605 private List<String> doGetCard(String name) throws IOException {
606 List<String> lines = new LinkedList<String>();
607
0b6140e4 608 File vcf = getFile(name);
a046fa49 609
0b6140e4
NR
610 if (vcf != null && vcf.exists()) {
611 Card card = new Card(vcf, Format.VCard21);
a046fa49 612
e4444b0b 613 // timestamp + data
0b6140e4
NR
614 lines.add(StringUtils.fromTime(card.getLastModified()));
615 lines.addAll(Vcard21Parser.toStrings(card));
a046fa49
NR
616 }
617
618 return lines;
619 }
620
621 /**
622 * Save the data to the new given resource.
623 *
624 * @param name
625 * the resource name to save
626 * @param data
627 * the data to save
628 *
cf77cb35
NR
629 * @return the date of last modification
630 *
a046fa49
NR
631 * @throws IOException
632 * in case of error
633 */
cf77cb35
NR
634 private String doPostCard(String name, List<String> data)
635 throws IOException {
a046fa49 636
0b6140e4
NR
637 File vcf = getFile(name);
638
639 if (vcf != null) {
640 Card card = new Card(Vcard21Parser.parseContact(data));
a046fa49 641 card.saveAs(vcf, Format.VCard21);
cf77cb35
NR
642
643 return StringUtils.fromTime(vcf.lastModified());
a046fa49 644 }
cf77cb35
NR
645
646 return "";
a046fa49 647 }
0b6140e4
NR
648
649 /**
650 * Return the {@link File} corresponding to the given resource name.
651 *
652 * @param name
653 * the resource name
654 *
655 * @return the corresponding {@link File} or NULL if the name was NULL or
656 * empty
657 */
658 private File getFile(String name) {
659 if (name != null && name.length() > 0) {
660 return new File(dataDir.getAbsolutePath() + File.separator + name);
661 }
662
663 return null;
664 }
a046fa49 665}