use of de.janrufmonitor.fritzbox.firmware.exception.GetCallerListException in project janrufmonitor by tbrandt77.
the class FritzBoxPhonebookManager method createCallerListFromFritzBoxPhonebook.
private void createCallerListFromFritzBoxPhonebook() {
Thread t = new Thread(new Runnable() {
Logger m_logger;
public void run() {
isSyncing = true;
this.m_logger = LogManager.getLogManager().getLogger(IJAMConst.DEFAULT_LOGGER);
if (this.m_logger.isLoggable(Level.FINE))
this.m_logger.fine("Starting JAM-FritzBoxPhonebookSync-Thread");
File mso_cache = new File(FBP_CACHE_PATH);
if (!mso_cache.exists())
mso_cache.mkdirs();
ICallerList cl = getRuntime().getCallerFactory().createCallerList();
FirmwareManager fwm = FirmwareManager.getInstance();
fwm.startup();
try {
if (!fwm.isLoggedIn())
fwm.login();
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("FritzBox Firmware created.");
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Login to FritzBox successfull.");
// check if phonebook is configured
String abId = getConfiguration().getProperty(CFG_ADDRESSBOOK, "0");
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Getting FritzBox phonebook ID: #" + abId);
int id = Integer.parseInt(abId);
String name = null;
try {
Map abs = fwm.getAddressbooks();
if (abs.containsKey(Integer.parseInt(abId))) {
name = (String) abs.get(Integer.parseInt(abId));
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Getting FritzBox phonebook name: " + name);
}
} catch (GetAddressbooksException e) {
this.m_logger.log(Level.WARNING, e.getMessage(), e);
}
List callers = null;
if (name != null) {
callers = fwm.getCallerList(id, name);
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Getting FritzBox phonebook callers: " + callers.size());
} else {
callers = fwm.getCallerList();
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Getting FritzBox default phonebook callers: " + callers.size());
}
if (callers.size() == 0) {
try {
getDatabaseHandler().deleteCallerList(getRuntime().getCallerFactory().createCallerList());
getDatabaseHandler().commit();
} catch (SQLException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
try {
getDatabaseHandler().rollback();
} catch (SQLException e1) {
this.m_logger.log(Level.SEVERE, e1.getMessage(), e1);
}
}
isSyncing = false;
return;
}
cl.add(FritzBoxMappingManager.getInstance().toCallerList(callers));
} catch (FritzBoxLoginException e2) {
this.m_logger.log(Level.SEVERE, e2.getMessage(), e2);
} catch (GetCallerListException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
} catch (IOException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
} catch (Throwable e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
}
try {
getDatabaseHandler().deleteCallerList(getRuntime().getCallerFactory().createCallerList());
getDatabaseHandler().insertOrUpdateCallerList(cl);
getDatabaseHandler().commit();
} catch (SQLException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
try {
getDatabaseHandler().rollback();
} catch (SQLException e1) {
this.m_logger.log(Level.SEVERE, e1.getMessage(), e1);
}
}
isSyncing = false;
if (this.m_logger.isLoggable(Level.FINE))
this.m_logger.fine("Stopping JAM-FritzBoxPhonebookSync-Thread");
}
});
t.setName("JAM-FritzBoxPhonebookSync-Thread-(non-deamon)");
t.start();
}
use of de.janrufmonitor.fritzbox.firmware.exception.GetCallerListException in project janrufmonitor by tbrandt77.
the class AbstractFritzBoxFirmware method getCallerList.
public List getCallerList() throws GetCallerListException, IOException {
if (!this.isInitialized())
throw new GetCallerListException("Could not get phone book from FritzBox: FritzBox firmware not initialized.");
InputStream in = null;
try {
in = this.getCallerListAsStream();
} catch (IOException e) {
throw new GetCallerListException(e.getMessage());
}
if (in == null)
return new ArrayList(0);
boolean started = false;
List result = new ArrayList();
StringBuffer parseBuffer = new StringBuffer();
InputStreamReader inr = new InputStreamReader(in, "UTF-8");
BufferedReader bufReader = new BufferedReader(inr);
// drop header
String line = bufReader.readLine();
// fasten the processing
bufReader.skip(4096);
while (bufReader.ready()) {
line = bufReader.readLine();
if (line.indexOf(">TrFonName") >= 0)
started = true;
if (started) {
parseBuffer.append(line);
parseBuffer.append(IJAMConst.CRLF);
}
}
bufReader.close();
in.close();
result.addAll(parseHtml(parseBuffer));
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Phonebook from FritzBox succuessfully fetched. List size: " + result.size());
return result;
}
use of de.janrufmonitor.fritzbox.firmware.exception.GetCallerListException in project janrufmonitor by tbrandt77.
the class AbstractFritzBoxFirmware method getCallerListAsStream.
private InputStream getCallerListAsStream() throws GetCallerListException, IOException {
long start = System.currentTimeMillis();
this.m_logger.info("Starting retrieving phone book...");
// The list should be updated now
// Get the csv file for processing
String urlstr = getProtocol() + this.m_address + ":" + this.m_port + getBaseURL();
URL url;
URLConnection urlConn;
DataOutputStream printout;
try {
this.m_logger.info("Calling FritzBox URL: " + urlstr);
url = new URL(urlstr);
} catch (MalformedURLException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
throw new GetCallerListException("Invalid URL: " + urlstr);
}
// If the url is valid load the data
if (url != null && getFetchCallerListPOSTData().trim().length() > 0) {
urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
// Sending postdata to the fritz box
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
printout = new DataOutputStream(urlConn.getOutputStream());
printout.writeBytes(getFetchCallerListPOSTData().replaceAll("\\$LANG", this.m_language) + getFetchCallerListURLAuthenticator().getAuthenticationToken());
printout.flush();
printout.close();
try {
Thread.sleep(1000);
} catch (InterruptedException e2) {
this.m_logger.log(Level.SEVERE, e2.getMessage(), e2);
}
try {
// Get response data from the box
ByteArrayOutputStream bos = new ByteArrayOutputStream();
this.m_logger.info("Fetching call list from FritzBox took " + (System.currentTimeMillis() - start) + "ms");
Stream.copy(urlConn.getInputStream(), bos);
ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
// this.m_logger.info(bos.toString());
this.m_logger.info("Finished retrieving call list took " + (System.currentTimeMillis() - start) + "ms");
urlConn.getInputStream().close();
return bin;
} catch (IOException e1) {
this.m_logger.log(Level.SEVERE, e1.getMessage(), e1);
throw new GetCallerListException(e1.getMessage());
}
}
return null;
}
use of de.janrufmonitor.fritzbox.firmware.exception.GetCallerListException in project janrufmonitor by tbrandt77.
the class UnitymediaFirmware method getCallerList.
public List getCallerList(int id, String name) throws GetCallerListException, IOException {
if (!this.isInitialized())
throw new GetCallerListException("Could not get phone book from FritzBox: FritzBox firmware not initialized.");
InputStream in = null;
try {
in = this.getCallerListAsStream(Integer.toString(id), name);
} catch (IOException e) {
throw new GetCallerListException(e.getMessage());
}
if (in == null)
return new ArrayList(0);
List result = new ArrayList();
StringBuffer xml = new StringBuffer();
InputStreamReader inr = new InputStreamReader(in, "iso-8859-1");
BufferedReader bufReader = new BufferedReader(inr);
// drop header
String line = bufReader.readLine();
while (bufReader.ready()) {
line = bufReader.readLine();
xml.append(line + " ");
}
bufReader.close();
in.close();
result.addAll(parseXML(xml, name));
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Phonebook from FritzBox succuessfully fetched. List size: " + result.size());
return result;
}
use of de.janrufmonitor.fritzbox.firmware.exception.GetCallerListException in project janrufmonitor by tbrandt77.
the class TR064FritzBoxFirmware method getCallerList.
public List getCallerList(int addressbookId, String addressbookName) throws GetCallerListException, IOException {
if (!this.isInitialized())
throw new GetCallerListException("Could not get phone book from FritzBox: FritzBox firmware not initialized.");
int size = 0;
try {
size = FritzBoxTR064Manager.getInstance().getPhonebookSize(this.m_user, this.m_password, this.m_server, (this.m_useHttp ? FritzBoxTR064Manager.getInstance().getDefaultFritzBoxTR064Port() : FritzBoxTR064Manager.getInstance().getDefaultFritzBoxTR064SecurePort(this.m_server)), (this.m_useHttp ? "http" : "https"), Integer.toString(addressbookId));
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Pre-calculated list size from FritzBox: " + size);
} catch (IOException e) {
throw new GetCallerListException(e.getMessage());
}
List result = new ArrayList();
for (int i = 0; i < size; i++) {
String xml = null;
try {
xml = FritzBoxTR064Manager.getInstance().getPhonebookEntry(this.m_user, this.m_password, this.m_server, (this.m_useHttp ? FritzBoxTR064Manager.getInstance().getDefaultFritzBoxTR064Port() : FritzBoxTR064Manager.getInstance().getDefaultFritzBoxTR064SecurePort(this.m_server)), (this.m_useHttp ? "http" : "https"), Integer.toString(addressbookId), Integer.toString(i));
} catch (IOException e) {
xml = null;
}
if (xml == null) {
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("No more data found in list. Break at list position: " + i);
break;
}
List<IPhonebookEntry> oneResult = FritzBoxMappingManager.getInstance().parseXmltoFritzBoxCallerList(new StringBuffer(xml), addressbookName);
if (oneResult.size() == 1) {
oneResult.get(0).setEntryID(Integer.toString(i));
}
result.addAll(oneResult);
}
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Phonebook from FritzBox succuessfully fetched. List size: " + result.size());
if (this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Final result list elements {IPhonebookEntry}: " + result.toString());
return result;
}
Aggregations