<%@ page import="org.jivesoftware.openfire.cluster.ClusterManager" %>
<%@ page import="org.jivesoftware.util.cache.CacheFactory" %>
<%@ page import="org.jivesoftware.openfire.container.GetAdminConsoleInfoTask" %>
<%@ page import="org.jivesoftware.util.StringUtils" %>
<%@ page import="org.jivesoftware.util.Base64" %>
<%@ page import="org.jivesoftware.openfire.XMPPServer" %>
<%@ page import="org.jivesoftware.openfire.auth.AuthToken" %>
<%
    if (!ClusterManager.isSeniorClusterMember()) {

        GetAdminConsoleInfoTask info = (GetAdminConsoleInfoTask) CacheFactory.doSynchronousClusterTask(
                new GetAdminConsoleInfoTask(), ClusterManager.getSeniorClusterMember().toByteArray());

        AuthToken authToken = (AuthToken) session.getAttribute("jive.admin.authToken");

        if (info != null && authToken != null && info.getBindInterface() != null) {
            StringBuilder sb = new StringBuilder();
            sb.append(request.isSecure() ? "https" : "http");
            sb.append("://");
            sb.append(info.getBindInterface());
            sb.append(":");
            sb.append(request.isSecure() ? info.getAdminSecurePort() : info.getAdminPort());
            sb.append("/login.jsp?login=true&username=").append(authToken.getUsername());
            sb.append("&secret=").append(StringUtils.hash(info.getAdminSecret()));
            sb.append("&nodeID=")
                    .append(Base64.encodeBytes(XMPPServer.getInstance().getNodeID().toByteArray(), Base64.URL_SAFE));
            sb.append("&url=/plugins/fastpath/workgroup-summary.jsp");

            response.sendRedirect(sb.toString());
            return;
        }
    }
%>
