Skip to content

Commit

Permalink
Merge branch 'master' into parallel-batch-processing
Browse files Browse the repository at this point in the history
  • Loading branch information
briandilley authored Jan 27, 2021
2 parents 41398a2 + 4d32e9d commit b69a88f
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 187 deletions.
13 changes: 8 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ plugins {
}

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

sourceCompatibility = 1.8
Expand All @@ -33,17 +33,20 @@ compileTestJava {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}

apply plugin: "maven"

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
ext {
jacksonVersion = '2.8.5'
springVersion = '4.3.5.RELEASE'
springBotVersion = '1.4.3.RELEASE'
jacksonVersion = '2.10.2'
springVersion = '4.3.26.RELEASE'
springBotVersion = '1.4.7.RELEASE'
jettyVersion = '9.4.0.RC3'
slf4jVersion = '1.7.22'
slf4jVersion = '1.7.30'
}

compile 'net.iharder:base64:2.3.9'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public JsonError resolveError(Throwable thrownException, Method method, List<Jso
}

String message = hasErrorMessage(resolver) ? resolver.message() : thrownException.getMessage();
return new JsonError(resolver.code(), message, new ErrorData(resolver.exception().getName(), message));
Object data = hasErrorData(resolver) ? resolver.data() : new ErrorData(resolver.exception().getName(), message);
return new JsonError(resolver.code(), message, data);
}

private JsonRpcError getResolverForException(Throwable thrownException, Method method) {
Expand All @@ -45,6 +46,11 @@ private boolean hasErrorMessage(JsonRpcError em) {
// noinspection ConstantConditions
return em.message() != null && em.message().trim().length() > 0;
}

private boolean hasErrorData(JsonRpcError em) {
// noinspection ConstantConditions
return em.data() != null && em.data().trim().length() > 0;
}

private boolean hasAnnotations(JsonRpcErrors errors) {
return errors != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ private JsonResponse handleObject(final ObjectNode node) {
return createResponseSuccess(jsonRpc, id, handler.result);
}
return new JsonResponse(null, JsonError.OK.code);
} catch (JsonParseException | JsonMappingException e) {
throw e; // rethrow this, it will be handled as PARSE_ERROR later
} catch (Throwable e) {
handler.error = e;
return handleError(id, jsonRpc, methodArgs, e);
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/googlecode/jsonrpc4j/JsonRpcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private boolean isIdValueNotCorrect(String id, ObjectNode jsonObject) {
}

protected boolean hasError(ObjectNode jsonObject) {
return jsonObject.has(ERROR) && jsonObject.get(ERROR) != null && !jsonObject.get(ERROR).isNull();
return jsonObject.has(ERROR) && jsonObject.get(ERROR) != null && !jsonObject.get(ERROR).isNull() && !(jsonObject.get(ERROR).isInt() && jsonObject.intValue()==0) ;
}

/**
Expand All @@ -343,6 +343,7 @@ private ObjectNode internalCreateRequest(String methodName, Object arguments, St
addParameters(arguments, request);
addAdditionalHeaders(request);
notifyBeforeRequestListener(request);
addNoneArguments(request);
return request;
}

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

private void addNoneArguments(ObjectNode request){
if (!request.has(PARAMS)){
//for none params add an empty array
request.set(PARAMS,mapper.valueToTree(new String[0]));
}
}

private void addCollectionArguments(Object arguments, ObjectNode request) {
Collection<?> args = Collection.class.cast(arguments);
if (!args.isEmpty()) {
Expand Down
42 changes: 20 additions & 22 deletions src/main/java/com/googlecode/jsonrpc4j/JsonRpcHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,21 @@
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.*;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import static com.googlecode.jsonrpc4j.JsonRpcBasicServer.ACCEPT_ENCODING;
import static com.googlecode.jsonrpc4j.JsonRpcBasicServer.CONTENT_ENCODING;
import static com.googlecode.jsonrpc4j.JsonRpcBasicServer.JSONRPC_CONTENT_TYPE;
import static com.googlecode.jsonrpc4j.JsonRpcBasicServer.*;
import static java.nio.charset.StandardCharsets.UTF_8;

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

byte[] errorText = e.getMessage().getBytes(UTF_8);
try (InputStream answer = getStream(connection.getErrorStream(), useGzip)) {
return super.readResponse(returnType, answer);
errorText = readErrorStream(answer, 1024);
PushbackInputStream wrappedStream = new PushbackInputStream(answer, errorText.length);
wrappedStream.unread(errorText);
return super.readResponse(returnType, wrappedStream);
} catch (IOException ef) {
throw new HttpException(readErrorString(connection), ef);
throw new HttpException(new String(errorText, UTF_8), ef);
}
}
} finally {
Expand Down Expand Up @@ -221,18 +220,17 @@ private InputStream getStream(final InputStream inputStream, final boolean useGz
return useGzip ? new GZIPInputStream(inputStream) : inputStream;
}

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

private void setupSsl(HttpURLConnection connection) {
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/com/googlecode/jsonrpc4j/JsonRpcMultiServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,35 @@
*/
@SuppressWarnings({"WeakerAccess", "unused"})
public class JsonRpcMultiServer extends JsonRpcServer {

public static final char DEFAULT_SEPARATOR = '.';
private static final Logger logger = LoggerFactory.getLogger(JsonRpcMultiServer.class);

private final Map<String, Object> handlerMap;
private final Map<String, Class<?>> interfaceMap;
private char separator = DEFAULT_SEPARATOR;

public JsonRpcMultiServer() {
this(new ObjectMapper());
logger.debug("created empty multi server");
}

public JsonRpcMultiServer(ObjectMapper mapper) {
super(mapper, null);
this.handlerMap = new HashMap<>();
this.interfaceMap = new HashMap<>();
}


public JsonRpcMultiServer(ObjectMapper mapper, boolean gzipResponses) {
super(mapper, null, null, gzipResponses);
this.handlerMap = new HashMap<>();
this.interfaceMap = new HashMap<>();
}

public JsonRpcMultiServer addService(String name, Object handler) {
return addService(name, handler, null);
}

public JsonRpcMultiServer addService(String name, Object handler, Class<?> remoteInterface) {
logger.debug("add service interface {} with handler {}", remoteInterface, handler);
handlerMap.put(name, handler);
Expand All @@ -69,15 +75,15 @@ public JsonRpcMultiServer addService(String name, Object handler, Class<?> remot
}
return this;
}

public char getSeparator() {
return this.separator;
}

public void setSeparator(char separator) {
this.separator = separator;
}

/**
* Returns the handler's class or interfaces. The serviceName is used
* to look up a registered handler.
Expand All @@ -96,7 +102,7 @@ protected Class<?>[] getHandlerInterfaces(String serviceName) {
return new Class<?>[]{getHandler(serviceName).getClass()};
}
}

/**
* Get the service name from the methodNode. JSON-RPC methods with the form
* Service.method will result in "Service" being returned in this case.
Expand All @@ -114,7 +120,7 @@ protected String getServiceName(final String methodName) {
}
return methodName;
}

/**
* Get the method name from the methodNode, stripping off the service name.
*
Expand All @@ -131,7 +137,7 @@ protected String getMethodName(final String methodName) {
}
return methodName;
}

/**
* Get the handler (object) that should be invoked to execute the specified
* RPC method based on the specified service name.
Expand Down
Loading

0 comments on commit b69a88f

Please sign in to comment.