1818
1919@ SuppressWarnings ({"rawtypes" , "unchecked" })
2020public class Uploader {
21+
22+ private final class Command {
23+ final static String add = "add" ;
24+ final static String remove = "remove" ;
25+ final static String replace = "replace" ;
26+ final static String removeAll = "remove_all" ;
27+
28+ private Command () {
29+ }
30+ }
31+
2132 public Map callApi (String action , Map <String , Object > params , Map options , Object file ) throws IOException {
2233 return strategy .callApi (action , params , options , file );
2334 }
@@ -267,26 +278,26 @@ public Map addTag(String tag, String[] publicIds, Map options) throws IOExceptio
267278 if (options == null )
268279 options = ObjectUtils .emptyMap ();
269280 boolean exclusive = ObjectUtils .asBoolean (options .get ("exclusive" ), false );
270- String command = exclusive ? "set_exclusive" : " add" ;
281+ String command = exclusive ? "set_exclusive" : Command . add ;
271282 return callTagsApi (tag , command , publicIds , options );
272283 }
273284
274285 public Map removeTag (String tag , String [] publicIds , Map options ) throws IOException {
275286 if (options == null )
276287 options = ObjectUtils .emptyMap ();
277- return callTagsApi (tag , " remove" , publicIds , options );
288+ return callTagsApi (tag , Command . remove , publicIds , options );
278289 }
279290
280291 public Map removeAllTags (String [] publicIds , Map options ) throws IOException {
281292 if (options == null )
282293 options = ObjectUtils .emptyMap ();
283- return callTagsApi (null , "remove_all" , publicIds , options );
294+ return callTagsApi (null , Command . removeAll , publicIds , options );
284295 }
285296
286297 public Map replaceTag (String tag , String [] publicIds , Map options ) throws IOException {
287298 if (options == null )
288299 options = ObjectUtils .emptyMap ();
289- return callTagsApi (tag , " replace" , publicIds , options );
300+ return callTagsApi (tag , Command . replace , publicIds , options );
290301 }
291302
292303 public Map callTagsApi (String tag , String command , String [] publicIds , Map options ) throws IOException {
@@ -302,6 +313,58 @@ public Map callTagsApi(String tag, String command, String[] publicIds, Map optio
302313 return callApi ("tags" , params , options , null );
303314 }
304315
316+ /**
317+ * Add a context keys and values. If a particular key already exists, the value associated with the key is updated.
318+ * @param context a map of key and value. Serialized to "key1=value1|key2=value2"
319+ * @param publicIds the public IDs of the resources to update
320+ * @param options additional options passed to the request
321+ * @return a list of public IDs that were updated
322+ * @throws IOException
323+ */
324+ public Map addContext (Map context , String [] publicIds , Map options ) throws IOException {
325+ return callContextApi (context , Command .add , publicIds , options );
326+ }
327+
328+ /**
329+ * Add a context keys and values. If a particular key already exists, the value associated with the key is updated.
330+ * @param context Serialized context in the form of "key1=value1|key2=value2"
331+ * @param publicIds the public IDs of the resources to update
332+ * @param options additional options passed to the request
333+ * @return a list of public IDs that were updated
334+ * @throws IOException
335+ */
336+ public Map addContext (String context , String [] publicIds , Map options ) throws IOException {
337+ return callContextApi (context , Command .add , publicIds , options );
338+ }
339+
340+ /**
341+ * Remove all custom context from the specified public IDs.
342+ * @param publicIds the public IDs of the resources to update
343+ * @param options additional options passed to the request
344+ * @return a list of public IDs that were updated
345+ * @throws IOException
346+ */
347+ public Map removeAllContext (String [] publicIds , Map options ) throws IOException {
348+ return callContextApi ((String )null , Command .removeAll , publicIds , options );
349+ }
350+
351+ protected Map callContextApi (Map context , String command , String [] publicIds , Map options ) throws IOException {
352+ return callContextApi (Util .encodeContext (context ), command , publicIds , options );
353+ }
354+
355+ protected Map callContextApi (String context , String command , String [] publicIds , Map options ) throws IOException {
356+ if (options == null )
357+ options = ObjectUtils .emptyMap ();
358+ Map <String , Object > params = new HashMap <String , Object >();
359+ if (context != null ) {
360+ params .put ("context" , context );
361+ }
362+ params .put ("command" , command );
363+ params .put ("type" , (String ) options .get ("type" ));
364+ params .put ("public_ids" , Arrays .asList (publicIds ));
365+ return callApi ("context" , params , options , null );
366+ }
367+
305368 private final static String [] TEXT_PARAMS = {"public_id" , "font_family" , "font_size" , "font_color" , "text_align" , "font_weight" , "font_style" ,
306369 "background" , "opacity" , "text_decoration" };
307370
0 commit comments