bd34c806b99ecd21278960206463b1e59f765783
[jpim.git] / src / de / j32 / avmfritz / FritzBox.java
1 package de.j32.avmfritz;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7
8 import org.xml.sax.SAXException;
9
10 import de.j32.httplib.HttpGETRequest;
11 import de.j32.httplib.HttpPOSTRequest;
12 import de.j32.httplib.HttpRequest;
13 import de.j32.httplib.HttpResponse;
14 import de.j32.util.Util;
15
16 public class FritzBox
17 {
18     String url_;
19     String sid_;
20
21     public FritzBox(String password, String url) throws SAXException,
22             IOException
23     {
24         url_ = url;
25
26         HttpResponse response = null;
27         try {
28             response = httpGet("cgi-bin/webcm").addParameter("getpage",
29                     "../html/login_sid.xml").execute();
30             LoginXML loginxml = new LoginXML(response);
31             response.close();
32             response = null;
33
34             if (loginxml.iswriteaccess()) {
35                 sid_ = loginxml.sid();
36                 return;
37             }
38
39             response = httpPost("cgi-bin/webcm")
40                     .addParameter("getpage", "../html/login_sid.xml")
41                     .addParameter("var:lang", "de")
42                     .addParameter("login:command/response",
43                             loginxml.response(password)).execute();
44             loginxml = new LoginXML(response);
45             response.close();
46             response = null;
47
48             if (!loginxml.iswriteaccess()) throw new RuntimeException(
49                     "FritzBox login failed");
50
51             sid_ = loginxml.sid();
52         }
53         finally {
54             Util.nothrowClose(response);
55         }
56
57     }
58
59     public InputStream exportAddressbook() throws IOException
60     {
61         return httpPostMultipart("cgi-bin/firmwarecfg")
62                 .addParameter("sid", sid_).addParameter("PhonebookId", "0")
63                 .addParameter("PhonebookExportName", "Telefonbuch")
64                 .addParameter("PhonebookExport", "").execute();
65     }
66
67     public OutputStream importAddressbook() throws IOException
68     {
69         return new ByteArrayOutputStream() {
70             public void close() throws IOException
71             {
72                 httpPostMultipart("cgi-bin/firmwarecfg")
73                         .addParameter("sid", sid_)
74                         .addParameter("PhonebookId", "0")
75                         .addParameter("PhonebookImportFile", toByteArray(),
76                                 "iso-8859-1").execute().close();
77             }
78         };
79     }
80
81     HttpRequest httpGet(String path)
82     {
83         return new HttpGETRequest(url_ + "/" + path);
84     }
85
86     HttpRequest httpPost(String path)
87     {
88         return new HttpPOSTRequest(url_ + "/" + path);
89     }
90
91     HttpRequest httpPostMultipart(String path)
92     {
93         return new HttpPOSTRequest(url_ + "/" + path).setMultipart(true);
94     }
95 }