Example usage for java.lang StringBuffer setLength

List of usage examples for java.lang StringBuffer setLength

Introduction

In this page you can find the example usage for java.lang StringBuffer setLength.

Prototype

@Override
public synchronized void setLength(int newLength) 

Source Link

Usage

From source file:com.niki.normalizer.Metaphone.java

/**
 * Find the metaphone value of a String. This is similar to the
 * soundex algorithm, but better at finding similar sounding words.
 * All input is converted to upper case.
 * Limitations: Input format is expected to be a single ASCII word
 * with only characters in the A - Z range, no punctuation or numbers.
 *
 * @param txt String to find the metaphone code for
 * @return A metaphone code corresponding to the String supplied
 *///from   w w  w  .  j  av a 2s  .co m
public static String metaphone(String txt) {
    boolean hard = false;
    if ((txt == null) || (txt.length() == 0)) {
        return "";
    }
    // single character is itself
    if (txt.length() == 1) {
        return txt.toUpperCase(java.util.Locale.ENGLISH);
    }

    char[] inwd = txt.toUpperCase(java.util.Locale.ENGLISH).toCharArray();

    StringBuffer local = new StringBuffer(40); // manipulate
    StringBuffer code = new StringBuffer(10); //   output
    // handle initial 2 characters exceptions
    switch (inwd[0]) {
    case 'K':
    case 'G':
    case 'P': /* looking for KN, etc*/
        if (inwd[1] == 'N') {
            local.append(inwd, 1, inwd.length - 1);
        } else {
            local.append(inwd);
        }
        break;
    case 'A': /* looking for AE */
        if (inwd[1] == 'E') {
            local.append(inwd, 1, inwd.length - 1);
        } else {
            local.append(inwd);
        }
        break;
    case 'W': /* looking for WR or WH */
        if (inwd[1] == 'R') { // WR -> R
            local.append(inwd, 1, inwd.length - 1);
            break;
        }
        if (inwd[1] == 'H') {
            local.append(inwd, 1, inwd.length - 1);
            local.setCharAt(0, 'W'); // WH -> W
        } else {
            local.append(inwd);
        }
        break;
    case 'X': /* initial X becomes S */
        inwd[0] = 'S';
        local.append(inwd);
        break;
    default:
        local.append(inwd);
    } // now local has working string with initials fixed

    int wdsz = local.length();
    int n = 0;

    while ((code.length() < 4) && (n < wdsz)) { // max code size of 4 works well
        char symb = local.charAt(n);
        // remove duplicate letters except C
        if ((symb != 'C') && (isPreviousChar(local, n, symb))) {
            n++;
        } else { // not dup
            switch (symb) {
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                if (n == 0) {
                    code.append(symb);
                }
                break; // only use vowel if leading char
            case 'B':
                if (isPreviousChar(local, n, 'M') && isLastChar(wdsz, n)) { // B is silent if word ends in MB
                    break;
                }
                code.append(symb);
                break;
            case 'C': // lots of C special cases
                /* discard if SCI, SCE or SCY */
                if (isPreviousChar(local, n, 'S') && !isLastChar(wdsz, n)
                        && (FRONTV.indexOf(local.charAt(n + 1)) >= 0)) {
                    break;
                }
                if (regionMatch(local, n, "CIA")) { // "CIA" -> X
                    code.append('X');
                    break;
                }
                if (!isLastChar(wdsz, n) && (FRONTV.indexOf(local.charAt(n + 1)) >= 0)) {
                    code.append('S');
                    break; // CI,CE,CY -> S
                }
                if (isPreviousChar(local, n, 'S') && isNextChar(local, n, 'H')) { // SCH->sk
                    code.append('K');
                    break;
                }
                if (isNextChar(local, n, 'H')) { // detect CH
                    if ((n == 0) && (wdsz >= 3) && isVowel(local, 2)) { // CH consonant -> K consonant
                        code.append('K');
                    } else {
                        code.append('X'); // CHvowel -> X
                    }
                } else {
                    code.append('K');
                }
                break;
            case 'D':
                if (!isLastChar(wdsz, n + 1) && isNextChar(local, n, 'G')
                        && (FRONTV.indexOf(local.charAt(n + 2)) >= 0)) { // DGE DGI DGY -> J 
                    code.append('J');
                    n += 2;
                } else {
                    code.append('T');
                }
                break;
            case 'G': // GH silent at end or before consonant
                if (isLastChar(wdsz, n + 1) && isNextChar(local, n, 'H')) {
                    break;
                }
                if (!isLastChar(wdsz, n + 1) && isNextChar(local, n, 'H') && !isVowel(local, n + 2)) {
                    break;
                }
                if ((n > 0) && (regionMatch(local, n, "GN") || regionMatch(local, n, "GNED"))) {
                    break; // silent G
                }
                if (isPreviousChar(local, n, 'G')) {
                    // NOTE: Given that duplicated chars are removed, I don't see how this can ever be true
                    hard = true;
                } else {
                    hard = false;
                }
                if (!isLastChar(wdsz, n) && (FRONTV.indexOf(local.charAt(n + 1)) >= 0) && (!hard)) {
                    code.append('J');
                } else {
                    code.append('K');
                }
                break;
            case 'H':
                if (isLastChar(wdsz, n)) {
                    break; // terminal H
                }
                if ((n > 0) && (VARSON.indexOf(local.charAt(n - 1)) >= 0)) {
                    break;
                }
                if (isVowel(local, n + 1)) {
                    code.append('H'); // Hvowel
                }
                break;
            case 'F':
            case 'J':
            case 'L':
            case 'M':
            case 'N':
            case 'R':
                code.append(symb);
                break;
            case 'K':
                if (n > 0) { // not initial
                    if (!isPreviousChar(local, n, 'C')) {
                        code.append(symb);
                    }
                } else {
                    code.append(symb); // initial K
                }
                break;
            case 'P':
                if (isNextChar(local, n, 'H')) {
                    // PH -> F
                    code.append('F');
                } else {
                    code.append(symb);
                }
                break;
            case 'Q':
                code.append('K');
                break;
            case 'S':
                if (regionMatch(local, n, "SH") || regionMatch(local, n, "SIO")
                        || regionMatch(local, n, "SIA")) {
                    code.append('X');
                } else {
                    code.append('S');
                }
                break;
            case 'T':
                if (regionMatch(local, n, "TIA") || regionMatch(local, n, "TIO")) {
                    code.append('X');
                    break;
                }
                if (regionMatch(local, n, "TCH")) {
                    // Silent if in "TCH"
                    break;
                }
                // substitute numeral 0 for TH (resembles theta after all)
                if (regionMatch(local, n, "TH")) {
                    code.append('0');
                } else {
                    code.append('T');
                }
                break;
            case 'V':
                code.append('F');
                break;
            case 'W':
            case 'Y': // silent if not followed by vowel
                if (!isLastChar(wdsz, n) && isVowel(local, n + 1)) {
                    code.append(symb);
                }
                break;
            case 'X':
                code.append('K');
                code.append('S');
                break;
            case 'Z':
                code.append('S');
                break;
            } // end switch
            n++;
        } // end else from symb != 'C'
        if (code.length() > 4) {
            code.setLength(4);
        }
    }
    return code.toString();
}

From source file:com.vuze.plugin.azVPN_Helper.Checker_AirVPN.java

@Override
protected boolean callRPCforPort(InetAddress bindIP, StringBuilder sReply) {
    InetAddress[] resolve = null;
    try {//w  w  w . j  av a 2  s.  c o m

        String user = getDefaultUsername();
        String pass = null;
        if (user == null || user.length() == 0) {
            user = config.getPluginStringParameter(PluginConstants.CONFIG_USER);
            pass = new String(config.getPluginByteParameter(PluginConstants.CONFIG_P, new byte[0]), "utf-8");
        } else {
            pass = getPassword();
        }

        if (user == null || user.length() == 0 || pass == null || pass.length() == 0) {
            addReply(sReply, CHAR_WARN, "airvpn.rpc.nocreds");
            return false;
        }

        // If Vuze has a proxy set up (Tools->Options->Connection->Proxy), then
        // we'll need to disable it for the URL
        AEProxySelector selector = AEProxySelectorFactory.getSelector();
        if (selector != null) {
            resolve = SystemDefaultDnsResolver.INSTANCE.resolve(VPN_DOMAIN);

            for (InetAddress address : resolve) {
                selector.setProxy(new InetSocketAddress(address, 443), Proxy.NO_PROXY);
            }
        }

        RequestConfig requestConfig;
        StringBuffer token = new StringBuffer();

        boolean skipLoginPage = false;
        boolean alreadyLoggedIn = false;
        PortInfo[] ports = null;

        if (httpClientContext == null) {
            httpClientContext = HttpClientContext.create();
        } else {
            PluginVPNHelper.log("Have existing context.  Trying to grab port list without logging in.");
            ports = scrapePorts(bindIP, token);
            // assume no token means we aren't logged in
            if (token.length() > 0) {
                PluginVPNHelper.log("Valid ports page. Skipping Login");
                skipLoginPage = true;
                alreadyLoggedIn = true;

                if (ports == null) {
                    addReply(sReply, CHAR_WARN, "airvpn.vpnhelper.rpc.notconnected");
                    return false;
                }
            } else {
                ports = null;
            }
        }

        if (!skipLoginPage) {
            String loginURL = null;
            String authKey = null;

            PluginVPNHelper.log("Getting Login post URL and auth_key");

            HttpGet getLoginPage = new HttpGet(VPN_LOGIN_URL);
            requestConfig = RequestConfig.custom().setLocalAddress(bindIP).setConnectTimeout(15000).build();
            getLoginPage.setConfig(requestConfig);
            getLoginPage.setHeader("User-Agent", USER_AGENT);

            CloseableHttpClient httpClientLoginPage = HttpClients.createDefault();
            CloseableHttpResponse loginPageResponse = httpClientLoginPage.execute(getLoginPage,
                    httpClientContext);

            BufferedReader rd = new BufferedReader(
                    new InputStreamReader(loginPageResponse.getEntity().getContent()));

            Pattern patAuthKey = Pattern.compile(REGEX_AuthKey);

            String line = "";
            while ((line = rd.readLine()) != null) {
                if (line.contains("<form") && line.matches(".*id=['\"]login['\"].*")) {
                    Matcher matcher = Pattern.compile(REGEX_ActionURL).matcher(line);
                    if (matcher.find()) {
                        loginURL = matcher.group(1);
                        if (authKey != null) {
                            break;
                        }
                    }
                }
                Matcher matcherAuthKey = patAuthKey.matcher(line);
                if (matcherAuthKey.find()) {
                    authKey = matcherAuthKey.group(1);
                    if (loginURL != null) {
                        break;
                    }
                }

                if (line.contains("['member_id']") && line.matches(".*parseInt\\s*\\(\\s*[1-9][0-9]*\\s*.*")) {
                    alreadyLoggedIn = true;
                }
            }
            rd.close();

            if (loginURL == null) {
                PluginVPNHelper.log("Could not scrape Login URL.  Using default");
                loginURL = "https://airvpn.org/index.php?app=core&module=global&section=login&do=process";
            }
            if (authKey == null) {
                addReply(sReply, CHAR_WARN, "vpnhelper.rpc.noauthkey");
                return false;
            }

            loginURL = UrlUtils.unescapeXML(loginURL);

            ///////////////////////////////

            if (alreadyLoggedIn) {
                PluginVPNHelper.log("Already Logged In");
            } else {
                PluginVPNHelper.log("Login URL:" + loginURL);
                //https://airvpn.org/index.php?app=core&module=global&section=login&do=process
                //https://airvpn.org/index.php?app=core&module=global&section=login&do=process

                HttpPost httpPostLogin = new HttpPost(loginURL);

                requestConfig = RequestConfig.custom().setLocalAddress(bindIP).setConnectTimeout(15000).build();

                httpPostLogin.setConfig(requestConfig);
                httpPostLogin.setHeader("User-Agent", USER_AGENT);

                CloseableHttpClient httpClient = HttpClients.createDefault();

                List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
                urlParameters.add(new BasicNameValuePair("ips_username", user));
                urlParameters.add(new BasicNameValuePair("ips_password", pass));
                urlParameters.add(new BasicNameValuePair("auth_key", authKey));
                urlParameters.add(new BasicNameValuePair("referer", "http://airvpn.org/"));
                urlParameters.add(new BasicNameValuePair("anonymous", "1"));
                urlParameters.add(new BasicNameValuePair("rememberMe", "1"));

                httpPostLogin.setEntity(new UrlEncodedFormEntity(urlParameters));

                CloseableHttpResponse response = httpClient.execute(httpPostLogin, httpClientContext);

                rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

                line = "";
                while ((line = rd.readLine()) != null) {
                }

                PluginVPNHelper.log("Login Result: " + response.getStatusLine().toString());
            }
        }

        ////////////////////////////

        if (ports == null) {
            ports = scrapePorts(bindIP, token);
            if (ports == null && token.length() > 0) {
                addReply(sReply, CHAR_WARN, "airvpn.vpnhelper.rpc.notconnected");
                return false;
            }
        }

        PluginVPNHelper.log("Found Ports: " + Arrays.toString(ports));

        int existingIndex = ourPortInList(ports);
        if (existingIndex >= 0) {
            addReply(sReply, CHAR_GOOD, "vpnhelper.port.from.rpc.match",
                    new String[] { ports[existingIndex].port });
            return true;
        }

        boolean gotPort = false;

        // There's a limit of 20 ports.  If [0] isn't ours and 20 of them are
        // created, then assume our detection of "ours" is broke and just use
        // the first one
        if (ports != null && ((ports.length > 0 && ports[0].ourBinding) || ports.length == 20)) {
            int port = Integer.parseInt(ports[0].port);
            gotPort = true;

            addReply(sReply, CHAR_GOOD, "vpnhelper.port.from.rpc", new String[] { Integer.toString(port) });

            changePort(port, sReply);
        } else if (ports != null) {
            // create port
            ports = createPort(bindIP, token);
            if (ports.length == 0) {
                // form post should have got the new port, but if it didn't, try
                // reloading the ports page again.
                token.setLength(0);
                ports = scrapePorts(bindIP, token);
            }

            PluginVPNHelper.log("Added a port. Ports: " + Arrays.toString(ports));

            existingIndex = ourPortInList(ports);
            if (existingIndex >= 0) {
                addReply(sReply, CHAR_GOOD, "vpnhelper.port.from.rpc.match",
                        new String[] { ports[existingIndex].port });
                return true;
            }

            if ((ports.length > 0 && ports[0].ourBinding) || ports.length == 20) {
                int port = Integer.parseInt(ports[0].port);
                gotPort = true;

                addReply(sReply, CHAR_GOOD, "vpnhelper.port.from.rpc", new String[] { Integer.toString(port) });

                changePort(port, sReply);
            }

        }

        if (!gotPort) {
            addReply(sReply, CHAR_WARN, "vpnhelper.rpc.no.connect", new String[] { bindIP.toString() });

            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        addReply(sReply, CHAR_BAD, "vpnhelper.rpc.no.connect", new String[] { bindIP + ": " + e.getMessage() });

        return false;
    } finally {
        AEProxySelector selector = AEProxySelectorFactory.getSelector();
        if (selector != null && resolve != null) {
            for (InetAddress address : resolve) {
                AEProxySelectorFactory.getSelector().removeProxy(new InetSocketAddress(address, 443));
            }
        }
    }
    return true;
}

From source file:com.dragonflow.StandardMonitor.URLMonitor.java

private static URLResults checkURLRetrieveDoneHere(HTTPRequestSettings httprequestsettings,
        URLContext urlcontext, String s, String s1, Array array, StringBuffer stringbuffer, long l, int i,
        long l1, StringBuffer stringbuffer1, StringBuffer stringbuffer2) {
    if ((debugURL & kDebugRequest) != 0) {
        toString(urlcontext, httprequestsettings.getUrl(), urlcontext.getRedirectBase(), s, s1,
                httprequestsettings.getProxy(), httprequestsettings.getProxyUserName(),
                httprequestsettings.getProxyPassword(), array, httprequestsettings.getAuthUserName(),
                httprequestsettings.getAuthPassword(), stringbuffer, l, i, l1, stringbuffer1, stringbuffer2);
    }/* w w w .j ava2  s  .c om*/
    if ((debugURL & kDebugRequest) != 0) {
        LogManager.log("RunMonitor", "checking URL... " + httprequestsettings.getUrl());
    }
    long l2 = kURLNoStatusError;
    long l3 = 0L;
    long l4 = 0L;
    long l5 = 0L;
    long l6 = 0L;
    long l7 = 0L;
    long l8 = 0L;
    long l9 = 0L;
    long l10 = 0L;
    long l11 = -1L;
    long l12 = 0L;
    boolean flag = false;
    String s2 = "";
    String s3 = null;
    urlcontext.addCookieParameters(array, httprequestsettings.getUrl());
    if (stringbuffer == null) {
        stringbuffer = new StringBuffer(kURLBufferSize);
        if ((debugURL & kDebugData) != 0) {
            LogManager.log("RunMonitor",
                    "URLMonitor.checkInternalURL() - CREATE contentBuffer size()=" + stringbuffer.length());
        }
    }
    boolean flag1 = urlcontext.getMonitor().getSetting("_concatURLRedirects").length() != 0;
    if (flag1 && concatBuffer == null) {
        concatBuffer = new StringBuffer(kURLBufferSize);
    }
    StringBuffer stringbuffer3 = new StringBuffer();
    String s4 = httprequestsettings.getProxy();
    try {
        Vector vector = finalHTTPClientlRequestPreparation(urlcontext, httprequestsettings, array, l1);
        long l14 = System.currentTimeMillis();
        ApacheHttpMethod apachehttpmethod = null;
        if ((debugURL & kDebugData) != 0) {
            LogManager.log("RunMonitor",
                    "URLMonitor.checkURLRetrieveDoneHere: url: " + httprequestsettings.getUrl());
        }
        if (vector.size() <= 0) {
            apachehttpmethod = ApacheHttpUtils.getRequest(httprequestsettings, stringbuffer3);
        } else {
            apachehttpmethod = ApacheHttpUtils.postPairs(httprequestsettings, vector, stringbuffer3);
        }
        l3 = System.currentTimeMillis() - l14;
        httprequestsettings.setProxy(s4);
        l11 = apachehttpmethod.getDaysUntilCertExpires();
        l2 = apachehttpmethod.getStatusCode();
        if ((debugURL & kDebugData) != 0) {
            LogManager.log("RunMonitor", "URLMonitor.checkURLRetrieveDoneHere: status: " + l2);
        }
        if (l1 - System.currentTimeMillis() <= 0L) {
            l2 = kURLTimeoutError;
        }
        l12 = fillContentBuffer(stringbuffer, apachehttpmethod, l, httprequestsettings);
        if (stringbuffer2 != null) {
            if (stringbuffer2.length() > 0) {
                stringbuffer2.append("SITEVIEW                                         BLANK LINE" + CRLF);
            }
            stringbuffer2.append(stringbuffer.toString());
        }
        l4 = apachehttpmethod.getDNSDuration();
        l5 = apachehttpmethod.getConnectDuration();
        l6 = apachehttpmethod.getResponseDuration();
        l7 = apachehttpmethod.getDownloadDuration();
        if ((debugURL & kDebugData) != 0) {
            LogManager.log("RunMonitor", "URLMonitor.checkURLRetrieveDoneHere - getDNSDuration()=" + l4
                    + " and getConnectDuration()=" + l5 + " on class ");
            LogManager.log("RunMonitor", "URLMonitor.checkURLRetrieveDoneHere - getResponseDuration()=" + l6
                    + " and getDownloadDuration()=" + l7 + " on class ");
        }
        l10 = stringbuffer.length();
        if ((debugURL & kDebugData) != 0) {
            LogManager.log("RunMonitor", "URLMonitor.checkInternalURL() - contentBuffer=" + stringbuffer);
        }
        if (apachehttpmethod.getContentLength() != -1L) {
            l10 = apachehttpmethod.getContentLength();
        }
        l8 = apachehttpmethod.getLastModified();
        l9 = apachehttpmethod.getDate();
        s3 = apachehttpmethod.getLocation();
        flag = apachehttpmethod.getRefreshRedirect();
        if (debugURL != 0) {
            System.out.println("Status6 header.status: " + l2);
        }
        if ((debugURL & kDebugReply) != 0) {
            if (l2 != 200L) {
                LogManager.log("RunMonitor", "status=" + l2 + ", reply=" + stringbuffer);
            } else {
                LogManager.log("RunMonitor", "status=" + l2);
            }
        }
    } catch (Exception exception) {
        String s6 = exception.getClass() + ", " + exception.getMessage();
        LogManager.log("Error", "url error, " + httprequestsettings.getUrl() + ", " + s6 + ", details: "
                + FileUtils.stackTraceText(exception));
        if (exception.getMessage().indexOf("timed out") >= 0) {
            l2 = kURLTimeoutError;
        } else {
            l2 = kURLNoStatusError;
            s2 = "" + kURLNoStatusError + " " + exception.toString();
        }
        if (debugURL != 0) {
            System.out.println("Status12 kSSL2NotFoundError: " + l2 + " exception: " + exception.toString());
        }
    } finally {
        if (stringbuffer3.length() > 0) {
            s2 = "status=" + l2 + " " + stringbuffer3.toString();
            if (l2 != 301L && l2 != 302L && l2 != 303L && l2 != 307L) {
                LogManager.log("Error", "URLMonitor. " + s2);
            }
        }
    }
    if (200L < l2 && l2 < 300L && urlcontext.getMonitor().getSetting("_urlEnable2xxStatus").length() == 0) {
        l2 = 200L;
    }
    if (debugURL != 0) {
        System.out.println("Status15 200: " + l2);
    }
    urlcontext.updateCookies(stringbuffer.toString(), httprequestsettings.getUrl());
    if (l2 == 301L || l2 == 302L || l2 == 303L || l2 == 307L || flag) {
        if (s3 != null && s3.length() > 0) {
            s3 = TextUtils.unescapeHTML(s3);
            s3 = resolveURL(s3, new URLInfo(urlcontext.getRedirectBase()), "");
            long l13 = urlcontext.getMonitor().getSettingAsLong("_urlRedirectMax", DEFAULT_MAX_REDIRECTS);
            boolean flag2 = urlcontext.getMonitor().getPropertyAsBoolean(pErrorOnRedirect);
            if ((long) i <= l13 && !flag2) {
                if ((debugURL & kDebugRequest) != 0) {
                    LogManager.log("RunMonitor", "URLMonitor.checkURLRetrieveDoneHere: redirect=" + s3);
                }
                if (stringbuffer1 != null) {
                    stringbuffer1.setLength(0);
                    stringbuffer1.append(s3);
                }
                if (flag1 && concatBuffer != null) {
                    concatBuffer.append(stringbuffer.toString());
                }
                httprequestsettings.setUrl(s3);
                urlcontext.setRedirectBase(s3);
                urlcontext.setRefererURL(s3);
                stringbuffer.setLength(0);
                URLResults urlresults1 = checkURLRetrieveDoneHere(httprequestsettings, urlcontext, s, s1, null,
                        stringbuffer, l, i + 1, l1, stringbuffer1, stringbuffer2);
                l2 = urlresults1.getStatus();
                l3 += urlresults1.getTotalDuration();
                l10 += urlresults1.getTotalBytes();
                l8 = urlresults1.getLastModified();
                l9 = urlresults1.getCurrentDate();
                l4 += urlresults1.getDnsTime();
                l5 += urlresults1.getConnectTime();
                l6 += urlresults1.getResponseTime();
                l7 += urlresults1.getDownloadTime();
                s2 = urlresults1.getErrorMessage();
                l12 = urlresults1.getHtmlTruncatedIfNonZero();
                if (debugURL != 0) {
                    System.out.println("Status16 redirectResult: " + l2);
                }
            }
        }
    } else {
        if (l2 == (long) kURLNoStatusError) {
            if (urlcontext.getMonitor().getSetting("_urlAllowNoStatus").length() > 0) {
                l2 = kURLok;
                if (debugURL != 0) {
                    System.out.println("Status17 kURLok: " + l2);
                }
            } else {
                LogManager.log("Error", "URL missing status: " + httprequestsettings.getUrl());
            }
        }
        if (flag1 && concatBuffer != null) {
            concatBuffer.append(stringbuffer.toString());
            stringbuffer.setLength(0);
            stringbuffer.append(concatBuffer.toString());
            concatBuffer = null;
            if ((debugURL & kDebugData) != 0) {
                LogManager.log("RunMonitor",
                        "URLMonitor.checkInternalURL() - added concatBuffer to front contentBuffer size()="
                                + stringbuffer.length());
            }
        }
        String s5 = stringbuffer.toString();
        if (l2 == 200L && s1.length() != 0) {
            int j = TextUtils.matchExpression(s5, s1);
            if (j != Monitor.kURLok && I18N.hasUnicode(s1)) {
                String s8 = getHTMLEncoding(s5);
                j = TextUtils.matchExpression(s5, I18N.UnicodeToString(s1, s8));
            }
            if (j == 200) {
                l2 = kURLContentErrorFound;
            }
            if (debugURL != 0) {
                System.out.println("Status18 kURLContentErrorFound: " + l2);
            }
        }
        if (l2 == 200L && s.length() != 0) {
            l2 = TextUtils.matchExpression(s5, s);
            if (debugURL != 0) {
                System.out.println("Status19 TextUtils.matchExpression(contents,match): " + l2);
            }
            if (l2 != (long) Monitor.kURLok && I18N.hasUnicode(s)) {
                String s7 = getHTMLEncoding(s5);
                l2 = TextUtils.matchExpression(s5, I18N.UnicodeToString(s, s7));
                if (debugURL != 0) {
                    System.out.println(
                            "Status20 TextUtils.matchExpression(contents, I18N.UnicodeToString(match,encoding): "
                                    + l2);
                }
            }
        }
    }
    if (urlcontext.getMonitor().getSetting("_urlDetailLogEnabled").length() > 0
            && httprequestsettings.getUrl().indexOf("get.exe") == -1) {
        LogManager.log(urlcontext.getMonitor().getSetting(pURLLogName),
                httprequestsettings.getUrl() + "\t" + l2 + "\t" + l3 + "\t" + l10 + "\t" + l8 + "\t" + l9 + "\t"
                        + l4 + "\t" + l5 + "\t" + l6 + "\t" + l7 + "\t"
                        + urlcontext.getMonitor().getProperty(pName) + "\t"
                        + urlcontext.getMonitor().getProperty(pGroupID) + "\t"
                        + urlcontext.getMonitor().getProperty(pID));
    }
    URLResults urlresults = new URLResults();
    urlresults.setStatus(l2);
    urlresults.setTotalDuration(l3);
    urlresults.setTotalBytes(l10);
    urlresults.setLastModified(l8);
    urlresults.setCurrentDate(l9);
    urlresults.setDnsTime(l4);
    urlresults.setConnectTime(l5);
    urlresults.setResponseTime(l6);
    urlresults.setDownloadTime(l7);
    urlresults.setDaysUntilCertExpires(l11);
    urlresults.setHtmlTruncatedIfNonZero(l12);
    urlresults.setErrorMessage(s2);
    if (debugURL != 0) {
        System.out.println("Status21 #############################results[0]: " + l2);
    }
    return urlresults;
}

From source file:com.concursive.connect.web.modules.wiki.utils.WikiToHTMLUtils.java

protected static boolean parseLine(WikiToHTMLContext context, Connection db, String line, StringBuffer main)
        throws Exception {
    if (!context.canAppend()) {
        return false;
    }/*from   ww w .ja v  a2s  .  c o  m*/
    boolean needsCRLF = true;
    boolean bold = false;
    boolean italic = false;
    boolean bolditalic = false;
    boolean underline = false;
    StringBuffer subject = new StringBuffer();
    StringBuffer sb = new StringBuffer();
    StringBuffer data = new StringBuffer();
    int linkL = 0;
    int linkR = 0;
    int attr = 0;
    int underlineAttr = 0;

    // parse characters
    for (int i = 0; i < line.length(); i++) {
        char c1 = line.charAt(i);
        String c = String.valueOf(c1);
        // False attr/links
        if (!"'".equals(c) && attr == 1) {
            data.append("'").append(c);
            attr = 0;
            continue;
        }
        if (!"_".equals(c) && underlineAttr == 1) {
            data.append("_").append(c);
            underlineAttr = 0;
            continue;
        }
        if (!"[".equals(c) && linkL == 1) {
            data.append("[").append(c);
            linkL = 0;
            continue;
        }
        if (!"]".equals(c) && linkR == 1) {
            data.append("]").append(c);
            linkR = 0;
            continue;
        }
        // Links
        if ("[".equals(c)) {
            ++linkL;
            continue;
        }
        if ("]".equals(c)) {
            ++linkR;
            if (linkL == 2 && linkR == 2) {
                flushData(data, sb);
                // Different type of links...
                String link = subject.toString();

                if (link.startsWith("Image:") || link.startsWith("image:")) {
                    // Image link
                    WikiImageLink wikiImageLink = new WikiImageLink(link, context.getProjectId(),
                            context.getImageList(), (i + 1 == line.length()), context.isEditMode(),
                            context.getServerUrl());
                    sb.append(wikiImageLink.getValue());
                    needsCRLF = wikiImageLink.getNeedsCRLF();
                } else if (link.startsWith("Video:") || link.startsWith("video:")) {
                    // Video link
                    WikiVideoLink wikiVideoLink = new WikiVideoLink(link, context.isEditMode(),
                            context.getServerUrl());
                    sb.append(wikiVideoLink.getValue());
                    needsCRLF = wikiVideoLink.getNeedsCRLF();
                } else {
                    // Any other kind of link
                    // Parser for inter-project wiki links
                    WikiLink wikiLink = new WikiLink(link, context.getProjectId());
                    // Place a wiki link
                    String cssClass = "wikiLink";
                    String url = null;
                    if (WikiLink.REFERENCE.equals(wikiLink.getStatus())) {
                        sb.append("<a class=\"wikiLink external\" target=\"_blank\" href=\""
                                + wikiLink.getEntity() + "\">" + StringUtils.toHtml(wikiLink.getName())
                                + "</a>");
                    } else {
                        LOG.debug("Found wiki link: " + wikiLink.toString());
                        Project thisProject = null;
                        if (wikiLink.getProjectId() > -1) {
                            LOG.debug("Loading wiki link project: " + wikiLink.getProjectId());
                            thisProject = ProjectUtils.loadProject(wikiLink.getProjectId());
                        } else {
                            thisProject = new Project();
                        }
                        // Links...
                        if ("profile".equalsIgnoreCase(wikiLink.getArea())) {
                            // Links to a profile page
                            cssClass = "wikiLink external";
                            url = context.getServerUrl() + "/show/" + thisProject.getUniqueId();
                        } else if ("badge".equalsIgnoreCase(wikiLink.getArea())) {
                            // Links to a badge
                            cssClass = "wikiLink external";
                            url = context.getServerUrl() + "/badge/" + wikiLink.getEntityId();
                        } else if ("wiki".equalsIgnoreCase(wikiLink.getArea())) {
                            // Links to another wiki page
                            if (StringUtils.hasText(wikiLink.getEntity())) {
                                url = context.getServerUrl() + "/show/" + thisProject.getUniqueId() + "/wiki/"
                                        + wikiLink.getEntityTitle();
                            } else {
                                url = context.getServerUrl() + "/show/" + thisProject.getUniqueId() + "/wiki";
                            }
                            // Check to see if this is an external wiki
                            if (context.getProjectId() > -1 && context.getProjectId() != thisProject.getId()) {
                                cssClass = "wikiLink external";
                            }
                            // Check to see if the target wiki exists to draw the wiki entry differently
                            if (!WikiList.checkExistsBySubject(db, wikiLink.getEntity(),
                                    wikiLink.getProjectId())) {
                                cssClass = "wikiLink newWiki";
                                // If user has access to edit, then use an edit link
                                if (hasUserProjectAccess(db, context.getUserId(), wikiLink.getProjectId(),
                                        wikiLink.getArea(), "edit")) {
                                    String wikiSubject = StringUtils.hasText(wikiLink.getEntity())
                                            ? "/" + wikiLink.getEntityTitle()
                                            : "";
                                    url = context.getServerUrl() + "/modify/" + thisProject.getUniqueId()
                                            + "/wiki" + wikiSubject;
                                }
                            }
                        } else {
                            cssClass = "wikiLink external";
                            url = context.getServerUrl() + "/show/" + thisProject.getUniqueId() + "/"
                                    + wikiLink.getArea().toLowerCase()
                                    + (StringUtils.hasText(wikiLink.getEntity()) ? "/" + wikiLink.getEntityId()
                                            : "");
                        }
                        // Display the resulting URL
                        if (wikiLink.getProjectId() == -1 || wikiLink.getProjectId() == context.getProjectId()
                                || hasUserProjectAccess(db, context.getUserId(), wikiLink.getProjectId(),
                                        wikiLink.getPermissionArea(), "view")) {
                            String rel = "";
                            if ("app".equals(wikiLink.getArea())) {
                                // open apps in a panel
                                rel = " rel=\"shadowbox\"";
                            }
                            sb.append("<a class=\"" + cssClass + "\" href=\"" + url + "\"" + rel + ">"
                                    + StringUtils.toHtml(wikiLink.getName().trim()) + "</a>");
                            if (wikiLink.getName().endsWith(" ")) {
                                sb.append(" ");
                            }
                        } else {
                            LOG.debug("USING DENIED LINK");
                            cssClass = "wikiLink denied";
                            sb.append("<a class=\"" + cssClass + "\" href=\"#\" onmouseover=\"window.status='"
                                    + StringUtils.jsStringEscape(url) + ";'\">"
                                    + StringUtils.toHtml(wikiLink.getName()) + "</a>");
                        }
                    }
                }
                subject.setLength(0);
                linkL = 0;
                linkR = 0;
            }
            continue;
        }
        if (!"[".equals(c) && linkL == 2 && !"]".equals(c)) {
            subject.append(c);
            continue;
        }
        // Attribute properties
        // TODO: Handle when there are more than 5 '''''
        if ("'".equals(c)) {
            ++attr;
            continue;
        }
        if ("_".equals(c)) {
            ++underlineAttr;
            continue;
        }
        if (!"_".equals(c) && underlineAttr == 2) {
            if (!underline) {
                flushData(data, sb);
                sb.append("<span style=\"text-decoration: underline;\">");
                data.append(c);
                underline = true;
            } else {
                flushData(data, sb);
                sb.append("</span>");
                data.append(c);
                underline = false;
            }
            underlineAttr = 0;
            continue;

        }
        if (!"'".equals(c) && attr > 1) {
            if (attr == 2) {
                if (!italic) {
                    flushData(data, sb);
                    sb.append("<em>");
                    data.append(c);
                    italic = true;
                } else {
                    flushData(data, sb);
                    sb.append("</em>");
                    data.append(c);
                    italic = false;
                }
                attr = 0;
                continue;
            }
            if (attr == 3) {
                if (!bold) {
                    flushData(data, sb);
                    sb.append("<strong>");
                    data.append(c);
                    bold = true;
                } else {
                    flushData(data, sb);
                    sb.append("</strong>");
                    data.append(c);
                    bold = false;
                }
                attr = 0;
                continue;
            }
            if (attr >= 5) {
                if (!bolditalic) {
                    flushData(data, sb);
                    sb.append("<strong><em>");
                    data.append(c);
                    bolditalic = true;
                } else {
                    flushData(data, sb);
                    sb.append("</em></strong>");
                    data.append(c);
                    bolditalic = false;
                }
                attr = attr - 5;
                // TODO: if attr > 0 then need to set bold/itals cout
                continue;
            }
        }
        data.append(c);
    }
    for (int x = 0; x < linkR; x++) {
        data.append("]");
    }
    for (int x = 0; x < linkL; x++) {
        data.append("[");
    }
    if (attr == 1) {
        data.append("'");
    }
    if (underlineAttr == 1) {
        data.append("_");
    }
    flushData(data, sb);
    if (italic) {
        sb.append("</em>");
    }
    if (underline) {
        sb.append("</span>");
    }
    if (bold) {
        sb.append("</strong>");
    }
    if (bolditalic) {
        sb.append("</em></strong>");
    }
    String newLine = sb.toString();

    // handle strikethrough
    newLine = StringUtils.replace(newLine, StringUtils.toHtmlValue("<s>"),
            "<span style=\"text-decoration: line-through;\">");
    newLine = StringUtils.replace(newLine, StringUtils.toHtmlValue("</s>"), "</span>");
    newLine = StringUtils.replace(newLine, "\n", "<br />");
    if (" ".equals(newLine)) {
        newLine = "&nbsp;";
    }
    main.append(newLine);
    return needsCRLF;
}

From source file:org.exoplatform.forum.service.impl.JCRDataStorage.java

public List<Watch> getWatchByUser(String userId) throws Exception {
    SessionProvider sProvider = CommonUtils.createSystemProvider();
    List<Watch> listWatches = new ArrayList<Watch>();
    try {/*from w w w .  j a  v a 2s.  c o  m*/
        Node categoryHome = getCategoryHome(sProvider);
        StringBuffer rootPath = new StringBuffer(categoryHome.getPath());
        QueryManager qm = categoryHome.getSession().getWorkspace().getQueryManager();
        StringBuffer queryString = new StringBuffer();
        queryString.append(JCR_ROOT).append(rootPath.toString()).append("//element(*,")
                .append(EXO_FORUM_WATCHING).append(")[(@").append(EXO_USER_WATCHING).append("='").append(userId)
                .append("') or (@").append(EXO_RSS_WATCHING).append("='").append(userId).append("')]");

        Query query = qm.createQuery(queryString.toString(), Query.XPATH);
        QueryResult result = query.execute();
        NodeIterator iterator = result.getNodes();
        Watch watch;
        Node node;
        List<String> users;
        List<String> RSSUsers;
        String emails[];
        String path;
        StringBuffer pathName = new StringBuffer();
        String typeNode;
        PropertyReader reader;
        while (iterator.hasNext()) {
            node = iterator.nextNode();
            reader = new PropertyReader(node);
            users = reader.list(EXO_USER_WATCHING, new ArrayList<String>());
            emails = reader.strings(EXO_EMAIL_WATCHING, new String[] {});
            RSSUsers = reader.list(EXO_RSS_WATCHING, new ArrayList<String>());
            rootPath.setLength(0);
            rootPath.append(categoryHome.getPath());
            path = node.getPath();
            pathName.setLength(0);
            if (node.isNodeType(Utils.TYPE_CATEGORY)) {
                typeNode = Utils.TYPE_CATEGORY;
            } else if (node.isNodeType(Utils.TYPE_FORUM)) {
                typeNode = Utils.TYPE_FORUM;
            } else {
                typeNode = Utils.TYPE_TOPIC;
            }

            for (String str : (path.replace(rootPath.toString() + "/", "")).split("/")) {
                rootPath.append("/");
                rootPath.append(str);
                if (!Utils.isEmpty(pathName.toString())) {
                    pathName.append(" > ");
                }
                pathName.append(((Node) categoryHome.getSession().getItem(rootPath.toString()))
                        .getProperty(EXO_NAME).getString());
            }
            watch = new Watch();
            watch.setId(node.getName());
            watch.setNodePath(path);
            watch.setUserId(userId);
            watch.setPath(pathName.toString());
            watch.setTypeNode(typeNode);
            if (users.contains(userId)) {
                watch.setEmail(emails[users.indexOf(userId)]);
                watch.setIsAddWatchByEmail(true);
            } else {
                watch.setIsAddWatchByEmail(false);
            }
            watch.setIsAddWatchByRSS(RSSUsers.contains(userId));
            listWatches.add(watch);
        }
        return listWatches;
    } catch (Exception e) {
        return listWatches;
    }
}

From source file:com.ikanow.infinit.e.api.knowledge.federated.SimpleFederatedQueryEngine.java

public BasicDBObject createDocFromJson(BasicDBList jsonList, String url, FederatedRequest request,
        SourceFederatedQueryConfigPojo endpointInfo) {
    BasicDBObject doc = null; // (don't create unless needed)
    BasicDBList ents = null;/*from w  w w .java 2s  .co  m*/
    StringBuffer entVals = null;
    HashSet<String> entDedup = null;

    if (_testMode) { // In test mode, need to return the JSON even if no entities are specified 
        doc = new BasicDBObject();
    }
    if (null != endpointInfo.docConversionMap) {
        for (Map.Entry<String, String> docInfo : endpointInfo.docConversionMap.entrySet()) {
            for (Object jsonObj : jsonList) {
                BasicDBObject json = (BasicDBObject) jsonObj;
                try {
                    String key = docInfo.getKey();
                    // (allow user to not prepend array: if they don't want to)
                    if ((1 == json.size()) && json.containsKey((Object) "array")) {
                        if (!key.startsWith("array:") && !key.startsWith(":array") && !key.startsWith("$:array")
                                && !key.startsWith("::") && !key.startsWith("$::")) {
                            if (key.startsWith(":")) { // jpath
                                key = ":array" + key;
                            } else if (key.startsWith("$:")) { // jpath
                                key = "$:array" + key.substring(1);
                            } else {
                                key = "array:" + key;
                            }
                        }
                    } //TESTED (by hand)
                    if (key.startsWith(":")) { // jpath
                        key = "$" + key;
                    }
                    // NOTE: *not* org.json.JSONArray
                    JSONArray candidateEntities = null;
                    if (key.startsWith("$")) {
                        JSONArray candidateEntities_tmp = JsonPath.read(json.toString(), key.replace(':', '.'));
                        if (null != candidateEntities_tmp) {
                            candidateEntities = new JSONArray();
                            for (Object o : candidateEntities_tmp) {
                                if (o instanceof String) {
                                    candidateEntities.add(o);
                                } else if (o instanceof JSONArray) {
                                    candidateEntities.addAll((JSONArray) o);
                                }
                            } //TESTED (displayUrl vs entities, 3.2)
                        }
                        //DEBUG
                        //System.out.println(candidateEntities);

                    } //(TESTED (permutations above by hand))
                    else {
                        String s = (String) MongoDbUtil.getProperty(json, key.replace(':', '.'));
                        if (null != s) {
                            candidateEntities = new JSONArray();
                            candidateEntities.add(s);
                        }
                    } //TESTED (3.1)                                 

                    if (null != candidateEntities)
                        for (int i = 0; i < candidateEntities.size(); ++i) {
                            Object o = candidateEntities.get(i);
                            if (!(o instanceof String)) {
                                continue;
                            }
                            String s = o.toString();
                            if (null == doc) {
                                doc = new BasicDBObject();
                                //(various fields added below)
                            }
                            if (docInfo.getValue().equalsIgnoreCase(DocumentPojo.displayUrl_)) {
                                doc.put(DocumentPojo.displayUrl_, s);
                            } //TESTED (3.1, 4.*)
                            else { // Entities!
                                if (null == ents) {
                                    ents = new BasicDBList();
                                }
                                String index = s.toLowerCase() + "/" + docInfo.getValue().toLowerCase();

                                if (null == entDedup) {
                                    entDedup = new HashSet<String>();
                                } else if (entDedup.contains(index)) { // Entity deduplication
                                    continue;
                                } //TESTED (3.2)
                                entDedup.add(index);

                                if (null == entVals) {
                                    entVals = new StringBuffer(": ");
                                } else {
                                    entVals.append(", ");
                                }
                                entVals.append(s);

                                String dimension = null;
                                if (null != endpointInfo.typeToDimensionMap) {
                                    try {
                                        dimension = EntityPojo.Dimension
                                                .valueOf(
                                                        endpointInfo.typeToDimensionMap.get(docInfo.getValue()))
                                                .toString();
                                    } catch (Exception e) {
                                    }
                                }
                                if (null == dimension) {
                                    dimension = EntityPojo.Dimension.What.toString();
                                } //TESTED (by hand)

                                // (alternative to "made up" values would be to go looking in the existing docs/ents?)
                                // (we'll try to avoid that for now...)
                                BasicDBObject ent = new BasicDBObject();
                                ent.put(EntityPojo.disambiguated_name_, s);
                                ent.put(EntityPojo.type_, docInfo.getValue());
                                ent.put(EntityPojo.dimension_, dimension);
                                ent.put(EntityPojo.relevance_, 1.0);
                                ent.put(EntityPojo.doccount_, 1L); // (ie relative to this query)
                                ent.put(EntityPojo.averageFreq_, 1.0);
                                ent.put(EntityPojo.datasetSignificance_, 10.0); // (ie relative to this query)
                                ent.put(EntityPojo.significance_, 10.0); // (ie relative to this query)
                                ent.put(EntityPojo.frequency_, 1.0);
                                ent.put(EntityPojo.index_, index);
                                ent.put(EntityPojo.queryCoverage_, 100.0); // (ie relative to this query)
                                ent.put(EntityPojo.totalfrequency_, 1.0); // (ie relative to this query)
                                ents.add(ent);
                            } //TESTED (3.1, 4.*)
                        }
                } catch (Exception e) {
                    //(do nothing? null or the wrong type)
                    //e.printStackTrace();
                }
            } //end loop over various JSON objects retrieved
        } //(End loop over doc conversion elements)
    } //TESTED (3.*, 4.*)

    if ((null == ents) && !_testMode) { // don't return unless there are any entities
        return null;
    } else if (null != doc) {
        // Insert mandatory fields:
        // (Note the query format is a little bit different, the following fields are converted to arrays:
        //  sourceKey, source, communityId, mediaType)
        doc.put(DocumentPojo._id_, new ObjectId());
        doc.put(DocumentPojo.url_, url);
        doc.put(DocumentPojo.created_, new Date());
        doc.put(DocumentPojo.modified_, new Date());
        doc.put(DocumentPojo.publishedDate_, new Date());
        doc.put(DocumentPojo.sourceKey_, endpointInfo.parentSource.getKey());
        doc.put(DocumentPojo.source_, endpointInfo.parentSource.getTitle());
        doc.put(DocumentPojo.communityId_, new ObjectId(request.communityIdStrs[0]));
        doc.put(DocumentPojo.mediaType_, endpointInfo.parentSource.getMediaType());
        doc.put(DocumentPojo.metadata_, new BasicDBObject("json", jsonList.toArray()));

        if ((null != entVals) && (entVals.length() > 165)) { // (arbitrary length)
            entVals.setLength(165);
            entVals.append("...");
        }
        doc.put(DocumentPojo.title_, new StringBuffer(endpointInfo.titlePrefix).append(": ")
                .append(request.requestParameter).append(entVals).toString());
        doc.put(DocumentPojo.entities_, ents);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        JsonParser jp = new JsonParser();
        JsonElement je = jp.parse(jsonList.toString());
        doc.put(DocumentPojo.description_, gson.toJson(je)); // (prettified JSON)            
    } //TESTED (3.*, 4.*)

    return doc;
}

From source file:com.jhh.hdb.sqlparser.MySemanticAnalyzer.java

private ASTNode findCTEFromName(QB qb, String cteName) {

    /*/*  www.j  a v  a 2  s.  c om*/
     * When saving a view definition all table references in the AST are qualified; including CTE references.
     * Where as CTE definitions have no DB qualifier; so we strip out the DB qualifier before searching in
     * <code>aliasToCTEs</code> map.
     */
    String currDB = SessionState.get().getCurrentDatabase();
    if (currDB != null && cteName.startsWith(currDB) && cteName.length() > currDB.length()
            && cteName.charAt(currDB.length()) == '.') {
        cteName = cteName.substring(currDB.length() + 1);
    }

    StringBuffer qId = new StringBuffer();
    if (qb.getId() != null) {
        qId.append(qb.getId());
    }

    while (qId.length() > 0) {
        String nm = qId + ":" + cteName;
        if (aliasToCTEs.containsKey(nm)) {
            return aliasToCTEs.get(nm);
        }
        int lastIndex = qId.lastIndexOf(":");
        lastIndex = lastIndex < 0 ? 0 : lastIndex;
        qId.setLength(lastIndex);
    }
    return aliasToCTEs.get(cteName);
}

From source file:com.dragonflow.StandardMonitor.URLOriginalMonitor.java

/**
 * //from   ww  w . j  av  a  2s .  co  m
 */
private static long[] checkInternalURL(SocketSession socketsession, String s, String s1, String s2, String s3,
        String s4, String s5, String s6, String s7, Array array, String s8, String s9, String s10,
        StringBuffer stringbuffer, long l, String s11, int i, long l1, StringBuffer stringbuffer1,
        StringBuffer stringbuffer2) {
    String s12 = "";
    if (s.lastIndexOf('#') != -1) {
        s = s.substring(0, s.lastIndexOf('#'));
    }
    if ((debugURL & kDebugRequest) != 0) {
        LogManager.log("RunMonitor", "checking URL... " + s);
    }
    int j = 0;
    if (socketsession.context.getSetting("_keepTryingForGoodStatus").length() > 0) {
        j = TextUtils.toInt(socketsession.context.getSetting("_keepTryingForGoodStatus"));
    }
    long l2 = kURLNoStatusError;
    long l3 = 0L;
    long l4 = System.currentTimeMillis();
    long l6 = 0L;
    long l7 = 0L;
    long l8 = 0L;
    long l9 = 0L;
    long l10 = 0L;
    long l11 = 0L;
    long l12 = 0L;
    boolean flag = false;
    URLInfo urlinfo = new URLInfo(s);
    String s16 = urlinfo.getProtocol();
    String s17 = urlinfo.getHost();
    int k = urlinfo.getConnectPort();
    boolean flag1 = false;
    boolean flag2 = false;
    if (SocketStream.getSSLFactory() != null) {
        flag1 = true;
        if (socketsession.context.getSetting("_sslJavaEnabled").length() > 0) {
            flag1 = true;
        } else if (socketsession.context.getSetting("_sslJavaDisabled").length() > 0) {
            flag1 = false;
        }
    }
    boolean flag3 = false;
    if (s8.startsWith(NT_CHALLENGE_RESPONSE_TAG)) {
        s8 = s8.substring(NT_CHALLENGE_RESPONSE_TAG.length());
        flag3 = true;
    }
    boolean flag4 = false;
    if (socketsession.context.getSetting("_urlMonitorUseApacheHttpClient").length() > 0) {
        flag4 = true;
    }
    SocketStream socketstream = null;
    String s18 = getUserAgent(array);
    if (s18.length() == 0) {
        s18 = socketsession.context.getSetting("_URLUserAgent");
    }
    String s19 = getSslgetOptions(array);
    if (s19.length() == 0) {
        s19 = socketsession.context.getSetting("_sslgetOptions");
    }
    String s20 = socketsession.context.getSetting("_URLMonitorProxyExceptions");
    String s21 = null;
    String s22 = "";
    String s23 = "";
    String s25 = "";
    String s26 = getContentType(array);
    String s27 = encodeParameters(array, s26);
    socketsession.addCookieParameters(array, s);
    if (stringbuffer == null) {
        stringbuffer = new StringBuffer(kURLBufferSize);
    }
    boolean flag5 = socketsession.context.getSetting("_concatURLRedirects").length() != 0;
    if (flag5 && concatBuffer == null) {
        concatBuffer = new StringBuffer(kURLBufferSize);
    }
    if (s8.length() > 0) {
        Base64Encoder base64encoder = new Base64Encoder(s8 + ":" + s9);
        s22 = "Authorization: Basic " + base64encoder.processString() + CRLF;
    }
    if (s6.length() > 0) {
        Base64Encoder base64encoder1 = new Base64Encoder(s6 + ":" + s7);
        String s24 = base64encoder1.processString();
        s25 = "Proxy-Authorization: Basic " + s24 + CRLF;
    }
    long l13 = l1 - System.currentTimeMillis();
    long l14 = l13 / 1000L;
    long l15 = -1L;
    CounterLock counterlock;

    // 617
    try {
        if (s10.length() != 0) {
            String s28 = "";
            if (s27.length() > 0) {
                s28 = HTTPRequest.encodeString(s27);
            }
            String s32;
            if (s10.indexOf("get.exe") != -1) {
                String s36 = socketsession.context.getSetting("_URLRemoteOptions");
                if (s36.length() == 0) {
                    s36 = "-ignoreErrors+-ignoreUnknownCA+-x";
                }
                if (flag3 && !s8.startsWith(NT_CHALLENGE_RESPONSE_TAG)) {
                    s8 = NT_CHALLENGE_RESPONSE_TAG + s8;
                }
                if (s18.length() > 0) {
                    if (s28.length() > 0) {
                        s28 = s28 + URLEncoder.encode("&");
                    }
                    s28 = s28 + URLEncoder.encode("User-Agent: " + s18);
                }
                if (s19.length() > 0) {
                    if (s28.length() > 0) {
                        s28 = s28 + URLEncoder.encode("&");
                    }
                    s28 = s28 + URLEncoder.encode("sslgetOptions: " + s19);
                }
                if (s11.length() > 0) {
                    String as[] = TextUtils.split(s11, CRLF);
                    for (int k1 = 0; k1 < as.length; k1++) {
                        if (s28.length() > 0) {
                            s28 = s28 + URLEncoder.encode("&");
                        }
                        if (getHeaderType(as[k1]) < 0) {
                            s28 = s28 + URLEncoder.encode("Custom-Header: ");
                        }
                        String s46 = as[k1];
                        int i3 = s46.indexOf("&");
                        if (i3 >= 0 && !s46.startsWith(URLSequenceMonitor.refererStartToken)) {
                            s46 = s46.substring(0, i3);
                        }
                        s28 = s28 + URLEncoder.encode(s46);
                    }

                }
                String s13 = socketsession.getCookieHeader(s, true);
                if (s13.length() > 0) {
                    String s40 = "";
                    if (s36.endsWith("-x")) {
                        s40 = "-x";
                        s36 = s36.substring(0, s36.length() - 2);
                    }
                    String as2[] = TextUtils.split(s13, CRLF);
                    for (int j2 = 0; j2 < as2.length; j2++) {
                        s36 = s36 + "-c+%22" + URLEncoder.encode(as2[j2]) + "%22+";
                    }

                    s36 = s36 + s40;
                }
                s32 = s10 + "?" + s36 + "+%22" + URLEncoder.encode(s) + "%22" + "+%22" + l14 + "%22" + "+%22"
                        + URLEncoder.encode(s8) + "%22" + "+%22" + URLEncoder.encode(s9) + "%22" + "+%22" + s28
                        + "%22" + "+%22" + URLEncoder.encode(s5) + "%22" + "+%22" + URLEncoder.encode(s6)
                        + "%22" + "+%22" + URLEncoder.encode(s7) + "%22";
            } else if (s10.indexOf("port.exe") != -1) {
                s32 = s10 + "&host=" + s;
            } else {
                s32 = s10 + "?host=" + s;
            }
            Array array2 = null;
            String s41 = "";
            String s43 = "";
            String s47 = "";
            String s49 = "";
            String s51 = "";
            String s54 = "";
            String s56 = "";
            if (Platform.isPortal()) {
                String s59 = HTTPUtils.getLocationIDByURL(s10);
                if (Portal.isPortalID(s59)) {
                    PortalSiteView portalsiteview = (PortalSiteView) Portal.getSiteViewForID(s59);
                    if (portalsiteview != null) {
                        s41 = portalsiteview.getProperty(PortalSiteView.pUserName);
                        s43 = portalsiteview.getProperty(PortalSiteView.pPassword);
                        s49 = portalsiteview.getProperty(PortalSiteView.pProxy);
                        s51 = portalsiteview.getProperty(PortalSiteView.pProxyUserName);
                        s54 = portalsiteview.getProperty(PortalSiteView.pProxyPassword);
                    }
                }
            }
            int k3 = i;
            if (socketsession.context == null)
                ;
            CounterLock counterlock1 = null;
            if (!socketsession.inRemoteRequest) {
                counterlock1 = getLocationLock(socketsession.context, s10, s);
            }
            try {
                long l18 = System.currentTimeMillis() + (l14 + 30L) * 1000L;
                socketsession.inRemoteRequest = true;
                long al2[] = check1URL(socketsession, s32, s1, s2, s3, s4, s49, s51, s54, array2, s41, s43, s47,
                        stringbuffer, l, s56, k3, l18, stringbuffer1, stringbuffer2);
                l2 = al2[0];
                if (j > 0) {
                    l2 = getURLStatus_ForBackupToRegularMeansOnly(socketsession, l2, s32, 0, j);
                }
                if (debugURL != 0) {
                    System.out.println("Status1: " + l2);
                }
                l3 = al2[1];
            } finally {
                socketsession.inRemoteRequest = false;
                if (counterlock1 != null) {
                    releaseLocationLock(counterlock1);
                }
            }

            String s65 = stringbuffer.toString();
            int i4 = s65.length();
            stringbuffer.setLength(0);
            int j4 = 0;
            while (j4 < i4) {
                int k4 = s65.indexOf("\r\r\n", j4);
                if (k4 < 0) {
                    stringbuffer.append(s65.substring(j4));
                    break;
                }
                stringbuffer.append(s65.substring(j4, k4) + "\r\n");
                j4 = k4 + 3;
            }

            String s68 = stringbuffer.toString();
            if (s10.indexOf("get.exe") != -1) {
                String s70 = "URLMonitorDuration: ";
                int k5 = s68.lastIndexOf(s70);
                if (k5 != -1) {
                    l3 = TextUtils.toLong(s68.substring(k5 + s70.length(), k5 + s70.length() + 10));
                }
                s70 = "URLMonitorStatus: ";
                k5 = s68.lastIndexOf(s70);
                long l21 = l2;
                if (k5 != -1) {
                    l21 = TextUtils.toLong(s68.substring(k5 + s70.length(), k5 + s70.length() + 10));
                }
                if (l21 != 200L || l2 != (long) kURLContentMatchError && l2 != (long) kURLContentErrorFound) {
                    if (k5 != -1) {
                        l2 = l21;
                        if (debugURL != 0) {
                            System.out.println("Status2 ssl: " + l2);
                        }
                    } else {
                        int j6 = s68.length();
                        if (j6 > 500) {
                            j6 = 500;
                        }
                        String s78 = s68.substring(0, j6);
                        if (j > 0) {
                            l21 = getURLStatus_ForBackupToRegularMeansOnly(socketsession, l21, s32, 0, j);
                        }
                        LogManager.log("Error", "Remote URL error, [" + HTTPUtils.getLocationIDByURL(s10) + "] "
                                + lookupStatus(l2) + ", " + s10 + ", " + s + ", detail: " + s78);
                        l2 = kURLRemoteMonitoringError;
                        if (debugURL != 0) {
                            System.out.println("Status3 kURLRemoteMonitoringError: " + l2);
                        }
                    }
                }
            }
            s68 = getHTTPContent(s68);
            int i5 = s68.lastIndexOf("\r\nURLMonitorStatus: ");
            if (i5 >= 0) {
                s68 = s68.substring(0, i5);
            }
            stringbuffer.setLength(0);
            stringbuffer.append(s68);
            l12 = stringbuffer.length();
        } else {

            if (flag3 || s16.equals("https") && !flag1 || flag4) {
                flag2 = true;
                counterlock = null;
                try {
                    if (s16.equals("https")) {
                        counterlock = getSSLGroupLock(socketsession.context);
                    }
                    if (flag4) {
                        Vector vector = new Vector();
                        if (socketsession.context.getSetting("_sslKeepAlive").length() > 0) {
                            if (s5.length() > 0) {
                                vector.add(new Header("Proxy-Connection", "Keep-Alive"));
                            } else {
                                vector.add(new Header("Connection", "Keep-Alive"));
                            }
                        }
                        vector.add(new Header("User-Agent", s18));
                        Header header = socketsession.getCookieHeader(s);
                        if (header != null) {
                            vector.add(header);
                        }
                        if (s27.length() > 0) {
                            vector.add(new Header("Content-Type", s26));
                        }
                        String s42 = "";
                        HTTPRequestSettings httprequestsettings = new HTTPRequestSettings(s, s8, s9, s42, null,
                                s5, s6, s7, vector, 1, (int) l13, (int) l13);
                        StringBuffer stringbuffer3 = new StringBuffer();
                        ApacheHttpMethod apachehttpmethod = null;
                        long l17 = System.currentTimeMillis();
                        apachehttpmethod = ApacheHttpUtils.getRequest(httprequestsettings, stringbuffer3);
                        l3 = System.currentTimeMillis() - l17;
                        l2 = apachehttpmethod.getStatusCode();
                        if (apachehttpmethod.getResponseBodyAsString() != null) {
                            stringbuffer2.append(apachehttpmethod.getResponseBodyAsString());
                        }
                        if (apachehttpmethod.getResponseBody() != null) {
                            l12 = apachehttpmethod.getResponseBody().length;
                        }
                    } else {
                        if (s5 == null) {
                            s5 = "";
                        }
                        Array array1 = new Array();
                        array1.add(Platform.getRoot() + "/tools/sslget");
                        if (s19.length() > 0) {
                            Array array3 = Platform.split(' ', s19);
                            Enumeration enumeration = array3.elements();
                            while (enumeration.hasMoreElements()) {
                                array1.add(enumeration.nextElement());
                            }
                        }
                        if (socketsession.context.getSetting("_sslKeepAlive").length() > 0) {
                            array1.add("-keepAlive");
                        }
                        boolean flag6 = socketsession.context.getSetting("_urlUnixSSL").length() == 0;
                        array1.add("-agent");
                        array1.add("\"" + s18 + "\"");
                        String s14 = s11;
                        s14 = socketsession.getCookieHeader(s, flag6) + s14;
                        if (s14.length() > 0) {
                            String as1[] = TextUtils.split(s14, CRLF);
                            for (int i2 = 0; i2 < as1.length; i2++) {
                                array1.add("-c");
                                array1.add("\"" + as1[i2] + "\"");
                            }

                        }
                        if (s27.length() > 0) {
                            array1.add("-c");
                            array1.add("\"" + CONTENT_TYPE_HEADER + s26 + "\"");
                        }
                        if (array != null) {
                            Enumeration enumeration1 = array.elements();
                            while (enumeration1.hasMoreElements()) {
                                String s44 = (String) enumeration1.nextElement();
                                if (TextUtils.startsWithIgnoreCase(s44, CUSTOM_HEADER)) {
                                    array1.add("-c");
                                    array1.add("\"" + s44.substring(CUSTOM_HEADER.length()) + "\"");
                                }
                            }
                        }
                        array1.add("-x");
                        array1.add(s);
                        array1.add("" + l14);
                        array1.add("\"" + s8 + "\"");
                        array1.add("\"" + s9 + "\"");
                        array1.add("\"" + s27 + "\"");
                        if (!hostIsProxyException(s20, s)) {
                            array1.add("\"" + s5 + "\"");
                        } else {
                            array1.add("\"\"");
                        }
                        array1.add("\"" + s6 + "\"");
                        array1.add("\"" + s7 + "\"");
                        SSLCounter++;
                        if (sslLock.current() == 0) {
                            LogManager.log("RunMonitor",
                                    "ssl block, " + ((sslLock.max - sslLock.current()) + 1) + ", " + s);
                        } else {
                            LogManager.log("RunMonitor", "ssl start, " + SSLCounter + ", " + s);
                        }
                        Array array4 = new Array();
                        l2 = sendSSLRequest(urlinfo.getHost(), socketsession, array1, sslLock, array4);
                        if (debugURL != 0) {
                            System.out.println("Status4 sendSSLRequest: " + l2);
                        }
                        SSLCounter--;
                        Enumeration enumeration2 = array4.elements();
                        int k2 = 0;
                        boolean flag7 = false;
                        l12 = -1L;
                        String s52 = "URLMonitorStatus:";
                        String s55 = "URLMonitorDuration:";
                        String s57 = "URLMonitorDNSDuration:";
                        String s60 = "URLMonitorConnectDuration:";
                        String s62 = "URLMonitorResponseDuration:";
                        String s66 = "URLMonitorDownloadDuration:";
                        boolean flag8 = true;
                        while (enumeration2.hasMoreElements()) {
                            String s69 = (String) enumeration2.nextElement();
                            if (stringbuffer2 != null) {
                                stringbuffer2.append(s69 + "\n");
                            }
                            if (++k2 > 2) {
                                int j5 = s69.indexOf(s52);
                                if (j5 >= 0) {
                                    String s71 = s69.substring(s69.length() - 10);
                                    l2 = TextUtils.toLong(s71);
                                    if (debugURL != 0) {
                                        System.out.println("Status5 statusString: " + l2);
                                    }
                                    flag8 = false;
                                }
                                int i6 = s69.indexOf(s55);
                                if (i6 >= 0) {
                                    String s72 = s69.substring(s69.length() - 10);
                                    l3 = TextUtils.toLong(s72);
                                }
                                i6 = s69.indexOf(s57);
                                if (i6 >= 0) {
                                    String s73 = s69.substring(s69.length() - 10);
                                    l6 = TextUtils.toLong(s73);
                                }
                                i6 = s69.indexOf(s60);
                                if (i6 >= 0) {
                                    String s74 = s69.substring(s69.length() - 10);
                                    l7 = TextUtils.toLong(s74);
                                }
                                i6 = s69.indexOf(s62);
                                if (i6 >= 0) {
                                    String s75 = s69.substring(s69.length() - 10);
                                    l8 = TextUtils.toLong(s75);
                                }
                                i6 = s69.indexOf(s66);
                                if (i6 >= 0) {
                                    String s76 = s69.substring(s69.length() - 10);
                                    l9 = TextUtils.toLong(s76);
                                }
                                if (k2 == 4 && s69.length() == 0) {
                                    flag7 = true;
                                }
                                if ((!flag7 || k2 % 2 != 0 || s69.length() != 0) && flag8) {
                                    stringbuffer.append(s69);
                                    stringbuffer.append('\n');
                                }
                            }
                        }
                        LogManager.log("RunMonitor", "SSL END, " + SSLCounter + ", " + l3 + "   " + s);
                        l12 = stringbuffer.length();
                    }
                    if (counterlock != null) {
                        releaseSSLGroupLock(counterlock);
                    }
                } catch (Exception exception1) {
                    if (counterlock != null) {
                        releaseSSLGroupLock(counterlock);
                    }
                    throw exception1;
                }
            } else {
                String s29 = urlinfo.getFile();
                String s33 = getHostHeader(array, s17, k, s16);
                String s37 = null;
                int j1 = -1;
                if (s5.length() != 0 && !isProxyException(s20, s17)) {
                    if (!s5.startsWith("http://")) {
                        s5 = "http://" + s5;
                    }
                    j1 = k;
                    s37 = s17;
                    URLInfo urlinfo1 = new URLInfo(s5);
                    s17 = urlinfo1.getHost();
                    k = urlinfo1.getConnectPort();
                    if (!s16.equals("https")) {
                        if (s29.equals("/") && !s.endsWith("/")) {
                            s29 = s + "/";
                        } else {
                            s29 = s;
                        }
                    }
                }
                String s45 = socketsession.getCookieHeader(s, false);
                String s48 = getRequestCommand(array);
                String s50 = socketsession.getVersion();
                String s53 = getRequestProtocol(array);
                if (s53.endsWith("/")) {
                    s53 = s53 + s50;
                }
                String s15 = s11;
                int j3 = -1;
                String s58 = s15;
                if ((j3 = s15.indexOf("&" + URLSequenceMonitor.refererEndToken)) != -1) {
                    s15 = s58.substring(0, j3);
                    s15 = s15 + s58.substring(j3 + ("&" + URLSequenceMonitor.refererEndToken).length());
                }
                if (s15.length() > 0 && !s15.endsWith(CRLF)) {
                    s15 = s15 + CRLF;
                }
                if (array != null) {
                    Enumeration enumeration3 = array.elements();
                    while (enumeration3.hasMoreElements()) {
                        String s63 = (String) enumeration3.nextElement();
                        if (TextUtils.startsWithIgnoreCase(s63, CUSTOM_HEADER)) {
                            s15 = s15 + s63.substring(CUSTOM_HEADER.length()) + CRLF;
                        }
                    }
                }
                String s61 = socketsession.getStreamEncoding();
                String s64 = "";
                if (s61.length() > 0) {
                    s64 = "Accept-charset: " + s61 + CRLF;
                }
                String s67 = s48 + " " + s29 + " " + s53 + CRLF + "User-Agent: " + s18 + CRLF + s64
                        + "Accept: */*" + CRLF + "Host: " + s33 + CRLF + s22 + s25 + s45 + s15;
                if (s27.length() == 0) {
                    s67 = s67 + CRLF;
                } else {
                    if (s61.length() > 0) {
                        s64 = "; charset=" + s61;
                    }
                    s67 = s67 + CONTENT_TYPE_HEADER + s26 + s64 + CRLF + "Content-length: " + s27.length()
                            + CRLF + CRLF + s27;
                }
                long l5 = System.currentTimeMillis();
                long l19 = l5;
                InetAddress inetaddress = InetAddress.getByName(s17);
                long l20 = System.currentTimeMillis();
                l6 = l20 - l19;
                String s77 = Platform.dottedIPString(inetaddress.getAddress()) + ":" + k;
                l19 = l20;
                socketstream = socketsession.connect(s77, inetaddress, k, s16, s37, j1, s6, s7, l1);
                l20 = System.currentTimeMillis();
                l7 = l20 - l19;
                Socket socket = socketstream.socket;
                int k6 = (int) (l1 - System.currentTimeMillis());
                if ((long) k6 <= kTimedOutValue) {
                    throw new InterruptedIOException();
                }
                if (s16.equals("https") && socketsession.context.getSetting("_urlCertDays").length() > 0) {
                    try {
                        SSLSession sslsession = ((SSLSocket) socketstream.socket).getSession();
                        X509Certificate ax509certificate[] = sslsession.getPeerCertificateChain();
                        int j7 = 0;
                        for (int k7 = 0; k7 < ax509certificate.length; k7++) {
                            X509Certificate x509certificate = ax509certificate[k7];
                            long l22 = System.currentTimeMillis();
                            long l23 = l22 + 0x5265c00L;
                            int i8 = 0;
                            while (true) {
                                Date date = new Date(l23);
                                try {
                                    x509certificate.checkValidity(date);
                                } catch (CertificateExpiredException certificateexpiredexception) {
                                    if (i8 == 0) {
                                        i8++;
                                    }
                                    break;
                                } catch (CertificateNotYetValidException certificatenotyetvalidexception) {
                                    break;
                                }
                                i8++;
                                l23 += 0x5265c00L;
                            }

                            if (i8 <= 0) {
                                continue;
                            }
                            if (j7 == 0) {
                                j7 = i8;
                                continue;
                            }
                            if (j7 > i8) {
                                j7 = i8;
                            }
                        }

                        if (j7 > 1) {
                            l15 = j7;
                        }
                        if (j7 == 1) {
                            l15 = 0L;
                        }
                    } catch (SSLPeerUnverifiedException sslpeerunverifiedexception) {
                        /* empty */
                    }
                }
                Platform.setSocketTimeout(socket, k6);
                if ((debugURL & kDebugRequest) != 0) {
                    LogManager.log("RunMonitor", "sending request... " + s67);
                }
                l19 = System.currentTimeMillis();
                socketstream.transmit(s67);
                if (stringbuffer2 != null) {
                    stringbuffer2.append(s67);
                }
                if ((debugURL & kDebugRequest) != 0) {
                    LogManager.log("RunMonitor", "request sent");
                }
                long al3[];
                if (s50.equals("1.0")) {
                    al3 = fillBuffer(socketsession, socketstream, l1, stringbuffer, l, l19);
                } else {
                    al3 = fillBufferParse(s48, socketsession, socketstream, socket, l1, stringbuffer, l, l19);
                }
                if (stringbuffer2 != null) {
                    stringbuffer2.append(stringbuffer.toString());
                }
                if (!socketstream.receivedReply || al3.length > 2 && al3[2] == -1L) {
                    socketstream.receivedEndOfStreamOnFirst = false;
                    LogManager.log("RunMonitor", "reopening connection to " + inetaddress);
                    socketstream.reconnect();
                    l19 = l20;
                    socketstream = socketsession.connect(s77, inetaddress, k, s16, s37, j1, s6, s7, l1);
                    l20 = System.currentTimeMillis();
                    l7 += l20 - l19;
                    Socket socket1 = socketstream.socket;
                    int i7 = (int) (l1 - System.currentTimeMillis());
                    if ((long) i7 <= kTimedOutValue) {
                        throw new InterruptedIOException();
                    }
                    Platform.setSocketTimeout(socket1, i7);
                    if (debugURL != 0) {
                        LogManager.log("RunMonitor", "sending request2..." + s67);
                        System.out.println("URLOriginalMonitor : sending request2..." + s67);
                    }
                    l19 = System.currentTimeMillis();
                    socketstream.transmit(s67);
                    if (stringbuffer2 != null) {
                        stringbuffer2.append(s67);
                    }
                    if (debugURL != 0) {
                        LogManager.log("RunMonitor", "request2 sent");
                        System.out.println("URLOriginalMonitor : request2 sent");
                    }
                    if (s50.equals("1.0")) {
                        al3 = fillBuffer(socketsession, socketstream, l1, stringbuffer, l, l19);
                    } else {
                        al3 = fillBufferParse(s48, socketsession, socketstream, socket1, l1, stringbuffer, l,
                                l19);
                    }
                    if (stringbuffer2 != null) {
                        stringbuffer2.append(stringbuffer.toString());
                    }
                }
                l12 = al3[0];
                l8 = al3[1];
                l20 = System.currentTimeMillis();
                l9 = l20 - l19 - l8;
                l3 = l20 - l5;
                if ((debugURL & kDebugIO) != 0) {
                    LogManager.log("RunMonitor",
                            "total, duration: " + l3 + ", clock: " + System.currentTimeMillis());
                }
            }
        }
        // 5637

        if ((debugURL & kDebugData) != 0) {
            LogManager.log("RunMonitor", "content=" + stringbuffer);
        }
        URLScannerInputStream urlscannerinputstream = new URLScannerInputStream(stringbuffer, l2);
        urlscannerinputstream.parse();
        if (urlscannerinputstream.contentLength != -1L) {
            l12 = urlscannerinputstream.contentLength;
        }
        l10 = urlscannerinputstream.lastModified;
        l11 = urlscannerinputstream.date;
        s21 = urlscannerinputstream.location;
        l2 = urlscannerinputstream.status;
        if (j > 0) {
            l2 = getURLStatus_ForBackupToRegularMeansOnly(socketsession, l2, s, 0, j);
        }
        if (debugURL != 0) {
            System.out.println("Status6 scanner.status: " + l2);
        }
        if ((debugURL & kDebugReply) != 0) {
            if (l2 != 200L) {
                LogManager.log("RunMonitor", "status=" + l2 + ", reply=" + stringbuffer);
            } else {
                LogManager.log("RunMonitor", "status=" + l2);
            }
        }
        if (socketstream != null) {
            socketstream.keepAlive = urlscannerinputstream.isKeepAlive();
        }
        if (!socketsession.inRemoteRequest) {
            flag = urlscannerinputstream.refreshRedirect;
        }
        // 5905
    } catch (UnknownHostException e) {
        l2 = kURLBadHostNameError;
        if (debugURL != 0) {
            System.out.println("Status7 kURLBadHostNameError: " + l2 + " exception: " + e.toString());
        }
    } catch (InterruptedIOException e) {
        l2 = kURLTimeoutError;
        if (debugURL != 0) {
            System.out.println("Status8 kURLTimeoutError: " + l2 + " exception: " + e.toString());
        }
    } catch (SocketException e) {
        if (debugURL != 0) {
            e.printStackTrace();
            LogManager.log("RunMonitor", "socket exception, " + e);
        }
        if (Platform.noRoute(e)) {
            l2 = kURLNoRouteToHostError;
            if (debugURL != 0) {
                System.out.println("Status9 kURLNoRouteToHostError: " + l2 + " exception: " + e.toString());
            }
        } else {
            l2 = kURLNoConnectionError;
            if (debugURL != 0) {
                System.out.println("Status10 kURLNoConnectionError: " + l2 + " exception: " + e.toString());
            }
            if (Platform.isWindows()) {
                l2 = kSSL2NotFoundError;
                if (debugURL != 0) {
                    System.out.println("Status11 kSSL2NotFoundError: " + l2 + " exception: " + e.toString());
                }
            }
        }
    } catch (Exception e) {
        String s34 = e.getClass() + ", " + e.getMessage();
        if (s34.indexOf("SSLException") != -1 && Platform.isWindows()) {
            String s38 = s17 + ":" + k;
            addSSL2Only(s38, s + ", " + s34);
            l2 = kSSL2NotFoundError;
            if (debugURL != 0) {
                System.out.println("Status12 kSSL2NotFoundError: " + l2 + " exception: " + e.toString());
            }
        } else {
            e.printStackTrace();
            l2 = kSSL2NotFoundError;
            if (debugURL != 0) {
                System.out.println("Status13 kSSL2NotFoundError: " + l2 + " exception: " + e.toString());
            }
        }
    } finally {
        if (j > 0) {
            l2 = getURLStatus_ForBackupToRegularMeansOnly(socketsession, l2, s, 0, j);
        }
        if (socketstream != null) {
            if (socketstream.receivedEndOfStreamOnFirst) {
                String s79 = s17 + ":" + k;
                addSSL2Only(s79, s + ", " + " first line null");
                l2 = kSSL2NotFoundError;
                if (debugURL != 0) {
                    System.out.println("Status14 kSSL2NotFoundError: " + l2);
                }
                socketstream.receivedEndOfStreamOnFirst = false;
            }
            socketsession.release(socketstream);
        }
    }

    // 6637

    if (200L < l2 && l2 < 300L && socketsession.context.getSetting("_urlEnable2xxStatus").length() == 0) {
        l2 = 200L;
    }
    if (debugURL != 0) {
        System.out.println("Status15 200: " + l2);
    }
    if (l2 == 301L || l2 == 302L || l2 == 303L || l2 == 307L || flag) {
        if (s21 != null) {
            s21 = TextUtils.unescapeHTML(s21);
            s21 = resolveURL(s21, new URLInfo(s1), "");
            socketsession.refererURL = s21;
            long l16 = socketsession.context.getSettingAsLong("_urlRedirectMax", DEFAULT_MAX_REDIRECTS);
            if ((long) i <= l16) {
                if ((debugURL & kDebugRequest) != 0) {
                    LogManager.log("RunMonitor", "redirect=" + s21);
                }
                socketsession.updateCookies(stringbuffer.toString(), s);
                if (stringbuffer1 != null) {
                    stringbuffer1.setLength(0);
                    stringbuffer1.append(s21);
                }
                if (flag5 && concatBuffer != null) {
                    concatBuffer.append(stringbuffer.toString());
                }
                stringbuffer.setLength(0);
                s8 = socketsession.originalUserName;
                s9 = socketsession.originalPassword;
                long al1[] = check1URL(socketsession, s21, s21, s2, s3, s4, s5, s6, s7, null, s8, s9, s2,
                        stringbuffer, l, s11, i + 1, l1, stringbuffer1, stringbuffer2);
                l2 = al1[0];
                if (j > 0) {
                    l2 = getURLStatus_ForBackupToRegularMeansOnly(socketsession, l2, s21, 0, j);
                }
                if (debugURL != 0) {
                    System.out.println("Status16 redirectResult: " + l2);
                }
                l10 = al1[3];
                l11 = al1[4];
                l3 += al1[1];
                l12 += al1[2];
                l6 += al1[5];
                l7 += al1[6];
                l8 += al1[7];
                l9 += al1[8];
            }
        }
    } else {
        if (l2 == (long) kURLNoStatusError) {
            if (socketsession.context.getSetting("_urlAllowNoStatus").length() > 0) {
                l2 = kURLok;
                if (debugURL != 0) {
                    System.out.println("Status17 kURLok: " + l2);
                }
            } else {
                LogManager.log("Error", "URL missing status: " + s);
            }
        }
        if (flag5 && concatBuffer != null) {
            concatBuffer.append(stringbuffer.toString());
            stringbuffer.setLength(0);
            stringbuffer.append(concatBuffer.toString());
            concatBuffer = null;
        }
        String s30 = I18N.UnicodeToString(stringbuffer.toString(), I18N.nullEncoding());
        if (l2 == 200L && s4.length() != 0) {
            int i1 = TextUtils.matchExpression(s30, s4);
            if (i1 != Monitor.kURLok && I18N.hasUnicode(s4)) {
                String s39 = getHTMLEncoding(s30);
                i1 = TextUtils.matchExpression(s30, I18N.UnicodeToString(s4, s39));
            }
            if (i1 == 200) {
                l2 = kURLContentErrorFound;
            }
            if (debugURL != 0) {
                System.out.println("Status18 kURLContentErrorFound: " + l2);
            }
        }
        if (l2 == 200L && s3.length() != 0) {
            l2 = TextUtils.matchExpression(s30, s3);
            if (debugURL != 0) {
                System.out.println("Status19 TextUtils.matchExpression(contents,match): " + l2);
            }
            if (l2 != (long) Monitor.kURLok && I18N.hasUnicode(s3)) {
                String s35 = getHTMLEncoding(s30);
                l2 = TextUtils.matchExpression(s30, I18N.UnicodeToString(s3, s35));
                if (debugURL != 0) {
                    System.out.println(
                            "Status20 TextUtils.matchExpression(contents, I18N.UnicodeToString(match,encoding): "
                                    + l2);
                }
            }
        }
    }
    if (socketsession.context.getSetting("_urlDetailLogEnabled").length() > 0 && s.indexOf("get.exe") == -1) {
        String s31 = "";
        if (s10.length() > 0) {
            s31 = "[" + HTTPUtils.getLocationIDByURL(s10) + "]";
        }
        LogManager.log(socketsession.context.getSetting(pURLLogName),
                s31 + s + "\t" + l2 + "\t" + l3 + "\t" + l12 + "\t" + l10 + "\t" + l11 + "\t" + l6 + "\t" + l7
                        + "\t" + l8 + "\t" + l9 + "\t" + socketsession.context.getProperty(pName) + "\t"
                        + socketsession.context.getProperty(pGroupID) + "\t"
                        + socketsession.context.getProperty(pID));
    }
    if (!socketsession.inRemoteRequest) {
        internalURLs++;
        internalURLBytes += l12;
        internalURLDuration += l3;
        if (l2 != 200L) {
            internalURLErrors++;
        }
    }
    if (s10.length() > 0) {
        internalRemoteURLs++;
        internalRemoteBytes += l12;
        internalRemoteDuration += l3;
        if (l2 != 200L) {
            internalRemoteErrors++;
        }
    } else if (s16.equals("https")) {
        if (flag2) {
            internalSecureURLs++;
            internalSecureBytes += l12;
            internalSecureDuration += l3;
            if (l2 != 200L) {
                internalSecureErrors++;
            }
        } else {
            internalSecureJavaURLs++;
            internalSecureJavaBytes += l12;
            internalSecureJavaDuration += l3;
            if (l2 != 200L) {
                internalSecureJavaErrors++;
            }
        }
    } else if (!socketsession.inRemoteRequest) {
        internalJavaURLs++;
        internalJavaBytes += l12;
        internalJavaDuration += l3;
        if (l2 != 200L) {
            internalJavaErrors++;
        }
    }
    long al[] = new long[10];
    al[0] = l2;
    if (debugURL != 0) {
        System.out.println("Status21 #############################results[0]: " + l2);
    }
    al[1] = l3;
    al[2] = l12;
    al[3] = l10;
    al[4] = l11;
    al[5] = l6;
    al[6] = l7;
    al[7] = l8;
    al[8] = l9;
    al[9] = l15;
    return al;
}

From source file:com.ikanow.infinit.e.api.knowledge.QueryHandler.java

private void testParsingCode() {

    // (these are used for logic testing below)
    List<BaseQueryBuilder> qtTerms = new LinkedList<BaseQueryBuilder>();
    List<StringBuffer> qtReadTerms = new LinkedList<StringBuffer>();

    // Various query terms

    AdvancedQueryPojo.QueryTermPojo qt0 = new AdvancedQueryPojo.QueryTermPojo();
    qt0.ftext = "ftext +test";
    AdvancedQueryPojo.QueryTermPojo qt1 = new AdvancedQueryPojo.QueryTermPojo();
    qt1.ftext = "ftext";
    qt1.etext = "etext +test";
    AdvancedQueryPojo.QueryTermPojo qt2 = new AdvancedQueryPojo.QueryTermPojo();
    qt2.etext = "etext test";
    qt2.entity = "entity:type";

    StringBuffer result = new StringBuffer();
    BaseQueryBuilder resJson = null;/*from ww w.j  a  v a2 s  . c om*/

    // "logic0":
    resJson = this.parseQueryTerm(qt0, result);
    qtTerms.add(resJson);
    qtReadTerms.add(new StringBuffer(result.toString()));
    String logic0a = new Gson().toJson(resJson);
    String logic0b = result.toString();
    String answer0a = "{\"queryString\":\"ftext +test\",\"fuzzyMinSim\":-1.0,\"boost\":-1.0,\"fuzzyPrefixLength\":-1,\"phraseSlop\":-1,\"tieBreaker\":-1.0}";
    String answer0b = "((ftext +test))";
    if (!logic0a.equals(answer0a) || !logic0b.equals(answer0b)) {
        System.out.println("Fail 0");
        System.out.println(logic0a);
        System.out.println(answer0a);
        System.out.println(logic0b);
        System.out.println(answer0b);
    }

    // "logic1":
    resJson = this.parseQueryTerm(qt1, result);
    qtTerms.add(resJson);
    qtReadTerms.add(new StringBuffer(result.toString()));
    String logic1a = new Gson().toJson(resJson);
    String logic1b = result.toString();
    String answer1a = "{\"clauses\":[{\"queryBuilder\":{\"queryString\":\"ftext\",\"fuzzyMinSim\":-1.0,\"boost\":-1.0,\"fuzzyPrefixLength\":-1,\"phraseSlop\":-1,\"tieBreaker\":-1.0},\"occur\":\"MUST\"},{\"queryBuilder\":{\"queryString\":\"etext\\\\ \\\\+test\",\"fuzzyMinSim\":-1.0,\"boost\":-1.0,\"fuzzyPrefixLength\":-1,\"phraseSlop\":-1,\"tieBreaker\":-1.0},\"occur\":\"MUST\"}],\"boost\":-1.0,\"minimumNumberShouldMatch\":-1}";
    String answer1b = "((ftext) AND (etext\\ \\+test))";
    if (!logic1a.equals(answer1a) || !logic1b.equals(answer1b)) {
        System.out.println("Fail 1");
        System.out.println(logic1a);
        System.out.println(answer1a);
        System.out.println(logic1b);
        System.out.println(answer1b);
    }

    // "logic2":
    resJson = this.parseQueryTerm(qt2, result);
    qtTerms.add(resJson);
    qtReadTerms.add(new StringBuffer(result.toString()));
    String logic2a = new Gson().toJson(resJson);
    String logic2b = result.toString();
    String answer2a = "{\"clauses\":[{\"queryBuilder\":{\"queryString\":\"etext\\\\ test\",\"fuzzyMinSim\":-1.0,\"boost\":-1.0,\"fuzzyPrefixLength\":-1,\"phraseSlop\":-1,\"tieBreaker\":-1.0},\"occur\":\"MUST\"},{\"queryBuilder\":{\"name\":\"entities.index\",\"value\":\"entity/type\",\"boost\":-1.0},\"occur\":\"MUST\"}],\"boost\":-1.0,\"minimumNumberShouldMatch\":-1}";
    String answer2b = "((etext\\ test) AND (entities.index:\"entity/type\"))";
    if (!logic2a.equals(answer2a) || !logic2b.equals(answer2b)) {
        System.out.println("Fail 2");
        System.out.println(logic2a);
        System.out.println(answer2a);
        System.out.println(logic2b);
        System.out.println(answer2b);
    }
    // (NOTE: this will fail .. the code used to have some odd logic to allow the format <entity-containing-/s>:<type>, which i just removed because lots of people want to put :s in the entity type)
    // (entityValue/entityType tested by logic3 below) 

    // Alias expansion (leave this commented out since results depend on current DB - ie check by eye)

    AdvancedQueryPojo.QueryTermPojo qt3a = new AdvancedQueryPojo.QueryTermPojo();
    qt3a.ftext = "ftext";
    qt3a.etext = "etext";
    qt3a.entity = "barack obama/person";
    qt3a.entityOpt = new AdvancedQueryPojo.QueryTermPojo.EntityOptionPojo();
    qt3a.entityOpt.expandAlias = true;
    AdvancedQueryPojo.QueryTermPojo qt3b = new AdvancedQueryPojo.QueryTermPojo();
    qt3b.entity = "new york city,new york,united states:city";
    qt3b.entityOpt = new AdvancedQueryPojo.QueryTermPojo.EntityOptionPojo();
    qt3b.entityOpt.expandAlias = true;
    AdvancedQueryPojo.QueryTermPojo qt3c = new AdvancedQueryPojo.QueryTermPojo();
    qt3c.entity = "entity3/type3";
    qt3c.entityOpt = new AdvancedQueryPojo.QueryTermPojo.EntityOptionPojo();
    qt3c.entityOpt.expandAlias = false;
    AdvancedQueryPojo.QueryTermPojo qt3d = new AdvancedQueryPojo.QueryTermPojo();
    qt3d.entity = "entity4/type4";
    qt3d.entityOpt = null;
    AdvancedQueryPojo.QueryTermPojo qt3e = new AdvancedQueryPojo.QueryTermPojo();
    qt3e.etext = "etext";
    qt3e.entityValue = "facebook inc"; // no entity type, ie shouldn't request anything  
    qt3e.entityOpt = new AdvancedQueryPojo.QueryTermPojo.EntityOptionPojo();
    qt3e.entityOpt.expandAlias = true;
    AdvancedQueryPojo.QueryTermPojo qt3f = new AdvancedQueryPojo.QueryTermPojo();
    qt3f.entityValue = "facebook inc";
    qt3f.entityType = "company";
    qt3f.entityOpt = new AdvancedQueryPojo.QueryTermPojo.EntityOptionPojo();
    qt3f.entityOpt.expandAlias = true;
    AdvancedQueryPojo.QueryTermPojo qt3g = new AdvancedQueryPojo.QueryTermPojo();
    qt3g.ftext = "No entity so should ignore the entityOpt parameters";
    qt3g.entityOpt = new AdvancedQueryPojo.QueryTermPojo.EntityOptionPojo();
    qt3g.entityOpt.expandAlias = true;
    List<AdvancedQueryPojo.QueryTermPojo> qtList = Arrays.asList(qt3a, qt3b, qt3c, qt3d, qt3e, qt3f, qt3g);
    this.handleEntityExpansion(null, qtList, null, "4c927585d591d31d7b37097a");

    String sAnswer_3_1 = "[barack obama/person, facebook inc/company, new york city,new york,united states/city]";
    String sResults_3_1 = Arrays.toString(_tmpEntityExpansionList.toArray());
    if (!sAnswer_3_1.equals(sResults_3_1)) {
        System.out.println("Fail 3.1");
        System.out.println(sAnswer_3_1);
        System.out.println(sResults_3_1);
    }
    String[] sResults_3_2 = _tmpAliasMap.get("barack obama/person").toArray(new String[0]);
    if (null != sResults_3_2) {
        //DEBUG
        //System.out.println(Arrays.toString(sResults_3_2));
        resJson = this.parseQueryTerm(qt3a, result);
        String logic3a_1 = new Gson().toJson(resJson);
        String logic3a_2 = result.toString();
        //DEBUG
        //System.out.println(logic3a_1); System.out.println(logic3a_2);
        if (!logic3a_2.contains("$aliases")) {
            System.out.println("Fail 3.2a");
            System.out.println(logic3a_1);
            System.out.println(logic3a_2);
        }
    } else {
        System.out.println("Fail 3.2a");
    }
    sResults_3_2 = _tmpAliasMap.get("facebook inc/company").toArray(new String[0]);
    if (null != sResults_3_2) {
        //DEBUG
        //System.out.println(Arrays.toString(sResults_3_2));
        resJson = this.parseQueryTerm(qt3b, result);
        String logic3b_1 = new Gson().toJson(resJson);
        String logic3b_2 = result.toString();
        //DEBUG
        //System.out.println(logic3b_1); System.out.println(logic3b_2);
        if (!logic3b_2.contains("$aliases")) {
            System.out.println("Fail 3.2b");
            System.out.println(logic3b_1);
            System.out.println(logic3b_2);
        }
    } else {
        System.out.println("Fail 3.2b");
    }
    sResults_3_2 = _tmpAliasMap.get("new york city,new york,united states/city").toArray(new String[0]);
    if (null != sResults_3_2) {
        //DEBUG
        //System.out.println(Arrays.toString(sResults_3_2));
        resJson = this.parseQueryTerm(qt3f, result);
        String logic3f_1 = new Gson().toJson(resJson);
        String logic3f_2 = result.toString();
        //DEBUG
        //System.out.println(logic3f_1); System.out.println(logic3f_2);
        if (!logic3f_2.contains("$aliases")) {
            System.out.println("Fail 3.2f");
            System.out.println(logic3f_1);
            System.out.println(logic3f_2);
        }
    } else {
        System.out.println("Fail 3.2f");
    }
    // Just check we don't normally get aliases:
    resJson = this.parseQueryTerm(qt3e, result);
    String logic3e_1 = new Gson().toJson(resJson);
    String logic3e_2 = result.toString();
    //DEBUG
    //System.out.println(logic3e_1); System.out.println(logic3e_2);
    if (logic3e_2.contains("$aliases")) {
        System.out.println("Fail 3.ef");
        System.out.println(logic3e_1);
        System.out.println(logic3e_2);
    }

    //Date debugging:
    _nNow = 1284666757165L; //Thu, 16 Sep 2010 19:52:37 GMT

    // Lots of nasty time cases, sigh
    AdvancedQueryPojo.QueryTermPojo qt5 = new AdvancedQueryPojo.QueryTermPojo();
    qt5.entity = "entity/type";
    qt5.time = new AdvancedQueryPojo.QueryTermPojo.TimeTermPojo();
    AdvancedQueryPojo.QueryTermPojo qt6 = new AdvancedQueryPojo.QueryTermPojo();
    qt6.time = new AdvancedQueryPojo.QueryTermPojo.TimeTermPojo();
    qt6.time.min = "1284666757164";
    qt6.time.max = "now";
    AdvancedQueryPojo.QueryTermPojo qt7 = new AdvancedQueryPojo.QueryTermPojo();
    qt7.time = new AdvancedQueryPojo.QueryTermPojo.TimeTermPojo();
    qt7.time.max = "1284666757164";
    AdvancedQueryPojo.QueryTermPojo qt8 = new AdvancedQueryPojo.QueryTermPojo();
    qt8.time = new AdvancedQueryPojo.QueryTermPojo.TimeTermPojo();
    qt8.time.min = "02/10/2000";
    qt8.time.max = "02.10.2000";
    AdvancedQueryPojo.QueryTermPojo qt9 = new AdvancedQueryPojo.QueryTermPojo();
    qt9.time = new AdvancedQueryPojo.QueryTermPojo.TimeTermPojo();
    qt9.time.min = "10 Feb 2000";
    qt9.time.max = "10 Feb 2000 00:00:00";
    AdvancedQueryPojo.QueryTermPojo qt9b = new AdvancedQueryPojo.QueryTermPojo();
    qt9b.time = new AdvancedQueryPojo.QueryTermPojo.TimeTermPojo();
    qt9b.time.min = "10 Feb 2000";
    qt9b.time.max = "10 Feb 2000";
    AdvancedQueryPojo.QueryTermPojo qt10 = new AdvancedQueryPojo.QueryTermPojo();
    qt10.time = new AdvancedQueryPojo.QueryTermPojo.TimeTermPojo();
    qt10.time.max = "20000210";

    // "logic5":
    resJson = this.parseQueryTerm(qt5, result);
    String logic5a = new Gson().toJson(resJson);
    String logic5b = result.toString();
    String answer5a = "{\"clauses\":[{\"queryBuilder\":{\"name\":\"entities.index\",\"value\":\"entity/type\",\"boost\":-1.0},\"occur\":\"MUST\"},{\"queryBuilder\":{\"filterBuilder\":{\"name\":\"publishedDate\",\"from\":0,\"to\":1284666757165,\"includeLower\":true,\"includeUpper\":true},\"boost\":1.0},\"occur\":\"MUST\"}],\"boost\":-1.0,\"minimumNumberShouldMatch\":-1}";
    String answer5b = "((entities.index:\"entity/type\") AND (publishedDate:[0 TO Thu Sep 16 15:52:37 EDT 2010]))";
    if (!logic5a.equals(answer5a) || !logic5b.equals(answer5b)) {
        System.out.println("Fail 5");
        System.out.println(logic5a);
        System.out.println(answer5a);
        System.out.println(logic5b);
        System.out.println(answer5b);
    }

    // "logic6":
    resJson = this.parseQueryTerm(qt6, result);
    String logic6a = new Gson().toJson(resJson);
    String logic6b = result.toString();
    String answer6a = "{\"filterBuilder\":{\"name\":\"publishedDate\",\"from\":1284666757164,\"to\":1284666757165,\"includeLower\":true,\"includeUpper\":true},\"boost\":1.0}";
    String answer6b = "((publishedDate:[Thu Sep 16 15:52:37 EDT 2010 TO Thu Sep 16 15:52:37 EDT 2010]))";
    if (!logic6a.equals(answer6a) || !logic6b.equals(answer6b)) {
        System.out.println("Fail 6");
        System.out.println(logic6a);
        System.out.println(answer6a);
        System.out.println(logic6b);
        System.out.println(answer6b);
    }

    // "logic7"
    resJson = this.parseQueryTerm(qt7, result);
    qtTerms.add(resJson);
    qtReadTerms.add(new StringBuffer(result.toString()));
    String logic7a = new Gson().toJson(resJson);
    String logic7b = result.toString();
    String answer7a = "{\"filterBuilder\":{\"name\":\"publishedDate\",\"from\":0,\"to\":1284666757164,\"includeLower\":true,\"includeUpper\":true},\"boost\":1.0}";
    String answer7b = "((publishedDate:[0 TO Thu Sep 16 15:52:37 EDT 2010]))";
    if (!logic7a.equals(answer7a) || !logic7b.equals(answer7b)) {
        System.out.println("Fail 7");
        System.out.println(logic7a);
        System.out.println(answer7a);
        System.out.println(logic7b);
        System.out.println(answer7b);
    }

    // "logic8"
    resJson = this.parseQueryTerm(qt8, result);
    String logic8a = new Gson().toJson(resJson);
    String logic8b = result.toString();
    String answer8a = "{\"filterBuilder\":{\"name\":\"publishedDate\",\"from\":950158800000,\"to\":950227199999,\"includeLower\":true,\"includeUpper\":true},\"boost\":1.0}";
    String answer8b = "((publishedDate:[Thu Feb 10 00:00:00 EST 2000 TO Thu Feb 10 18:59:59 EST 2000]))";
    if (!logic8a.equals(answer8a) || !logic8b.equals(answer8b)) {
        System.out.println("Fail 8");
        System.out.println(logic8a);
        System.out.println(answer8a);
        System.out.println(logic8b);
        System.out.println(answer8b);
    }

    // "logic9" (different to 8 because hour specified)
    resJson = this.parseQueryTerm(qt9, result);
    String logic9a = new Gson().toJson(resJson);
    String logic9b = result.toString();
    String answer9a = "{\"filterBuilder\":{\"name\":\"publishedDate\",\"from\":950158800000,\"to\":950158800000,\"includeLower\":true,\"includeUpper\":true},\"boost\":1.0}";
    String answer9b = "((publishedDate:[Thu Feb 10 00:00:00 EST 2000 TO Thu Feb 10 00:00:00 EST 2000]))";
    if (!logic9a.equals(answer9a) || !logic9b.equals(answer9b)) {
        System.out.println("Fail 9");
        System.out.println(logic9a);
        System.out.println(answer9a);
        System.out.println(logic9b);
        System.out.println(answer9b);
    }

    // "logic9b" (answer identical to 8...)
    resJson = this.parseQueryTerm(qt9b, result);
    String logic9ba = new Gson().toJson(resJson);
    String logic9bb = result.toString();
    String answer9ba = "{\"filterBuilder\":{\"name\":\"publishedDate\",\"from\":950158800000,\"to\":950227199999,\"includeLower\":true,\"includeUpper\":true},\"boost\":1.0}";
    String answer9bb = "((publishedDate:[Thu Feb 10 00:00:00 EST 2000 TO Thu Feb 10 18:59:59 EST 2000]))";
    if (!logic9ba.equals(answer9ba) || !logic9bb.equals(answer9bb)) {
        System.out.println("Fail 9b");
        System.out.println(logic9ba);
        System.out.println(answer9ba);
        System.out.println(logic9bb);
        System.out.println(answer9bb);
    }

    // "logic10" 
    resJson = this.parseQueryTerm(qt10, result);
    String logic10a = new Gson().toJson(resJson);
    String logic10b = result.toString();
    String answer10a = "{\"filterBuilder\":{\"name\":\"publishedDate\",\"from\":0,\"to\":950227199999,\"includeLower\":true,\"includeUpper\":true},\"boost\":1.0}";
    String answer10b = "((publishedDate:[0 TO Thu Feb 10 18:59:59 EST 2000]))";
    if (!logic10a.equals(answer10a) || !logic10b.equals(answer10b)) {
        System.out.println("Fail 10");
        System.out.println(logic10a);
        System.out.println(answer10a);
        System.out.println(logic10b);
        System.out.println(answer10b);
    }

    // GEO test cases:
    AdvancedQueryPojo.QueryTermPojo qt11 = new AdvancedQueryPojo.QueryTermPojo();
    qt11.geo = new AdvancedQueryPojo.QueryTermPojo.GeoTermPojo();
    qt11.geo.centerll = "40.12,-71.34";
    qt11.geo.dist = "100km";
    AdvancedQueryPojo.QueryTermPojo qt12 = new AdvancedQueryPojo.QueryTermPojo();
    qt12.geo = new AdvancedQueryPojo.QueryTermPojo.GeoTermPojo();
    qt12.geo.centerll = "(4.1,-171.34)";
    qt12.geo.dist = "100";

    AdvancedQueryPojo.QueryTermPojo qt13 = new AdvancedQueryPojo.QueryTermPojo();
    qt13.geo = new AdvancedQueryPojo.QueryTermPojo.GeoTermPojo();
    qt13.geo.minll = "(4.1,-171.34)";
    qt13.geo.maxll = "40.12,-71.34";

    AdvancedQueryPojo.QueryTermPojo qt14 = new AdvancedQueryPojo.QueryTermPojo();
    qt14.geo = new AdvancedQueryPojo.QueryTermPojo.GeoTermPojo();
    qt14.geo.minll = "4.1,-171.34";
    qt14.geo.maxll = "(40.12,-71.34)";

    // "logic11"

    resJson = this.parseQueryTerm(qt11, result);
    qtTerms.add(resJson);
    qtReadTerms.add(new StringBuffer(result.toString()));
    String logic11a = new Gson().toJson(resJson);
    String logic11b = result.toString();
    String answer11a = "{\"filterBuilder\":{\"name\":\"locs\",\"distance\":\"100km\",\"lat\":40.12,\"lon\":-71.34},\"boost\":1.0}";
    String answer11b = "((dist(*.geotag, (40.12,-71.34)) < 100km))";
    if (!logic11a.equals(answer11a) || !logic11b.equals(answer11b)) {
        System.out.println("Fail 11");
        System.out.println(logic11a);
        System.out.println(answer11a);
        System.out.println(logic11b);
        System.out.println(answer11b);
    }

    // "logic12"

    resJson = this.parseQueryTerm(qt12, result);
    String logic12a = new Gson().toJson(resJson);
    String logic12b = result.toString();
    String answer12a = "{\"filterBuilder\":{\"name\":\"locs\",\"distance\":\"100.0km\",\"lat\":4.1,\"lon\":-171.34},\"boost\":1.0}";
    String answer12b = "((dist(*.geotag, (4.1,-171.34)) < 100))";
    if (!logic12a.equals(answer12a) || !logic12b.equals(answer12b)) {
        System.out.println("Fail 12");
        System.out.println(logic12a);
        System.out.println(answer12a);
        System.out.println(logic12b);
        System.out.println(answer12b);
    }

    // "logic13"

    resJson = this.parseQueryTerm(qt13, result);
    String logic13a = new Gson().toJson(resJson);
    String logic13b = result.toString();
    String answer13a = "{\"filterBuilder\":{\"name\":\"locs\",\"topLeft\":{\"lat\":40.12,\"lon\":-171.34},\"bottomRight\":{\"lat\":4.1,\"lon\":-71.34}},\"boost\":1.0}";
    String answer13b = "((*.geotag: [(4.1,-171.34), (40.12,-71.34)]))";
    if (!logic13a.equals(answer13a) || !logic13b.equals(answer13b)) {
        System.out.println("Fail 13");
        System.out.println(logic13a);
        System.out.println(answer13a);
        System.out.println(logic13b);
        System.out.println(answer13b);
    }

    // "logic14"

    resJson = this.parseQueryTerm(qt14, result);
    String logic14a = new Gson().toJson(resJson);
    String logic14b = result.toString();
    String answer14a = "{\"filterBuilder\":{\"name\":\"locs\",\"topLeft\":{\"lat\":40.12,\"lon\":-171.34},\"bottomRight\":{\"lat\":4.1,\"lon\":-71.34}},\"boost\":1.0}";
    String answer14b = "((*.geotag: [(4.1,-171.34), (40.12,-71.34)]))";
    if (!logic14a.equals(answer14a) || !logic14b.equals(answer14b)) {
        System.out.println("Fail 14");
        System.out.println(logic14a);
        System.out.println(answer14a);
        System.out.println(logic14b);
        System.out.println(answer14b);
    }

    // Logic test code   

    // (saved 5 terms in the qtTerms and qtReadTerms: 0,1,2,7,11)

    String parser1 = "1 and 2 AND 3";
    SimpleBooleanParser.SimpleBooleanParserMTree tree = SimpleBooleanParser.parseExpression(parser1);
    String parserres = SimpleBooleanParser.traverse(tree, false);
    String parserans = "$0: & (3 2 1 ) ";
    if (!parserans.equals(parserres)) {
        System.out.println("Fail p1");
        System.out.println(parser1);
        System.out.println(parserres);
    }
    BoolQueryBuilder bq = QueryBuilders.boolQuery();
    result.setLength(0);
    this.parseLogicRecursive(tree, bq, qtTerms.toArray(new BaseQueryBuilder[6]),
            qtReadTerms.toArray(new StringBuffer[6]), result);
    String parseransQ = "((etext\\ test) AND (entities.index:\"entity/type\")) and ((ftext) AND (etext\\ \\+test)) and ((ftext +test))";
    if (!parseransQ.equals(result.toString())) {
        System.out.println("Fail p1");
        System.out.println(parseransQ);
        System.out.println(result.toString());
    }

    String parser2 = "1 or 2 and 3 or 4";
    tree = SimpleBooleanParser.parseExpression(parser2);
    parserres = SimpleBooleanParser.traverse(tree, false);
    parserans = "$0: | ($1 1 ) $1: | (4 $2 ) $2: & (3 2 ) ";
    if (!parserans.equals(parserres)) {
        System.out.println("Fail p2");
        System.out.println(parser2);
        System.out.println(parserres);
    }
    bq = QueryBuilders.boolQuery();
    result.setLength(0);
    this.parseLogicRecursive(tree, bq, qtTerms.toArray(new BaseQueryBuilder[6]),
            qtReadTerms.toArray(new StringBuffer[6]), result);
    parseransQ = "(((publishedDate:[0 TO Thu Sep 16 15:52:37 EDT 2010])) or (((etext\\ test) AND (entities.index:\"entity/type\")) and ((ftext) AND (etext\\ \\+test)))) or ((ftext +test))";
    if (!parseransQ.equals(result.toString())) {
        System.out.println("Fail p2");
        System.out.println(parseransQ);
        System.out.println(result.toString());
    }

    String parser3 = "(1 or 2) and 3 or 4";
    tree = SimpleBooleanParser.parseExpression(parser3);
    parserres = SimpleBooleanParser.traverse(tree, false);
    parserans = "$0: | (4 $1 ) $1: & (3 $2 ) $2: | (2 1 ) ";
    if (!parserans.equals(parserres)) {
        System.out.println("Fail p3");
        System.out.println(parser3);
        System.out.println(parserres);
    }
    bq = QueryBuilders.boolQuery();
    result.setLength(0);
    this.parseLogicRecursive(tree, bq, qtTerms.toArray(new BaseQueryBuilder[6]),
            qtReadTerms.toArray(new StringBuffer[6]), result);
    parseransQ = "((publishedDate:[0 TO Thu Sep 16 15:52:37 EDT 2010])) or (((etext\\ test) AND (entities.index:\"entity/type\")) and (((ftext) AND (etext\\ \\+test)) or ((ftext +test))))";
    if (!parseransQ.equals(result.toString())) {
        System.out.println("Fail p3");
        System.out.println(parseransQ);
        System.out.println(result.toString());
    }

    String parser4 = "1 or 2 and (3 or 4)";
    tree = SimpleBooleanParser.parseExpression(parser4);
    parserres = SimpleBooleanParser.traverse(tree, false);
    parserans = "$0: | ($1 1 ) $1: & ($2 2 ) $2: | (4 3 ) ";
    if (!parserans.equals(parserres)) {
        System.out.println("Fail p4");
        System.out.println(parser4);
        System.out.println(parserres);
    }
    bq = QueryBuilders.boolQuery();
    result.setLength(0);
    this.parseLogicRecursive(tree, bq, qtTerms.toArray(new BaseQueryBuilder[6]),
            qtReadTerms.toArray(new StringBuffer[6]), result);
    parseransQ = "((((publishedDate:[0 TO Thu Sep 16 15:52:37 EDT 2010])) or ((etext\\ test) AND (entities.index:\"entity/type\"))) and ((ftext) AND (etext\\ \\+test))) or ((ftext +test))";
    if (!parseransQ.equals(result.toString())) {
        System.out.println("Fail p4");
        System.out.println(parseransQ);
        System.out.println(result.toString());
    }

    String parser5 = "1 or not 2 and not (3 or 4)";
    tree = SimpleBooleanParser.parseExpression(parser5);
    parserres = SimpleBooleanParser.traverse(tree, false);
    parserans = "$0: | ($1 1 ) $1: & ($2 -2 ) $2: -| (4 3 ) ";
    if (!parserans.equals(parserres)) {
        System.out.println("Fail p5");
        System.out.println(parser5);
        System.out.println(parserres);
    }
    bq = QueryBuilders.boolQuery();
    result.setLength(0);
    this.parseLogicRecursive(tree, bq, qtTerms.toArray(new BaseQueryBuilder[6]),
            qtReadTerms.toArray(new StringBuffer[6]), result);
    parseransQ = "(not (((publishedDate:[0 TO Thu Sep 16 15:52:37 EDT 2010])) or ((etext\\ test) AND (entities.index:\"entity/type\"))) and not ((ftext) AND (etext\\ \\+test))) or ((ftext +test))";
    if (!parseransQ.equals(result.toString())) {
        System.out.println("Fail p5");
        System.out.println(parseransQ);
        System.out.println(result.toString());
    }

    String parser6 = "not (1 or (2 and (3 or 4) and 5))";
    tree = SimpleBooleanParser.parseExpression(parser6);
    parserres = SimpleBooleanParser.traverse(tree, false);
    parserans = "$0: & ($1 ) $1: -| ($2 1 ) $2: & (5 $3 2 ) $3: | (4 3 ) ";
    if (!parserans.equals(parserres)) {
        System.out.println("Fail p6");
        System.out.println(parser6);
        System.out.println(parserres);
    }
    bq = QueryBuilders.boolQuery();
    result.setLength(0);
    this.parseLogicRecursive(tree, bq, qtTerms.toArray(new BaseQueryBuilder[6]),
            qtReadTerms.toArray(new StringBuffer[6]), result);
    parseransQ = "not ((((dist(*.geotag, (40.12,-71.34)) < 100km)) and (((publishedDate:[0 TO Thu Sep 16 15:52:37 EDT 2010])) or ((etext\\ test) AND (entities.index:\"entity/type\"))) and ((ftext) AND (etext\\ \\+test))) or ((ftext +test)))";
    if (!parseransQ.equals(result.toString())) {
        System.out.println("Fail p6");
        System.out.println(parseransQ);
        System.out.println(result.toString());
    }

    String parser7 = "not (1 or (2 and (3 or 4) or 5))";
    tree = SimpleBooleanParser.parseExpression(parser7);
    parserres = SimpleBooleanParser.traverse(tree, false);
    parserans = "$0: & ($1 ) $1: -| ($2 1 ) $2: | (5 $3 ) $3: & ($4 2 ) $4: | (4 3 ) ";
    if (!parserans.equals(parserres)) {
        System.out.println("Fail p7");
        System.out.println(parser7);
        System.out.println(parserres);
    }
    bq = QueryBuilders.boolQuery();
    result.setLength(0);
    this.parseLogicRecursive(tree, bq, qtTerms.toArray(new BaseQueryBuilder[6]),
            qtReadTerms.toArray(new StringBuffer[6]), result);
    parseransQ = "not ((((dist(*.geotag, (40.12,-71.34)) < 100km)) or ((((publishedDate:[0 TO Thu Sep 16 15:52:37 EDT 2010])) or ((etext\\ test) AND (entities.index:\"entity/type\"))) and ((ftext) AND (etext\\ \\+test)))) or ((ftext +test)))";
    if (!parseransQ.equals(result.toString())) {
        System.out.println("Fail p7");
        System.out.println(parseransQ);
        System.out.println(result.toString());
    }

    // Pure parsing tests:

    String parser8 = "( 1 OR 2 ) OR ( 3 OR 4 ) AND 5";
    tree = SimpleBooleanParser.parseExpression(parser8);
    parserres = SimpleBooleanParser.traverse(tree, false);
    parserans = "$0: | ($1 $2 ) $2: | (2 1 ) $1: & (5 $3 ) $3: | (4 3 ) ";
    if (!parserans.equals(parserres)) {
        System.out.println("Fail p8");
        System.out.println(parser8);
        System.out.println(parserres);
    }

    String parser9 = "(( 1 OR 2 ) OR ( 3 OR 4 )) AND 5";
    tree = SimpleBooleanParser.parseExpression(parser9);
    parserres = SimpleBooleanParser.traverse(tree, false);
    parserans = "$0: & (5 $1 ) $1: | ($2 $3 ) $3: | (2 1 ) $2: | (4 3 ) ";
    if (!parserans.equals(parserres)) {
        System.out.println("Fail p9");
        System.out.println(parser9);
        System.out.println(parserres);
    }

    // Some proximity test code

    // First off, check out the distance code
    Double d1 = getDistance("1000");
    if (d1 != 1000.0) {
        System.out.println("1000 vs " + d1);
    }
    d1 = getDistance("10000m");
    if (d1 != 10000 * 1.150779) {
        System.out.println("1000m vs " + d1);
    }
    d1 = getDistance("1000mi");
    if (d1 != 1000 * 1.150779) {
        System.out.println("1000mi vs " + d1);
    }
    d1 = getDistance("1000km");
    if (d1 != 1000.0) {
        System.out.println("1000km vs " + d1);
    }
    d1 = getDistance("1000nm");
    if (d1 != 1000.0 * 1.852) {
        System.out.println("1000nm vs " + d1);
    }

    // Then interval test code
    Long l1 = getInterval("month", 'x');
    if (2592000000L != l1) {
        System.out.println("month vs " + l1);
    }
    l1 = getInterval("1", 'd'); // (day)
    if (86400000L != l1) {
        System.out.println("1d vs " + l1);
    }
    l1 = getInterval("10", 'm'); // (month)
    if (25920000000L != l1) {
        System.out.println("10m vs " + l1);
    }
    l1 = getInterval("1", 'y'); // (year)
    if (31536000000L != l1) {
        System.out.println("1y vs " + l1);
    }

    // OK this is the difficult bit:
    AdvancedQueryPojo.QueryScorePojo scoreParams = new AdvancedQueryPojo.QueryScorePojo();
    // Can't unit test this properly, so just rely on the "TEST CODE"
    //NO PROXIMITY SCORING
    addProximityBasedScoring(QueryBuilders.matchAllQuery(), null, scoreParams, null, false);

    // Geo only:
    scoreParams.geoProx = new AdvancedQueryPojo.QueryScorePojo.GeoProxTermPojo();
    scoreParams.geoProx.ll = "10.0,20.0";
    scoreParams.geoProx.decay = "100km";
    addProximityBasedScoring(QueryBuilders.matchAllQuery(), null, scoreParams, null, false);

    // Geo+time:
    scoreParams.geoProx.ll = "(10.0,20.0)"; // (double check this version works)
    scoreParams.geoProx.decay = "1000nm";
    scoreParams.timeProx = new AdvancedQueryPojo.QueryScorePojo.TimeProxTermPojo();
    scoreParams.timeProx.decay = "month";
    scoreParams.timeProx.time = "2000-01-01";
    addProximityBasedScoring(QueryBuilders.matchAllQuery(), null, scoreParams, null, false);

    // Time only:
    scoreParams.geoProx = null;
    scoreParams.timeProx.decay = "1m";
    addProximityBasedScoring(QueryBuilders.matchAllQuery(), null, scoreParams, null, false);
}