Skip to content

Commit b69a88f

Browse files
authored
Merge branch 'master' into parallel-batch-processing
2 parents 41398a2 + 4d32e9d commit b69a88f

File tree

12 files changed

+248
-187
lines changed

12 files changed

+248
-187
lines changed

build.gradle

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ plugins {
1515
}
1616

1717
description = 'This project aims to provide the facility to easily implement JSON-RPC for the java programming language.'
18-
version = '1.5.3'
18+
version = '1.5.3-2'
1919
group = 'com.github.briandilley.jsonrpc4j'
2020

2121
sourceCompatibility = 1.8
@@ -33,17 +33,20 @@ compileTestJava {
3333
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
3434
}
3535

36+
apply plugin: "maven"
37+
3638
repositories {
39+
mavenLocal()
3740
mavenCentral()
3841
}
3942

4043
dependencies {
4144
ext {
42-
jacksonVersion = '2.8.5'
43-
springVersion = '4.3.5.RELEASE'
44-
springBotVersion = '1.4.3.RELEASE'
45+
jacksonVersion = '2.10.2'
46+
springVersion = '4.3.26.RELEASE'
47+
springBotVersion = '1.4.7.RELEASE'
4548
jettyVersion = '9.4.0.RC3'
46-
slf4jVersion = '1.7.22'
49+
slf4jVersion = '1.7.30'
4750
}
4851

4952
compile 'net.iharder:base64:2.3.9'

src/main/java/com/googlecode/jsonrpc4j/AnnotationsErrorResolver.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public JsonError resolveError(Throwable thrownException, Method method, List<Jso
2222
}
2323

2424
String message = hasErrorMessage(resolver) ? resolver.message() : thrownException.getMessage();
25-
return new JsonError(resolver.code(), message, new ErrorData(resolver.exception().getName(), message));
25+
Object data = hasErrorData(resolver) ? resolver.data() : new ErrorData(resolver.exception().getName(), message);
26+
return new JsonError(resolver.code(), message, data);
2627
}
2728

2829
private JsonRpcError getResolverForException(Throwable thrownException, Method method) {
@@ -45,6 +46,11 @@ private boolean hasErrorMessage(JsonRpcError em) {
4546
// noinspection ConstantConditions
4647
return em.message() != null && em.message().trim().length() > 0;
4748
}
49+
50+
private boolean hasErrorData(JsonRpcError em) {
51+
// noinspection ConstantConditions
52+
return em.data() != null && em.data().trim().length() > 0;
53+
}
4854

4955
private boolean hasAnnotations(JsonRpcErrors errors) {
5056
return errors != null;

src/main/java/com/googlecode/jsonrpc4j/JsonRpcBasicServer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ private JsonResponse handleObject(final ObjectNode node) {
455455
return createResponseSuccess(jsonRpc, id, handler.result);
456456
}
457457
return new JsonResponse(null, JsonError.OK.code);
458+
} catch (JsonParseException | JsonMappingException e) {
459+
throw e; // rethrow this, it will be handled as PARSE_ERROR later
458460
} catch (Throwable e) {
459461
handler.error = e;
460462
return handleError(id, jsonRpc, methodArgs, e);

src/main/java/com/googlecode/jsonrpc4j/JsonRpcClient.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ private boolean isIdValueNotCorrect(String id, ObjectNode jsonObject) {
325325
}
326326

327327
protected boolean hasError(ObjectNode jsonObject) {
328-
return jsonObject.has(ERROR) && jsonObject.get(ERROR) != null && !jsonObject.get(ERROR).isNull();
328+
return jsonObject.has(ERROR) && jsonObject.get(ERROR) != null && !jsonObject.get(ERROR).isNull() && !(jsonObject.get(ERROR).isInt() && jsonObject.intValue()==0) ;
329329
}
330330

331331
/**
@@ -343,6 +343,7 @@ private ObjectNode internalCreateRequest(String methodName, Object arguments, St
343343
addParameters(arguments, request);
344344
addAdditionalHeaders(request);
345345
notifyBeforeRequestListener(request);
346+
addNoneArguments(request);
346347
return request;
347348
}
348349

@@ -423,6 +424,13 @@ private boolean isCollectionArguments(Object arguments) {
423424
return arguments != null && Collection.class.isInstance(arguments);
424425
}
425426

427+
private void addNoneArguments(ObjectNode request){
428+
if (!request.has(PARAMS)){
429+
//for none params add an empty array
430+
request.set(PARAMS,mapper.valueToTree(new String[0]));
431+
}
432+
}
433+
426434
private void addCollectionArguments(Object arguments, ObjectNode request) {
427435
Collection<?> args = Collection.class.cast(arguments);
428436
if (!args.isEmpty()) {

src/main/java/com/googlecode/jsonrpc4j/JsonRpcHttpClient.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,21 @@
66
import javax.net.ssl.HostnameVerifier;
77
import javax.net.ssl.HttpsURLConnection;
88
import javax.net.ssl.SSLContext;
9-
import java.io.BufferedReader;
10-
import java.io.ByteArrayOutputStream;
11-
import java.io.IOException;
12-
import java.io.InputStream;
13-
import java.io.InputStreamReader;
14-
import java.io.OutputStream;
9+
import java.io.*;
1510
import java.lang.reflect.Type;
1611
import java.net.HttpURLConnection;
1712
import java.net.Proxy;
1813
import java.net.URL;
14+
import java.util.Arrays;
1915
import java.util.Collections;
2016
import java.util.HashMap;
2117
import java.util.Map;
2218
import java.util.Map.Entry;
2319
import java.util.zip.GZIPInputStream;
2420
import java.util.zip.GZIPOutputStream;
2521

26-
import static com.googlecode.jsonrpc4j.JsonRpcBasicServer.ACCEPT_ENCODING;
27-
import static com.googlecode.jsonrpc4j.JsonRpcBasicServer.CONTENT_ENCODING;
28-
import static com.googlecode.jsonrpc4j.JsonRpcBasicServer.JSONRPC_CONTENT_TYPE;
22+
import static com.googlecode.jsonrpc4j.JsonRpcBasicServer.*;
23+
import static java.nio.charset.StandardCharsets.UTF_8;
2924

3025
/**
3126
* A JSON-RPC client that uses the HTTP protocol.
@@ -155,10 +150,14 @@ public Object invoke(String methodName, Object argument, Type returnType, Map<St
155150
throw new HttpException("Caught error with no response body.", e);
156151
}
157152

153+
byte[] errorText = e.getMessage().getBytes(UTF_8);
158154
try (InputStream answer = getStream(connection.getErrorStream(), useGzip)) {
159-
return super.readResponse(returnType, answer);
155+
errorText = readErrorStream(answer, 1024);
156+
PushbackInputStream wrappedStream = new PushbackInputStream(answer, errorText.length);
157+
wrappedStream.unread(errorText);
158+
return super.readResponse(returnType, wrappedStream);
160159
} catch (IOException ef) {
161-
throw new HttpException(readErrorString(connection), ef);
160+
throw new HttpException(new String(errorText, UTF_8), ef);
162161
}
163162
}
164163
} finally {
@@ -221,18 +220,17 @@ private InputStream getStream(final InputStream inputStream, final boolean useGz
221220
return useGzip ? new GZIPInputStream(inputStream) : inputStream;
222221
}
223222

224-
private static String readErrorString(final HttpURLConnection connection) {
225-
try (InputStream stream = connection.getErrorStream()) {
226-
StringBuilder buffer = new StringBuilder();
227-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"))) {
228-
for (int ch = reader.read(); ch >= 0; ch = reader.read()) {
229-
buffer.append((char) ch);
230-
}
231-
}
232-
return buffer.toString();
233-
} catch (IOException e) {
234-
return e.getMessage();
223+
private static byte[] readErrorStream(InputStream errorStream, int maxLength) throws IOException {
224+
int b;
225+
int pos = 0;
226+
byte[] buffer = new byte[maxLength];
227+
while (pos < buffer.length && (b = errorStream.read(buffer, pos, buffer.length - pos)) != -1) {
228+
pos += b;
229+
}
230+
if (pos == 0) {
231+
throw new IOException("Empty error stream");
235232
}
233+
return pos < buffer.length? Arrays.copyOf(buffer, pos): buffer;
236234
}
237235

238236
private void setupSsl(HttpURLConnection connection) {

src/main/java/com/googlecode/jsonrpc4j/JsonRpcMultiServer.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,35 @@
3838
*/
3939
@SuppressWarnings({"WeakerAccess", "unused"})
4040
public class JsonRpcMultiServer extends JsonRpcServer {
41-
41+
4242
public static final char DEFAULT_SEPARATOR = '.';
4343
private static final Logger logger = LoggerFactory.getLogger(JsonRpcMultiServer.class);
44-
44+
4545
private final Map<String, Object> handlerMap;
4646
private final Map<String, Class<?>> interfaceMap;
4747
private char separator = DEFAULT_SEPARATOR;
48-
48+
4949
public JsonRpcMultiServer() {
5050
this(new ObjectMapper());
5151
logger.debug("created empty multi server");
5252
}
53-
53+
5454
public JsonRpcMultiServer(ObjectMapper mapper) {
5555
super(mapper, null);
5656
this.handlerMap = new HashMap<>();
5757
this.interfaceMap = new HashMap<>();
5858
}
59-
59+
60+
public JsonRpcMultiServer(ObjectMapper mapper, boolean gzipResponses) {
61+
super(mapper, null, null, gzipResponses);
62+
this.handlerMap = new HashMap<>();
63+
this.interfaceMap = new HashMap<>();
64+
}
65+
6066
public JsonRpcMultiServer addService(String name, Object handler) {
6167
return addService(name, handler, null);
6268
}
63-
69+
6470
public JsonRpcMultiServer addService(String name, Object handler, Class<?> remoteInterface) {
6571
logger.debug("add service interface {} with handler {}", remoteInterface, handler);
6672
handlerMap.put(name, handler);
@@ -69,15 +75,15 @@ public JsonRpcMultiServer addService(String name, Object handler, Class<?> remot
6975
}
7076
return this;
7177
}
72-
78+
7379
public char getSeparator() {
7480
return this.separator;
7581
}
76-
82+
7783
public void setSeparator(char separator) {
7884
this.separator = separator;
7985
}
80-
86+
8187
/**
8288
* Returns the handler's class or interfaces. The serviceName is used
8389
* to look up a registered handler.
@@ -96,7 +102,7 @@ protected Class<?>[] getHandlerInterfaces(String serviceName) {
96102
return new Class<?>[]{getHandler(serviceName).getClass()};
97103
}
98104
}
99-
105+
100106
/**
101107
* Get the service name from the methodNode. JSON-RPC methods with the form
102108
* Service.method will result in "Service" being returned in this case.
@@ -114,7 +120,7 @@ protected String getServiceName(final String methodName) {
114120
}
115121
return methodName;
116122
}
117-
123+
118124
/**
119125
* Get the method name from the methodNode, stripping off the service name.
120126
*
@@ -131,7 +137,7 @@ protected String getMethodName(final String methodName) {
131137
}
132138
return methodName;
133139
}
134-
140+
135141
/**
136142
* Get the handler (object) that should be invoked to execute the specified
137143
* RPC method based on the specified service name.

0 commit comments

Comments
 (0)