Skip to content

Commit

Permalink
Merge pull request #293 from cyb3r4nt/jsonrpc4j-basic-server-improvem…
Browse files Browse the repository at this point in the history
…ents

Improvements in JsonRpcBasicServer
  • Loading branch information
briandilley authored May 24, 2023
2 parents 71949a2 + dbb4f1b commit a70fe66
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 51 deletions.
94 changes: 43 additions & 51 deletions src/main/java/com/googlecode/jsonrpc4j/JsonRpcBasicServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
* A JSON-RPC request server reads JSON-RPC requests from an input stream and writes responses to an output stream.
* Can even run on Android system.
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public class JsonRpcBasicServer {

public static final String JSONRPC_CONTENT_TYPE = "application/json-rpc";
Expand All @@ -56,7 +55,7 @@ public class JsonRpcBasicServer {
public static final String NULL = "null";
private static final Logger logger = LoggerFactory.getLogger(JsonRpcBasicServer.class);
private static final ErrorResolver DEFAULT_ERROR_RESOLVER = new MultipleErrorResolver(AnnotationsErrorResolver.INSTANCE, DefaultErrorResolver.INSTANCE);
private static Pattern BASE64_PATTERN = Pattern.compile("[A-Za-z0-9_=-]+");
private static final Pattern BASE64_PATTERN = Pattern.compile("[A-Za-z0-9_=-]+");
private static Class<? extends Annotation> WEB_PARAM_ANNOTATION_CLASS;
private static Method WEB_PARAM_NAME_METHOD;

Expand Down Expand Up @@ -219,14 +218,6 @@ static InputStream createInputStream(String method, String id, String params) th
return new ByteArrayInputStream(envelope.toString().getBytes(StandardCharsets.UTF_8));
}

public RequestInterceptor getRequestInterceptor() {
return requestInterceptor;
}

public void setRequestInterceptor(RequestInterceptor requestInterceptor) {
this.requestInterceptor = requestInterceptor;
}

/**
* Handles a single request from the given {@link InputStream},
* that is to say that a single {@link JsonNode} is read from
Expand Down Expand Up @@ -647,7 +638,7 @@ private Object convertAndLogParam(Method method, List<JsonNode> params, int para
}

private boolean hasReturnValue(Method m) {
return m.getGenericReturnType() != null;
return !"void".equalsIgnoreCase(m.getGenericReturnType().getTypeName());
}

private Object[] convertJsonToParameters(Method m, List<JsonNode> params) throws IOException {
Expand Down Expand Up @@ -766,7 +757,7 @@ private AMethodWithItsArgs findBestMethodByParamsNode(Set<Method> methods, JsonN
} else if (paramsNode.isObject()) {
matchedMethod = findBestMethodUsingParamNames(methods, collectFieldNames(paramsNode), (ObjectNode) paramsNode);
} else {
throw new IllegalArgumentException("Unknown params node type: " + paramsNode.toString());
throw new IllegalArgumentException("Unknown params node type: " + paramsNode);
}
if (matchedMethod == null) {
matchedMethod = findBestMethodForVarargs(methods, paramsNode);
Expand Down Expand Up @@ -990,12 +981,6 @@ private boolean isNumericAssignable(Class<?> type) {
|| long.class.isAssignableFrom(type) || float.class.isAssignableFrom(type) || double.class.isAssignableFrom(type);
}

private JsonError writeAndFlushValueError(OutputStream output, ErrorObjectWithJsonError value) throws IOException {
logger.debug("failed {}", value);
writeAndFlushValue(output, value.node);
return value.error;
}

/**
* Writes and flushes a value to the given {@link OutputStream}
* and prevents Jackson from closing it. Also writes newline.
Expand Down Expand Up @@ -1153,24 +1138,6 @@ public void setBatchExecutorService(ExecutorService batchExecutorService) {
public void setParallelBatchProcessingTimeout(long parallelBatchProcessingTimeout) {
this.parallelBatchProcessingTimeout = parallelBatchProcessingTimeout;
}

private static class ErrorObjectWithJsonError {
private final ObjectNode node;
private final JsonError error;

public ErrorObjectWithJsonError(ObjectNode node, JsonError error) {
this.node = node;
this.error = error;
}

@Override
public String toString() {
return "ErrorObjectWithJsonError{" +
"node=" + node +
", error=" + error +
'}';
}
}

/**
* Simple inner class for the {@code findXXX} methods.
Expand Down Expand Up @@ -1355,34 +1322,59 @@ public ParameterCount() {
allNames = null;
method = null;
}

public int getTypeCount() {
return typeCount;
}

public int getNameCount() {
return nameCount;
}

}

private static class ParameterConvertException extends RuntimeException {
private final int paramIndex;
/**
* Gets the {@link RequestInterceptor} instance.
*
* @return previously set request interceptor object instance. There are no interceptors
* by default, and this method returns {@code null}, if interceptor was not previously set.
*/
public RequestInterceptor getRequestInterceptor() {
return requestInterceptor;
}

private ParameterConvertException(int index, Throwable throwable) {
super(throwable);
this.paramIndex = index;
}
/**
* Sets the {@link RequestInterceptor} instance
*
* @param requestInterceptor interceptor object instance,
* which will be invoked prior to any JSON-RPC service being invoked.
*/
public void setRequestInterceptor(RequestInterceptor requestInterceptor) {
this.requestInterceptor = requestInterceptor;
}

/**
* Gets the collection of {@link JsonRpcInterceptor} instances.
*
* @return collection of previously set {@link JsonRpcInterceptor} instances.
* There are no interceptors by default, and this method returns empty collection, if
* interceptors were not previously set.
*/
public List<JsonRpcInterceptor> getInterceptorList() {
return interceptorList;
}

/**
* Sets the collection of {@link JsonRpcInterceptor} instances.
*
* @param interceptorList collection of {@link JsonRpcInterceptor} instances, which are
* called at different stages of request handling.
* Parameter cannot be {@code null}.
*/
public void setInterceptorList(List<JsonRpcInterceptor> interceptorList) {
if (interceptorList == null) {
throw new IllegalArgumentException("Interceptors list can't be null");
}
this.interceptorList = interceptorList;
}

private static class ParameterConvertException extends RuntimeException {
private final int paramIndex;

private ParameterConvertException(int index, Throwable throwable) {
super(throwable);
this.paramIndex = index;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ public void callOverloadedMethodExtraParams() throws Exception {
jsonRpcServer.handleRequest(messageWithListParamsStream(1, "overloadedMethod", param1, param2, param3), byteArrayOutputStream);
assertEquals(param1 + param2, decodeAnswer(byteArrayOutputStream).get(JsonRpcBasicServer.RESULT).textValue());
}

@Test
public void callVoidMethod() throws Exception {
mockService.voidMethod(intParam1);
EasyMock.replay(mockService);
jsonRpcServer.handleRequest(
messageWithListParamsStream(1, "voidMethod", intParam1),
byteArrayOutputStream
);
assertNull(decodeAnswer(byteArrayOutputStream).get(JsonRpcBasicServer.RESULT).textValue());
EasyMock.verify(mockService);
}

@Test
public void idIntegerType() throws Exception {
Expand Down Expand Up @@ -431,6 +443,8 @@ public interface ServiceInterface {
String overloadedMethod(int intParam1, int intParam2);

String throwsMethod(String param1) throws CustomTestException;

void voidMethod(int intParam1);
}

}

0 comments on commit a70fe66

Please sign in to comment.