de8e63f81d1fa58cecb166a027200401be21ac25
[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                                 System.out.println("sending to fritzbox");
78                                 httpPostMultipart("cgi-bin/firmwarecfg")
79                                         .addParameter("sid", sid_)
80                                         .addParameter("PhonebookId", "0")
81                                         .addParameter("PhonebookImportFile", toByteArray(), "iso-8859-1")
82                                                 .execute()
83                                                 .close();
84                         }
85                 };
86         }
87         
88         HttpRequest httpGet(String path)
89         {
90                 return new HttpGETRequest(url_ + "/" + path);
91         }
92         
93         HttpRequest httpPost(String path)
94         {
95                 return new HttpPOSTRequest(url_ + "/" + path);
96         }
97         
98         HttpRequest httpPostMultipart(String path)
99         {
100                 return new HttpPOSTRequest(url_ + "/" + path).setMultipart(true);
101         }
102 }