📄 webportalservlet.java~10~
字号:
/*****************************************************************************
* (C) Copyright 2004 。
* 保留对所有使用、复制、修改和发布整个软件和相关文档的权利。
* 本计算机程序受著作权法和国际公约的保护,未经授权擅自复制或
* 传播本程序的全部或部分,可能受到严厉的民事和刑事制裁,并
* 在法律允许的范围内受到最大可能的起诉。
*/
/*****************************************************************************
* @作者:Golden Peng
* @版本: 1.0
* @时间: 2002-10-08
*/
/*****************************************************************************
* 修改记录清单
* 修改人 :
* 修改记录:
* 修改时间:
* 修改描述:
*
*/
package com.corp.bisc.ebiz.base;
import javax.servlet.http.*;
import java.util.*;
import org.w3c.dom.*;
import com.corp.bisc.ebiz.util.*;
import com.corp.bisc.ebiz.member.*;
import com.corp.bisc.ebiz.exception.*;
import com.corp.bisc.ebiz.security.*;
import com.ibm.xml.parsers.*;
import org.xml.sax.InputSource;
import org.apache.log4j.*;
import java.sql.*;
import javax.servlet.*;
import javax.naming.*;
import javax.sql.*;
import java.io.*;
//import com.sse.portal.message.MessagePool;
/**
* Title: PetroChina ariba project
* Description:
* Copyright: Copyright (c) 2001
* Company: IBM
* @author Xiang Li
* @version 1.0
*/
public class WebPortalServlet extends HttpServlet
{
protected static String SESS_PREV_URL = "_PreviousUrl";
protected static String SESS_PREV_KEY = "_PreviousKey";
protected static String REQ_ERROR_KEY = "_error";
protected static String REQ_REDIR_URL = "";
protected static String REQ_OLD_URL = "";
protected String configFile = null;
protected String tblConstant = null;
// private static LDAPUtils ldapHandler = null;
private static ConnPoolHandler dbHandler = null;
private static AuthenticateProvider provider = null;
protected static String encoding = "UTF-8";
public static ConfigRepository confRepository = null;
protected Category logWriter = null;
static private String LEAVE_PREFIX = "[O]:";
static private String ENTER_PREFIX = "[I]:";
static
{
// initialize the log manager using System.out console at the first stage of
// the system initialization
try
{
Category category = Category.getRoot();
category.setPriority(Priority.DEBUG);
FileAppender appender = new FileAppender();
appender.setFile("System.out");
category.addAppender(appender);
}
catch(Exception e)
{
e.printStackTrace();
}
}
/* pattern = [[allow|deny]:]172.28[.0.0|.*.*]
pattherns = pattern | pattern | pattern */
public boolean allowAccessFromIP(String ip , String patterns)
{
if (patterns == null) return true;
StringTokenizer tokens = new StringTokenizer(patterns , "|");
boolean success = false;
while(tokens.hasMoreTokens())
{
String pattern = tokens.nextToken();
boolean isAllow = true;
if (pattern.startsWith(ActionDef.PATTERN_HEADING_DENY))
{
if (logWriter.isDebugEnabled())
logWriter.debug("Try to match DENY pattern [" + pattern + "] with ip [" + ip + "]");
pattern = pattern.substring(ActionDef.PATTERN_HEADING_DENY.length());
if (matchIP(ip , pattern))
{
success = false;
if (logWriter.isDebugEnabled())
logWriter.debug("DENY pattern [" + pattern + "] with ip [" + ip + "]: success" );
break;
}
else
{
if (logWriter.isDebugEnabled())
logWriter.debug("DENY pattern [" + pattern + "] with ip [" + ip + "]: failed" );
continue;
}
}
else
{
if (logWriter.isDebugEnabled())
logWriter.debug("Try to match ALLOW pattern [" + pattern + "] with ip [" + ip + "]");
if (!pattern.startsWith(ActionDef.PATTERN_HEADING_ALLOW))
{
if (logWriter.isEnabledFor(Priority.WARN))
logWriter.warn("IP Pattern has no stardard heading , using default value: [" + ActionDef.PATTERN_HEADING_ALLOW + "]");
}
else
{
pattern = pattern.substring(ActionDef.PATTERN_HEADING_ALLOW.length());
}
if (matchIP(ip , pattern))
{
success = true;
if (logWriter.isDebugEnabled())
logWriter.debug("ALLOW pattern [" + pattern + "] with ip [" + ip + "]: success" );
break;
}
else
{
if (logWriter.isDebugEnabled())
logWriter.debug("ALLOW pattern [" + pattern + "] with ip [" + ip + "]: failed" );
continue;
}
}
}
return success;
}
/**
* @param request
* @param response
* @roseuid 3B53E58A00D8
*/
public void destroy() {
System.out.println("in servelet destroy");
// MessagePool.getHandle().close();
}
/**
* @param request
* @param response
* @roseuid 3B53E5640079
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException
{
performTask(request, response);
}
/**
* @param request
* @param response
* @roseuid 3B53E58A00D8
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException
{
performTask(request, response);
}
/**
* 在本函数中作系统的初始化工作
*/
public void init(ServletConfig _config) throws javax.servlet.ServletException
{
super.init(_config);
logWriter = Category.getInstance(getClass());
// Get the filename of the configuration file
configFile = _config.getInitParameter("configFile");
/*
if (configFile == null) {
System.out.println("missing the init parameter [configFile]");
throw new ServletException("missing the init parameter [configFile]");
}
loadConfig();
getServletContext().setAttribute("com.corp.bisc.ebiz.portaldb",confRepository.dbHandler);
System.out.println("------------->>>> WebPortalServlet initialization finished <<<<--------------");
*/
initServlet(configFile);
}
/**
* 此处插入方法描述。
* 创建日期:(2002-7-26 11:59:34)
* @param configfile java.lang.String
*/
public void initServlet(String configFile) throws javax.servlet.ServletException{
System.out.println("------------->>>> WebPortalServlet initializing...... <<<<--------------");
if (configFile == null) {
System.out.println("missing the init parameter [configFile]");
throw new ServletException("missing the init parameter [configFile]");
}
loadConfig();
getServletContext().setAttribute("com.corp.bisc.ebiz.portaldb",confRepository.dbHandler);
System.out.println("------------->>>> WebPortalServlet initialization finished <<<<--------------");
}
/**
* Insert the method's description here.
* Creation date: (2002-5-13 17:28:17)
*/
private synchronized void loadConfig() throws ServletException
{
try
{
Document doc = null;
DOMParser parser = new DOMParser();
//configFile must be absolute path
if (configFile == null)
{
String errMsg = "Error:The file name of the configuration file is missing";
System.out.println(errMsg);
throw new ServletException(errMsg);
}
parser.parse(configFile);
ConfigRepository rpsp = new ConfigRepository();
rpsp.ReadConfig(parser.getDocument().getDocumentElement());
confRepository = rpsp;
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace(System.out);
throw new ServletException(e.getLocalizedMessage());
}
}
/**
* 此处插入方法描述。
* 创建日期:(2002-6-24 11:19:58)
* @param args java.lang.String[]
*/
public static void main(String[] args)
{
WebPortalServlet web = new WebPortalServlet();
System.out.println("Match IP result:" + web.allowAccessFromIP("172.28.1.1" , "deny:1.0.0.0|allow:171.28.0.0|deny:172.0.0.0|allow:172.0.0.0"));
}
static public boolean matchIP(String ip , String pattern)
{
if (pattern == null) return true;
String patternTmp = pattern;
while(patternTmp.endsWith(".0") || patternTmp.endsWith(".*"))
{
int len = patternTmp.length();
patternTmp = patternTmp.substring(0 , len-2);
}
if (patternTmp.equals("0") || patternTmp.equals("*"))
{
return true;
}
return ip.startsWith(patternTmp);
}
/**
* @param request
* @param response
* @roseuid 3B53E5A00152
*/
protected void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException
{
long timeStartServlet = System.currentTimeMillis();
logWriter.debug("<<<<<<<<<<<<<<<<<<< Start to serve user request , analyzing the request now >>>>>>>>>>>>>>>");
String reqUrl = request.getRequestURI();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -