Example usage for org.apache.shiro.web.util WebUtils getRequiredWebEnvironment

List of usage examples for org.apache.shiro.web.util WebUtils getRequiredWebEnvironment

Introduction

In this page you can find the example usage for org.apache.shiro.web.util WebUtils getRequiredWebEnvironment.

Prototype

public static WebEnvironment getRequiredWebEnvironment(ServletContext sc) throws IllegalStateException 

Source Link

Document

Find the Shiro WebEnvironment for this web application, which is typically loaded via the org.apache.shiro.web.env.EnvironmentLoaderListener .

Usage

From source file:presentation.webgui.vitroappservlet.Common.java

License:Open Source License

public static String printDDMenu(String context_App_RealPath, HttpServletRequest request) {
    StringBuilder authInfoAndButtonHTMLBld = new StringBuilder();
    // todo: if commons is refactored as singleton, we could do this only once and store it as a class member (the currentUser object)
    boolean foundWebEnvInAppContext = false;
    if (Common.getCommon().getAppContext() != null) {
        WebEnvironment webEnv = WebUtils.getRequiredWebEnvironment(Common.getCommon().getAppContext());
        WebSecurityManager webSecurityManager = webEnv.getWebSecurityManager();
        if (webSecurityManager != null) {
            SecurityUtils.setSecurityManager(webSecurityManager);
            foundWebEnvInAppContext = true;
            LOG.info("Success: Retrieved WebEnvironment from context! ");
        }/*from   w  w w  . j a  va2  s . c  om*/
    }
    //       // get the currently executing user:
    //        Subject currentUser = SecurityUtils.getSubject();
    if (!foundWebEnvInAppContext) {
        LOG.info("Unable to retrieve WebEnvironment from context! ");
        Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(
                "classpath:shiro.ini");
        org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
    }

    // A simple Shiro environment is set up
    // get the currently executing user:
    Subject currentUser = SecurityUtils.getSubject();

    // Tests with session variables (todo: remove this after verifying what works and what not -session range / expiration / cleanup)
    Session session = currentUser.getSession();
    String value = (String) session.getAttribute("someKey");
    if (value == null || value.trim().isEmpty()) {
        LOG.info("Session did not have the value stored! ");
        session.setAttribute("someKey", "aValue");
        value = (String) session.getAttribute("someKey");
    }
    if (value.equals("aValue")) {
        LOG.info("Retrieved the correct value! [" + value + "]");
    }

    authInfoAndButtonHTMLBld.append("<li id=\"loginout\">");
    Field[] list = currentUser.getClass().getDeclaredFields();
    for (Field f : list)
        LOG.info(f.getName());
    if (currentUser.isAuthenticated()) {
        String myRole = "";

        LOG.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
        if (currentUser.hasRole("vsp")) {
            LOG.info("Role: VSP!");
            myRole = "advanced";

        } else if (currentUser.hasRole("wsie")) {
            LOG.info("Role: WSI Enabler!");
            myRole = "advanced";
        } else if (currentUser.hasRole("user")) {
            LOG.info("Role: Simple User!");
            myRole = "user";
        } else {
            LOG.info("Undefined Role.");
            myRole = "Undefined";
        }
        //authInfoAndButtonHTMLBld.append("Hello ");
        //authInfoAndButtonHTMLBld.append( currentUser.getPrincipal());
        //authInfoAndButtonHTMLBld.append(" (");
        //authInfoAndButtonHTMLBld.append(myRole);
        authInfoAndButtonHTMLBld.append("<a href=\"" + request.getContextPath() + "/logout\">Logout</a>");
        //all done - log out!
        //currentUser.logout();
    } else {
        LOG.info("Not Authenticated!");
        authInfoAndButtonHTMLBld.append("<a href=\"" + request.getContextPath() + "/login.jsp\" >Login</a>");
    }
    authInfoAndButtonHTMLBld.append("</li>");

    StringBuilder strBuildToRet = new StringBuilder();
    strBuildToRet.append("");
    try {
        String menuWrapperfileContents = readFile(
                context_App_RealPath + File.separator + "topMenuActions" + File.separator + "_proDD.htm",
                "UTF-8");
        String menuUserActionsContents = "";
        if (currentUser.isAuthenticated())
            menuUserActionsContents = readFile(context_App_RealPath + File.separator + "topMenuActions"
                    + File.separator + "_proUserActions.htm", "UTF-8");

        menuWrapperfileContents = menuWrapperfileContents.replaceAll("#userRoleMenuActionsPlaceHolder#",
                menuUserActionsContents);
        // menuWrapperfileContents = menuWrapperfileContents.replaceAll("#vspRoleMenuActionsPlaceHolder#", menuVSPActionsContents);
        // menuWrapperfileContents = menuWrapperfileContents.replaceAll("#wsieRoleMenuActionsPlaceHolder#", menuWSIEActionsContents);
        //menuWrapperfileContents = menuWrapperfileContents.replaceAll("#auxMenuActionsPlaceHolder#", menuAuxActionsContents);
        // as a final step we replace the plcholder for the contextPATH info
        menuWrapperfileContents = menuWrapperfileContents.replaceAll("#plcholder#", request.getContextPath());
        strBuildToRet.append("<div class=\"navbar navbar-fixed-top\">");
        strBuildToRet.append("<div id=\"bar\" class=\"navbar-inner\">");
        strBuildToRet.append("<ul class=\"nav nav-pills\">");
        strBuildToRet.append(
                "<li id=\"dashboardLogo\"><a href=\"" + request.getContextPath() + "\">&nbsp;</a></li>");
        strBuildToRet.append("</ul>");
        strBuildToRet.append("<div class=\"container\" id=\"buttonbar\">");
        //strBuildToRet.append("<div class=\"row-fluid\" align=\"center\">");
        strBuildToRet.append("<ul class=\"nav nav-pills\">");
        strBuildToRet.append(menuWrapperfileContents);
        strBuildToRet.append("</ul>");
        strBuildToRet.append("<ul class=\"nav nav-pills pull-right\">");
        strBuildToRet.append(authInfoAndButtonHTMLBld.toString());
        strBuildToRet.append("</ul>");
        strBuildToRet.append("</div>");
        strBuildToRet.append("<div style=\"position:absolute;top:42px;right:0;\">");
        strBuildToRet.append("<a href=\"http://www.linkedin.com/groups/VITRO-4305849\">");
        strBuildToRet.append("<img src=\"" + request.getContextPath()
                + "/img/btn_cofollow_badge.png\" alt=\"Follow VITRO on LinkedIn\"></a>");
        strBuildToRet.append("</div>");
        //strBuildToRet.append("</div>") ;
        strBuildToRet.append("</div>");
        strBuildToRet.append("</div>");
    } catch (IOException ioe) {
        System.out.print(ioe.getMessage());
    }
    return strBuildToRet.toString();
}

From source file:presentation.webgui.vitroappservlet.Common.java

License:Open Source License

public static String printDDBody(String context_App_RealPath, HttpServletRequest request) {
    StringBuilder authInfoAndButtonHTMLBld = new StringBuilder();
    // todo: if commons is refactored as singleton, we could do this only once and store it as a class member (the currentUser object)
    boolean foundWebEnvInAppContext = false;
    if (Common.getCommon().getAppContext() != null) {
        WebEnvironment webEnv = WebUtils.getRequiredWebEnvironment(Common.getCommon().getAppContext());
        WebSecurityManager webSecurityManager = webEnv.getWebSecurityManager();
        if (webSecurityManager != null) {
            SecurityUtils.setSecurityManager(webSecurityManager);
            foundWebEnvInAppContext = true;
            LOG.info("Success: Retrieved WebEnvironment from context! ");
        }//from   w ww .  jav a2 s .  c  o  m
    }
    //       // get the currently executing user:
    //        Subject currentUser = SecurityUtils.getSubject();
    if (!foundWebEnvInAppContext) {
        LOG.info("Unable to retrieve WebEnvironment from context! ");
        Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(
                "classpath:shiro.ini");
        org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
    }

    // A simple Shiro environment is set up
    // get the currently executing user:
    Subject currentUser = SecurityUtils.getSubject();

    // Tests with session variables (todo: remove this after verifying what works and what not -session range / expiration / cleanup)
    Session session = currentUser.getSession();
    String value = (String) session.getAttribute("someKey");
    if (value == null || value.trim().isEmpty()) {
        LOG.info("Session did not have the value stored! ");
        session.setAttribute("someKey", "aValue");
        value = (String) session.getAttribute("someKey");
    }
    if (value.equals("aValue")) {
        LOG.info("Retrieved the correct value! [" + value + "]");
    }

    Field[] list = currentUser.getClass().getDeclaredFields();
    for (Field f : list)
        LOG.info(f.getName());
    if (currentUser.isAuthenticated()) {
        authInfoAndButtonHTMLBld.append("<div class=\"container\" style=\"padding-top: 100px;\">");
        authInfoAndButtonHTMLBld.append("</div>");
    } else {
        authInfoAndButtonHTMLBld.append("<div class=\"container\" style=\"padding-top: 100px;\">");
        authInfoAndButtonHTMLBld.append("</div>");
        authInfoAndButtonHTMLBld.append("<div id=\"notloggedin\" class=\"well\">");
        authInfoAndButtonHTMLBld.append("Login to use the VITRO functionalities!");
        authInfoAndButtonHTMLBld.append("</div>");
        // authInfoAndButtonHTMLBld.append("<div id=\"logoHome\" align=\"center\">");
        // authInfoAndButtonHTMLBld.append("<img src=" + request.getContextPath() +"/img/Vitrologo.jpg>");
        // authInfoAndButtonHTMLBld.append("</div>");
    }

    StringBuilder strBuildToRet = new StringBuilder();
    strBuildToRet.append("");

    // strBuildToRet.append("<div id=\"bar\"><table id=general_table><tr>");
    // strBuildToRet.append(menuWrapperfileContents);
    strBuildToRet.append(authInfoAndButtonHTMLBld.toString());
    // strBuildToRet.append("</tr></table></div>") ;

    return strBuildToRet.toString();
}

From source file:presentation.webgui.vitroappservlet.Common.java

License:Open Source License

public static String printSideMenu(String context_App_RealPath, HttpServletRequest request) {
    StringBuilder authInfoAndButtonHTMLBld = new StringBuilder();
    // todo: if commons is refactored as singleton, we could do this only once and store it as a class member (the currentUser object)
    boolean foundWebEnvInAppContext = false;
    if (Common.getCommon().getAppContext() != null) {
        WebEnvironment webEnv = WebUtils.getRequiredWebEnvironment(Common.getCommon().getAppContext());
        WebSecurityManager webSecurityManager = webEnv.getWebSecurityManager();
        if (webSecurityManager != null) {
            SecurityUtils.setSecurityManager(webSecurityManager);
            foundWebEnvInAppContext = true;
            LOG.info("Success: Retrieved WebEnvironment from context! ");
        }//from   w  ww  . j  a v  a  2s  .  c  o  m
    }
    //       // get the currently executing user:
    //        Subject currentUser = SecurityUtils.getSubject();
    if (!foundWebEnvInAppContext) {
        LOG.info("Unable to retrieve WebEnvironment from context! ");
        Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(
                "classpath:shiro.ini");
        org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
    }

    // A simple Shiro environment is set up
    // get the currently executing user:
    Subject currentUser = SecurityUtils.getSubject();

    // Tests with session variables (todo: remove this after verifying what works and what not -session range / expiration / cleanup)
    Session session = currentUser.getSession();
    String value = (String) session.getAttribute("someKey");
    if (value == null || value.trim().isEmpty()) {
        LOG.info("Session did not have the value stored! ");
        session.setAttribute("someKey", "aValue");
        value = (String) session.getAttribute("someKey");
    }
    if (value.equals("aValue")) {
        LOG.info("Retrieved the correct value! [" + value + "]");
    }

    Field[] list = currentUser.getClass().getDeclaredFields();
    for (Field f : list)
        LOG.info(f.getName());
    if (currentUser.isAuthenticated()) {
        String myRole = "";

        LOG.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
        if (currentUser.hasRole("vsp")) {
            LOG.info("Role: advanced!");
            myRole = "advanced";

        } else if (currentUser.hasRole("wsie")) {
            LOG.info("Role: WSI Enabler!");
            myRole = "advanced";
        } else if (currentUser.hasRole("user")) {
            LOG.info("Role: Simple User!");
            myRole = "user";
        } else {
            LOG.info("Undefined Role.");
            myRole = "Undefined";
        }

        authInfoAndButtonHTMLBld.append("<div id=\"sidebar\" class=\"sidebar-nav\">");
        authInfoAndButtonHTMLBld.append("<ul class=\"nav nav-tabs nav-stacked\">");

        //demo layout
        if (myRole.equals("user") || myRole.equals("advanced")) {
            authInfoAndButtonHTMLBld.append("<li id=\"srv-custnew\"><a href=\"" + request.getContextPath()
                    + "/roleEndUser/newservice.jsp\">New service</a></li>");
            authInfoAndButtonHTMLBld.append("<li id=\"srv-new\"><a href=\"" + request.getContextPath()
                    + "/roleEndUser/GetComposedServiceDeployListAdvanced\">Deploy services</a></li>");
            authInfoAndButtonHTMLBld.append("<li id=\"srv-list\"><a href=\"" + request.getContextPath()
                    + "/roleEndUser/GetComposedServiceListAction\">Manage services</a></li>");
        }
        //reserved for WSI enabler?
        // if (currentUser.hasRole("wsie")){
        //    authInfoAndButtonHTMLBld.append("<li id=\"sens-edit\"><a href=\"#\">Edit existing sensors</a></li>");
        //    authInfoAndButtonHTMLBld.append("<li id=\"sens-discover\"><a href=\"#\">Discover new sensors</a></li>");
        //    authInfoAndButtonHTMLBld.append("<li id=\"sens-remove\"><a href=\"#\">Remove sensors</a></li>");
        //}

        if (myRole.equals("advanced")) {
            authInfoAndButtonHTMLBld.append("<li id=\"WSIE\" class=\"dropdown all-camera-dropdown\">");
            authInfoAndButtonHTMLBld.append(
                    "<a class=\"dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">Advanced settings<b class=\"caret\"></b></a>");
            authInfoAndButtonHTMLBld.append(" <ul class=\"dropdown-menu\">");
            authInfoAndButtonHTMLBld.append("<li data-filter-camera-type=\"all\"><a href=\""
                    + request.getContextPath() + "/roleWSIE/WSIEnewIsland.jsp\">Register new Island</a></li>");
            //authInfoAndButtonHTMLBld.append("<li data-filter-camera-type=\"all\"><a href=\""+request.getContextPath()+"/roleWSIE/WSIEeditIslands.jsp\">View Islands</a></li>");
            authInfoAndButtonHTMLBld.append("<li data-filter-camera-type=\"all\"><a href=\""
                    + request.getContextPath() + "/roleVSP/VSPeditGateways.jsp\">Manage gateways</a></li>");
            authInfoAndButtonHTMLBld.append("</ul>");
            authInfoAndButtonHTMLBld.append("</li>");
        }

        authInfoAndButtonHTMLBld.append("</ul>");
        authInfoAndButtonHTMLBld.append("</div>");

        //<li class="dropdown all-camera-dropdown">
        //      <a class="dropdown-toggle" data-toggle="dropdown" href="#">Control panel<b class="caret"></b></a>
        //                <ul class="dropdown-menu">
        //                  <li data-filter-camera-type="all"><a data-toggle="tab" href="#plcholder#/help/helpcontents.jsp"">Help Topics</a></li>
        //                  <li data-filter-camera-type="all"><a data-toggle="tab" href="#plcholder#/help/aboutapp.jsp">About</a></li>
        //                </ul>
        //              </li>

    } else {

    }

    StringBuilder strBuildToRet = new StringBuilder();
    strBuildToRet.append("");

    // strBuildToRet.append("<div id=\"bar\"><table id=general_table><tr>");
    // strBuildToRet.append(menuWrapperfileContents);
    strBuildToRet.append(authInfoAndButtonHTMLBld.toString());
    // strBuildToRet.append("</tr></table></div>") ;

    return strBuildToRet.toString();
}