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
- {
- System.out.println("sending to fritzbox");
- 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);
- }
+ 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);
+ }
}