Unverified Commit 12e52f86 authored by Yang Libin's avatar Yang Libin Committed by GitHub

feat: update util packages (#39)

* feat: update util packages * feat: remove json and common-lang3 packages
parent 0853d8ee
...@@ -76,21 +76,11 @@ ...@@ -76,21 +76,11 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<version>2.13.0</version> <version>2.13.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.13.2</version> <version>4.13.2</version>
......
...@@ -10,6 +10,9 @@ import java.util.Base64; ...@@ -10,6 +10,9 @@ import java.util.Base64;
*/ */
public class Base64Util { public class Base64Util {
private Base64Util() {
}
public static byte[] encodeUrl(byte[] input) { public static byte[] encodeUrl(byte[] input) {
byte[] base64 = Base64.getEncoder().encode(input); byte[] base64 = Base64.getEncoder().encode(input);
for (int i = 0; i < base64.length; ++i) { for (int i = 0; i < base64.length; ++i) {
......
...@@ -16,6 +16,9 @@ import java.util.Map; ...@@ -16,6 +16,9 @@ import java.util.Map;
* @since 2021/7/28 14:55 * @since 2021/7/28 14:55
*/ */
public class HttpUtil { public class HttpUtil {
private HttpUtil() {
}
/** /**
* Send a get request * Send a get request
* *
...@@ -106,7 +109,7 @@ public class HttpUtil { ...@@ -106,7 +109,7 @@ public class HttpUtil {
Map<String, String> headers) throws IOException { Map<String, String> headers) throws IOException {
// set content type // set content type
if (headers == null) { if (headers == null) {
headers = new HashMap<String, String>(); headers = new HashMap<>(2);
} }
headers.put("Content-Type", "application/x-www-form-urlencoded"); headers.put("Content-Type", "application/x-www-form-urlencoded");
......
package io.github.doocs.im.util; package io.github.doocs.im.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.commons.lang3.SerializationException; import com.fasterxml.jackson.databind.json.JsonMapper;
import java.io.IOException;
/** /**
* JSON tool * JSON tool
...@@ -18,48 +15,22 @@ import java.io.IOException; ...@@ -18,48 +15,22 @@ import java.io.IOException;
*/ */
public class JsonUtil { public class JsonUtil {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final ObjectMapper INDENT_OUTPUT_OBJECT_MAPPER = new ObjectMapper();
static { static {
OBJECT_MAPPER.enable(MapperFeature.USE_GETTERS_AS_SETTERS); JsonMapper.builder().enable(MapperFeature.USE_GETTERS_AS_SETTERS);
OBJECT_MAPPER.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS); JsonMapper.builder().enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS);
OBJECT_MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); OBJECT_MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
INDENT_OUTPUT_OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
INDENT_OUTPUT_OBJECT_MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
INDENT_OUTPUT_OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
} }
private JsonUtil() { private JsonUtil() {
} }
public static String obj2Str(Object object) { public static String obj2Str(Object object) throws JsonProcessingException {
String str = ""; return OBJECT_MAPPER.writeValueAsString(object);
try {
str = OBJECT_MAPPER.writeValueAsString(object);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return str;
} }
public static <T> T str2Obj(String str, Class<T> cls) { public static <T> T str2Obj(String str, Class<T> cls) throws JsonProcessingException {
T object = null; return OBJECT_MAPPER.readValue(str, cls);
try {
object = OBJECT_MAPPER.readValue(str, cls);
} catch (IOException e) {
e.printStackTrace();
}
return object;
}
public static void writeValueWithoutNullValue(Object object) {
try {
if (null != object) {
INDENT_OUTPUT_OBJECT_MAPPER.writeValueAsString(object);
}
} catch (IOException e) {
throw new SerializationException(e);
}
} }
} }
package io.github.doocs.im.util; package io.github.doocs.im.util;
import org.json.JSONObject; import com.fasterxml.jackson.core.JsonProcessingException;
import javax.crypto.Mac; import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
...@@ -9,6 +9,8 @@ import java.security.InvalidKeyException; ...@@ -9,6 +9,8 @@ import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Base64; import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.Deflater; import java.util.zip.Deflater;
/** /**
...@@ -19,6 +21,10 @@ import java.util.zip.Deflater; ...@@ -19,6 +21,10 @@ import java.util.zip.Deflater;
*/ */
public class SigUtil { public class SigUtil {
private SigUtil() {
}
/** /**
* 【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据 * 【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
* <p> * <p>
...@@ -116,13 +122,12 @@ public class SigUtil { ...@@ -116,13 +122,12 @@ public class SigUtil {
private static String genUserSig(long sdkAppId, String key, String userid, long expire, byte[] userbuf) { private static String genUserSig(long sdkAppId, String key, String userid, long expire, byte[] userbuf) {
long currTime = System.currentTimeMillis() / 1000; long currTime = System.currentTimeMillis() / 1000;
JSONObject sigDoc = new JSONObject(); Map<String, Object> sigDoc = new HashMap<>(8);
sigDoc.put("TLS.ver", "2.0"); sigDoc.put("TLS.ver", "2.0");
sigDoc.put("TLS.identifier", userid); sigDoc.put("TLS.identifier", userid);
sigDoc.put("TLS.sdkappid", sdkAppId); sigDoc.put("TLS.sdkappid", sdkAppId);
sigDoc.put("TLS.expire", expire); sigDoc.put("TLS.expire", expire);
sigDoc.put("TLS.time", currTime); sigDoc.put("TLS.time", currTime);
String base64UserBuf = null; String base64UserBuf = null;
if (null != userbuf) { if (null != userbuf) {
base64UserBuf = Base64.getEncoder().encodeToString(userbuf).replaceAll("\\s*", ""); base64UserBuf = Base64.getEncoder().encodeToString(userbuf).replaceAll("\\s*", "");
...@@ -134,7 +139,11 @@ public class SigUtil { ...@@ -134,7 +139,11 @@ public class SigUtil {
} }
sigDoc.put("TLS.sig", sig); sigDoc.put("TLS.sig", sig);
Deflater compressor = new Deflater(); Deflater compressor = new Deflater();
compressor.setInput(sigDoc.toString().getBytes(StandardCharsets.UTF_8)); try {
compressor.setInput(JsonUtil.obj2Str(sigDoc).getBytes(StandardCharsets.UTF_8));
} catch (JsonProcessingException e) {
compressor.setInput(new byte[]{});
}
compressor.finish(); compressor.finish();
byte[] compressedBytes = new byte[2048]; byte[] compressedBytes = new byte[2048];
int compressedBytesLength = compressor.deflate(compressedBytes); int compressedBytesLength = compressor.deflate(compressedBytes);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment