improve monitor support

This commit is contained in:
wenshao
2013-08-16 04:31:59 +08:00
parent cf0b19a29e
commit 73f72e0f94
3 changed files with 97 additions and 86 deletions
@@ -21,7 +21,9 @@ import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.alibaba.druid.support.http.util.IPAddress;
import com.alibaba.druid.support.http.util.IPRange;
@@ -31,7 +33,7 @@ import com.alibaba.druid.util.StringUtils;
import com.alibaba.druid.util.Utils;
@SuppressWarnings("serial")
public class ResourceSerlvet extends HttpServlet {
public abstract class ResourceSerlvet extends HttpServlet {
private final static Log LOG = LogFactory.getLog(ResourceSerlvet.class);
@@ -175,4 +177,86 @@ public class ResourceSerlvet extends HttpServlet {
}
response.getWriter().write(text);
}
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String contextPath = request.getContextPath();
String servletPath = request.getServletPath();
String requestURI = request.getRequestURI();
response.setCharacterEncoding("utf-8");
if (contextPath == null) { // root context
contextPath = "";
}
String uri = contextPath + servletPath;
String path = requestURI.substring(contextPath.length() + servletPath.length());
if (!isPermittedRequest(request)) {
path = "/nopermit.html";
returnResourceFile(path, uri, response);
return;
}
if ("/submitLogin".equals(path)) {
String usernameParam = request.getParameter(PARAM_NAME_USERNAME);
String passwordParam = request.getParameter(PARAM_NAME_PASSWORD);
if (username.equals(usernameParam) && password.equals(passwordParam)) {
request.getSession().setAttribute(SESSION_USER_KEY, username);
response.getWriter().print("success");
} else {
response.getWriter().print("error");
}
return;
}
if (isRequireAuth()
&& session.getAttribute(SESSION_USER_KEY) == null
&& !("/login.html".equals(path) || path.startsWith("/css") || path.startsWith("/js") || path.startsWith("/img"))) {
if (contextPath == null || contextPath.equals("") || contextPath.equals("/")) {
response.sendRedirect("/druid/login.html");
} else {
response.sendRedirect("druid/login.html");
}
return;
}
if ("".equals(path)) {
if (contextPath == null || contextPath.equals("") || contextPath.equals("/")) {
response.sendRedirect("/druid/index.html");
} else {
response.sendRedirect("druid/index.html");
}
return;
}
if ("/".equals(path)) {
response.sendRedirect("index.html");
return;
}
if (path.indexOf(".json") >= 0) {
String fullUrl = path;
if (request.getQueryString() != null && request.getQueryString().length() > 0) {
fullUrl += "?" + request.getQueryString();
}
response.getWriter().print(process(fullUrl));
return;
}
// find file in resources path
returnResourceFile(path, uri, response);
}
public boolean isRequireAuth() {
return this.username != null;
}
public boolean isPermittedRequest(HttpServletRequest request) {
String remoteAddress = request.getRemoteAddr();
return isPermittedRequest(remoteAddress);
}
protected abstract String process(String url);
}
@@ -25,9 +25,6 @@ import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.alibaba.druid.stat.DruidStatService;
import com.alibaba.druid.support.logging.Log;
@@ -159,7 +156,7 @@ public class StatViewServlet extends ResourceSerlvet {
* @param url 要连接的服务地址
* @return 调用服务后返回的json字符串
*/
private String process(String url) {
protected String process(String url) {
String resp = null;
if (jmxUrl == null) {
resp = statService.service(url);
@@ -194,83 +191,4 @@ public class StatViewServlet extends ResourceSerlvet {
return resp;
}
public boolean isRequireAuth() {
return this.username != null;
}
public boolean isPermittedRequest(HttpServletRequest request) {
String remoteAddress = request.getRemoteAddr();
return isPermittedRequest(remoteAddress);
}
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String contextPath = request.getContextPath();
String servletPath = request.getServletPath();
String requestURI = request.getRequestURI();
response.setCharacterEncoding("utf-8");
if (contextPath == null) { // root context
contextPath = "";
}
String uri = contextPath + servletPath;
String path = requestURI.substring(contextPath.length() + servletPath.length());
if (!isPermittedRequest(request)) {
path = "/nopermit.html";
returnResourceFile(path, uri, response);
return;
}
if ("/submitLogin".equals(path)) {
String usernameParam = request.getParameter(PARAM_NAME_USERNAME);
String passwordParam = request.getParameter(PARAM_NAME_PASSWORD);
if (username.equals(usernameParam) && password.equals(passwordParam)) {
request.getSession().setAttribute(SESSION_USER_KEY, username);
response.getWriter().print("success");
} else {
response.getWriter().print("error");
}
return;
}
if (isRequireAuth()
&& session.getAttribute(SESSION_USER_KEY) == null
&& !("/login.html".equals(path) || path.startsWith("/css") || path.startsWith("/js") || path.startsWith("/img"))) {
if (contextPath == null || contextPath.equals("") || contextPath.equals("/")) {
response.sendRedirect("/druid/login.html");
} else {
response.sendRedirect("druid/login.html");
}
return;
}
if ("".equals(path)) {
if (contextPath == null || contextPath.equals("") || contextPath.equals("/")) {
response.sendRedirect("/druid/index.html");
} else {
response.sendRedirect("druid/index.html");
}
return;
}
if ("/".equals(path)) {
response.sendRedirect("index.html");
return;
}
if (path.indexOf(".json") >= 0) {
String fullUrl = path;
if (request.getQueryString() != null && request.getQueryString().length() > 0) {
fullUrl += "?" + request.getQueryString();
}
response.getWriter().print(process(fullUrl));
return;
}
// find file in resources path
returnResourceFile(path, uri, response);
}
}
@@ -21,12 +21,12 @@ import java.util.Set;
import com.alibaba.druid.support.http.ResourceSerlvet;
@SuppressWarnings("serial")
public class MonitorClientServlet extends ResourceSerlvet {
public class MonitorServlet extends ResourceSerlvet {
private String mappingPath = "support/http/resources";
private Set<String> mapping = new HashSet<String>();
public MonitorClientServlet(){
public MonitorServlet(){
super("support/monitor/resources");
mapping.add("/css/bootstrap.min.css");
@@ -42,4 +42,13 @@ public class MonitorClientServlet extends ResourceSerlvet {
return super.getFilePath(fileName);
}
@Override
protected String process(String url) {
// data.json?type=dataSource
// data.json?type=sql
// data.json?type=webapp
// data.json?type=weburi
return null;
}
}