32a389ac284d5db61e3bf1f8dff0514f82316dff
[jpim.git] / src / de / j32 / pimstuff / conduit / FritzAddressbookConduit.java
1 package de.j32.pimstuff.conduit;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6
7 import org.xml.sax.SAXException;
8
9 import de.j32.avmfritz.FritzBox;
10 import de.j32.util.Util;
11
12 public class FritzAddressbookConduit implements Conduit
13 {
14     FritzBox fb_;
15
16     public FritzAddressbookConduit() throws ConduitException
17     {
18         try {
19             fb_ = new FritzBox(Config.get("fritzbox-password"), Config.get(
20                     "fritzbox-url", "http://fritz.box"));
21         }
22         catch (SAXException e) {
23             throw new ConduitException(e);
24         }
25         catch (IOException e) {
26             throw new ConduitException(e);
27         }
28     }
29
30     @Override
31     public Exporter exporter() throws ConduitException
32     {
33         OutputStream os = null;
34         FritzAddressbookExporter exporter = null;
35         try {
36             os = fb_.importAddressbook();
37             exporter = new FritzAddressbookExporter(os);
38         }
39         catch (IOException e) {
40             throw new ConduitException(e);
41         }
42         finally {
43             if (exporter == null) Util.nothrowClose(os);
44         }
45         return exporter;
46     }
47
48     @Override
49     public Importer importer() throws ConduitException
50     {
51         InputStream is = null;
52         FritzAddressbookImporter importer = null;
53         try {
54             is = fb_.exportAddressbook();
55             importer = new FritzAddressbookImporter(is);
56         }
57         catch (IOException e) {
58             throw new ConduitException(e);
59         }
60         catch (SAXException e) {
61             throw new ConduitException(e);
62         }
63         finally {
64             if (importer == null) Util.nothrowClose(is);
65         }
66         return importer;
67     }
68 }