001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.beans;
017:
018: import java.io.Serializable;
019: import java.net.URL;
020: import java.text.DateFormat;
021: import java.text.SimpleDateFormat;
022: import java.util.Date;
023:
024: import javax.servlet.http.HttpServletRequest;
025:
026: /**
027: * 外部网站引用记录,对应表dlog_site_referer的一条记录
028: * 建表语句:
029: * create table dlog_visit (
030: visitID INTEGER not null,
031: remoteAddr CHAR(15) not null,
032: requestURL VARCHAR(100) not null,
033: userAgent VARCHAR(100) null,
034: visitDate CHAR(8) not null,
035: visitTime CHAR(6) not null,
036: refererURL VARCHAR(200) null,
037: constraint PK_DLOG_VISIT primary key (visitID)
038: )
039: * @author Winter Lau
040: */
041: public class RefererBean implements Serializable {
042:
043: public final static String DATE_FORMAT = "yyyyMMdd";
044: public final static String TIME_FORMAT = "HHmmss";
045:
046: public final static DateFormat DATE = new SimpleDateFormat(
047: DATE_FORMAT);
048: public final static DateFormat TIME = new SimpleDateFormat(
049: TIME_FORMAT);
050:
051: protected int id;
052: protected String remoteAddr;
053: protected String refererURL;
054: protected String requestURL;
055: protected String userAgent;
056: protected String visitDate;
057: protected String visitTime;
058:
059: /**
060: * 该构造函数是提供给Hibernate初始化Bean时用
061: */
062: public RefererBean() {
063: }
064:
065: /**
066: * 从反问请求实例中初始化外部引用记录
067: * @param request
068: */
069: public RefererBean(HttpServletRequest request) {
070: refererURL = request.getHeader("referer");
071: remoteAddr = request.getRemoteAddr();
072: userAgent = request.getHeader("user-agent");
073: StringBuffer ru = request.getRequestURL();
074: if (request.getQueryString() != null) {
075: ru.append('?');
076: ru.append(request.getQueryString());
077: }
078: setRequestURL(ru.toString());
079: Date curTime = new Date();
080: visitDate = DATE.format(curTime);
081: visitTime = TIME.format(curTime);
082: curTime = null;
083: }
084:
085: /**
086: * 判断该外部引用是否来自于自身站点
087: * @param request
088: * @return
089: */
090: public boolean isFromOutsite() {
091: if (refererURL != null)
092: try {
093: URL req_url = new URL(requestURL);
094: URL ref_url = new URL(refererURL);
095: return !req_url.getHost().equalsIgnoreCase(
096: ref_url.getHost());
097: } catch (Exception e) {
098: }
099: return false;
100: }
101:
102: public int getId() {
103: return id;
104: }
105:
106: public void setId(int id) {
107: this .id = id;
108: }
109:
110: public String getRequestURL() {
111: return requestURL;
112: }
113:
114: public void setRequestURL(String reqUrl) {
115: this .requestURL = reqUrl;
116: trimReqUrl();
117: }
118:
119: protected void trimReqUrl() {
120: if (requestURL == null)
121: return;
122: String url = "";
123: int mainParamIdx = requestURL.indexOf("main=");
124: if (mainParamIdx != -1) {
125: int splitIdx = requestURL.indexOf("&", mainParamIdx);
126: if (splitIdx != -1) {
127: url = requestURL.substring(0, mainParamIdx);
128: if ((splitIdx + 1) < requestURL.length()) {
129: url += requestURL.substring(splitIdx + 1);
130: }
131: } else {
132: url = requestURL.substring(0, mainParamIdx - 1);
133: }
134: requestURL = url;
135: }
136: }
137:
138: public String getRefererURL() {
139: return refererURL;
140: }
141:
142: public void setRefererURL(String refererURL) {
143: this .refererURL = refererURL;
144: }
145:
146: public String getRemoteAddr() {
147: return remoteAddr;
148: }
149:
150: public void setRemoteAddr(String remoteAddr) {
151: this .remoteAddr = remoteAddr;
152: }
153:
154: public String getUserAgent() {
155: return userAgent;
156: }
157:
158: public void setUserAgent(String userAgent) {
159: this .userAgent = userAgent;
160: }
161:
162: public String getVisitDate() {
163: return visitDate;
164: }
165:
166: public void setVisitDate(String visitDate) {
167: this .visitDate = visitDate;
168: }
169:
170: public String getVisitTime() {
171: return visitTime;
172: }
173:
174: public void setVisitTime(String visitTime) {
175: this .visitTime = visitTime;
176: }
177:
178: public static void main(String[] args) {
179: Date curTime = new Date();
180: String visitDate = DATE.format(curTime);
181: String visitTime = TIME.format(curTime);
182: System.out.println("visitDate=" + visitDate);
183: System.out.println("visitTime=" + visitTime);
184: }
185: }
|