add HessianSerializer
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-02-13 11:56:06 +08:00
parent ebecb83c05
commit b864ec8161
33 changed files with 687 additions and 35 deletions

View File

@@ -1,3 +1,27 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.ehlxr.did;
import io.github.ehlxr.did.common.JsonUtils;

View File

@@ -24,9 +24,6 @@
package io.github.ehlxr.did.adapter;
import io.github.ehlxr.did.common.Try;
import io.github.ehlxr.did.serializer.SerializerHolder;
import java.io.Serializable;
/**
@@ -56,12 +53,6 @@ public class Message<T> implements Serializable {
*/
private T content;
public Message(byte type, byte flag, T content) {
this.type = type;
this.flag = flag;
this.content = content;
}
public Message() {
}
@@ -86,20 +77,31 @@ public class Message<T> implements Serializable {
}
public int getLength() {
return getContent().length;
return length;
}
public byte[] getContent() {
return Try.<T, byte[]>of(SerializerHolder.get()::serializer).apply(content).get();
public void setLength(int length) {
this.length = length;
}
public T getContent() {
return content;
}
// public int getLength() {
// return getContent().length;
// }
// public byte[] getContent() {
// return Try.<T, byte[]>of(SerializerHolder.get()::serializer).apply(content).get();
// }
public void setContent(T content) {
this.content = content;
}
public T content(Class<T> clazz) {
return SerializerHolder.get().deserializer(getContent(), clazz);
}
// public T content(Class<T> clazz) {
// return SerializerHolder.get().deserializer(getContent(), clazz);
// }
@Override
public String toString() {
@@ -114,6 +116,7 @@ public class Message<T> implements Serializable {
public static final class MessageBuilder<T> {
private byte type;
private byte flag;
private int length;
private T content;
private MessageBuilder() {
@@ -129,6 +132,11 @@ public class Message<T> implements Serializable {
return this;
}
public MessageBuilder<T> length(int length) {
this.length = length;
return this;
}
public MessageBuilder<T> content(T content) {
this.content = content;
return this;
@@ -138,6 +146,7 @@ public class Message<T> implements Serializable {
Message<T> message = new Message<>();
message.setType(type);
message.setFlag(flag);
message.setLength(length);
message.setContent(content);
return message;
}

View File

@@ -91,6 +91,7 @@ public class MessageDecoder extends LengthFieldBasedFrameDecoder {
return Message.newBuilder()
.type(type)
.flag(flag)
.length(bytes.length)
.content(SerializerHolder.get().deserializer(bytes, SdkProto.class))
.build();
}

View File

@@ -25,6 +25,8 @@
package io.github.ehlxr.did.adapter;
import io.github.ehlxr.did.SdkProto;
import io.github.ehlxr.did.common.Try;
import io.github.ehlxr.did.serializer.SerializerHolder;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
@@ -40,11 +42,13 @@ public class MessageEncoder extends MessageToByteEncoder<Message<SdkProto>> {
@Override
protected void encode(ChannelHandlerContext ctx, Message<SdkProto> msg, ByteBuf out) {
Objects.requireNonNull(msg, "encode failed 'cause of recive message is null");
SdkProto content = msg.getContent();
byte[] bytes = Try.<SdkProto, byte[]>of(SerializerHolder.get()::serializer).apply(content).get();
out.writeByte(msg.getType());
out.writeByte(msg.getFlag());
out.writeInt(msg.getLength());
out.writeBytes(msg.getContent());
out.writeInt(bytes.length);
out.writeBytes(bytes);
}
}

View File

@@ -1,3 +1,27 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.ehlxr.did.serializer;
import io.github.ehlxr.did.extension.SPI;

View File

@@ -1,3 +1,27 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.ehlxr.did.serializer;
import io.github.ehlxr.did.common.Constants;
@@ -7,7 +31,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Objects;
import java.util.Set;
/**
* @author ehlxr

View File

@@ -0,0 +1,70 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.ehlxr.did.serializer.impl;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import io.github.ehlxr.did.common.Try;
import io.github.ehlxr.did.serializer.Serializer;
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author ehlxr
* @since 2021-02-09 11:07.
*/
public class HessianSerializer implements Serializer {
@Override
public <T> byte[] serializer(T obj) {
return Try.<T, byte[]>of(o -> {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
HessianOutput ho = new HessianOutput(bos);
ho.writeObject(obj);
ho.flush();
return bos.toByteArray();
}).trap(Throwable::printStackTrace).apply(obj).get();
}
@Override
public <T> T deserializer(byte[] bytes, Class<T> clazz) {
return Try.<byte[], T>of(bs -> {
ByteArrayInputStream bis = new ByteArrayInputStream(bs);
HessianInput hi = new HessianInput(bis);
Object o = hi.readObject();
return clazz.isInstance(o) ? clazz.cast(o) : null;
}).trap(Throwable::printStackTrace).apply(bytes).get();
}
}

View File

@@ -22,9 +22,10 @@
* THE SOFTWARE.
*/
package io.github.ehlxr.did.serializer;
package io.github.ehlxr.did.serializer.impl;
import io.github.ehlxr.did.common.Try;
import io.github.ehlxr.did.serializer.Serializer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

View File

@@ -22,9 +22,10 @@
* THE SOFTWARE.
*/
package io.github.ehlxr.did.serializer;
package io.github.ehlxr.did.serializer.impl;
import io.github.ehlxr.did.common.Try;
import io.github.ehlxr.did.serializer.Serializer;
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;

View File

@@ -1,3 +1,4 @@
#protostuff1=io.github.ehlxr.did.serializer.ProtostuffSerializer
protostuff=io.github.ehlxr.did.serializer.ProtostuffSerializer
io.github.ehlxr.did.serializer.JdkSerializer
#protostuff1=io.github.ehlxr.did.serializer.impl.ProtostuffSerializer
protostuff=io.github.ehlxr.did.serializer.impl.ProtostuffSerializer
io.github.ehlxr.did.serializer.impl.JdkSerializer
io.github.ehlxr.did.serializer.impl.HessianSerializer