Skip to content

Commit 854f430

Browse files
authored
[#981]hessian support needs non-serializable model (#982)
1 parent 3356ac6 commit 854f430

File tree

9 files changed

+326
-3
lines changed

9 files changed

+326
-3
lines changed

.github/workflows/maven.yml

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ jobs:
2020
uses: actions/setup-java@v1
2121
with:
2222
java-version: 1.8
23+
- name: Set up Maven
24+
uses: stCarolas/[email protected]
25+
with:
26+
maven-version: 3.8.4
2327
- uses: actions/cache@v1
2428
with:
2529
path: ~/.m2/repository

integration-tests/discovery-tests/common/src/main/java/com/huaweicloud/sample/hessian/HessianService.java

+4
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@ public interface HessianService {
2828
@PostMapping(path = "generic", consumes = "x-application/hessian2",
2929
produces = "x-application/hessian2")
3030
Generic<Base> generic(@RequestBody Generic<Base> b);
31+
32+
@PostMapping(path = "nonSerializableModel", consumes = "x-application/hessian2",
33+
produces = "x-application/hessian2")
34+
NonSerializableModel nonSerializableModel(@RequestBody NonSerializableModel b);
3135
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
3+
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
4+
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.huaweicloud.sample.hessian;
18+
19+
public class NonSerializableModel {
20+
private String age;
21+
22+
public String getAge() {
23+
return age;
24+
}
25+
26+
public void setAge(String age) {
27+
this.age = age;
28+
}
29+
}

integration-tests/discovery-tests/order-consumer/src/main/java/com/huaweicloud/sample/hessian/HessianController.java

+8
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,17 @@ public String testHessian() {
3636
testChildBase();
3737
testGenericBase();
3838
testGenericChildBase();
39+
testNonSerializableModel();
3940
return "success";
4041
}
4142

43+
private void testNonSerializableModel() {
44+
NonSerializableModel model = new NonSerializableModel();
45+
model.setAge("age");
46+
NonSerializableModel result = hessianService.nonSerializableModel(model);
47+
check("age", result.getAge(), "wrong NonSerializableModel");
48+
}
49+
4250
private void testGenericChildBase() {
4351
ChildBase b = new ChildBase();
4452
b.setName("n");

integration-tests/discovery-tests/price-provider/src/main/java/com/huaweicloud/sample/hessian/HessianController.java

+5
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,9 @@ public Base base(Base b) {
3232
public Generic<Base> generic(Generic<Base> b) {
3333
return b;
3434
}
35+
36+
@Override
37+
public NonSerializableModel nonSerializableModel(NonSerializableModel b) {
38+
return b;
39+
}
3540
}

spring-cloud-huawei-parents/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
<plugin>
169169
<groupId>com.github.odavid.maven.plugins</groupId>
170170
<artifactId>mixin-maven-plugin</artifactId>
171-
<version>0.1-alpha-39</version>
171+
<version>0.1-alpha-40</version>
172172
<extensions>true</extensions>
173173
</plugin>
174174
<plugin>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
3+
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
4+
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
/*
18+
* Licensed to the Apache Software Foundation (ASF) under one or more
19+
* contributor license agreements. See the NOTICE file distributed with
20+
* this work for additional information regarding copyright ownership.
21+
* The ASF licenses this file to You under the Apache License, Version 2.0
22+
* (the "License"); you may not use this file except in compliance with
23+
* the License. You may obtain a copy of the License at
24+
*
25+
* http://www.apache.org/licenses/LICENSE-2.0
26+
*
27+
* Unless required by applicable law or agreed to in writing, software
28+
* distributed under the License is distributed on an "AS IS" BASIS,
29+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30+
* See the License for the specific language governing permissions and
31+
* limitations under the License.
32+
*/
33+
package com.huaweicloud.hessian;
34+
35+
import java.io.IOException;
36+
import java.io.InputStream;
37+
import java.lang.reflect.Type;
38+
39+
import org.apache.dubbo.common.serialize.Cleanable;
40+
import org.apache.dubbo.common.serialize.ObjectInput;
41+
import org.apache.dubbo.common.serialize.hessian2.Hessian2SerializerFactory;
42+
43+
import com.alibaba.com.caucho.hessian.io.Hessian2Input;
44+
45+
/**
46+
* Hessian2 object input implementation. This file is based on original Dubbo implementation.
47+
* This purpose is to support non-serializable model
48+
*/
49+
public class Hessian2ObjectInput implements ObjectInput, Cleanable {
50+
51+
private static ThreadLocal<Hessian2Input> INPUT_TL = ThreadLocal.withInitial(() -> {
52+
Hessian2Input h2i = new Hessian2Input(null);
53+
Hessian2SerializerFactory factory = new Hessian2SerializerFactory();
54+
factory.setAllowNonSerializable(true);
55+
h2i.setSerializerFactory(factory);
56+
h2i.setCloseStreamOnClose(true);
57+
return h2i;
58+
});
59+
60+
private final Hessian2Input mH2i;
61+
62+
public Hessian2ObjectInput(InputStream is) {
63+
mH2i = INPUT_TL.get();
64+
mH2i.init(is);
65+
}
66+
67+
@Override
68+
public boolean readBool() throws IOException {
69+
return mH2i.readBoolean();
70+
}
71+
72+
@Override
73+
public byte readByte() throws IOException {
74+
return (byte) mH2i.readInt();
75+
}
76+
77+
@Override
78+
public short readShort() throws IOException {
79+
return (short) mH2i.readInt();
80+
}
81+
82+
@Override
83+
public int readInt() throws IOException {
84+
return mH2i.readInt();
85+
}
86+
87+
@Override
88+
public long readLong() throws IOException {
89+
return mH2i.readLong();
90+
}
91+
92+
@Override
93+
public float readFloat() throws IOException {
94+
return (float) mH2i.readDouble();
95+
}
96+
97+
@Override
98+
public double readDouble() throws IOException {
99+
return mH2i.readDouble();
100+
}
101+
102+
@Override
103+
public byte[] readBytes() throws IOException {
104+
return mH2i.readBytes();
105+
}
106+
107+
@Override
108+
public String readUTF() throws IOException {
109+
return mH2i.readString();
110+
}
111+
112+
@Override
113+
public Object readObject() throws IOException {
114+
return mH2i.readObject();
115+
}
116+
117+
@Override
118+
@SuppressWarnings("unchecked")
119+
public <T> T readObject(Class<T> cls) throws IOException,
120+
ClassNotFoundException {
121+
return (T) mH2i.readObject(cls);
122+
}
123+
124+
@Override
125+
public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFoundException {
126+
return readObject(cls);
127+
}
128+
129+
public InputStream readInputStream() throws IOException {
130+
return mH2i.readInputStream();
131+
}
132+
133+
@Override
134+
public void cleanup() {
135+
if(mH2i != null) {
136+
mH2i.reset();
137+
}
138+
}
139+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
3+
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
4+
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
/*
18+
* Licensed to the Apache Software Foundation (ASF) under one or more
19+
* contributor license agreements. See the NOTICE file distributed with
20+
* this work for additional information regarding copyright ownership.
21+
* The ASF licenses this file to You under the Apache License, Version 2.0
22+
* (the "License"); you may not use this file except in compliance with
23+
* the License. You may obtain a copy of the License at
24+
*
25+
* http://www.apache.org/licenses/LICENSE-2.0
26+
*
27+
* Unless required by applicable law or agreed to in writing, software
28+
* distributed under the License is distributed on an "AS IS" BASIS,
29+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30+
* See the License for the specific language governing permissions and
31+
* limitations under the License.
32+
*/
33+
package com.huaweicloud.hessian;
34+
35+
import java.io.IOException;
36+
import java.io.OutputStream;
37+
38+
import org.apache.dubbo.common.serialize.Cleanable;
39+
import org.apache.dubbo.common.serialize.ObjectOutput;
40+
import org.apache.dubbo.common.serialize.hessian2.Hessian2SerializerFactory;
41+
42+
import com.alibaba.com.caucho.hessian.io.Hessian2Output;
43+
44+
/**
45+
* Hessian2 object output implementation. This file is based on original Dubbo implementation.
46+
* This purpose is to support non-serializable model.
47+
*/
48+
public class Hessian2ObjectOutput implements ObjectOutput, Cleanable {
49+
50+
private static ThreadLocal<Hessian2Output> OUTPUT_TL = ThreadLocal.withInitial(() -> {
51+
Hessian2Output h2o = new Hessian2Output(null);
52+
Hessian2SerializerFactory factory = new Hessian2SerializerFactory();
53+
factory.setAllowNonSerializable(true);
54+
h2o.setSerializerFactory(factory);
55+
h2o.setCloseStreamOnClose(true);
56+
return h2o;
57+
});
58+
59+
private final Hessian2Output mH2o;
60+
61+
public Hessian2ObjectOutput(OutputStream os) {
62+
mH2o = OUTPUT_TL.get();
63+
mH2o.init(os);
64+
}
65+
66+
@Override
67+
public void writeBool(boolean v) throws IOException {
68+
mH2o.writeBoolean(v);
69+
}
70+
71+
@Override
72+
public void writeByte(byte v) throws IOException {
73+
mH2o.writeInt(v);
74+
}
75+
76+
@Override
77+
public void writeShort(short v) throws IOException {
78+
mH2o.writeInt(v);
79+
}
80+
81+
@Override
82+
public void writeInt(int v) throws IOException {
83+
mH2o.writeInt(v);
84+
}
85+
86+
@Override
87+
public void writeLong(long v) throws IOException {
88+
mH2o.writeLong(v);
89+
}
90+
91+
@Override
92+
public void writeFloat(float v) throws IOException {
93+
mH2o.writeDouble(v);
94+
}
95+
96+
@Override
97+
public void writeDouble(double v) throws IOException {
98+
mH2o.writeDouble(v);
99+
}
100+
101+
@Override
102+
public void writeBytes(byte[] b) throws IOException {
103+
mH2o.writeBytes(b);
104+
}
105+
106+
@Override
107+
public void writeBytes(byte[] b, int off, int len) throws IOException {
108+
mH2o.writeBytes(b, off, len);
109+
}
110+
111+
@Override
112+
public void writeUTF(String v) throws IOException {
113+
mH2o.writeString(v);
114+
}
115+
116+
@Override
117+
public void writeObject(Object obj) throws IOException {
118+
mH2o.writeObject(obj);
119+
}
120+
121+
@Override
122+
public void flushBuffer() throws IOException {
123+
mH2o.flushBuffer();
124+
}
125+
126+
public OutputStream getOutputStream() throws IOException {
127+
return mH2o.getBytesOutputStream();
128+
}
129+
130+
@Override
131+
public void cleanup() {
132+
if(mH2o != null) {
133+
mH2o.reset();
134+
}
135+
}
136+
}

spring-cloud-starter-huawei/spring-cloud-starter-huawei-hessian/src/main/java/com/huaweicloud/hessian/HessianHttpMessageConverter.java

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
package com.huaweicloud.hessian;
1919

20-
import org.apache.dubbo.common.serialize.hessian2.Hessian2ObjectInput;
21-
import org.apache.dubbo.common.serialize.hessian2.Hessian2ObjectOutput;
2220
import org.springframework.http.HttpInputMessage;
2321
import org.springframework.http.HttpOutputMessage;
2422
import org.springframework.http.MediaType;

0 commit comments

Comments
 (0)