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);
}
Aggregations