Operation jsonOperation = findOperation(method);
Object result = null;
// Invoke the get operation on the service implementation
Message requestMessage = messageFactory.createMessage();
requestMessage.setOperation(jsonOperation);
requestMessage.getHeaders().put("RequestMessage", request);
if (jsonOperation.getWrapper().getDataBinding().equals(JSONDataBinding.NAME)) {
requestMessage.setBody(new Object[]{jsonReq.toString()});
} else {
requestMessage.setBody(args);
}
//result = wire.invoke(jsonOperation, args);
Message responseMessage = null;
try {
responseMessage = endpoint.getInvocationChain(jsonOperation).getHeadInvoker().invoke(requestMessage);
} catch (RuntimeException re) {
if (re.getCause() instanceof javax.security.auth.login.LoginException) {
throw re;
} else {
//some other exception
JSONRPCResult errorResult = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, re);
return errorResult.toString().getBytes("UTF-8");
}
}
if (!responseMessage.isFault()) {
//successful execution of the invocation
if (jsonOperation.getWrapper().getDataBinding().equals(JSONDataBinding.NAME)) {
result = responseMessage.getBody();
return result.toString().getBytes("UTF-8");
} else {
if (jsonOperation.getOutputType().getLogical().size() == 0) {
// void operation (json-rpc notification)
try {
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("result", "");
//get response to send to client
return jsonResponse.toString().getBytes("UTF-8");
} catch (Exception e) {
throw new ServiceRuntimeException("Unable to create JSON response", e);
}
} else {
// regular operation returning some value
try {
result = responseMessage.getBody();
JSONObject jsonResponse = new JSONObject();
//JSONObject put will remove the entry if it's value is null
//and per javadoc, we should pass JSONObject.NULL
if(result == null) {
jsonResponse.put("result", JSONObject.NULL);
} else {
jsonResponse.put("result", result);
}
jsonResponse.putOpt("id", id);
//get response to send to client
return jsonResponse.toString().getBytes("UTF-8");
} catch (Exception e) {
throw new ServiceRuntimeException("Unable to create JSON response", e);
}
}
}
} else {
//exception thrown while executing the invocation
Throwable exception = (Throwable)responseMessage.getBody();
JSONRPCResult errorResult = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, exception );
return errorResult.toString().getBytes("UTF-8");
}
}