Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
   */
  
  package com.jfinal.plugin.druid;
  
 import java.util.List;
替代 StatViewServlet
 
 @SuppressWarnings("unused")
 public class DruidStatViewHandler extends Handler {
 	
 	private String visitPath = "/druid";
 	
 	public DruidStatViewHandler(String visitPath) {
 		this. = visitPath;
 		this. = new IDruidStatViewAuth(){
 			public boolean isPermitted(HttpServletRequest request) {
 				return true;
 			}};
 	}
 	
 	public DruidStatViewHandler(String visitPath , IDruidStatViewAuth druidStatViewAuth) {
 		this. = visitPath;
 		this. = druidStatViewAuth;
 	}
 	
 	public void handle(String targetHttpServletRequest requestHttpServletResponse responseboolean[] isHandled) {
 		if (target.startsWith()) {
 			isHandled[0] = true;
 			
 			if (target.equals() && !target.endsWith("/index.html")) {
 				HandlerKit.redirect(target += "/index.html"requestresponseisHandled);
 				return ;
 			}
 			
 			try {
 				.service(requestresponse);
 			} catch (Exception e) {
 				throw new RuntimeException(e);
 			}
 		}
 		else {
 			.handle(targetrequestresponseisHandled);
 		}
 	}
 	
 		
 		private static final long serialVersionUID = 2898674199964021798L;
 		
 		public boolean isPermittedRequest(HttpServletRequest request) {
 			return .isPermitted(request);
 		}
 		
 		public void service(HttpServletRequest requestHttpServletResponse responsethrows ServletExceptionIOException {
 	        HttpSession session = request.getSession();
 	        String contextPath = request.getContextPath();
 	        // String servletPath = request.getServletPath();
 	        String requestURI = request.getRequestURI();
 
 	        response.setCharacterEncoding(.);
 
 	        if (contextPath == null) { // root context
 	            contextPath = "";
 	        }
 	        // String uri = contextPath + servletPath;
 	        // String path = requestURI.substring(contextPath.length() + servletPath.length());
 	        int index = contextPath.length() + .length();
 	        String uri = requestURI.substring(0, index);
 	        String path = requestURI.substring(index);
 
 	        if (!isPermittedRequest(request)) {
 	            path = "/nopermit.html";
 	            returnResourceFile(pathuriresponse);
 	            return;
 	        }
	        if ("/submitLogin".equals(path)) {
	            String usernameParam = request.getParameter();
	            String passwordParam = request.getParameter();
	            if (.equals(usernameParam) && .equals(passwordParam)) {
	                request.getSession().setAttribute();
	                response.getWriter().print("success");
	            } else {
	                response.getWriter().print("error");
	            }
	            return;
	        }
	        if (isRequireAuth()
	            && session.getAttribute() == null
	            && !("/login.html".equals(path) || path.startsWith("/css") || path.startsWith("/js") || path.startsWith("/img"))) {
	            if (contextPath.equals("") || contextPath.equals(.)) {
	                response.sendRedirect("/login.html");
	            } else {
	                response.sendRedirect("login.html");
	            }
	            return;
	        }
	        if ("".equals(path)) {
	            if (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.contains(".json")) {
	            String fullUrl = path;
	            if (request.getQueryString() != null && request.getQueryString().length() > 0) {
	                fullUrl += . + request.getQueryString();
	            }
	            response.getWriter().print(.service(fullUrl));
	            return;
	        }
	        // find file in resources path
	        returnResourceFile(pathuriresponse);
	    }
        protected void returnResourceFile(String fileNameString uri,
			if (fileName.endsWith(".jpg")) {
                        + fileName);
				if (bytes != null) {
					response.getOutputStream().write(bytes);
				}
				return;
			}
			String text = Utils.readFromResource( + fileName);
			if (text == null) {
				response.sendRedirect(uri + "/index.html");
				return;
			}
			if (fileName.endsWith(".css")) {
				response.setContentType("text/css;charset=utf-8");
else if (fileName.endsWith(".js")) {
				response.setContentType("text/javascript;charset=utf-8");
			}
			response.getWriter().write(text);
		}
	}
    private final static Log    LOG                         = LogFactory.getLog(StatViewServlet.class);
    private static final long   serialVersionUID            = 1L;
    public static final String  PARAM_NAME_RESET_ENABLE     = "resetEnable";
    public static final String  PARAM_NAME_ALLOW            = "allow";
    public static final String  PARAM_NAME_DENY             = "deny";
    public static final String  PARAM_NAME_USERNAME         = "loginUsername";
    public static final String  PARAM_NAME_PASSWORD         = "loginPassword";
    public static final String  SESSION_USER_KEY            = "druid-user";
    private final static String RESOURCE_PATH               = "support/http/resources";
    private final static String TEMPLATE_PAGE_RESOURCE_PATH =  + "/template.html";
    private DruidStatService    statService                 = DruidStatService.getInstance();
    public String               templatePage;
    private List<IPRange>       allowList                   = new ArrayList<IPRange>();
    private List<IPRange>       denyList                    = new ArrayList<IPRange>();
    private String              username                    = null;
    private String              password                    = null;
New to GrepCode? Check out our FAQ X