Skip to content

Commit 1370263

Browse files
Merge pull request #390 from rabbitmq/rabbitmq-java-client-378-json-rpc-abstraction
Add JSON abstraction for JSON RPC support
2 parents 22d9401 + d79a8b0 commit 1370263

19 files changed

+993
-318
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<slf4j.version>1.7.25</slf4j.version>
5858
<metrics.version>3.2.6</metrics.version>
5959
<micrometer.version>1.0.2</micrometer.version>
60+
<jackson.version>2.9.6</jackson.version>
6061
<logback.version>1.2.3</logback.version>
6162
<commons-cli.version>1.1</commons-cli.version>
6263
<junit.version>4.12</junit.version>
@@ -640,6 +641,12 @@
640641
<version>${micrometer.version}</version>
641642
<optional>true</optional>
642643
</dependency>
644+
<dependency>
645+
<groupId>com.fasterxml.jackson.core</groupId>
646+
<artifactId>jackson-databind</artifactId>
647+
<version>${jackson.version}</version>
648+
<optional>true</optional>
649+
</dependency>
643650
<dependency>
644651
<groupId>commons-cli</groupId>
645652
<artifactId>commons-cli</artifactId>

src/main/java/com/rabbitmq/tools/json/JSONReader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
import java.util.List;
4545
import java.util.Map;
4646

47+
/**
48+
* Will be removed in 6.0
49+
*
50+
* @deprecated Use a third-party JSON library, e.g. Jackson or Gson
51+
*/
4752
public class JSONReader {
4853

4954
private static final Object OBJECT_END = new Object();

src/main/java/com/rabbitmq/tools/json/JSONSerializable.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
/**
2020
* Interface for classes that wish to control their own serialization.
21+
*
22+
* Will be removed in 6.0
23+
*
24+
* @deprecated Use a third-party JSON library, e.g. Jackson or Gson
2125
*/
2226
public interface JSONSerializable {
2327
/**

src/main/java/com/rabbitmq/tools/json/JSONUtil.java

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// If you have any questions regarding licensing, please contact us at
1414
// info@rabbitmq.com.
1515

16-
1716
package com.rabbitmq.tools.json;
1817

1918
import org.slf4j.Logger;
@@ -34,56 +33,52 @@
3433
*/
3534
public class JSONUtil {
3635

37-
private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtil.class);
36+
private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtil.class);
37+
3838
/**
3939
* Uses reflection to fill public fields and Bean properties of
4040
* the target object from the source Map.
4141
*/
4242
public static Object fill(Object target, Map<String, Object> source)
43-
throws IntrospectionException, IllegalAccessException, InvocationTargetException
44-
{
45-
return fill(target, source, true);
43+
throws IntrospectionException, IllegalAccessException, InvocationTargetException {
44+
return fill(target, source, true);
4645
}
4746

4847
/**
4948
* Uses reflection to fill public fields and optionally Bean
5049
* properties of the target object from the source Map.
5150
*/
5251
public static Object fill(Object target, Map<String, Object> source, boolean useProperties)
53-
throws IntrospectionException, IllegalAccessException, InvocationTargetException
54-
{
55-
if (useProperties) {
56-
BeanInfo info = Introspector.getBeanInfo(target.getClass());
52+
throws IntrospectionException, IllegalAccessException, InvocationTargetException {
53+
if (useProperties) {
54+
BeanInfo info = Introspector.getBeanInfo(target.getClass());
5755

58-
PropertyDescriptor[] props = info.getPropertyDescriptors();
59-
for (int i = 0; i < props.length; ++i) {
60-
PropertyDescriptor prop = props[i];
61-
String name = prop.getName();
62-
Method setter = prop.getWriteMethod();
63-
if (setter != null && !Modifier.isStatic(setter.getModifiers())) {
64-
//System.out.println(target + " " + name + " <- " + source.get(name));
65-
setter.invoke(target, source.get(name));
66-
}
67-
}
68-
}
56+
PropertyDescriptor[] props = info.getPropertyDescriptors();
57+
for (int i = 0; i < props.length; ++i) {
58+
PropertyDescriptor prop = props[i];
59+
String name = prop.getName();
60+
Method setter = prop.getWriteMethod();
61+
if (setter != null && !Modifier.isStatic(setter.getModifiers())) {
62+
setter.invoke(target, source.get(name));
63+
}
64+
}
65+
}
6966

70-
Field[] ff = target.getClass().getDeclaredFields();
71-
for (int i = 0; i < ff.length; ++i) {
72-
Field field = ff[i];
67+
Field[] ff = target.getClass().getDeclaredFields();
68+
for (int i = 0; i < ff.length; ++i) {
69+
Field field = ff[i];
7370
int fieldMod = field.getModifiers();
74-
if (Modifier.isPublic(fieldMod) && !(Modifier.isFinal(fieldMod) ||
75-
Modifier.isStatic(fieldMod)))
76-
{
77-
//System.out.println(target + " " + field.getName() + " := " + source.get(field.getName()));
78-
try {
79-
field.set(target, source.get(field.getName()));
80-
} catch (IllegalArgumentException iae) {
81-
// no special error processing required
71+
if (Modifier.isPublic(fieldMod) && !(Modifier.isFinal(fieldMod) ||
72+
Modifier.isStatic(fieldMod))) {
73+
try {
74+
field.set(target, source.get(field.getName()));
75+
} catch (IllegalArgumentException iae) {
76+
// no special error processing required
8277
}
83-
}
84-
}
78+
}
79+
}
8580

86-
return target;
81+
return target;
8782
}
8883

8984
/**
@@ -92,14 +87,14 @@ public static Object fill(Object target, Map<String, Object> source, boolean use
9287
* source Map.
9388
*/
9489
public static void tryFill(Object target, Map<String, Object> source) {
95-
try {
96-
fill(target, source);
97-
} catch (IntrospectionException ie) {
98-
LOGGER.error("Error in tryFill", ie);
99-
} catch (IllegalAccessException iae) {
100-
LOGGER.error("Error in tryFill", iae);
101-
} catch (InvocationTargetException ite) {
102-
LOGGER.error("Error in tryFill", ite);
103-
}
90+
try {
91+
fill(target, source);
92+
} catch (IntrospectionException ie) {
93+
LOGGER.error("Error in tryFill", ie);
94+
} catch (IllegalAccessException iae) {
95+
LOGGER.error("Error in tryFill", iae);
96+
} catch (InvocationTargetException ite) {
97+
LOGGER.error("Error in tryFill", ite);
98+
}
10499
}
105100
}

src/main/java/com/rabbitmq/tools/json/JSONWriter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
import java.util.Map;
5454
import java.util.Set;
5555

56+
/**
57+
* Will be removed in 6.0
58+
* @deprecated Use a third-party JSON library, e.g. Jackson or Gson
59+
*/
5660
public class JSONWriter {
5761
private boolean indentMode = false;
5862
private int indentLevel = 0;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2018 Pivotal Software, Inc. All rights reserved.
2+
//
3+
// This software, the RabbitMQ Java client library, is triple-licensed under the
4+
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
5+
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
6+
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
7+
// please see LICENSE-APACHE2.
8+
//
9+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
10+
// either express or implied. See the LICENSE file for specific language governing
11+
// rights and limitations of this software.
12+
//
13+
// If you have any questions regarding licensing, please contact us at
14+
// info@rabbitmq.com.
15+
16+
package com.rabbitmq.tools.jsonrpc;
17+
18+
import com.rabbitmq.tools.json.JSONReader;
19+
import com.rabbitmq.tools.json.JSONWriter;
20+
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
/**
25+
* Simple {@link JsonRpcMapper} based on homegrown JSON utilities.
26+
* Handles integers, doubles, strings, booleans, and arrays of those types.
27+
* <p>
28+
* For a more comprehensive set of features, use {@link JacksonJsonRpcMapper}.
29+
* <p>
30+
* Will be removed in 6.0
31+
*
32+
* @see JsonRpcMapper
33+
* @see JacksonJsonRpcMapper
34+
* @since 5.4.0
35+
* @deprecated use {@link JacksonJsonRpcMapper} instead
36+
*/
37+
public class DefaultJsonRpcMapper implements JsonRpcMapper {
38+
39+
@Override
40+
public JsonRpcRequest parse(String requestBody, ServiceDescription description) {
41+
@SuppressWarnings("unchecked")
42+
Map<String, Object> request = (Map<String, Object>) new JSONReader().read(requestBody);
43+
return new JsonRpcRequest(
44+
request.get("id"), request.get("version").toString(), request.get("method").toString(),
45+
((List<?>) request.get("params")).toArray()
46+
);
47+
}
48+
49+
@Override
50+
public JsonRpcResponse parse(String responseBody, Class<?> expectedType) {
51+
@SuppressWarnings("unchecked")
52+
Map<String, Object> map = (Map<String, Object>) (new JSONReader().read(responseBody));
53+
Map<String, Object> error;
54+
JsonRpcException exception = null;
55+
if (map.containsKey("error")) {
56+
error = (Map<String, Object>) map.get("error");
57+
exception = new JsonRpcException(
58+
new JSONWriter().write(error),
59+
(String) error.get("name"),
60+
error.get("code") == null ? 0 : (Integer) error.get("code"),
61+
(String) error.get("message"),
62+
error
63+
);
64+
}
65+
return new JsonRpcResponse(map.get("result"), map.get("error"), exception);
66+
}
67+
68+
@Override
69+
public String write(Object input) {
70+
return new JSONWriter().write(input);
71+
}
72+
}

0 commit comments

Comments
 (0)