Skip to content

Commit

Permalink
Merge pull request #33 from pugwoo/develop-daily
Browse files Browse the repository at this point in the history
release 1.2.2
  • Loading branch information
pugwoo authored Oct 23, 2023
2 parents 41044db + b2e15e1 commit 3a4dbc0
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 40 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2023年10月23日
v1.2.2 - [fix] 修复IOUtils.readAll(InputStream, String)方法读取文件时,最后一行没有回车符却加上回车符的问题
- [enhance] 增加jakarta.servlet-api,同时支持jakarta.servlet-api和javax.servlet-api

2023年9月28日
v1.2.1 - [add] 新增DateUtils.diffDays(LocalDate,LocalDate)方法
- [fix] 修复IOUtils.readAll(InputStream, String)方法读取文件时,trim掉最后一个回车符的问题
Expand Down
14 changes: 1 addition & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<dependency>
<groupId>com.pugwoo</groupId>
<artifactId>woo-utils</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</dependency>
```

Expand All @@ -20,15 +20,3 @@
</dependency>
```

### 2. 需要使用者根据实际使用的功能自行导入的jar包:

servlet一般由运行容器提供,实际项目中也不用特别提供,所以使用provided方式引入:

```xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
```
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.pugwoo</groupId>
<artifactId>woo-utils</artifactId>
<packaging>jar</packaging>
<version>1.2.1</version>
<version>1.2.2</version>

<name>woo-utils</name>
<description>the common utils</description>
Expand Down Expand Up @@ -72,6 +72,12 @@
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
23 changes: 7 additions & 16 deletions src/main/java/com/pugwoo/wooutils/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,17 @@ public static byte[] readAllAndClose(InputStream in) throws IOException {
* @return
*/
public static String readAll(InputStream in, String charset) {
BufferedReader reader = null;
try {
InputStreamReader inputStreamReader = new InputStreamReader(in, charset);
reader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append("\n");
StringBuilder textBuilder = new StringBuilder();
try (Reader reader = new BufferedReader(new InputStreamReader(in, charset))) {
int c;
while ((c = reader.read()) != -1) {
textBuilder.append((char) c);
}
}
return stringBuilder.toString();
return textBuilder.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/pugwoo/wooutils/net/Browser.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ public void addRequestHeader(HttpServletRequest request) {
}
}

/**设置请求时的头部,该设置是Browser实例全局的。<br>
* 设置HttpServletRequest的所有头部信息
* @param request HttpServletRequest
*/
public void addRequestHeader(jakarta.servlet.http.HttpServletRequest request) {
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
addRequestHeader(headerName, request.getHeader(headerName));
}
}

/**
* 自行添加cookie
* @param domain 域名,例如abc.com
Expand Down
124 changes: 117 additions & 7 deletions src/main/java/com/pugwoo/wooutils/net/CookieUtils.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.pugwoo.wooutils.net;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* cookie相关功能,cookie默认都用URLEncode编码和解码。
* 1. 支持指定domain,所有path都设置为根目录,不支持子path的cookie,也不建议这样用,因为path很不稳定。
Expand Down Expand Up @@ -49,6 +48,32 @@ public static String getCookieValue(HttpServletRequest request, String name) {
}
return null;
}

/**
* 根据name读取cookie的值
* @param request
* @param name
* @return
*/
public static String getCookieValue(jakarta.servlet.http.HttpServletRequest request, String name) {
if(name == null) {
return null;
}
jakarta.servlet.http.Cookie[] cookies = request.getCookies();
if (null != cookies) {
for (jakarta.servlet.http.Cookie cookie : cookies) {
if(name.equals(cookie.getName())) {
try {
return URLDecoder.decode(cookie.getValue(), "UTF-8");
} catch (UnsupportedEncodingException e) {
LOGGER.error("URLEncoder.decode fail, value:{}", cookie.getValue(), e);
return cookie.getValue();
}
}
}
}
return null;
}

/**
* 根据name读取cookie的值,支持多个相同cookie name的情况
Expand Down Expand Up @@ -76,6 +101,33 @@ public static List<String> getCookieValues(HttpServletRequest request, String na
}
return cookieList;
}

/**
* 根据name读取cookie的值,支持多个相同cookie name的情况
* @param request
* @param name
* @return
*/
public static List<String> getCookieValues(jakarta.servlet.http.HttpServletRequest request, String name) {
if(name == null) {
return null;
}
List<String> cookieList = new ArrayList<String>();
jakarta.servlet.http.Cookie[] cookies = request.getCookies();
if (null != cookies) {
for (jakarta.servlet.http.Cookie cookie : cookies) {
if(name.equals(cookie.getName())) {
try {
cookieList.add(URLDecoder.decode(cookie.getValue(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
LOGGER.error("URLEncoder.decode fail, value:{}", cookie.getValue(), e);
cookieList.add(cookie.getValue());
}
}
}
}
return cookieList;
}

/**
* 增加cookie,如果name相同的话,浏览器会把cookie值追加要已有的之上
Expand Down Expand Up @@ -105,6 +157,35 @@ public static void addCookie(HttpServletResponse response, String name, String v
}
response.addCookie(cookie);
}

/**
* 增加cookie,如果name相同的话,浏览器会把cookie值追加要已有的之上
*
* @param response
* @param name cookie名字
* @param value cookie值,不建议为null值
* @param domain 指定域名,null表示不指定
* @param expireSeconds cookie生命周期 以秒为单位,当设置为0时,cookie默认有效期10年;如果删除,请用removeCookie方法
*/
public static void addCookie(jakarta.servlet.http.HttpServletResponse response, String name, String value,
String domain, int expireSeconds) {
try {
value = value == null ? null : URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
LOGGER.error("URLEncoder.encode fail, value:{}", value, e);
}
jakarta.servlet.http.Cookie cookie = new jakarta.servlet.http.Cookie(name, value);
cookie.setPath("/");
if(domain != null) {
cookie.setDomain(domain);
}
if (expireSeconds == 0) {
cookie.setMaxAge(10 * 365 * 24 * 3600);
} else {
cookie.setMaxAge(expireSeconds);
}
response.addCookie(cookie);
}

/**
* 增加cookie,如果name相同的话,浏览器会把cookie值追加要已有的之上。有效期100年。
Expand All @@ -119,6 +200,19 @@ public static void addCookie(HttpServletResponse response, String name, String v
addCookie(response, name, value, domain, 0);
}

/**
* 增加cookie,如果name相同的话,浏览器会把cookie值追加要已有的之上。有效期100年。
*
* @param response
* @param name
* @param value
* @param domain
*/
public static void addCookie(jakarta.servlet.http.HttpServletResponse response, String name, String value,
String domain) {
addCookie(response, name, value, domain, 0);
}

/**
* 删除cookie
* @param response
Expand All @@ -134,5 +228,21 @@ public static void removeCookie(HttpServletResponse response, String name, Strin
cookie.setMaxAge(0); // delete
response.addCookie(cookie);
}

/**
* 删除cookie
* @param response
* @param name
* @param domain 当为null时表示不指定
*/
public static void removeCookie(jakarta.servlet.http.HttpServletResponse response, String name, String domain) {
jakarta.servlet.http.Cookie cookie = new jakarta.servlet.http.Cookie(name, "");
cookie.setPath("/");
if(domain != null) {
cookie.setDomain(domain);
}
cookie.setMaxAge(0); // delete
response.addCookie(cookie);
}

}
Loading

0 comments on commit 3a4dbc0

Please sign in to comment.