Example usage for java.lang ClassCastException toString

List of usage examples for java.lang ClassCastException toString

Introduction

In this page you can find the example usage for java.lang ClassCastException toString.

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:servlets.module.challenge.BrokenCryptoHomeMade.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    initLists();/* w w w. j av a2s  . c o  m*/
    //Setting IpAddress To Log and taking header for original IP if forwarded from proxy
    ShepherdLogManager.setRequestIp(request.getRemoteAddr(), request.getHeader("X-Forwarded-For"));
    HttpSession ses = request.getSession(true);
    String htmlOutput = new String();
    PrintWriter out = response.getWriter();
    if (Validate.validateSession(ses)) {
        ShepherdLogManager.setRequestIp(request.getRemoteAddr(), request.getHeader("X-Forwarded-For"),
                ses.getAttribute("userName").toString());
        log.debug(levelName + " servlet accessed by: " + ses.getAttribute("userName").toString());
        Cookie tokenCookie = Validate.getToken(request.getCookies());
        Object tokenParmeter = request.getParameter("csrfToken");
        if (Validate.validateTokens(tokenCookie, tokenParmeter)) {
            //Translation Stuff
            Locale locale = new Locale(Validate.validateLanguage(request.getSession()));
            ResourceBundle bundle = ResourceBundle
                    .getBundle("i18n.servlets.challenges.insecureCryptoStorage.insecureCryptoStorage", locale);
            out.print(getServletInfo());
            try {
                if (ses.getAttribute("homemadebadanswers") == null) {
                    ses.setAttribute("homemadebadanswers", 0);
                }

                int homemadebadanswers;
                try {
                    homemadebadanswers = (int) ses.getAttribute("homemadebadanswers");
                } catch (ClassCastException e) {
                    homemadebadanswers = Integer.parseInt((String) ses.getAttribute("homemadebadanswers"));
                }
                log.debug(homemadebadanswers + "previous bad attempts");
                if (homemadebadanswers < 5) {
                    String submittedSolution = request.getParameter("theSubmission");
                    String expectedSolution = BrokenCryptoHomeMade.generateUserSolutionKeyOnly(
                            BrokenCryptoHomeMade.challenges.get(4).get(1),
                            ses.getAttribute("userName").toString());
                    if (submittedSolution.equals(expectedSolution)) {
                        log.debug("Correct Solution Submitted for 'This Challenge'. Returning Key");
                        htmlOutput = "<h2 class='title'>" + bundle.getString("result.wellDone") + "</h2>"
                                + "<p>" + bundle.getString("result.youDidIt") + "<br />"
                                + bundle.getString("result.resultKey") + " <a>"
                                + Hash.generateUserSolution(
                                        Getter.getModuleResultFromHash(getServletContext().getRealPath(""),
                                                levelHash),
                                        (String) ses.getAttribute("userName"))
                                + "</a>";
                    } else {
                        log.debug("Expected: " + expectedSolution);
                        log.debug("Got     : " + submittedSolution);
                        htmlOutput = "<h2 class='title'>"
                                + bundle.getString("insecureCyrptoStorage.homemade.badanswer") + "</h2><p>"
                                + bundle.getString("insecureCyrptoStorage.homemade.badanswer.warning") + "</p>";
                        homemadebadanswers++;
                        if (homemadebadanswers >= 5) {
                            htmlOutput += "<p>"
                                    + bundle.getString("insecureCyrptoStorage.homemade.badanswer.lockedOut")
                                    + "</p>";
                        } else {
                            htmlOutput += "<p>"
                                    + bundle.getString("insecureCyrptoStorage.homemade.badanswer.notLockedOut")
                                    + "</p>";
                            ses.setAttribute("homemadebadanswers", homemadebadanswers);
                        }
                    }
                } else {
                    htmlOutput += "<h2 class='title'>"
                            + bundle.getString("insecureCyrptoStorage.homemade.badanswer") + "</h2><p>"
                            + bundle.getString("insecureCyrptoStorage.homemade.badanswer.lockedOut") + "</p>";
                }
            } catch (Exception e) {
                log.debug("Exception: " + e.toString());
                htmlOutput += "<p>" + bundle.getString("result.failed") + "</p>";
            }
        }
    } else {
        log.error(levelName + " servlet accessed with no session");
    }
    out.write(htmlOutput);
    out.close();
}