Search in sources :

Example 1 with AbstractURLRequester

use of de.janrufmonitor.repository.web.AbstractURLRequester in project janrufmonitor by tbrandt77.

the class AbstractWebCallerManager method getCaller.

public ICaller getCaller(IPhonenumber p) throws CallerNotFoundException {
    if (p == null)
        throw new CallerNotFoundException("Phone number is not set (null). No caller found.");
    if (p.isClired())
        throw new CallerNotFoundException("Phone number is CLIR. Identification impossible.");
    if (PhonenumberAnalyzer.getInstance(this.getRuntime()).isInternal(p))
        throw new CallerNotFoundException("Phone number is internal number.");
    IPhonenumber number = this.getRuntime().getCallerFactory().createPhonenumber(p.getTelephoneNumber());
    if (isUsedCache() && this.m_unidentified != null && this.m_unidentified.containsKey(number.getTelephoneNumber())) {
        Integer io = (Integer) this.m_unidentified.get(number.getTelephoneNumber());
        if (io.intValue() == 10) {
            this.m_unidentified.remove(number.getTelephoneNumber());
        } else {
            this.m_unidentified.put(number.getTelephoneNumber(), new Integer(io.intValue() + 1));
        }
        throw new CallerNotFoundException("Phone number " + number.getTelephoneNumber() + " not identified. (cached)");
    }
    ICaller caller = null;
    if (isUsedCache() && this.m_cache != null && this.m_cache.containsKey(number)) {
        caller = (ICaller) this.m_cache.get(number);
        if (this.m_logger.isLoggable(Level.INFO))
            this.m_logger.info("Taking caller from cache: " + caller);
        if (caller != null)
            return caller;
    } else {
        if (this.m_logger.isLoggable(Level.INFO))
            this.m_logger.info("Number is not cached, start identifiing ...");
        IPhonenumber pn = null;
        try {
            ICallerManager defaultManager = this.getRuntime().getCallerManagerFactory().getDefaultCallerManager();
            if (defaultManager != null && defaultManager.isActive() && defaultManager.isSupported(IIdentifyCallerRepository.class)) {
                ICaller defaultIdentified = ((IIdentifyCallerRepository) defaultManager).getCaller(number);
                pn = defaultIdentified.getPhoneNumber();
            }
        } catch (CallerNotFoundException e) {
        // ignore this exception
        }
        // check correct country code
        if (!this.getSupportedIntAreaCode().equalsIgnoreCase("00")) {
            if (pn != null && !pn.getIntAreaCode().equalsIgnoreCase(this.getSupportedIntAreaCode()))
                throw new CallerNotFoundException("Phone number has country code " + pn.getIntAreaCode() + ". Caller manager " + this.getID() + " is supporting only " + this.getSupportedIntAreaCode());
            // added 2013/07/22: number format compatibility 0911234567 must be equal +49 (911) 234567
            if (pn != null)
                number.setTelephoneNumber(pn.getTelephoneNumber());
        }
        // added 2009/05/28
        // add detection of web services which provides number in middle of URL
        String url = this.getURL();
        if (url.indexOf(IJAMConst.GLOBAL_VARIABLE_WEBCM_NUMBER) > 0) {
            url = StringUtils.replaceString(url, IJAMConst.GLOBAL_VARIABLE_WEBCM_NUMBER, (number.getTelephoneNumber().startsWith("0") ? "" : "0") + number.getTelephoneNumber());
        } else {
            // added 2010/11/18: added URL attribute parsing
            String urlx = url;
            url = Formatter.getInstance(this.getRuntime()).parse(url, pn);
            if (urlx.equalsIgnoreCase(url))
                url += (number.getTelephoneNumber().startsWith("0") ? "" : "0") + number.getTelephoneNumber();
        }
        if (this.m_logger.isLoggable(Level.INFO))
            this.m_logger.info("URL to call: " + url);
        AbstractURLRequester r = this.createURLRequester(url, this.getSkipBytes(), number.getTelephoneNumber());
        try {
            long ts = System.currentTimeMillis();
            Executor ex = new Executor(r);
            Thread t = new Thread(ex);
            t.start();
            while (t.isAlive() && (System.currentTimeMillis() - ts < r.getTimeout())) {
                Thread.sleep(100);
            }
            if (ex != null && ex.isFailed()) {
                if (this.m_unidentified != null && !this.m_unidentified.containsKey(number.getTelephoneNumber()))
                    this.m_unidentified.put(number.getTelephoneNumber(), new Integer(1));
                throw new CallerNotFoundException("Phone number " + number.getTelephoneNumber() + " not identified.", (ex.getException() != null ? ex.getException() : new Exception()));
            }
            if (t.isAlive())
                throw new Exception("Identification thread is blocking.");
        // r.go();
        } catch (Exception e) {
            if (this.m_unidentified != null && !this.m_unidentified.containsKey(number.getTelephoneNumber()))
                this.m_unidentified.put(number.getTelephoneNumber(), new Integer(1));
            throw new CallerNotFoundException("Phone number " + number.getTelephoneNumber() + " not identified: " + e.getMessage(), e);
        }
        IAttributeMap m = r.getAttributes();
        if (pn == null)
            pn = r.getPhonenumber();
        if (!getKeepSourceNumber() && pn != null && r.getPhonenumber() != null && !r.getPhonenumber().getTelephoneNumber().endsWith(pn.getTelephoneNumber())) {
            pn = r.getPhonenumber();
            if (this.m_logger.isLoggable(Level.INFO))
                this.m_logger.info("Incoming call number " + number.getTelephoneNumber() + " was not identified but extension " + pn.getTelephoneNumber());
        }
        caller = this.getRuntime().getCallerFactory().createCaller(pn);
        caller.setAttributes(m);
        IAttribute cm = this.getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_CALLERMANAGER, this.getID());
        caller.setAttribute(cm);
        // add caller to cache
        if (this.m_cache != null)
            this.m_cache.put(number, caller);
        return caller;
    }
    throw new CallerNotFoundException("No caller found for number " + number);
}
Also used : AbstractURLRequester(de.janrufmonitor.repository.web.AbstractURLRequester) IIdentifyCallerRepository(de.janrufmonitor.repository.types.IIdentifyCallerRepository) ICaller(de.janrufmonitor.framework.ICaller) IAttribute(de.janrufmonitor.framework.IAttribute) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Aggregations

IAttribute (de.janrufmonitor.framework.IAttribute)1 IAttributeMap (de.janrufmonitor.framework.IAttributeMap)1 ICaller (de.janrufmonitor.framework.ICaller)1 IPhonenumber (de.janrufmonitor.framework.IPhonenumber)1 IIdentifyCallerRepository (de.janrufmonitor.repository.types.IIdentifyCallerRepository)1 AbstractURLRequester (de.janrufmonitor.repository.web.AbstractURLRequester)1