@@ -209,29 +209,29 @@ public MerkleNode _new(Optional<String> template) throws IOException {
209209 public MerkleNode patch (Multihash base , String command , Optional <byte []> data , Optional <String > name , Optional <Multihash > target ) throws IOException {
210210 if (!ObjectPatchTypes .contains (command ))
211211 throw new IllegalStateException ("Illegal Object.patch command type: " +command );
212- String targetPath = "object/patch?arg=" + base .toBase58 () + "&arg=" + command ;
212+ String targetPath = "object/patch/" + command + " ?arg=" + base .toBase58 ();
213213 if (name .isPresent ())
214214 targetPath += "&arg=" + name .get ();
215215 if (target .isPresent ())
216216 targetPath += "&arg=" + target .get ().toBase58 ();
217217
218218 switch (command ) {
219219 case "add-link" :
220- if (!name . isPresent () || ! target .isPresent ())
220+ if (!target .isPresent ())
221221 throw new IllegalStateException ("add-link requires name and target!" );
222- return MerkleNode .fromJSON (retrieveMap (targetPath ));
223222 case "rm-link" :
224223 if (!name .isPresent ())
225- throw new IllegalStateException ("rm- link requires name!" );
224+ throw new IllegalStateException ("link name is required !" );
226225 return MerkleNode .fromJSON (retrieveMap (targetPath ));
227226 case "set-data" :
228- if (!data .isPresent ())
229- throw new IllegalStateException ("set-data requires data!" );
230- return MerkleNode .fromJSON (postMap (targetPath , data .get (), Collections .EMPTY_MAP ));
231227 case "append-data" :
232228 if (!data .isPresent ())
233- throw new IllegalStateException ("append-data requires data!" );
234- return MerkleNode .fromJSON (postMap (targetPath , data .get (), Collections .EMPTY_MAP ));
229+ throw new IllegalStateException ("set-data requires data!" );
230+ Multipart m = new Multipart ("http://" + host + ":" + port + version +"object/patch/" +command +"?arg=" +base .toBase58 ()+"&stream-channels=true" , "UTF-8" );
231+ m .addFilePart ("file" , new NamedStreamable .ByteArrayWrapper (data .get ()));
232+ String res = m .finish ();
233+ return MerkleNode .fromJSON (JSONParser .parse (res ));
234+
235235 default :
236236 throw new IllegalStateException ("Unimplemented" );
237237 }
@@ -419,14 +419,18 @@ private static byte[] get(URL target) throws IOException {
419419 conn .setRequestMethod ("GET" );
420420 conn .setRequestProperty ("Content-Type" , "application/json" );
421421
422- InputStream in = conn .getInputStream ();
423- ByteArrayOutputStream resp = new ByteArrayOutputStream ();
422+ try {
423+ InputStream in = conn .getInputStream ();
424+ ByteArrayOutputStream resp = new ByteArrayOutputStream ();
424425
425- byte [] buf = new byte [4096 ];
426- int r ;
427- while ((r =in .read (buf )) >= 0 )
428- resp .write (buf , 0 , r );
429- return resp .toByteArray ();
426+ byte [] buf = new byte [4096 ];
427+ int r ;
428+ while ((r = in .read (buf )) >= 0 )
429+ resp .write (buf , 0 , r );
430+ return resp .toByteArray ();
431+ } catch (IOException e ) {
432+ throw new RuntimeException ("Trailer: " + conn .getHeaderFields ().get ("Trailer" ), e );
433+ }
430434 }
431435
432436 private Map postMap (String path , byte [] body , Map <String , String > headers ) throws IOException {
0 commit comments