88package multichain .command .builders ;
99
1010import java .io .IOException ;
11+ import java .io .UnsupportedEncodingException ;
1112import java .util .ArrayList ;
1213import java .util .Arrays ;
1314import java .util .HashMap ;
2324import org .apache .http .HttpResponse ;
2425import org .apache .http .auth .AuthScope ;
2526import org .apache .http .auth .UsernamePasswordCredentials ;
27+ import org .apache .http .client .ClientProtocolException ;
2628import org .apache .http .client .CredentialsProvider ;
2729import org .apache .http .client .methods .HttpPost ;
2830import org .apache .http .entity .StringEntity ;
@@ -133,7 +135,9 @@ protected enum CommandEnum
133135 SUBSCRIBE ,
134136 UNSUBSCRIBE ,
135137 VALIDATEADDRESS ,
136- VERIFYMESSAGE
138+ VERIFYMESSAGE ,
139+ SENDWITHDATA ,
140+ SENDWITHDATAFROM
137141 }
138142
139143 protected void initialize (String ip , String port , String login , String password ) {
@@ -165,33 +169,12 @@ protected Object execute(CommandEnum command, Object... parameters) throws Multi
165169 if (httpclient != null && httppost != null ) {
166170 try {
167171 // Generate Mapping of calling arguments
168- Map <String , Object > entityValues = new HashMap <String , Object >();
169- entityValues .put ("id" , UUID .randomUUID ().toString ());
170- entityValues .put ("method" , command .toString ().toLowerCase ());
171- List <Object > paramList = new ArrayList <Object >(Arrays .asList (parameters ));
172- entityValues .put ("params" , paramList );
173-
172+ Map <String , Object > entityValues = prepareMap (command , parameters );
174173 // Generate the entity and initialize request
175- StringEntity rpcEntity = new StringEntity ( formatJson ( entityValues ) );
174+ StringEntity rpcEntity = prepareRpcEntity ( entityValues );
176175 httppost .setEntity (rpcEntity );
177-
178176 // Execute the request and get the answer
179- HttpResponse response = httpclient .execute (httppost );
180- HttpEntity entity = response .getEntity ();
181-
182- String rpcAnswer = EntityUtils .toString (entity );
183-
184- final Gson gson = new GsonBuilder ().create ();
185- final MultiChainRPCAnswer multiChainRPCAnswer = gson .fromJson (rpcAnswer , MultiChainRPCAnswer .class );
186-
187- if (multiChainRPCAnswer != null && multiChainRPCAnswer .getError () == null ) {
188- return multiChainRPCAnswer .getResult ();
189- } else if (multiChainRPCAnswer != null && multiChainRPCAnswer .getError () != null ) {
190- throw new MultichainException ("code :" + multiChainRPCAnswer .getError ().get ("code" ).toString (),
191- "message : " + multiChainRPCAnswer .getError ().get ("message" ).toString ());
192- } else {
193- throw new MultichainException (null , "General RPC Exceution Technical Error" );
194- }
177+ return executeRequest ();
195178
196179 } catch (IOException e ) {
197180 e .printStackTrace ();
@@ -202,7 +185,38 @@ protected Object execute(CommandEnum command, Object... parameters) throws Multi
202185 "MultiChainCommand not initialized, please specify ip, port, user and pwd !" );
203186
204187 }
188+ }
189+
190+ protected StringEntity prepareRpcEntity (Map <String , Object > entityValues ) throws UnsupportedEncodingException {
191+ return new StringEntity (formatJson (entityValues ));
192+ }
193+
194+ private Object executeRequest () throws IOException , ClientProtocolException , MultichainException {
195+ HttpResponse response = httpclient .execute (httppost );
196+ HttpEntity entity = response .getEntity ();
197+
198+ String rpcAnswer = EntityUtils .toString (entity );
199+
200+ final Gson gson = new GsonBuilder ().create ();
201+ final MultiChainRPCAnswer multiChainRPCAnswer = gson .fromJson (rpcAnswer , MultiChainRPCAnswer .class );
202+
203+ if (multiChainRPCAnswer != null && multiChainRPCAnswer .getError () == null ) {
204+ return multiChainRPCAnswer .getResult ();
205+ } else if (multiChainRPCAnswer != null && multiChainRPCAnswer .getError () != null ) {
206+ throw new MultichainException ("code :" + multiChainRPCAnswer .getError ().get ("code" ).toString (),
207+ "message : " + multiChainRPCAnswer .getError ().get ("message" ).toString ());
208+ } else {
209+ throw new MultichainException (null , "General RPC Exceution Technical Error" );
210+ }
211+ }
205212
213+ private Map <String , Object > prepareMap (CommandEnum command , Object ... parameters ) {
214+ Map <String , Object > entityValues = new HashMap <String , Object >();
215+ entityValues .put ("id" , UUID .randomUUID ().toString ());
216+ entityValues .put ("method" , command .toString ().toLowerCase ());
217+ List <Object > paramList = new ArrayList <Object >(Arrays .asList (parameters ));
218+ entityValues .put ("params" , paramList );
219+ return entityValues ;
206220 }
207221
208222 @ SuppressWarnings ("rawtypes" )
0 commit comments