@ SuppressWarnings ( "unchecked" ) public List < RpcResponse > request ( List < RpcRequest > reqList ) { List < RpcResponse > respList = new ArrayList < RpcResponse > ( ) ; List < Map > marshaledReqs = new ArrayList < Map > ( ) ; Map < String , RpcRequest > byReqId = new HashMap < String , RpcRequest > ( ) ; for ( RpcRequest req : reqList ) { try { marshaledReqs . add ( req . marshal ( contract ) ) ; byReqId . put ( req . getId ( ) , req ) ; } catch ( RpcException e ) { respList . add ( new RpcResponse ( req , e ) ) ; } } InputStream is = null ; try { ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ; serializer . write ( marshaledReqs , bos ) ; bos . close ( ) ; byte [ ] data = bos . toByteArray ( ) ; is = requestRaw ( data ) ; List < Map > responses = serializer . readList ( is ) ; for ( Map map : responses ) { String id = ( String ) map . get ( "id" ) ; if ( id != null ) { RpcRequest req = byReqId . get ( id ) ; if ( req == null ) { // TODO: ?? log error? } else { byReqId . remove ( id ) ; respList . add ( unmarshal ( req , map ) ) ; } } else { // TODO: ?? log error? } } if ( byReqId . size ( ) > 0 ) { for ( RpcRequest req : byReqId . values ( ) ) { String msg = "No response in batch for request " + req . getId ( ) ; RpcException exc = RpcException . Error . INVALID_RESP . exc ( msg ) ; RpcResponse resp = new RpcResponse ( req , exc ) ; respList . add ( resp ) ; } } } catch ( IOException e ) { String msg = "IOException requesting batch " + " from: " + endpoint + " - " + e . getMessage ( ) ; RpcException exc = RpcException . Error . INTERNAL . exc ( msg ) ; respList . add ( new RpcResponse ( null , exc ) ) ; } finally { closeQuietly ( is ) ; } return respList ; }
Makes JSON - RPC batch request against the remote server as a single HTTP request .