Rename pimstuff -> jpim and move to Maven
[jpim.git] / src / main / java / de / j32 / jpim / conduit / FritzAddressbookConduit.java
diff --git a/src/main/java/de/j32/jpim/conduit/FritzAddressbookConduit.java b/src/main/java/de/j32/jpim/conduit/FritzAddressbookConduit.java
new file mode 100644 (file)
index 0000000..9a39a1c
--- /dev/null
@@ -0,0 +1,68 @@
+package de.j32.jpim.conduit;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.xml.sax.SAXException;
+
+import de.j32.avmfritz.FritzBox;
+import de.j32.util.Util;
+
+public class FritzAddressbookConduit implements Conduit
+{
+    FritzBox fb_;
+
+    public FritzAddressbookConduit() throws ConduitException
+    {
+        try {
+            fb_ = new FritzBox(Config.get("fritzbox-password"), Config.get(
+                    "fritzbox-url", "http://fritz.box"));
+        }
+        catch (SAXException e) {
+            throw new ConduitException(e);
+        }
+        catch (IOException e) {
+            throw new ConduitException(e);
+        }
+    }
+
+    @Override
+    public Exporter exporter() throws ConduitException
+    {
+        OutputStream os = null;
+        FritzAddressbookExporter exporter = null;
+        try {
+            os = fb_.importAddressbook();
+            exporter = new FritzAddressbookExporter(os);
+        }
+        catch (IOException e) {
+            throw new ConduitException(e);
+        }
+        finally {
+            if (exporter == null) Util.nothrowClose(os);
+        }
+        return exporter;
+    }
+
+    @Override
+    public Importer importer() throws ConduitException
+    {
+        InputStream is = null;
+        FritzAddressbookImporter importer = null;
+        try {
+            is = fb_.exportAddressbook();
+            importer = new FritzAddressbookImporter(is);
+        }
+        catch (IOException e) {
+            throw new ConduitException(e);
+        }
+        catch (SAXException e) {
+            throw new ConduitException(e);
+        }
+        finally {
+            if (importer == null) Util.nothrowClose(is);
+        }
+        return importer;
+    }
+}