Skip to content

Commit f856af1

Browse files
authored
Merge pull request briandilley#175 from im-scooter/master
Fixes for issue briandilley#174
2 parents add8272 + d91f35a commit f856af1

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,13 @@ private Throwable createThrowable(String typeName, String message) throws Illega
8989
}
9090

9191
/**
92-
* Resolves original exception type name into an actual {@link Class<?>}.
92+
* Resolves original exception type name into an actual {@link Class}.
9393
* Override this, if you want custom behaviour for handling exceptions,
9494
* i.e.: Default to RuntimeException.
9595
*
9696
* @param typeName Original exception type name thrown on the server.
97-
* @throws ClassNotFoundException
97+
* @return the resolved throwable
98+
* @throws ClassNotFoundException - if throwable class has not been found
9899
*/
99100
protected Class<? extends Throwable> resolveThrowableClass(String typeName) throws ClassNotFoundException {
100101
Class<?> clazz;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,11 @@ private JsonNode invoke(Object target, Method method, List<JsonNode> params) thr
496496

497497
if (method.getGenericParameterTypes().length == 1 && method.isVarArgs()) {
498498
convertedParams = new Object[params.size()];
499-
ObjectMapper mapper = new ObjectMapper();
499+
// ObjectMapper mapper = new ObjectMapper();
500500

501501
for (int i = 0; i < params.size(); i++) {
502502
JsonNode jsonNode = params.get(i);
503-
Class type = getJavaTypeForJsonType(jsonNode.getNodeType());
503+
Class<?> type = getJavaTypeForJsonType(jsonNode.getNodeType());
504504
Object object = mapper.convertValue(jsonNode, type);
505505
logger.debug(String.format("[%s] param: %s -> %s", method.getName(), i, type.getName()));
506506
convertedParams[i] = object;

src/main/java/com/googlecode/jsonrpc4j/spring/rest/JsonRpcResponseErrorHandler.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
import java.nio.charset.Charset;
1818
import java.util.HashSet;
1919
import java.util.Set;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
2022

21-
public enum JsonRpcResponseErrorHandler
23+
public class JsonRpcResponseErrorHandler
2224
implements ResponseErrorHandler {
2325

24-
INSTANCE;
26+
public static final JsonRpcResponseErrorHandler INSTANCE = new JsonRpcResponseErrorHandler();
2527

28+
private final Logger logger = LoggerFactory.getLogger(this.getClass());
29+
2630
/**
2731
* for supported codes see {@link com.googlecode.jsonrpc4j.DefaultHttpStatusCodeProvider}
2832
*/
@@ -45,7 +49,7 @@ public boolean hasError(ClientHttpResponse response)
4549
// Checks the content type. If application/json-rpc then allow handler to read message
4650
final MediaType contentType = response.getHeaders().getContentType();
4751
if (MappingJacksonRPC2HttpMessageConverter.APPLICATION_JSON_RPC.isCompatibleWith(contentType))
48-
return true;
52+
return false;
4953
}
5054

5155
return
@@ -89,15 +93,16 @@ private byte[] getResponseBody(ClientHttpResponse response) {
8993
return FileCopyUtils.copyToByteArray(responseBody);
9094
}
9195
} catch (IOException ex) {
92-
throw new RuntimeException(ex.getMessage(), ex);
96+
// No body in response 401 for example
97+
logger.debug("Can not read resonse body", ex);
9398
}
9499
return new byte[0];
95100
}
96101

97102
private Charset getCharset(ClientHttpResponse response) {
98103
HttpHeaders headers = response.getHeaders();
99104
MediaType contentType = headers.getContentType();
100-
return contentType != null ? contentType.getCharSet() : null;
105+
return contentType != null ? contentType.getCharset() : null;
101106
}
102107

103108
}

src/main/java/com/googlecode/jsonrpc4j/spring/rest/JsonRpcRestClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.List;
2727
import java.util.Map;
2828
import java.util.concurrent.atomic.AtomicReference;
29+
import org.springframework.web.client.ResponseErrorHandler;
2930

3031
@SuppressWarnings({"unused", "WeakerAccess"})
3132
public class JsonRpcRestClient extends JsonRpcClient implements IJsonRpcClient {
@@ -87,8 +88,8 @@ private void initRestTemplate() {
8788
this.restTemplate.setMessageConverters(restMessageConverters);
8889
}
8990

90-
// use specific JSON-RPC erro handler
91-
if (!(restTemplate.getErrorHandler() instanceof JsonRpcResponseErrorHandler))
91+
// use specific JSON-RPC error handler if it has not been changed to custom
92+
if (restTemplate.getErrorHandler() instanceof org.springframework.web.client.DefaultResponseErrorHandler)
9293
restTemplate.setErrorHandler(JsonRpcResponseErrorHandler.INSTANCE);
9394
}
9495

src/main/java/com/googlecode/jsonrpc4j/spring/rest/MappingJacksonRPC2HttpMessageConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ protected void writeInternal(Object object, HttpOutputMessage outputMessage)
171171
* @return the JSON encoding to use (never <code>null</code>)
172172
*/
173173
private JsonEncoding getJsonEncoding(MediaType contentType) {
174-
if (contentType != null && contentType.getCharSet() != null) {
175-
Charset charset = contentType.getCharSet();
174+
if (contentType != null && contentType.getCharset() != null) {
175+
Charset charset = contentType.getCharset();
176176
for (JsonEncoding encoding : JsonEncoding.values()) {
177177
if (charset.name().equals(encoding.getJavaName())) {
178178
return encoding;

0 commit comments

Comments
 (0)