Created
September 22, 2014 18:49
-
-
Save cfalzone/54efc3a30e6f385ab1a9 to your computer and use it in GitHub Desktop.
Server ID Servlet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.aquent; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.net.InetAddress; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.HttpSession; | |
import com.dotmarketing.business.APILocator; | |
import com.dotmarketing.business.CacheLocator; | |
import com.dotmarketing.business.DotCacheAdministrator; | |
import com.dotmarketing.business.UserAPI; | |
import com.dotmarketing.common.db.DotConnect; | |
import com.dotmarketing.common.model.ContentletSearch; | |
import com.dotmarketing.portlets.contentlet.business.ContentletAPI; | |
import com.dotmarketing.portlets.contentlet.model.Contentlet; | |
import com.dotmarketing.util.Logger; | |
import com.dotmarketing.util.UtilMethods; | |
import com.liferay.portal.model.User; | |
public class ServerIdServlet extends HttpServlet { | |
private static final long serialVersionUID = -3884797058995115356L; | |
private static final String PASSCODE = "*****"; | |
private ContentletAPI conAPI = APILocator.getContentletAPI(); | |
private UserAPI userAPI = APILocator.getUserAPI(); | |
private User sysUser = null; | |
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { | |
doPost(request, response); | |
} | |
@SuppressWarnings("rawtypes") | |
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { | |
response.setContentType("text/html; charset=UTF-8"); | |
response.setCharacterEncoding("UTF-8"); | |
PrintWriter out = response.getWriter(); | |
HttpSession session = request.getSession(); | |
String pass = ""; | |
if(UtilMethods.isSet(session.getAttribute("serverid_pass"))) pass = (String) session.getAttribute("serverid_pass"); | |
if(UtilMethods.isSet(request.getParameter("sidp"))) { | |
pass = request.getParameter("sidp"); | |
session.setAttribute("serverid_pass", pass); | |
} | |
String action = ""; | |
if(UtilMethods.isSet(request.getParameter("a"))) action = request.getParameter("a"); | |
if(action.equals("logout")) { | |
pass = ""; | |
session.setAttribute("serverid_pass", ""); | |
} | |
String localhostname = InetAddress.getLocalHost().getHostName(); | |
out.println("<html><head><title>ServerID Servlet</title>" + | |
"<link type='text/css' rel='stylesheet' href='//assets.aquent.com/lib/normalize/2.1.3/normalize.min.css' /> " + | |
"<link type='text/css' rel='stylesheet' href='//assets.aquent.com/lib/foundation/4.3.1-custom-aquent/css/foundation.css' /> " + | |
"<link type='text/css' rel='stylesheet' href='//assets.aquent.com/fonts/brandon-grotesque/brandon-grotesque-bold.css' /> " + | |
"<link type='text/css' rel='stylesheet' href='//assets.aquent.com/fonts/brandon-grotesque/brandon-grotesque-medium.css' /> " + | |
"<link type='text/css' rel='stylesheet' href='//assets.aquent.com/fonts/aquent-magic/current/aquent-magic.min.css' />" + | |
"</head><body><nav class='top-bar' data-topbar><ul class='title-area'><li class='name'><h1 style='color:white'>"+localhostname+"</h1></li>" + | |
"<li class='toggle-topbar menu-icon'><a href='#'><span>Menu</span></a></li>"); | |
if(pass.equals(PASSCODE)) { | |
out.println("<section class='top-bar-section'><ul class='right'>" + | |
"<li><a "+(action.equals("flush_cache") ? "class='active' " : "")+"href='?a=flush_cache'>Flush The Cache</a></li>" + | |
"<li><a "+(action.equals("health_check") ? "class='active' " : "")+"href='?a=health_check'>Health Check</a></li>" + | |
"<li><a href='?a=logout'>Logout</a></li>" + | |
"</ul></section>"); | |
} | |
out.println("</ul></nav>"); | |
boolean showLogin = true; | |
try { | |
sysUser = userAPI.getSystemUser(); | |
} catch(Exception e) { | |
Logger.error(this,"Unable to get system User", e); | |
pass = ""; | |
action = ""; | |
session.setAttribute("serverid_pass", ""); | |
out.println("<div class='row alert-box alert'>No System User Found</div>"); | |
} | |
if(action.equals("logout")) { | |
out.println("<div class='row alert-box'>You are logged out</div>"); | |
} | |
if(action.equals("health_check")) { | |
showLogin = false; | |
out.println("<div class='row'><h2>Health Check</h2></div><div class='row'>" + | |
"<table><thead><tr><th>Check</th><th>Status</th><th>Errors</th></tr></thead><tbody>"); | |
// see if we have a system User | |
if(sysUser == null) out.println("<tr><th>System User</th><td><span class='alert radius label'>FAIL</span></td><td>See Log</td><tr>"); | |
else out.println("<tr><th>System User</th><td><span class='success radius label'>PASS</span></td><td> </td><tr>"); | |
// This is ID of our assets host: | |
String query = "+identifier:23836f6d-6a92-446f-b147-29e4724eedd8"; | |
// Index Check | |
boolean indexCheck = false; | |
String indexErrors = ""; | |
try { | |
List<ContentletSearch> cs = conAPI.searchIndex(query, 1, 0, "modDate desc", sysUser, false); | |
if(cs != null && cs.size() == 1) indexCheck = true; | |
} catch(Exception e) { | |
Logger.error(this, "Unable to indexSearch on the assets host", e); | |
indexErrors = e.getMessage(); | |
} | |
if(indexCheck) out.println("<tr><th>Index Check</th><td><span class='success radius label'>PASS</span></td><td> </td><tr>"); | |
else out.println("<tr><th>Index Check</th><td><span class='alert radius label'>FAIL</span></td><td>"+indexErrors+"</td><tr>"); | |
// Cache Check -- this goes all the way to db if the host is not in the cache, but it's what I can think of | |
boolean cacheCheck = false; | |
String cacheErrors = ""; | |
try { | |
List<Contentlet> s = conAPI.search(query, 1, 0, "modDate desc", sysUser, false); | |
if(s != null && s.size() == 1) cacheCheck = true; | |
} catch(Exception e) { | |
Logger.error(this, "Unable to search on the assets host", e); | |
cacheErrors = e.getMessage(); | |
} | |
if(cacheCheck) out.println("<tr><th>Cache Check</th><td><span class='success radius label'>PASS</span></td><td> </td><tr>"); | |
else out.println("<tr><th>Cache Check</th><td><span class='alert radius label'>FAIL</span></td><td>"+cacheErrors+"</td><tr>"); | |
// DB Check | |
String sql = "select inode from contentlet where identifier='23836f6d-6a92-446f-b147-29e4724eedd8'"; | |
boolean dbCheck = false; | |
String dbErrors = ""; | |
DotConnect dc = new DotConnect(); | |
dc.setSQL(sql); | |
try { | |
ArrayList r = dc.getResults("jdbc/dotCMSPool"); | |
if(r != null && r.size() >= 1) dbCheck = true; | |
} catch (Exception e) { | |
Logger.error(this, "Unable to db query for the assets host", e); | |
dbErrors = e.getMessage(); | |
} | |
if(dbCheck) out.println("<tr><th>DB Check</th><td><span class='success radius label'>PASS</span></td><td> </td><tr>"); | |
else out.println("<tr><th>DB Check</th><td><span class='alert radius label'>FAIL</span></td><td>"+dbErrors+"</td><tr>"); | |
out.println("</tbody></table></div>"); | |
} | |
if(pass.equals(PASSCODE)) { | |
if(action.equals("flush_cache")) { | |
DotCacheAdministrator cache = CacheLocator.getCacheAdministrator(); | |
cache.flushAll(); | |
out.println("<div class='row'><h2>Cache Flush</h2></div>" + | |
"<div class='row alert-box success'>The Cache was Flushed</div>"); | |
} else if(action.equals("")) { | |
out.println("<div class='row'><h2>Welcome</h2></div>" + | |
"<div class='row'>Use the navigation to pick a tool</div>"); | |
} | |
} else if(showLogin) { | |
out.println("<div class='row'><h2>What's the secret word?</h2></div>" + | |
((UtilMethods.isSet(pass)) ? "<div class='row alert-box alert'>That was not Correct</div>" : "") + | |
"<form method='post' action='/app/ServerID'><div class='row'>" + | |
"<div class='large-3 columns'><label class='right' for='sidp'>Pass Code:</label></div>" + | |
"<div class='large-6 columns'><input type='password' name='sidp' id='sidp' /></div>" + | |
"<div class='large-3 columns'><input type='submit' class='button' /></div>" + | |
"</div></form>"); | |
session.setAttribute("serverid_pass", ""); | |
} | |
out.println("<script src='//assets.aquent.com/lib/jquery/jquery-1.10.2.min.js'></script>" + | |
"<script src='//assets.aquent.com/lib/foundation/4.3.1-custom-aquent/js/foundation.js'></script>" + | |
"<script src='//assets.aquent.com/lib/foundation/4.3.1-custom-aquent/js/foundation.topbar.js'></script>" + | |
"<script>$(document).foundation();</script>" + | |
"</body></html>"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment