c1852e789ad78bb84da05f7e4efaad98759071e7
[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)
22                 throws SAXException, IOException
23         {
24                 url_ = url;
25                 
26                 HttpResponse response = null;
27                 try {
28                         response = httpGet("cgi-bin/webcm")
29                                 .addParameter("getpage", "../html/login_sid.xml")
30                                 .execute();
31                         LoginXML loginxml = new LoginXML(response);
32                         response.close();
33                         response = null;
34
35                         if (loginxml.iswriteaccess()) {
36                                 sid_ = loginxml.sid();
37                                 return;
38                         }
39
40                         response = httpPost("cgi-bin/webcm")
41                                 .addParameter("getpage", "../html/login_sid.xml")
42                                 .addParameter("var:lang", "de")
43                                 .addParameter("login:command/response",
44                                         loginxml.response(password)).execute();
45                         loginxml = new LoginXML(response);
46                         response.close();
47                         response = null;
48
49                         if (!loginxml.iswriteaccess())
50                                 throw new RuntimeException("FritzBox login failed");
51
52                         sid_ = loginxml.sid();
53                 }
54                 finally {
55                         Util.nothrowClose(response);
56                 }
57                 
58         }
59         
60         public InputStream exportAddressbook()
61                 throws IOException
62         {
63                 return httpPostMultipart("cgi-bin/firmwarecfg")
64                                         .addParameter("sid", sid_)
65                                         .addParameter("PhonebookId", "0")
66                                         .addParameter("PhonebookExportName", "Telefonbuch")
67                                         .addParameter("PhonebookExport", "").execute();
68         }
69         
70         public OutputStream importAddressbook()
71                 throws IOException
72         {
73                 return new ByteArrayOutputStream() {
74                         public void close()
75                                 throws IOException
76                         {
77                                 httpPostMultipart("cgi-bin/firmwarecfg")
78                                         .addParameter("sid", sid_)
79                                         .addParameter("PhonebookId", "0")
80                                         .addParameter("PhonebookImportFile", toByteArray(), "iso-8859-1")
81                                                 .execute()
82                                                 .close();
83                         }
84                 };
85         }
86         
87         HttpRequest httpGet(String path)
88         {
89                 return new HttpGETRequest(url_ + "/" + path);
90         }
91         
92         HttpRequest httpPost(String path)
93         {
94                 return new HttpPOSTRequest(url_ + "/" + path);
95         }
96         
97         HttpRequest httpPostMultipart(String path)
98         {
99                 return new HttpPOSTRequest(url_ + "/" + path).setMultipart(true);
100         }
101 }