Example usage for org.apache.commons.lang3 StringUtils countMatches

List of usage examples for org.apache.commons.lang3 StringUtils countMatches

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils countMatches.

Prototype

public static int countMatches(final CharSequence str, final char ch) 

Source Link

Document

Counts how many times the char appears in the given string.

A null or empty ("") String input returns 0 .

 StringUtils.countMatches(null, *)       = 0 StringUtils.countMatches("", *)         = 0 StringUtils.countMatches("abba", 0)  = 0 StringUtils.countMatches("abba", 'a')   = 2 StringUtils.countMatches("abba", 'b')  = 2 StringUtils.countMatches("abba", 'x') = 0 

Usage

From source file:wsattacker.sso.openid.attacker.evaluation.strategies.LengthDeviationAndCountingMatchesStrategy.java

@Override
public ServiceProvider.User determineAuthenticatedUser(String pageSource, String url,
        ServiceProvider serviceProvider) {

    float success = 0.0f;
    float failure = 0.0f;

    AbstractStringMetric metric = new ChapmanLengthDeviation();

    for (String attackerSuccessPageSource : serviceProvider.getAttackerSuccessPageSources()) {
        float currentSuccess = metric.getSimilarity(pageSource, attackerSuccessPageSource);
        //System.out.println("success: " + currentSuccess);

        success += currentSuccess;/* w w  w. j a v a  2 s  .c  o m*/
    }

    for (String failurePageSource : serviceProvider.getFailurePageSources()) {
        float currentFailure = metric.getSimilarity(pageSource, failurePageSource);
        //System.out.println("failure: " + currentFailure);

        failure += currentFailure;
    }

    if (success > failure) {
        String victimUsername = serviceProvider.getVictimUsername();
        String attackerUsername = serviceProvider.getAttackerUsername();

        int victimMatches = StringUtils.countMatches(pageSource, StringUtils.capitalize(victimUsername));
        victimMatches += StringUtils.countMatches(pageSource, victimUsername.toLowerCase());
        int attackerMatches = StringUtils.countMatches(pageSource, StringUtils.capitalize(attackerUsername));
        attackerMatches += StringUtils.countMatches(pageSource, attackerUsername.toLowerCase());

        //System.out.println("victimMatches: " + victimMatches + ", attackerMatches: " + attackerMatches);

        if (victimMatches > attackerMatches) {
            return ServiceProvider.User.VICTIM;
        } else if (attackerMatches > victimMatches) {
            return ServiceProvider.User.ATTACKER;
        } else {
            return ServiceProvider.User.NONE;
        }
    } else {
        return User.ERROR;
    }
}

From source file:wsattacker.sso.openid.attacker.evaluation.strategies.LevenshteinAndCountingMatchesStrategy.java

@Override
public ServiceProvider.User determineAuthenticatedUser(String pageSource, String url,
        ServiceProvider serviceProvider) {

    /* First: Compare URLs */
    List<String> victimSuccessUrls = serviceProvider.getVictimSuccessUrls();
    List<String> attackerSuccessUrls = serviceProvider.getAttackerSuccessUrls();
    List<String> failureUrls = serviceProvider.getFailureUrls();

    boolean successUrlEqual = true;
    boolean failureUrlNotEqual = true;
    for (String victimSuccessUrl : victimSuccessUrls) {
        for (String attackerSuccessUrl : attackerSuccessUrls) {
            if (!victimSuccessUrl.equalsIgnoreCase(attackerSuccessUrl)) {
                successUrlEqual = false;
                break;
            }/*from   w  w w  . j av a 2 s .  c o m*/
        }

        for (String failureUrl : failureUrls) {
            if (victimSuccessUrl.equalsIgnoreCase(failureUrl)) {
                failureUrlNotEqual = false;
                break;
            }
        }
    }

    if (successUrlEqual && failureUrlNotEqual) {
        System.out.println("Determinte authenticated user by URL...");

        if (!url.equalsIgnoreCase(victimSuccessUrls.get(0))) {
            return User.ERROR;
        }
    }

    String pageBody = extractBody(pageSource);

    List<Callable<Float>> callables = new ArrayList<>();

    for (String attackerSuccessPageSource : serviceProvider.getAttackerSuccessPageSources()) {
        callables.add(new StringSimilarityCallable(pageBody, extractBody(attackerSuccessPageSource)));
    }

    List<Future<Float>> attackerSuccessResults = null;
    try {
        attackerSuccessResults = executor.invokeAll(callables);
    } catch (InterruptedException ex) {
        Logger.getLogger(ServiceProvider.class.getName()).log(Level.SEVERE, null, ex);
    }

    callables.clear();
    for (String victimSuccessPageSource : serviceProvider.getVictimSuccessPageSources()) {
        callables.add(new StringSimilarityCallable(pageBody, extractBody(victimSuccessPageSource)));
    }

    List<Future<Float>> victimSuccessResults = null;
    try {
        victimSuccessResults = executor.invokeAll(callables);
    } catch (InterruptedException ex) {
        Logger.getLogger(ServiceProvider.class.getName()).log(Level.SEVERE, null, ex);
    }

    callables.clear();
    for (String failurePageSource : serviceProvider.getFailurePageSources()) {
        callables.add(new StringSimilarityCallable(pageBody, extractBody(failurePageSource)));
    }

    List<Future<Float>> failureResults = null;
    try {
        failureResults = executor.invokeAll(callables);
    } catch (InterruptedException ex) {
        Logger.getLogger(ServiceProvider.class.getName()).log(Level.SEVERE, null, ex);
    }

    float success = 0.0f;
    float failure = 0.0f;
    float victimSuccess = 0.0f;
    float attackerSuccess = 0.0f;

    for (Future<Float> result : attackerSuccessResults) {
        try {
            float currentAttackerSuccess = result.get();
            System.out.println("currentAttackerSuccess: " + currentAttackerSuccess);
            attackerSuccess += currentAttackerSuccess;
        } catch (InterruptedException ex) {
            Logger.getLogger(ServiceProvider.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExecutionException ex) {
            Logger.getLogger(ServiceProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    for (Future<Float> result : victimSuccessResults) {
        try {
            float currentVictimSuccess = result.get();
            System.out.println("currentVictimSuccess: " + currentVictimSuccess);
            victimSuccess += currentVictimSuccess;
        } catch (InterruptedException ex) {
            Logger.getLogger(ServiceProvider.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExecutionException ex) {
            Logger.getLogger(ServiceProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    for (Future<Float> result : failureResults) {
        try {
            float currentFailure = result.get();
            System.out.println("currentFailure: " + currentFailure);

            failure += currentFailure;
        } catch (InterruptedException ex) {
            Logger.getLogger(ServiceProvider.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExecutionException ex) {
            Logger.getLogger(ServiceProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    success = victimSuccess < attackerSuccess ? (victimSuccess / victimSuccessResults.size())
            : (attackerSuccess / attackerSuccessResults.size());
    failure /= failureResults.size();

    if (success < failure) {
        String victimUsername = serviceProvider.getVictimUsername();
        String attackerUsername = serviceProvider.getAttackerUsername();

        int victimMatches = StringUtils.countMatches(pageSource, StringUtils.capitalize(victimUsername));
        victimMatches += StringUtils.countMatches(pageSource, victimUsername.toLowerCase());
        int attackerMatches = StringUtils.countMatches(pageSource, StringUtils.capitalize(attackerUsername));
        attackerMatches += StringUtils.countMatches(pageSource, attackerUsername.toLowerCase());

        System.out.println("victimMatches: " + victimMatches + ", attackerMatches: " + attackerMatches);

        if (victimMatches > attackerMatches) {
            System.out.println("authenticated user: VICTIM");
            return ServiceProvider.User.VICTIM;
        } else if (attackerMatches > victimMatches) {
            System.out.println("authenticated user: ATTACKER");
            return ServiceProvider.User.ATTACKER;
        } else {
            System.out.println("authenticated user: NONE");
            return ServiceProvider.User.NONE;
        }
    } else {
        System.out.println("success: " + success + ", failure: " + failure);
        System.out.println("authenticated user: ERROR");
        return ServiceProvider.User.ERROR;
    }
}

From source file:yatze.FXMLDocumentController.java

@FXML
private void ones(ActionEvent event) {
    onesButton.setDisable(true);//from   w w  w.  ja  v a2  s . c  om
    String diceString = "";
    for (int i : dice) {
        diceString = diceString + Integer.toString(i);
    }
    int ones2 = StringUtils.countMatches(diceString, "1");
    hetkeSumma = 1 * ones2;
    kogusumma = kogusumma + hetkeSumma;
    numbSumma = numbSumma + hetkeSumma;
    ones.setText(Integer.toString(hetkeSumma));
    resetRolls();
}

From source file:yatze.FXMLDocumentController.java

@FXML
private void twos(ActionEvent event) {
    twosButton.setDisable(true);//w w w .  j av a2s  .c  o m
    String diceString = "";
    for (int i : dice) {
        diceString = diceString + Integer.toString(i);
    }
    int twos2 = StringUtils.countMatches(diceString, "2");
    hetkeSumma = 2 * twos2;
    kogusumma = kogusumma + hetkeSumma;
    numbSumma = numbSumma + hetkeSumma;
    twos.setText(Integer.toString(hetkeSumma));
    resetRolls();
}

From source file:yatze.FXMLDocumentController.java

@FXML
private void threes(ActionEvent event) {
    threesButton.setDisable(true);//w w w  . j a v  a 2 s.c  o  m
    String diceString = "";
    for (int i : dice) {
        diceString = diceString + Integer.toString(i);
    }
    int threes2 = StringUtils.countMatches(diceString, "3");
    hetkeSumma = 3 * threes2;
    kogusumma = kogusumma + hetkeSumma;
    numbSumma = numbSumma + hetkeSumma;
    threes.setText(Integer.toString(hetkeSumma));
    resetRolls();
}

From source file:yatze.FXMLDocumentController.java

@FXML
private void fours(ActionEvent event) {
    foursButton.setDisable(true);/*from  w w w. j  a  v a 2  s.co  m*/
    String diceString = "";
    for (int i : dice) {
        diceString = diceString + Integer.toString(i);
    }
    int fours2 = StringUtils.countMatches(diceString, "4");
    hetkeSumma = 4 * fours2;
    kogusumma = kogusumma + hetkeSumma;
    numbSumma = numbSumma + hetkeSumma;
    fours.setText(Integer.toString(hetkeSumma));
    resetRolls();
}

From source file:yatze.FXMLDocumentController.java

@FXML
private void fives(ActionEvent event) {
    fivesButton.setDisable(true);/* ww  w.j  a  va  2s.  c o m*/
    String diceString = "";
    for (int i : dice) {
        diceString = diceString + Integer.toString(i);
    }
    int fives2 = StringUtils.countMatches(diceString, "5");
    hetkeSumma = 5 * fives2;
    kogusumma = kogusumma + hetkeSumma;
    numbSumma = numbSumma + hetkeSumma;
    fives.setText(Integer.toString(hetkeSumma));
    resetRolls();
}

From source file:yatze.FXMLDocumentController.java

@FXML
private void sixes(ActionEvent event) {
    String diceString = "";
    for (int i : dice) {
        diceString = diceString + Integer.toString(i);
    }/*from   w  ww  .jav a 2 s . c  o  m*/
    int sixes2 = StringUtils.countMatches(diceString, "6");
    hetkeSumma = 6 * sixes2;
    kogusumma = kogusumma + hetkeSumma;
    numbSumma = numbSumma + hetkeSumma;
    sixes.setText(Integer.toString(hetkeSumma));
    sixesButton.setDisable(true);
    resetRolls();
}

From source file:yatze.FXMLDocumentController.java

@FXML
private void onePair(ActionEvent event) {
    onePairButton.setDisable(true);/*from w  ww . ja  v a 2s .  c o  m*/
    String diceString = "";
    for (int i : dice) {
        diceString = diceString + Integer.toString(i);
    }
    int ones2 = StringUtils.countMatches(diceString, "1");
    int twos2 = StringUtils.countMatches(diceString, "2");
    int threes2 = StringUtils.countMatches(diceString, "3");
    int fours2 = StringUtils.countMatches(diceString, "4");
    int fives2 = StringUtils.countMatches(diceString, "5");
    int sixes2 = StringUtils.countMatches(diceString, "6");
    if (ones2 == 3 || twos2 == 3 || threes2 == 3 || fours2 == 3 || fives2 == 3 || sixes2 == 3) {
        kogusumma = kogusumma + hetkeSumma;
        kolmPaar.setText(Integer.toString(hetkeSumma));
    }
    resetRolls();
}

From source file:yatze.FXMLDocumentController.java

@FXML
private void twoPair(ActionEvent event) {
    String diceString = "";
    twoPairButton.setDisable(true);//from  w  w  w  .ja v  a 2 s  .c o  m
    for (int i : dice) {
        diceString = diceString + Integer.toString(i);
    }
    int ones2 = StringUtils.countMatches(diceString, "1");
    int twos2 = StringUtils.countMatches(diceString, "2");
    int threes2 = StringUtils.countMatches(diceString, "3");
    int fours2 = StringUtils.countMatches(diceString, "4");
    int fives2 = StringUtils.countMatches(diceString, "5");
    int sixes2 = StringUtils.countMatches(diceString, "6");
    if (ones2 == 4 || twos2 == 4 || threes2 == 4 || fours2 == 4 || fives2 == 4 || sixes2 == 4) {
        kogusumma = kogusumma + hetkeSumma;
        neliPaar.setText(Integer.toString(hetkeSumma));
    }
    resetRolls();
}