|
|
@@ -0,0 +1,108 @@
|
|
|
+package com.hrsk.cloud.eg.infrastructure.clinent.utils;
|
|
|
+
|
|
|
+import com.alibaba.cola.exception.SysException;
|
|
|
+import com.caucho.hessian.io.Hessian2Input;
|
|
|
+import com.caucho.hessian.io.Hessian2Output;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.hrsk.cloud.op.eg.domain.api.FunctionObject;
|
|
|
+import com.hrsk.cloud.op.eg.domain.api.HttpApiConfig;
|
|
|
+import com.hrsk.cloud.op.eg.domain.api.KeyValueObject;
|
|
|
+import com.hrsk.cloud.op.eg.domain.api.ValueObject;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author: bianlanzhou
|
|
|
+ * @create: 2024-08-02 08:43
|
|
|
+ * @description: hessian工具类
|
|
|
+ **/
|
|
|
+@Slf4j
|
|
|
+public class HessianUtils {
|
|
|
+ public static byte[] serialize(Object object){
|
|
|
+ Hessian2Output ho = null;
|
|
|
+ try{
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ ho = new Hessian2Output(bos);
|
|
|
+ ho.writeObject(object);
|
|
|
+ ho.flush();
|
|
|
+ return bos.toByteArray();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("序列化对象异常!",e);
|
|
|
+ throw new SysException("序列化对象异常!",e);
|
|
|
+ }finally {
|
|
|
+ if(ho !=null){
|
|
|
+ try {
|
|
|
+ ho.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("序列化流关闭失败!",e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** Hessian反序列化 */
|
|
|
+ public static Object deserialize(byte[] bytes){
|
|
|
+ Hessian2Input hi = null;
|
|
|
+ try{
|
|
|
+ ByteArrayInputStream is = new ByteArrayInputStream(bytes);
|
|
|
+ hi = new Hessian2Input(is);
|
|
|
+ return hi.readObject();
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("反序列化对象异常!",e);
|
|
|
+ throw new SysException("反序列化对象异常!",e);
|
|
|
+ }finally {
|
|
|
+ if(hi !=null){
|
|
|
+ try {
|
|
|
+ hi.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("序列化流关闭失败!",e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ FunctionObject md5 = new FunctionObject();
|
|
|
+ md5.setCode("md5");
|
|
|
+ HttpApiConfig hac = new HttpApiConfig()
|
|
|
+ .setUrl("https://baidu.com");
|
|
|
+ hac.setType("http");
|
|
|
+ List<KeyValueObject> h = Lists.newArrayList();
|
|
|
+ KeyValueObject h1 = new KeyValueObject();
|
|
|
+ h1.setKey("secretKey");
|
|
|
+ h1.setType("text");
|
|
|
+ h1.setValue("Khw99%$l%-2gF7P4jXglp_-jQqQirtYnOY5D");
|
|
|
+ h.add(h1);
|
|
|
+ KeyValueObject h2 = new KeyValueObject();
|
|
|
+ h2.setKey("merchantNo");
|
|
|
+ h2.setType("spel");
|
|
|
+ h2.setValue("#input.merchantNo");
|
|
|
+ h.add(h2);
|
|
|
+ List<ValueObject> p = Lists.newArrayList();
|
|
|
+ ValueObject p1 = new ValueObject();
|
|
|
+ p1.setType("text");
|
|
|
+ p1.setValue("123");
|
|
|
+ ValueObject p2 = new ValueObject();
|
|
|
+ p2.setType("text");
|
|
|
+ p2.setValue("456");
|
|
|
+ p2.getFunctions().add(md5);
|
|
|
+ p.add(p1);
|
|
|
+ p.add(p2);
|
|
|
+ List<KeyValueObject> s = Lists.newArrayList();
|
|
|
+ KeyValueObject s1 = new KeyValueObject();
|
|
|
+ s1.setKey("username");
|
|
|
+ s1.setType("spel");
|
|
|
+ s1.setValue("#input.username");
|
|
|
+ KeyValueObject s2 = new KeyValueObject();
|
|
|
+ s2.setKey("channel");
|
|
|
+ s2.setType("spel");
|
|
|
+ s2.setValue("#input.channel");
|
|
|
+ s.add(s2);
|
|
|
+ hac.setPath(p).setSearch(s).setHeader(h).setMethod("GET");
|
|
|
+ hac.setContent("{\"merchant\":\"xxxxxxfffff\",\"phone\":\"#{\\\"value\\\":\\\"#input.phone\\\",\\\"type\\\": \\\"spel\\\",\\\"functions\\\":[ {\\\"code\\\": \\\"md5\\\"}]}\"}");
|
|
|
+ }
|
|
|
+}
|