SqlCheckAjax.jsp
6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<%@page import="java.util.regex.Pattern"%>
<%@page import="sun.misc.BASE64Decoder"%>
<%@ page import="weaver.general.*,weaver.conn.*" %>
<%@ page import="weaver.hrm.*" %>
<%@page import="java.sql.*"%>
<%@page import="net.sf.json.JSONObject"%>
<jsp:useBean id="automaticconnect" class="weaver.workflow.automatic.automaticconnect" scope="page" />
<%
out.clear();
response.setContentType("text/xml;charset=UTF-8");
String sqlcontent = Util.null2String(request.getParameter("sql"));
BASE64Decoder decoder = new BASE64Decoder();
try {
String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
if(Pattern.matches(base64Pattern, sqlcontent)){
sqlcontent=new String(decoder.decodeBuffer(sqlcontent), "UTF-8").replace("%2B","+");
}
} catch (Exception e1) {
}
String datasourceid = Util.null2String(request.getParameter("datasourceid"));
String isFormMode = Util.null2String(request.getParameter("isFormMode"));
boolean isCanUse = false;
User user = HrmUserVarify.getUser (request , response) ;
JSONObject jsonObject = new JSONObject();
if(user==null){
if("1".equals(isFormMode)){
jsonObject.put("isCanUse",isCanUse);
jsonObject.put("errormsg","-101");
response.getWriter().write(jsonObject.toString());
}
return;
}
boolean userRight = HrmUserVarify.checkUserRight("WorkflowManage:All", user);
if (!userRight && !HrmUserVarify.checkUserRight("ModeSetting:All", user)) {
if("1".equals(isFormMode)){
jsonObject.put("isCanUse",isCanUse);
jsonObject.put("errormsg","-102");
response.getWriter().write(jsonObject.toString());
}
return;
}
String errormsg = "";
try{
ConnStatement statement = null;
int index = sqlcontent.indexOf("doFieldSQL(\"");
if(index > -1){
sqlcontent = sqlcontent.substring(index+12);
index = sqlcontent.lastIndexOf("\")");
if(index > -1){
sqlcontent = sqlcontent.substring(0, index);
}
}
sqlcontent = sqlcontent.trim();
if(!"".equals(sqlcontent) && !sqlcontent.toLowerCase().startsWith("select")){
if("1".equals(isFormMode)){
jsonObject.put("isCanUse",isCanUse);
jsonObject.put("errormsg","");
response.getWriter().write(jsonObject.toString());
}
return;
}
sqlcontent = sqlcontent.replaceAll("\\'\\$([0-9]*)\\$\\'", "'19700101'");
sqlcontent = sqlcontent.replaceAll("\\$([0-9]*)\\$", "'19700101'");
sqlcontent = sqlcontent.replaceAll("\\$(-[0-9]*)\\$", "'19700101'");
sqlcontent = sqlcontent.replaceAll("\\$([currentdate]*)\\$", "1970-01-01");
sqlcontent = sqlcontent.replaceAll("\\$([billid|currentuser|currentdept|wfcreater|wfcredept|id|requestid]*)\\$", "0");
sqlcontent = sqlcontent.replaceAll("PARM\\(.*?\\)","0");
if(!"".equals(sqlcontent)){
if("".equals(datasourceid) || "null".equals(datasourceid)){
try{
statement = new ConnStatement();
statement.setStatementSql(sqlcontent);
statement.executeQuery();
isCanUse = true;
}catch(Exception e){
errormsg = e.getMessage();
isCanUse = false;
}finally{
try{
statement.close();
statement = null;
}catch(Exception e){
errormsg = e.getMessage();
//极有可能出错,出错时不做任何处理
}
}
}else{//数据源
Connection conn = null;
Statement stmt = null;
try{
String sql = "select a.*,b.driverclass, b.driverurl from datasourcesetting a,datasource_type b where a.pointid ='"+datasourceid+"' and b.dbtype=a.type";
String dbtype = "";
RecordSet RecordSet = new RecordSet();
RecordSet.execute(sql);
if(RecordSet.next()){
dbtype = RecordSet.getString("type");
}
if(dbtype.equals("db2")){
String url = Util.null2String(RecordSet.getString("url"));
String host = Util.null2String(RecordSet.getString("host"));
String port = Util.null2String(RecordSet.getString("port"));
String dbname = Util.null2String(RecordSet.getString("dbname"));
url = SecurityHelper.decrypt(SecurityHelper.KEY, url);
host = SecurityHelper.decrypt(SecurityHelper.KEY, host);
port = SecurityHelper.decrypt(SecurityHelper.KEY, port);
dbname = SecurityHelper.decrypt(SecurityHelper.KEY, dbname);
String forname = Util.null2String(RecordSet.getString("driverclass"));
String turl = Util.null2String(RecordSet.getString("driverurl"));
String username = Util.null2String(RecordSet.getString("username"));
String password = Util.null2String(RecordSet.getString("password"));
String iscode = Util.null2String(RecordSet.getString("iscode"));
if("1".equals(iscode)) {
username = SecurityHelper.decrypt(SecurityHelper.KEY, username);
password = SecurityHelper.decrypt(SecurityHelper.KEY, password);
}
if(!"".equals(turl)) {
turl = turl.replace("[host]", host).replace("[port]", port);
if(turl.indexOf("[servicename]") > -1) {// informix专用
String servicename = "myserver";
if(dbname.indexOf("@$") > -1) {
String dbnametemp = dbname;
dbname = dbnametemp.substring(0, dbnametemp.indexOf("@$"));
servicename = dbnametemp.substring(dbnametemp.indexOf("@$") + 2);
}
turl = turl.replace("[dbname]", dbname).replace("[servicename]", servicename);
} else {
turl = turl.replace("[dbname]", dbname);
}
}
Driver driver = (Driver)Class.forName(forname).newInstance();
DriverManager.registerDriver(driver);
conn = DriverManager.getConnection(turl, username, password);
}else{
conn = automaticconnect.getConnection("datasource."+datasourceid);//获得外部连接
}
stmt = conn.createStatement();
stmt.executeQuery(sqlcontent);
isCanUse = true;
}catch(Exception e){
errormsg = e.getMessage();
isCanUse = false;
}finally{
try{
stmt.close();
conn.close();
}catch(Exception e){
errormsg = e.getMessage();
}
}
}
}else{
isCanUse = true;
}
}catch(Exception e){
errormsg = e.getMessage();
isCanUse = false;
}
if(isCanUse){
errormsg = "";
}else{
if(errormsg.equals("null")){
errormsg = "";
}
}
if("1".equals(isFormMode)){
jsonObject.put("isCanUse",isCanUse);
jsonObject.put("errormsg",errormsg);
response.getWriter().write(jsonObject.toString());
return;
}
%>
<information>
<iscanuse><%=isCanUse%></iscanuse>
</information>