--- /dev/null
+package de.j32.avmfritz;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.xml.sax.SAXException;
+
+import de.j32.httplib.HttpGETRequest;
+import de.j32.httplib.HttpPOSTRequest;
+import de.j32.httplib.HttpRequest;
+import de.j32.httplib.HttpResponse;
+import de.j32.util.Util;
+
+public class FritzBox
+{
+ String url_;
+ String sid_;
+
+ public FritzBox(String password, String url) throws SAXException,
+ IOException
+ {
+ url_ = url;
+
+ HttpResponse response = null;
+ try {
+ response = httpGet("cgi-bin/webcm").addParameter("getpage",
+ "../html/login_sid.xml").execute();
+ LoginXML loginxml = new LoginXML(response);
+ response.close();
+ response = null;
+
+ if (loginxml.iswriteaccess()) {
+ sid_ = loginxml.sid();
+ return;
+ }
+
+ response = httpPost("cgi-bin/webcm")
+ .addParameter("getpage", "../html/login_sid.xml")
+ .addParameter("var:lang", "de")
+ .addParameter("login:command/response",
+ loginxml.response(password)).execute();
+ loginxml = new LoginXML(response);
+ response.close();
+ response = null;
+
+ if (!loginxml.iswriteaccess()) throw new RuntimeException(
+ "FritzBox login failed");
+
+ sid_ = loginxml.sid();
+ }
+ finally {
+ Util.nothrowClose(response);
+ }
+
+ }
+
+ public InputStream exportAddressbook() throws IOException
+ {
+ return httpPostMultipart("cgi-bin/firmwarecfg")
+ .addParameter("sid", sid_).addParameter("PhonebookId", "0")
+ .addParameter("PhonebookExportName", "Telefonbuch")
+ .addParameter("PhonebookExport", "").execute();
+ }
+
+ public OutputStream importAddressbook() throws IOException
+ {
+ return new ByteArrayOutputStream() {
+ public void close() throws IOException
+ {
+ httpPostMultipart("cgi-bin/firmwarecfg")
+ .addParameter("sid", sid_)
+ .addParameter("PhonebookId", "0")
+ .addParameter("PhonebookImportFile", toByteArray(),
+ "iso-8859-1").execute().close();
+ }
+ };
+ }
+
+ HttpRequest httpGet(String path)
+ {
+ return new HttpGETRequest(url_ + "/" + path);
+ }
+
+ HttpRequest httpPost(String path)
+ {
+ return new HttpPOSTRequest(url_ + "/" + path);
+ }
+
+ HttpRequest httpPostMultipart(String path)
+ {
+ return new HttpPOSTRequest(url_ + "/" + path).setMultipart(true);
+ }
+}