Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
xuxueli committed Jun 30, 2024
1 parent 0fc8ec7 commit b4abab4
Show file tree
Hide file tree
Showing 6 changed files with 364 additions and 1 deletion.
22 changes: 21 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@
<!-- quasar(fiber) -->
<quasar-core.version>0.7.9</quasar-core.version>
<quasar-maven-plugin.version>0.7.9</quasar-maven-plugin.version>

<!-- spring(pipeline) -->
<spring.version>5.3.34</spring.version>
<!-- javax(net tool) -->
<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
<!-- freemarker(ftl tool) -->
<freemarker.version>2.3.33</freemarker.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -93,6 +97,22 @@
<scope>provided</scope>
</dependency>

<!-- ********************** servlet-api(support web) ********************** -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-api.version}</version>
<scope>provided</scope>
</dependency>

<!-- ********************** freemarker(support ftl tool) ********************** -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker.version}</version>
<scope>provided</scope>
</dependency>

</dependencies>


Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/xxl/tool/cache/CacheData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//package com.xxl.tool.cache;
//
///**
// * simpie local cache tool
// *
// * @author xuxueli 2018-01-22 21:37:34
// */
//public class CacheData {
//
// private String key;
// private Object val;
// private long timeoutTime;
//
// public CacheData() {
// }
// public CacheData(String key, Object val, long timeoutTime) {
// this.key = key;
// this.val = val;
// this.timeoutTime = timeoutTime;
// }
//
// public String getKey() {
// return key;
// }
//
// public void setKey(String key) {
// this.key = key;
// }
//
// public Object getVal() {
// return val;
// }
//
// public void setVal(Object val) {
// this.val = val;
// }
//
// public long getTimeoutTime() {
// return timeoutTime;
// }
//
// public void setTimeoutTime(long timeoutTime) {
// this.timeoutTime = timeoutTime;
// }
//
//}
98 changes: 98 additions & 0 deletions src/main/java/com/xxl/tool/cache/CacheTool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//package com.xxl.tool.cache;
//
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.concurrent.ConcurrentMap;
//
///**
// * local cache tool
// *
// * // TODO,data + conf + manage + tool;超时、定长、LRU、LFU、主动更新
// *
// * @author xuxueli 2018-01-22 21:37:34
// */
//public class CacheTool {
//
// // cache data
// private static ConcurrentMap<String, CacheData> cacheRepository = new ConcurrentHashMap<>();
//
//
// /**
// * set cache
// *
// * @param key
// * @param val
// * @param cacheTime
// * @return
// */
// public static boolean set(String key, Object val, long cacheTime){
//
// // clean timeout cache, before set new cache (avoid cache too much)
// cleanTimeoutCache();
//
// // set new cache
// if (key==null || key.trim().length()==0) {
// return false;
// }
// if (val == null) {
// remove(key);
// }
// if (cacheTime <= 0) {
// remove(key);
// }
// long timeoutTime = System.currentTimeMillis() + cacheTime;
// CacheData localCacheData = new CacheData(key, val, timeoutTime);
// cacheRepository.put(localCacheData.getKey(), localCacheData);
// return true;
// }
//
// /**
// * remove cache
// *
// * @param key
// * @return
// */
// public static boolean remove(String key){
// if (key==null || key.trim().length()==0) {
// return false;
// }
// cacheRepository.remove(key);
// return true;
// }
//
// /**
// * get cache
// *
// * @param key
// * @return
// */
// public static Object get(String key){
// if (key==null || key.trim().length()==0) {
// return null;
// }
// CacheData localCacheData = cacheRepository.get(key);
// if (localCacheData!=null && System.currentTimeMillis()<localCacheData.getTimeoutTime()) {
// return localCacheData.getVal();
// } else {
// remove(key);
// return null;
// }
// }
//
// /**
// * clean timeout cache
// *
// * @return
// */
// public static boolean cleanTimeoutCache(){
// if (!cacheRepository.keySet().isEmpty()) {
// for (String key: cacheRepository.keySet()) {
// CacheData localCacheData = cacheRepository.get(key);
// if (localCacheData!=null && System.currentTimeMillis()>=localCacheData.getTimeoutTime()) {
// cacheRepository.remove(key);
// }
// }
// }
// return true;
// }
//
//}
19 changes: 19 additions & 0 deletions src/main/java/com/xxl/tool/exception/BizException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.xxl.tool.exception;

/**
* @author xuxueli 2024-06-30
*/
public class BizException extends RuntimeException {

public BizException() {
}

public BizException(String message) {
super(message);
}

public BizException(Throwable cause) {
super(cause);
}

}
41 changes: 41 additions & 0 deletions src/main/java/com/xxl/tool/ftl/FtlTool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.xxl.tool.ftl;

import com.xxl.tool.exception.BizException;
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.template.Configuration;
import freemarker.template.TemplateHashModel;

/**
* ftl tool
*
* @author xuxueli 2018-01-17 20:37:48
*/
public class FtlTool {

// 静态包装器
private static BeansWrapper wrapper = new BeansWrapperBuilder(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS).build(); //BeansWrapper.getDefaultInstance();

/**
* 生成 freemarker 自定义方法
*
* <pre>
* // 自定义方法包装;
* TemplateHashModel model = FtlTool.generateStaticModel(I18nUtil.class.getName())
* // 注入SpringMVC响应对象,逻辑运营在 “AsyncHandlerInterceptor.postHandle” 中;
* modelAndView.addObject("I18nUtil", model);
* </pre>
*
* @param packageName
* @return
*/
public static TemplateHashModel generateStaticModel(String packageName) {
try {
TemplateHashModel staticModels = wrapper.getStaticModels();
TemplateHashModel fileStatics = (TemplateHashModel) staticModels.get(packageName);
return fileStatics;
} catch (Exception e) {
throw new BizException(e);
}
}
}
139 changes: 139 additions & 0 deletions src/main/java/com/xxl/tool/net/CookieTool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package com.xxl.tool.net;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
* Cookie Tool
*
* @author xuxueli 2015-12-12 18:01:06
*/
public class CookieTool {

/**
* 默认缓存时间,单位/秒, 2H
*/
private static final int COOKIE_MAX_AGE = Integer.MAX_VALUE;
/**
* 保存路径,根路径
*/
private static final String COOKIE_PATH = "/";

/**
* add cookie
*
* @param response
* @param key
* @param value
* @param domain
* @param path
* @param maxAge : >0 存活秒数,=0 删除, <0 浏览器推出则销毁;
* @param isHttpOnly
*/
private static void set(HttpServletResponse response, String key, String value,
String domain, String path, int maxAge, boolean isHttpOnly) {

// encode value
try {
value = URLEncoder.encode(value, "utf-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException();
}

// add cookie
Cookie cookie = new Cookie(key, value);
if (domain != null) {
cookie.setDomain(domain);
}
cookie.setPath(path);
cookie.setMaxAge(maxAge);
cookie.setHttpOnly(isHttpOnly);
response.addCookie(cookie);
}

/**
* 新增 cookie
*
* @param response
* @param key
* @param value
* @param ifRemember : true - 永不过期,false - 浏览器推出则销毁;
*/
public static void set(HttpServletResponse response, String key, String value, boolean ifRemember) {
int age = ifRemember?COOKIE_MAX_AGE:-1;
set(response, key, value, null, COOKIE_PATH, age, true);
}

/**
* 新增 cookie
*
* @param response
* @param key
* @param value
* @param maxAge
*/
public static void set(HttpServletResponse response, String key, String value, int maxAge) {
set(response, key, value, null, COOKIE_PATH, maxAge, true);
}

/**
* 删除 cookie
*
* @param request
* @param response
* @param key
*/
public static void remove(HttpServletRequest request, HttpServletResponse response, String key) {
Cookie cookie = get(request, key);
if (cookie != null) {
set(response, key, "", null, COOKIE_PATH, 0, true);
}
}

/**
* get cookie
*
* @param request
* @param key
*/
private static Cookie get(HttpServletRequest request, String key) {
Cookie[] arr_cookie = request.getCookies();
if (arr_cookie != null && arr_cookie.length > 0) {
for (Cookie cookie : arr_cookie) {
if (cookie.getName().equals(key)) {
return cookie;
}
}
}
return null;
}

/**
* 查询 cookie value
*
* @param request
* @param key
* @return
*/
public static String getValue(HttpServletRequest request, String key) {
Cookie cookie = get(request, key);
if (cookie == null) {
return null;
}

// decode value
String value = cookie.getValue();
try {
value = URLDecoder.decode(value, "utf-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}

return value;
}

}

0 comments on commit b4abab4

Please sign in to comment.