From be9a35bd8165565f1a585faf88e0ac812cb0de05 Mon Sep 17 00:00:00 2001 From: melin Date: Mon, 4 Feb 2013 11:22:24 +0800 Subject: [PATCH 1/4] =?UTF-8?q?oracle=20sql=20=E8=AF=AD=E5=8F=A5=E4=B8=8D?= =?UTF-8?q?=E8=83=BD=E5=8C=85=E5=90=AB=E4=B8=8B=E5=88=92=E7=BA=BF=E5=BC=80?= =?UTF-8?q?=E5=A4=B4=E7=9A=84=E5=8D=95=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/alibaba/druid/sql/PagerUtils.java | 10 +++---- .../sql/PagerUtilsTest_Limit_oracle_0.java | 28 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/alibaba/druid/sql/PagerUtils.java b/src/main/java/com/alibaba/druid/sql/PagerUtils.java index 764543b3d4..a1dfa98f6d 100644 --- a/src/main/java/com/alibaba/druid/sql/PagerUtils.java +++ b/src/main/java/com/alibaba/druid/sql/PagerUtils.java @@ -123,10 +123,10 @@ public class PagerUtils { } OracleSelectQueryBlock countQueryBlock = new OracleSelectQueryBlock(); - countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("_X"), "*"))); - countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLIdentifierExpr("ROWNUM"), "_RN")); + countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*"))); + countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLIdentifierExpr("ROWNUM"), "RN")); - countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "_X")); + countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX")); countQueryBlock.setWhere(new SQLBinaryOpExpr(new SQLIdentifierExpr("ROWNUM"), // SQLBinaryOperator.LessThanOrEqual, // new SQLNumberExpr(count + offset))); @@ -136,8 +136,8 @@ public class PagerUtils { OracleSelectQueryBlock offsetQueryBlock = new OracleSelectQueryBlock(); offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr())); - offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "_XX")); - offsetQueryBlock.setWhere(new SQLBinaryOpExpr(new SQLIdentifierExpr("_RN"), // + offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX")); + offsetQueryBlock.setWhere(new SQLBinaryOpExpr(new SQLIdentifierExpr("RN"), // SQLBinaryOperator.GreaterThan, // new SQLNumberExpr(offset))); diff --git a/src/test/java/com/alibaba/druid/bvt/sql/PagerUtilsTest_Limit_oracle_0.java b/src/test/java/com/alibaba/druid/bvt/sql/PagerUtilsTest_Limit_oracle_0.java index 6481dca199..5f6798739a 100644 --- a/src/test/java/com/alibaba/druid/bvt/sql/PagerUtilsTest_Limit_oracle_0.java +++ b/src/test/java/com/alibaba/druid/bvt/sql/PagerUtilsTest_Limit_oracle_0.java @@ -12,11 +12,11 @@ public class PagerUtilsTest_Limit_oracle_0 extends TestCase { public void test_oracle_oderby_0() throws Exception { String sql = "select * from t order by id"; String result = PagerUtils.limit(sql, JdbcConstants.ORACLE, 0, 10); - Assert.assertEquals("SELECT _X.*, ROWNUM AS _RN" + // + Assert.assertEquals("SELECT XX.*, ROWNUM AS RN" + // "\nFROM (SELECT *" + // "\n\tFROM t" + // "\n\tORDER BY id" + // - "\n\t) _X" + // + "\n\t) XX" + // "\nWHERE ROWNUM <= 10", result); } @@ -32,39 +32,39 @@ public class PagerUtilsTest_Limit_oracle_0 extends TestCase { String sql = "select * from t"; String result = PagerUtils.limit(sql, JdbcConstants.ORACLE, 10, 10); Assert.assertEquals("SELECT *" + // - "\nFROM (SELECT _X.*, ROWNUM AS _RN" + // + "\nFROM (SELECT XX.*, ROWNUM AS RN" + // "\n\tFROM (SELECT *" + // "\n\t\tFROM t" + // - "\n\t\t) _X" + // + "\n\t\t) XX" + // "\n\tWHERE ROWNUM <= 20" + // - "\n\t) _XX" + // - "\nWHERE _RN > 10", result); + "\n\t) XXX" + // + "\nWHERE RN > 10", result); } public void test_oracle_2() throws Exception { String sql = "select * from t"; String result = PagerUtils.limit(sql, JdbcConstants.ORACLE, 20, 10); Assert.assertEquals("SELECT *" + // - "\nFROM (SELECT _X.*, ROWNUM AS _RN" + // + "\nFROM (SELECT XX.*, ROWNUM AS RN" + // "\n\tFROM (SELECT *" + // "\n\t\tFROM t" + // - "\n\t\t) _X" + // + "\n\t\t) XX" + // "\n\tWHERE ROWNUM <= 30" + // - "\n\t) _XX" + // - "\nWHERE _RN > 20", result); + "\n\t) XXX" + // + "\nWHERE RN > 20", result); } public void test_oracle_3() throws Exception { String sql = "select id, name, salary from t order by id, name"; String result = PagerUtils.limit(sql, JdbcConstants.ORACLE, 20, 10); Assert.assertEquals("SELECT *" + // - "\nFROM (SELECT _X.*, ROWNUM AS _RN" + // + "\nFROM (SELECT XX.*, ROWNUM AS RN" + // "\n\tFROM (SELECT id, name, salary" + // "\n\t\tFROM t" + // "\n\t\tORDER BY id, name" + // - "\n\t\t) _X" + // + "\n\t\t) XX" + // "\n\tWHERE ROWNUM <= 30" + // - "\n\t) _XX" + // - "\nWHERE _RN > 20", result); + "\n\t) XXX" + // + "\nWHERE RN > 20", result); } } From 376e27826c41c188e5b19c2f2bbd4c0bc3e27646 Mon Sep 17 00:00:00 2001 From: melin Date: Tue, 2 Apr 2013 21:31:57 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E7=99=BB=E5=BD=95=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC=E9=A1=B5=E9=9D=A2=E9=94=99=E8=AF=AF=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../druid/support/http/StatViewServlet.java | 554 +++++++++--------- 1 file changed, 277 insertions(+), 277 deletions(-) diff --git a/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java b/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java index 2342ccb306..4926a21bd8 100644 --- a/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java +++ b/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java @@ -1,277 +1,277 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.support.http; - -import java.io.IOException; -import java.util.ArrayList; -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.stat.DruidStatService; -import com.alibaba.druid.support.http.util.IPAddress; -import com.alibaba.druid.support.http.util.IPRange; -import com.alibaba.druid.support.logging.Log; -import com.alibaba.druid.support.logging.LogFactory; -import com.alibaba.druid.util.IOUtils; -import com.alibaba.druid.util.StringUtils; - -/** - * 注意:避免直接调用Druid相关对象例如DruidDataSource等,相关调用要到DruidStatManagerFacade里用反射实现 - * - * @author sandzhang - */ -public class StatViewServlet extends HttpServlet { - - 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 = RESOURCE_PATH + "/template.html"; - - private DruidStatService statService = DruidStatService.getInstance(); - - public String templatePage; - - private List allowList = new ArrayList(); - private List denyList = new ArrayList(); - - private String username = null; - private String password = null; - - public void init() throws ServletException { - initAuthEnv(); - - try { - templatePage = IOUtils.readFromResource(TEMPLATE_PAGE_RESOURCE_PATH); - - } catch (IOException e) { - throw new ServletException("error read templatePage:" + TEMPLATE_PAGE_RESOURCE_PATH, e); - } - - try { - String param = getInitParameter(PARAM_NAME_RESET_ENABLE); - if (param != null && param.trim().length() != 0) { - param = param.trim(); - boolean resetEnable = Boolean.parseBoolean(param); - statService.setResetEnable(resetEnable); - } - } catch (Exception e) { - String msg = "initParameter config error, resetEnable : " + getInitParameter(PARAM_NAME_RESET_ENABLE); - LOG.error(msg, e); - } - - try { - String param = getInitParameter(PARAM_NAME_ALLOW); - if (param != null && param.trim().length() != 0) { - param = param.trim(); - String[] items = param.split(","); - - for (String item : items) { - if (item == null || item.length() == 0) { - continue; - } - - IPRange ipRange = new IPRange(item); - allowList.add(ipRange); - } - } - } catch (Exception e) { - String msg = "initParameter config error, allow : " + getInitParameter(PARAM_NAME_ALLOW); - LOG.error(msg, e); - } - - try { - String param = getInitParameter(PARAM_NAME_DENY); - if (param != null && param.trim().length() != 0) { - param = param.trim(); - String[] items = param.split(","); - - for (String item : items) { - if (item == null || item.length() == 0) { - continue; - } - - IPRange ipRange = new IPRange(item); - denyList.add(ipRange); - } - } - } catch (Exception e) { - String msg = "initParameter config error, deny : " + getInitParameter(PARAM_NAME_DENY); - LOG.error(msg, e); - } - } - - private void initAuthEnv() { - String paramUserName = getInitParameter(PARAM_NAME_USERNAME); - if (!StringUtils.isEmpty(paramUserName)) { - this.username = paramUserName; - } - - String paramPassword = getInitParameter(PARAM_NAME_PASSWORD); - if (!StringUtils.isEmpty(paramPassword)) { - this.password = paramPassword; - } - } - - public boolean isRequireAuth() { - return this.username != null; - } - - public boolean isPermittedRequest(HttpServletRequest request) { - String remoteAddress = request.getRemoteAddr(); - return isPermittedRequest(remoteAddress); - } - - public boolean isPermittedRequest(String remoteAddress) { - boolean ipV6 = remoteAddress != null && remoteAddress.indexOf(':') != -1; - - if (ipV6) { - if (denyList.size() == 0 && allowList.size() == 0) { - return true; - } - } - - IPAddress ipAddress = new IPAddress(remoteAddress); - - for (IPRange range : denyList) { - if (range.isIPAddressInRange(ipAddress)) { - return false; - } - } - - if (allowList.size() > 0) { - for (IPRange range : allowList) { - if (range.isIPAddressInRange(ipAddress)) { - return true; - } - } - - return false; - } - - return true; - } - - 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("/login.html"); - } else { - response.sendRedirect("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(statService.service(fullUrl)); - return; - } - - // find file in resources path - returnResourceFile(path, uri, response); - } - - private void returnResourceFile(String fileName, String uri, HttpServletResponse response) throws ServletException, - IOException { - if (fileName.endsWith(".jpg")) { - byte[] bytes = IOUtils.readByteArrayFromResource(RESOURCE_PATH + fileName); - if (bytes != null) { - response.getOutputStream().write(bytes); - } - - return; - } - - String text = IOUtils.readFromResource(RESOURCE_PATH + 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); - } - -} +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.support.http; + +import java.io.IOException; +import java.util.ArrayList; +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.stat.DruidStatService; +import com.alibaba.druid.support.http.util.IPAddress; +import com.alibaba.druid.support.http.util.IPRange; +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; +import com.alibaba.druid.util.IOUtils; +import com.alibaba.druid.util.StringUtils; + +/** + * 注意:避免直接调用Druid相关对象例如DruidDataSource等,相关调用要到DruidStatManagerFacade里用反射实现 + * + * @author sandzhang + */ +public class StatViewServlet extends HttpServlet { + + 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 = RESOURCE_PATH + "/template.html"; + + private DruidStatService statService = DruidStatService.getInstance(); + + public String templatePage; + + private List allowList = new ArrayList(); + private List denyList = new ArrayList(); + + private String username = null; + private String password = null; + + public void init() throws ServletException { + initAuthEnv(); + + try { + templatePage = IOUtils.readFromResource(TEMPLATE_PAGE_RESOURCE_PATH); + + } catch (IOException e) { + throw new ServletException("error read templatePage:" + TEMPLATE_PAGE_RESOURCE_PATH, e); + } + + try { + String param = getInitParameter(PARAM_NAME_RESET_ENABLE); + if (param != null && param.trim().length() != 0) { + param = param.trim(); + boolean resetEnable = Boolean.parseBoolean(param); + statService.setResetEnable(resetEnable); + } + } catch (Exception e) { + String msg = "initParameter config error, resetEnable : " + getInitParameter(PARAM_NAME_RESET_ENABLE); + LOG.error(msg, e); + } + + try { + String param = getInitParameter(PARAM_NAME_ALLOW); + if (param != null && param.trim().length() != 0) { + param = param.trim(); + String[] items = param.split(","); + + for (String item : items) { + if (item == null || item.length() == 0) { + continue; + } + + IPRange ipRange = new IPRange(item); + allowList.add(ipRange); + } + } + } catch (Exception e) { + String msg = "initParameter config error, allow : " + getInitParameter(PARAM_NAME_ALLOW); + LOG.error(msg, e); + } + + try { + String param = getInitParameter(PARAM_NAME_DENY); + if (param != null && param.trim().length() != 0) { + param = param.trim(); + String[] items = param.split(","); + + for (String item : items) { + if (item == null || item.length() == 0) { + continue; + } + + IPRange ipRange = new IPRange(item); + denyList.add(ipRange); + } + } + } catch (Exception e) { + String msg = "initParameter config error, deny : " + getInitParameter(PARAM_NAME_DENY); + LOG.error(msg, e); + } + } + + private void initAuthEnv() { + String paramUserName = getInitParameter(PARAM_NAME_USERNAME); + if (!StringUtils.isEmpty(paramUserName)) { + this.username = paramUserName; + } + + String paramPassword = getInitParameter(PARAM_NAME_PASSWORD); + if (!StringUtils.isEmpty(paramPassword)) { + this.password = paramPassword; + } + } + + public boolean isRequireAuth() { + return this.username != null; + } + + public boolean isPermittedRequest(HttpServletRequest request) { + String remoteAddress = request.getRemoteAddr(); + return isPermittedRequest(remoteAddress); + } + + public boolean isPermittedRequest(String remoteAddress) { + boolean ipV6 = remoteAddress != null && remoteAddress.indexOf(':') != -1; + + if (ipV6) { + if (denyList.size() == 0 && allowList.size() == 0) { + return true; + } + } + + IPAddress ipAddress = new IPAddress(remoteAddress); + + for (IPRange range : denyList) { + if (range.isIPAddressInRange(ipAddress)) { + return false; + } + } + + if (allowList.size() > 0) { + for (IPRange range : allowList) { + if (range.isIPAddressInRange(ipAddress)) { + return true; + } + } + + return false; + } + + return true; + } + + 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(statService.service(fullUrl)); + return; + } + + // find file in resources path + returnResourceFile(path, uri, response); + } + + private void returnResourceFile(String fileName, String uri, HttpServletResponse response) throws ServletException, + IOException { + if (fileName.endsWith(".jpg")) { + byte[] bytes = IOUtils.readByteArrayFromResource(RESOURCE_PATH + fileName); + if (bytes != null) { + response.getOutputStream().write(bytes); + } + + return; + } + + String text = IOUtils.readFromResource(RESOURCE_PATH + 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); + } + +} From fadf602488ee990a1d862d90f6b63fe8842e0b84 Mon Sep 17 00:00:00 2001 From: melin Date: Tue, 2 Apr 2013 22:28:19 +0800 Subject: [PATCH 3/4] =?UTF-8?q?Revert=20"=E7=99=BB=E5=BD=95=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E8=B7=B3=E8=BD=AC=E9=A1=B5=E9=9D=A2=E9=94=99=E8=AF=AF?= =?UTF-8?q?=EF=BC=81"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 376e27826c41c188e5b19c2f2bbd4c0bc3e27646. --- .../druid/support/http/StatViewServlet.java | 554 +++++++++--------- 1 file changed, 277 insertions(+), 277 deletions(-) diff --git a/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java b/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java index 4926a21bd8..2342ccb306 100644 --- a/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java +++ b/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java @@ -1,277 +1,277 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.support.http; - -import java.io.IOException; -import java.util.ArrayList; -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.stat.DruidStatService; -import com.alibaba.druid.support.http.util.IPAddress; -import com.alibaba.druid.support.http.util.IPRange; -import com.alibaba.druid.support.logging.Log; -import com.alibaba.druid.support.logging.LogFactory; -import com.alibaba.druid.util.IOUtils; -import com.alibaba.druid.util.StringUtils; - -/** - * 注意:避免直接调用Druid相关对象例如DruidDataSource等,相关调用要到DruidStatManagerFacade里用反射实现 - * - * @author sandzhang - */ -public class StatViewServlet extends HttpServlet { - - 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 = RESOURCE_PATH + "/template.html"; - - private DruidStatService statService = DruidStatService.getInstance(); - - public String templatePage; - - private List allowList = new ArrayList(); - private List denyList = new ArrayList(); - - private String username = null; - private String password = null; - - public void init() throws ServletException { - initAuthEnv(); - - try { - templatePage = IOUtils.readFromResource(TEMPLATE_PAGE_RESOURCE_PATH); - - } catch (IOException e) { - throw new ServletException("error read templatePage:" + TEMPLATE_PAGE_RESOURCE_PATH, e); - } - - try { - String param = getInitParameter(PARAM_NAME_RESET_ENABLE); - if (param != null && param.trim().length() != 0) { - param = param.trim(); - boolean resetEnable = Boolean.parseBoolean(param); - statService.setResetEnable(resetEnable); - } - } catch (Exception e) { - String msg = "initParameter config error, resetEnable : " + getInitParameter(PARAM_NAME_RESET_ENABLE); - LOG.error(msg, e); - } - - try { - String param = getInitParameter(PARAM_NAME_ALLOW); - if (param != null && param.trim().length() != 0) { - param = param.trim(); - String[] items = param.split(","); - - for (String item : items) { - if (item == null || item.length() == 0) { - continue; - } - - IPRange ipRange = new IPRange(item); - allowList.add(ipRange); - } - } - } catch (Exception e) { - String msg = "initParameter config error, allow : " + getInitParameter(PARAM_NAME_ALLOW); - LOG.error(msg, e); - } - - try { - String param = getInitParameter(PARAM_NAME_DENY); - if (param != null && param.trim().length() != 0) { - param = param.trim(); - String[] items = param.split(","); - - for (String item : items) { - if (item == null || item.length() == 0) { - continue; - } - - IPRange ipRange = new IPRange(item); - denyList.add(ipRange); - } - } - } catch (Exception e) { - String msg = "initParameter config error, deny : " + getInitParameter(PARAM_NAME_DENY); - LOG.error(msg, e); - } - } - - private void initAuthEnv() { - String paramUserName = getInitParameter(PARAM_NAME_USERNAME); - if (!StringUtils.isEmpty(paramUserName)) { - this.username = paramUserName; - } - - String paramPassword = getInitParameter(PARAM_NAME_PASSWORD); - if (!StringUtils.isEmpty(paramPassword)) { - this.password = paramPassword; - } - } - - public boolean isRequireAuth() { - return this.username != null; - } - - public boolean isPermittedRequest(HttpServletRequest request) { - String remoteAddress = request.getRemoteAddr(); - return isPermittedRequest(remoteAddress); - } - - public boolean isPermittedRequest(String remoteAddress) { - boolean ipV6 = remoteAddress != null && remoteAddress.indexOf(':') != -1; - - if (ipV6) { - if (denyList.size() == 0 && allowList.size() == 0) { - return true; - } - } - - IPAddress ipAddress = new IPAddress(remoteAddress); - - for (IPRange range : denyList) { - if (range.isIPAddressInRange(ipAddress)) { - return false; - } - } - - if (allowList.size() > 0) { - for (IPRange range : allowList) { - if (range.isIPAddressInRange(ipAddress)) { - return true; - } - } - - return false; - } - - return true; - } - - 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(statService.service(fullUrl)); - return; - } - - // find file in resources path - returnResourceFile(path, uri, response); - } - - private void returnResourceFile(String fileName, String uri, HttpServletResponse response) throws ServletException, - IOException { - if (fileName.endsWith(".jpg")) { - byte[] bytes = IOUtils.readByteArrayFromResource(RESOURCE_PATH + fileName); - if (bytes != null) { - response.getOutputStream().write(bytes); - } - - return; - } - - String text = IOUtils.readFromResource(RESOURCE_PATH + 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); - } - -} +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.support.http; + +import java.io.IOException; +import java.util.ArrayList; +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.stat.DruidStatService; +import com.alibaba.druid.support.http.util.IPAddress; +import com.alibaba.druid.support.http.util.IPRange; +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; +import com.alibaba.druid.util.IOUtils; +import com.alibaba.druid.util.StringUtils; + +/** + * 注意:避免直接调用Druid相关对象例如DruidDataSource等,相关调用要到DruidStatManagerFacade里用反射实现 + * + * @author sandzhang + */ +public class StatViewServlet extends HttpServlet { + + 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 = RESOURCE_PATH + "/template.html"; + + private DruidStatService statService = DruidStatService.getInstance(); + + public String templatePage; + + private List allowList = new ArrayList(); + private List denyList = new ArrayList(); + + private String username = null; + private String password = null; + + public void init() throws ServletException { + initAuthEnv(); + + try { + templatePage = IOUtils.readFromResource(TEMPLATE_PAGE_RESOURCE_PATH); + + } catch (IOException e) { + throw new ServletException("error read templatePage:" + TEMPLATE_PAGE_RESOURCE_PATH, e); + } + + try { + String param = getInitParameter(PARAM_NAME_RESET_ENABLE); + if (param != null && param.trim().length() != 0) { + param = param.trim(); + boolean resetEnable = Boolean.parseBoolean(param); + statService.setResetEnable(resetEnable); + } + } catch (Exception e) { + String msg = "initParameter config error, resetEnable : " + getInitParameter(PARAM_NAME_RESET_ENABLE); + LOG.error(msg, e); + } + + try { + String param = getInitParameter(PARAM_NAME_ALLOW); + if (param != null && param.trim().length() != 0) { + param = param.trim(); + String[] items = param.split(","); + + for (String item : items) { + if (item == null || item.length() == 0) { + continue; + } + + IPRange ipRange = new IPRange(item); + allowList.add(ipRange); + } + } + } catch (Exception e) { + String msg = "initParameter config error, allow : " + getInitParameter(PARAM_NAME_ALLOW); + LOG.error(msg, e); + } + + try { + String param = getInitParameter(PARAM_NAME_DENY); + if (param != null && param.trim().length() != 0) { + param = param.trim(); + String[] items = param.split(","); + + for (String item : items) { + if (item == null || item.length() == 0) { + continue; + } + + IPRange ipRange = new IPRange(item); + denyList.add(ipRange); + } + } + } catch (Exception e) { + String msg = "initParameter config error, deny : " + getInitParameter(PARAM_NAME_DENY); + LOG.error(msg, e); + } + } + + private void initAuthEnv() { + String paramUserName = getInitParameter(PARAM_NAME_USERNAME); + if (!StringUtils.isEmpty(paramUserName)) { + this.username = paramUserName; + } + + String paramPassword = getInitParameter(PARAM_NAME_PASSWORD); + if (!StringUtils.isEmpty(paramPassword)) { + this.password = paramPassword; + } + } + + public boolean isRequireAuth() { + return this.username != null; + } + + public boolean isPermittedRequest(HttpServletRequest request) { + String remoteAddress = request.getRemoteAddr(); + return isPermittedRequest(remoteAddress); + } + + public boolean isPermittedRequest(String remoteAddress) { + boolean ipV6 = remoteAddress != null && remoteAddress.indexOf(':') != -1; + + if (ipV6) { + if (denyList.size() == 0 && allowList.size() == 0) { + return true; + } + } + + IPAddress ipAddress = new IPAddress(remoteAddress); + + for (IPRange range : denyList) { + if (range.isIPAddressInRange(ipAddress)) { + return false; + } + } + + if (allowList.size() > 0) { + for (IPRange range : allowList) { + if (range.isIPAddressInRange(ipAddress)) { + return true; + } + } + + return false; + } + + return true; + } + + 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("/login.html"); + } else { + response.sendRedirect("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(statService.service(fullUrl)); + return; + } + + // find file in resources path + returnResourceFile(path, uri, response); + } + + private void returnResourceFile(String fileName, String uri, HttpServletResponse response) throws ServletException, + IOException { + if (fileName.endsWith(".jpg")) { + byte[] bytes = IOUtils.readByteArrayFromResource(RESOURCE_PATH + fileName); + if (bytes != null) { + response.getOutputStream().write(bytes); + } + + return; + } + + String text = IOUtils.readFromResource(RESOURCE_PATH + 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); + } + +} From 06d9d1597efd166770693ee9b8f9ec2ee92e06cd Mon Sep 17 00:00:00 2001 From: melin Date: Tue, 2 Apr 2013 22:28:59 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E7=99=BB=E5=BD=95=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E5=AE=9A=E4=BD=8D=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/alibaba/druid/support/http/StatViewServlet.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java b/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java index 2342ccb306..69b0a490ed 100644 --- a/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java +++ b/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java @@ -215,10 +215,10 @@ public class StatViewServlet extends HttpServlet { 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("/login.html"); + if (contextPath == null || contextPath.equals("") || contextPath.equals("/")) { + response.sendRedirect("/druid/login.html"); } else { - response.sendRedirect("login.html"); + response.sendRedirect("druid/login.html"); } return; }