parent
e1026c8002
commit
cdc4a479c3
@ -1,17 +0,0 @@
|
||||
package xyz.wbsite.dbtool.web.frame.auth;
|
||||
|
||||
import xyz.wbsite.dbtool.web.frame.validation.DictValidator;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Constraint(validatedBy = DictValidator.class)
|
||||
public @interface Verification {
|
||||
String name() default "";
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package xyz.wbsite.dbtool.web.frame.schedule;
|
||||
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.support.CronTrigger;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
public abstract class RunCronTask extends RunTask {
|
||||
|
||||
public abstract String cron();
|
||||
|
||||
@Override
|
||||
public ScheduledFuture<?> schedule(ThreadPoolTaskScheduler poolTaskScheduler) {
|
||||
Assert.notNull(poolTaskScheduler, "ThreadPoolTaskScheduler must not be null");
|
||||
return poolTaskScheduler.schedule(this, new CronTrigger(cron()));
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package xyz.wbsite.dbtool.web.frame.schedule;
|
||||
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
public abstract class RunDelayRepeatTask extends RunTask {
|
||||
|
||||
public abstract Duration interval();
|
||||
|
||||
@Override
|
||||
public ScheduledFuture<?> schedule(ThreadPoolTaskScheduler poolTaskScheduler) {
|
||||
Assert.notNull(poolTaskScheduler, "ThreadPoolTaskScheduler must not be null");
|
||||
return poolTaskScheduler.scheduleWithFixedDelay(this, interval());
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package xyz.wbsite.dbtool.web.frame.schedule;
|
||||
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
public abstract class RunFixRepeatTask extends RunTask {
|
||||
|
||||
public abstract Duration interval();
|
||||
|
||||
@Override
|
||||
public ScheduledFuture<?> schedule(ThreadPoolTaskScheduler poolTaskScheduler) {
|
||||
Assert.notNull(poolTaskScheduler, "ThreadPoolTaskScheduler must not be null");
|
||||
return poolTaskScheduler.scheduleAtFixedRate(this, interval());
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package xyz.wbsite.dbtool.web.frame.schedule;
|
||||
|
||||
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
public abstract class RunTask implements Runnable {
|
||||
|
||||
public abstract String taskId();
|
||||
|
||||
public String taskName(){
|
||||
return "";
|
||||
}
|
||||
|
||||
public String taskNote(){
|
||||
return "";
|
||||
}
|
||||
|
||||
public abstract ScheduledFuture<?> schedule(ThreadPoolTaskScheduler poolTaskScheduler);
|
||||
|
||||
public void configChange(ThreadPoolTaskScheduler scheduler) {
|
||||
ScheduledFuture<?> schedule = scheduler.schedule(this, new Date());
|
||||
if (!schedule.cancel(true)) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package xyz.wbsite.dbtool.web.frame.utils;
|
||||
|
||||
/**
|
||||
* BytesUtil - 字节数组工具类
|
||||
*
|
||||
* @author wangbing
|
||||
* @version 0.0.1
|
||||
* @since 2017-01-01
|
||||
*/
|
||||
public class BytesUtil {
|
||||
|
||||
private static final char[] HEXES = {
|
||||
'0', '1', '2', '3',
|
||||
'4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b',
|
||||
'c', 'd', 'e', 'f'
|
||||
};
|
||||
|
||||
/**
|
||||
* byte数组转16进制字符串
|
||||
*/
|
||||
public static String bytes2Hex(byte[] bytes) {
|
||||
if (bytes == null || bytes.length == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder hex = new StringBuilder();
|
||||
|
||||
for (byte b : bytes) {
|
||||
hex.append(HEXES[(b >> 4) & 0x0F]);
|
||||
hex.append(HEXES[b & 0x0F]);
|
||||
}
|
||||
|
||||
return hex.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 16进制字符串转byte数组
|
||||
*/
|
||||
public static byte[] hex2Bytes(String hex) {
|
||||
if (hex == null || hex.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
char[] hexChars = hex.toCharArray();
|
||||
byte[] bytes = new byte[hexChars.length / 2];
|
||||
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
bytes[i] = (byte) Integer.parseInt("" + hexChars[i * 2] + hexChars[i * 2 + 1], 16);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package xyz.wbsite.dbtool.web.frame.utils;
|
||||
|
||||
|
||||
public class Formatter {
|
||||
|
||||
public static String formatJava(String content) {
|
||||
// 聚合
|
||||
content = content.replaceAll("\\s+|\\n", " ");
|
||||
content = content.replaceAll(";\\s", ";");
|
||||
content = content.replaceAll("\\s?\\{\\s?", "{");
|
||||
content = content.replaceAll("\\s?}\\s?", "}");
|
||||
content = content.replaceAll("\\*/\\s?", "\\*/");
|
||||
//分散
|
||||
content = content.replaceAll("\\*/", "*/\n");
|
||||
content = content.replaceAll(";", ";\n");
|
||||
content = content.replaceAll("\\{", "\n{\n");
|
||||
content = content.replaceAll("}", "}\n");
|
||||
return content;
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
package xyz.wbsite.dbtool.web.frame.utils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* RequestUtil
|
||||
*
|
||||
* @author wangbing
|
||||
* @version 0.0.1
|
||||
* @since 2017-01-01
|
||||
*/
|
||||
public class RequestUtil {
|
||||
|
||||
/**
|
||||
* 获取请求放IP
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public static String getIp(HttpServletRequest request) {
|
||||
String ip = request.getHeader("X-Forwarded-For");
|
||||
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("Proxy-Client-IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||
}
|
||||
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("HTTP_CLIENT_IP");
|
||||
}
|
||||
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
||||
}
|
||||
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getRemoteAddr();
|
||||
}
|
||||
|
||||
} else if (ip.length() > 15) {
|
||||
String[] ips = ip.split(",");
|
||||
for (int index = 0; index < ips.length; index++) {
|
||||
String strIp = (String) ips[index];
|
||||
if (!("unknown".equalsIgnoreCase(strIp))) {
|
||||
ip = strIp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取情况方客户端信息
|
||||
*
|
||||
* @param request 请求
|
||||
* @return 客户端信息
|
||||
*/
|
||||
public static String getUserAgent(HttpServletRequest request) {
|
||||
return request.getHeader("User-Agent");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取转发至错误页之前的请求URL
|
||||
*
|
||||
* @param request 请求
|
||||
* @return 请求URL
|
||||
*/
|
||||
public static String getErrorUrl(HttpServletRequest request) {
|
||||
if (request.getAttribute("javax.servlet.error.request_uri") != null) {
|
||||
return (String) request.getAttribute("javax.servlet.error.request_uri");
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
package xyz.wbsite.dbtool.web.frame.utils;
|
||||
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import xyz.wbsite.dbtool.web.frame.auth.LocalData;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class SqlUtil {
|
||||
|
||||
public static void exec(String sql) {
|
||||
SqlSession sqlSession = null;
|
||||
try {
|
||||
SqlSessionFactory factory = LocalData.getBean(SqlSessionFactory.class);
|
||||
sqlSession = factory.openSession(true);
|
||||
Connection connection = sqlSession.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql);
|
||||
statement.execute();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(String.format("[ %s ]执行错误!", sql));
|
||||
} finally {
|
||||
if (sqlSession != null) sqlSession.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static int insert(String sql) {
|
||||
return update(sql);
|
||||
}
|
||||
|
||||
public static int delete(String sql) {
|
||||
return update(sql);
|
||||
}
|
||||
|
||||
public static int update(String sql) {
|
||||
SqlSession sqlSession = null;
|
||||
int result = 0;
|
||||
try {
|
||||
SqlSessionFactory factory = LocalData.getBean(SqlSessionFactory.class);
|
||||
sqlSession = factory.openSession(true);
|
||||
Connection connection = sqlSession.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql);
|
||||
result = statement.executeUpdate();
|
||||
statement.close();
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(String.format("[ %s ]执行错误!", sql));
|
||||
} finally {
|
||||
if (sqlSession != null) sqlSession.close();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> List<T> select(String sql, Class<T> t) {
|
||||
List<T> result = new ArrayList<>();
|
||||
try {
|
||||
SqlSessionFactory factory = LocalData.getBean(SqlSessionFactory.class);
|
||||
SqlSession sqlSession = factory.openSession(true);
|
||||
Connection connection = sqlSession.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
T instance = t.newInstance();
|
||||
Field[] fields = t.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
Method method = ClassUtil.setMethod(field.getName(), t, field.getType());
|
||||
if (field.getType() == String.class) {
|
||||
String v = resultSet.getString(field.getName());
|
||||
method.invoke(instance, v);
|
||||
} else if (field.getType() == Boolean.class || field.getType() == boolean.class) {
|
||||
boolean v = resultSet.getBoolean(field.getName());
|
||||
method.invoke(instance, v);
|
||||
} else if (field.getType() == Byte.class || field.getType() == byte.class) {
|
||||
byte v = resultSet.getByte(field.getName());
|
||||
method.invoke(instance, v);
|
||||
} else if (field.getType() == Short.class || field.getType() == short.class) {
|
||||
short v = resultSet.getShort(field.getName());
|
||||
method.invoke(instance, v);
|
||||
} else if (field.getType() == Character.class || field.getType() == char.class) {
|
||||
short v = resultSet.getShort(field.getName());
|
||||
method.invoke(instance, (char) v);
|
||||
} else if (field.getType() == Integer.class || field.getType() == int.class) {
|
||||
int v = resultSet.getInt(field.getName());
|
||||
method.invoke(instance, v);
|
||||
} else if (field.getType() == Long.class || field.getType() == long.class) {
|
||||
long v = resultSet.getLong(field.getName());
|
||||
method.invoke(instance, v);
|
||||
} else if (field.getType() == Float.class || field.getType() == float.class) {
|
||||
float v = resultSet.getFloat(field.getName());
|
||||
method.invoke(instance, v);
|
||||
} else if (field.getType() == Double.class || field.getType() == double.class) {
|
||||
double v = resultSet.getDouble(field.getName());
|
||||
method.invoke(instance, v);
|
||||
} else if (field.getType() == Byte[].class || field.getType() == byte[].class) {
|
||||
byte[] v = resultSet.getBytes(field.getName());
|
||||
method.invoke(instance, v);
|
||||
} else if (field.getType() == Date.class) {
|
||||
Date v = resultSet.getDate(field.getName());
|
||||
method.invoke(instance, v);
|
||||
} else {
|
||||
String v = resultSet.getString(field.getName());
|
||||
method.invoke(instance, v);
|
||||
}
|
||||
}
|
||||
result.add(instance);
|
||||
}
|
||||
statement.close();
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InstantiationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package xyz.wbsite.dbtool.web.frame.validation;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Constraint(validatedBy = DictValidator.class)
|
||||
public @interface Dict {
|
||||
String message() default "字典项错误";
|
||||
|
||||
String name() default "";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package xyz.wbsite.dbtool.web.frame.validation;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class DictValidator implements ConstraintValidator<Dict, String> {
|
||||
|
||||
private String name;
|
||||
|
||||
@Override
|
||||
public void initialize(Dict constraint) {
|
||||
name = constraint.name();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String o, ConstraintValidatorContext constraintValidatorContext) {
|
||||
if (null == o) {
|
||||
return true;
|
||||
} else if (name == null) {
|
||||
constraintValidatorContext.disableDefaultConstraintViolation();
|
||||
constraintValidatorContext.buildConstraintViolationWithTemplate("字典名称为空").addConstraintViolation();
|
||||
return false;
|
||||
} else {
|
||||
// name 字典名称
|
||||
HashSet<String> codeSet = new HashSet<>();
|
||||
if (codeSet.contains(o)) {
|
||||
return true;
|
||||
} else {
|
||||
constraintValidatorContext.disableDefaultConstraintViolation();
|
||||
constraintValidatorContext.buildConstraintViolationWithTemplate("非法的字典[" + name + "]值").addConstraintViolation();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue