Search in sources :

Example 6 with GetCallerListException

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();
}
Also used : FritzBoxLoginException(de.janrufmonitor.fritzbox.firmware.exception.FritzBoxLoginException) GetCallerListException(de.janrufmonitor.fritzbox.firmware.exception.GetCallerListException) SQLException(java.sql.SQLException) IOException(java.io.IOException) Logger(java.util.logging.Logger) ICallerList(de.janrufmonitor.framework.ICallerList) FirmwareManager(de.janrufmonitor.fritzbox.firmware.FirmwareManager) GetAddressbooksException(de.janrufmonitor.fritzbox.firmware.exception.GetAddressbooksException) ICallerList(de.janrufmonitor.framework.ICallerList) List(java.util.List) File(java.io.File) Map(java.util.Map)

Example 7 with GetCallerListException

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;
}
Also used : GetCallerListException(de.janrufmonitor.fritzbox.firmware.exception.GetCallerListException) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException)

Example 8 with GetCallerListException

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;
}
Also used : MalformedURLException(java.net.MalformedURLException) GetCallerListException(de.janrufmonitor.fritzbox.firmware.exception.GetCallerListException) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 9 with GetCallerListException

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;
}
Also used : GetCallerListException(de.janrufmonitor.fritzbox.firmware.exception.GetCallerListException) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException)

Example 10 with GetCallerListException

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;
}
Also used : GetCallerListException(de.janrufmonitor.fritzbox.firmware.exception.GetCallerListException) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IPhonebookEntry(de.janrufmonitor.fritzbox.IPhonebookEntry)

Aggregations

GetCallerListException (de.janrufmonitor.fritzbox.firmware.exception.GetCallerListException)10 IOException (java.io.IOException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 List (java.util.List)6 ArrayList (java.util.ArrayList)5 BufferedReader (java.io.BufferedReader)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DataOutputStream (java.io.DataOutputStream)4 InputStream (java.io.InputStream)4 InputStreamReader (java.io.InputStreamReader)4 MalformedURLException (java.net.MalformedURLException)4 URL (java.net.URL)4 URLConnection (java.net.URLConnection)4 ICallerList (de.janrufmonitor.framework.ICallerList)1 IPhonebookEntry (de.janrufmonitor.fritzbox.IPhonebookEntry)1 FirmwareManager (de.janrufmonitor.fritzbox.firmware.FirmwareManager)1 FritzBoxLoginException (de.janrufmonitor.fritzbox.firmware.exception.FritzBoxLoginException)1 GetAddressbooksException (de.janrufmonitor.fritzbox.firmware.exception.GetAddressbooksException)1 File (java.io.File)1 SQLException (java.sql.SQLException)1