Search in sources :

Example 1 with GetCallerImageException

use of de.janrufmonitor.fritzbox.firmware.exception.GetCallerImageException in project janrufmonitor by tbrandt77.

the class FritzBoxMappingManager method mapFritzBoxCallerToJam.

public ICaller mapFritzBoxCallerToJam(IPhonebookEntry pe) throws IOException {
    if (this.m_logger.isLoggable(Level.INFO))
        this.m_logger.info("Processing FritzBox phonebook caller: " + pe.toString());
    List phones = new ArrayList(3);
    IAttributeMap attributes = getRuntime().getCallerFactory().createAttributeMap();
    attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_CALLERMANAGER, FritzBoxPhonebookManager.ID));
    if (pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_LASTNAME) != null) {
        attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_LASTNAME, pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_LASTNAME)));
    } else {
        try {
            attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_LASTNAME, StringEscapeUtils.unescapeHtml(pe.getName())));
        } catch (Exception ex) {
            this.m_logger.log(Level.WARNING, ex.getMessage(), ex);
            attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_LASTNAME, pe.getName()));
        }
    }
    if (pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_FIRSTNAME) != null)
        attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_FIRSTNAME, pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_FIRSTNAME)));
    if (pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_STREET) != null)
        attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_STREET, pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_STREET)));
    if (pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_STREET_NO) != null)
        attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_STREET_NO, pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_STREET_NO)));
    if (pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_POSTAL_CODE) != null)
        attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_POSTAL_CODE, pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_POSTAL_CODE)));
    if (pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_CITY) != null)
        attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_CITY, pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_CITY)));
    if (pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_COUNTRY) != null)
        attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_COUNTRY, pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_COUNTRY)));
    if (pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_CATEGORY) != null) {
        attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_CATEGORY, pe.getJamInfo(IJAMConst.ATTRIBUTE_NAME_CATEGORY)));
    }
    if (!FirmwareManager.getInstance().isInstance(TR064FritzBoxFirmware.class)) {
        if (pe.getAddressbook() != null && pe.getAddressbook().length() > 0)
            attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_CATEGORY, pe.getAddressbook()));
    }
    if (pe.getEmail() != null && pe.getEmail().length() > 0) {
        attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_EMAIL, pe.getEmail()));
    }
    if (pe.getEntryID() != null && pe.getEntryID().length() > 0) {
        attributes.add(getRuntime().getCallerFactory().createAttribute("fb_entryID", pe.getEntryID()));
    }
    if (pe.getModTime() != null && pe.getModTime().length() > 0) {
        attributes.add(getRuntime().getCallerFactory().createAttribute("fb_mod_time", pe.getModTime()));
    }
    if (pe.getCatergory() != null && pe.getCatergory().length() > 0) {
        attributes.add(getRuntime().getCallerFactory().createAttribute("fb_category", pe.getCatergory()));
    }
    if (pe.getImageURL() != null && pe.getImageURL().length() > 0) {
        attributes.add(getRuntime().getCallerFactory().createAttribute("fb_imageURL", pe.getImageURL()));
    }
    Map phs = pe.getPhones();
    Iterator entries = phs.keySet().iterator();
    String key = null;
    IPhonenumber phone = null;
    while (entries.hasNext()) {
        key = (String) entries.next();
        // added 2016/01/19: remove internal FB AB numbers from contact list.
        if (key != null && key.startsWith("**"))
            continue;
        if (key != null && !PhonenumberAnalyzer.getInstance(getRuntime()).isInternal(key) && !PhonenumberAnalyzer.getInstance(getRuntime()).isClired(key)) {
            if (this.m_logger.isLoggable(Level.INFO)) {
                this.m_logger.info("FritzBox raw number: " + key);
            }
            phone = PhonenumberAnalyzer.getInstance(getRuntime()).toIdentifiedPhonenumber(key);
            if (this.m_logger.isLoggable(Level.INFO)) {
                this.m_logger.info("FritzBox identified number: " + phone);
            }
            if (phone != null) {
                phones.add(phone);
                attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_NUMBER_TYPE + phone.getTelephoneNumber(), (String) phs.get(key)));
                if (pe.getNumberID(key) != null) {
                    attributes.add(getRuntime().getCallerFactory().createAttribute("fb_number_id_" + phone.getTelephoneNumber(), pe.getNumberID(key)));
                }
                if (pe.getNumberPrio(key) != null) {
                    attributes.add(getRuntime().getCallerFactory().createAttribute("fb_number_prio_" + phone.getTelephoneNumber(), pe.getNumberPrio(key)));
                }
                if (pe.getNumberQuickDial(key) != null) {
                    attributes.add(getRuntime().getCallerFactory().createAttribute("fb_number_quickdial_" + phone.getTelephoneNumber(), pe.getNumberQuickDial(key)));
                }
                if (pe.getNumberType(key) != null) {
                    attributes.add(getRuntime().getCallerFactory().createAttribute("fb_number_type_" + phone.getTelephoneNumber(), pe.getNumberType(key)));
                }
                if (pe.getNumberVanity(key) != null) {
                    attributes.add(getRuntime().getCallerFactory().createAttribute("fb_number_vanity_" + phone.getTelephoneNumber(), pe.getNumberVanity(key)));
                }
            }
        }
        if (PhonenumberAnalyzer.getInstance(PIMRuntime.getInstance()).isInternal((key.trim()))) {
            phone = PhonenumberAnalyzer.getInstance(getRuntime()).toInternalPhonenumber(key);
            if (phone != null) {
                phones.add(phone);
                attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_NUMBER_TYPE + phone.getTelephoneNumber(), (String) phs.get(key)));
                if (pe.getNumberID(key) != null) {
                    attributes.add(getRuntime().getCallerFactory().createAttribute("fb_number_id_" + phone.getTelephoneNumber(), pe.getNumberID(key)));
                }
                if (pe.getNumberPrio(key) != null) {
                    attributes.add(getRuntime().getCallerFactory().createAttribute("fb_number_prio_" + phone.getTelephoneNumber(), pe.getNumberPrio(key)));
                }
                if (pe.getNumberQuickDial(key) != null) {
                    attributes.add(getRuntime().getCallerFactory().createAttribute("fb_number_quickdial_" + phone.getTelephoneNumber(), pe.getNumberQuickDial(key)));
                }
                if (pe.getNumberType(key) != null) {
                    attributes.add(getRuntime().getCallerFactory().createAttribute("fb_number_type_" + phone.getTelephoneNumber(), pe.getNumberType(key)));
                }
                if (pe.getNumberVanity(key) != null) {
                    attributes.add(getRuntime().getCallerFactory().createAttribute("fb_number_vanity_" + phone.getTelephoneNumber(), pe.getNumberVanity(key)));
                }
            }
        }
    }
    if (phones.size() == 0)
        return null;
    String img = pe.getImageBase64();
    if (img == null && pe.getImageURL() != null) {
        // get from FB directly
        try {
            img = pe.getImageURL();
            img = StringUtils.replaceString(img, "file:///var", "/download.lua?path=/var");
            img = FirmwareManager.getInstance().getCallerImage(img);
        } catch (GetCallerImageException e) {
            this.m_logger.log(Level.SEVERE, e.getMessage(), e);
        }
    }
    if (img != null) {
        // 2015/11/06: added image support in FB phonebook
        ByteArrayInputStream in = new ByteArrayInputStream(Base64Decoder.decode(img).getBytes("iso-8859-1"));
        File photoDir = new File(PathResolver.getInstance().getPhotoDirectory());
        if (!photoDir.exists())
            photoDir.mkdirs();
        FileOutputStream out = new FileOutputStream(new File(photoDir, ((IPhonenumber) phones.get(0)).getTelephoneNumber() + ".png"));
        Stream.copy(in, out, true);
        attributes.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH, new File(PathResolver.getInstance().getPhotoDirectory(), ((IPhonenumber) phones.get(0)).getTelephoneNumber() + ".png").getAbsolutePath()));
    }
    if (pe.getUniqueID() != null && pe.getUniqueID().length() > 0) {
        return getRuntime().getCallerFactory().createCaller(pe.getUniqueID(), null, phones, attributes);
    }
    return getRuntime().getCallerFactory().createCaller(null, phones, attributes);
}
Also used : ArrayList(java.util.ArrayList) TR064FritzBoxFirmware(de.janrufmonitor.fritzbox.firmware.TR064FritzBoxFirmware) GetCallerImageException(de.janrufmonitor.fritzbox.firmware.exception.GetCallerImageException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) Iterator(java.util.Iterator) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) ArrayList(java.util.ArrayList) ICallerList(de.janrufmonitor.framework.ICallerList) List(java.util.List) Map(java.util.Map) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) File(java.io.File) GetCallerImageException(de.janrufmonitor.fritzbox.firmware.exception.GetCallerImageException) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Aggregations

IAttributeMap (de.janrufmonitor.framework.IAttributeMap)1 ICallerList (de.janrufmonitor.framework.ICallerList)1 IPhonenumber (de.janrufmonitor.framework.IPhonenumber)1 TR064FritzBoxFirmware (de.janrufmonitor.fritzbox.firmware.TR064FritzBoxFirmware)1 GetCallerImageException (de.janrufmonitor.fritzbox.firmware.exception.GetCallerImageException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXException (org.xml.sax.SAXException)1