/** * Same as {@link #set(int,Object) set(int,Object)}, but the key is a slash-delimited path used to traverse entries * in this POJO. * * <p> * For example, the following code is equivalent: * </p> * <p class='bcode w800'> * ObjectMap m = getObjectMap(); * * <jc>// Long way</jc> * m.getObjectMap(<js>"foo"</js>).getObjectList(<js>"bar"</js>).getObjectMap(<js>"0"</js>).put(<js>"baz"</js>, 123); * * <jc>// Using this method</jc> * m.putAt(<js>"foo/bar/0/baz"</js>, 123); * </p> * * <p> * This method uses the {@link PojoRest} class to perform the lookup, so the map can contain any of the various * class types that the {@link PojoRest} class supports (e.g. beans, collections, arrays). * * @param path The path to the entry. * @param o The new value. * @return The previous value, or <jk>null</jk> if the entry doesn't exist. */ public Object putAt(String path, Object o) { return getPojoRest().put(path, o); }
/** * Same as {@link #set(int,Object) set(int,Object)}, but the key is a slash-delimited path used to traverse entries * in this POJO. * * <p> * For example, the following code is equivalent: * </p> * <p class='bcode w800'> * ObjectMap m = getObjectMap(); * * <jc>// Long way</jc> * m.getObjectMap(<js>"foo"</js>).getObjectList(<js>"bar"</js>).getObjectMap(<js>"0"</js>).put(<js>"baz"</js>, 123); * * <jc>// Using this method</jc> * m.putAt(<js>"foo/bar/0/baz"</js>, 123); * </p> * * <p> * This method uses the {@link PojoRest} class to perform the lookup, so the map can contain any of the various * class types that the {@link PojoRest} class supports (e.g. beans, collections, arrays). * * @param path The path to the entry. * @param o The new value. * @return The previous value, or <jk>null</jk> if the entry doesn't exist. */ public Object putAt(String path, Object o) { return getPojoRest().put(path, o); }
/** * Same as <code>put(String,Object)</code>, but the key is a slash-delimited path used to traverse entries in this * POJO. * * <p> * For example, the following code is equivalent: * </p> * <p class='bcode w800'> * ObjectMap m = getObjectMap(); * * <jc>// Long way</jc> * m.getObjectMap(<js>"foo"</js>).getObjectList(<js>"bar"</js>).getObjectMap(<js>"0"</js>) * .put(<js>"baz"</js>, 123); * * <jc>// Using this method</jc> * m.putAt(<js>"foo/bar/0/baz"</js>, 123); * </p> * * <p> * This method uses the {@link PojoRest} class to perform the lookup, so the map can contain any of the various * class types that the {@link PojoRest} class supports (e.g. beans, collections, arrays). * * @param path The path to the entry. * @param o The new value. * @return The previous value, or <jk>null</jk> if the entry doesn't exist. */ public Object putAt(String path, Object o) { return getPojoRest().put(path, o); }
/** * Same as {@link #set(int,Object) set(int,Object)}, but the key is a slash-delimited path used to traverse entries * in this POJO. * * <p> * For example, the following code is equivalent: * </p> * <p class='bcode w800'> * ObjectMap m = getObjectMap(); * * <jc>// Long way</jc> * m.getObjectMap(<js>"foo"</js>).getObjectList(<js>"bar"</js>).getObjectMap(<js>"0"</js>).put(<js>"baz"</js>, 123); * * <jc>// Using this method</jc> * m.putAt(<js>"foo/bar/0/baz"</js>, 123); * </p> * * <p> * This method uses the {@link PojoRest} class to perform the lookup, so the map can contain any of the various * class types that the {@link PojoRest} class supports (e.g. beans, collections, arrays). * * @param path The path to the entry. * @param o The new value. * @return The previous value, or <jk>null</jk> if the entry doesn't exist. */ public Object putAt(String path, Object o) { return getPojoRest().put(path, o); }
/** * Same as <code>put(String,Object)</code>, but the key is a slash-delimited path used to traverse entries in this * POJO. * * <p> * For example, the following code is equivalent: * </p> * <p class='bcode w800'> * ObjectMap m = getObjectMap(); * * <jc>// Long way</jc> * m.getObjectMap(<js>"foo"</js>).getObjectList(<js>"bar"</js>).getObjectMap(<js>"0"</js>) * .put(<js>"baz"</js>, 123); * * <jc>// Using this method</jc> * m.putAt(<js>"foo/bar/0/baz"</js>, 123); * </p> * * <p> * This method uses the {@link PojoRest} class to perform the lookup, so the map can contain any of the various * class types that the {@link PojoRest} class supports (e.g. beans, collections, arrays). * * @param path The path to the entry. * @param o The new value. * @return The previous value, or <jk>null</jk> if the entry doesn't exist. */ public Object putAt(String path, Object o) { return getPojoRest().put(path, o); }
/** * Same as <code>put(String,Object)</code>, but the key is a slash-delimited path used to traverse entries in this * POJO. * * <p> * For example, the following code is equivalent: * </p> * <p class='bcode w800'> * ObjectMap m = getObjectMap(); * * <jc>// Long way</jc> * m.getObjectMap(<js>"foo"</js>).getObjectList(<js>"bar"</js>).getObjectMap(<js>"0"</js>) * .put(<js>"baz"</js>, 123); * * <jc>// Using this method</jc> * m.putAt(<js>"foo/bar/0/baz"</js>, 123); * </p> * * <p> * This method uses the {@link PojoRest} class to perform the lookup, so the map can contain any of the various * class types that the {@link PojoRest} class supports (e.g. beans, collections, arrays). * * @param path The path to the entry. * @param o The new value. * @return The previous value, or <jk>null</jk> if the entry doesn't exist. */ public Object putAt(String path, Object o) { return getPojoRest().put(path, o); }
@Test public void testBasic() { // TODO: Need to write some exhaustive tests here. Will open work item // to do that later. PojoRest model = new PojoRest(new ObjectMap()); // An empty model. // Do a PUT model.put("A", new ObjectMap()); model.put("A/B", new ObjectMap()); model.put("A/B/C", "A new string"); assertEquals("{A:{B:{C:'A new string'}}}", model.toString()); // Do a POST to a list. model.put("A/B/C", new LinkedList()); model.post("A/B/C", "String #1"); model.post("A/B/C", "String #2"); assertEquals("{A:{B:{C:['String #1\','String #2']}}}", model.toString()); // Do some GETs String s = (String) model.get("A/B/C/0"); assertEquals("String #1", s); Map m = (Map) model.get("A/B"); assertEquals("{C:['String #1','String #2']}", m.toString()); }
@Test public void testGetRootObject() throws Exception { PojoRest model = new PojoRest(new AddressBook()); assertTrue(model.getRootObject() instanceof AddressBook); model.put("", "foobar"); assertTrue(model.getRootObject() instanceof String); model.put("", null); assertNull(model.getRootObject()); }
/** * [PUT /people/{id}/*] * Change property on Person bean. */ @RestMethod(name="PUT", path="/people/{id}/*", guards=AdminGuard.class ) public String updatePerson(RestRequest req, @Path int id) throws Exception { try { Person p = findPerson(id); String pathRemainder = req.getPathRemainder(); PojoRest r = new PojoRest(p); ClassMeta<?> cm = r.getClassMeta(pathRemainder); Object in = req.getBody(cm); r.put(pathRemainder, in); return "PUT successful"; } catch (Exception e) { throw new RestException(SC_BAD_REQUEST, "PUT unsuccessful").initCause(e); } }
/** * [PUT /addresses/{id}/*] * Change property on Address bean. */ @RestMethod(name="PUT", path="/addresses/{id}/*", guards=AdminGuard.class ) public String updateAddress(RestRequest req, @Path int id) throws Exception { try { Address a = findAddress(id); String pathInfo = req.getPathInfo(); PojoRest r = new PojoRest(a); ClassMeta<?> cm = r.getClassMeta(pathInfo); Object in = req.getBody(cm); r.put(pathInfo, in); return "PUT successful"; } catch (Exception e) { throw new RestException(SC_BAD_REQUEST, "PUT unsuccessful").initCause(e); } }
@Test public void testRootLocked() throws Exception { PojoRest model = new PojoRest(new AddressBook()).setRootLocked(); try { model.put("", new AddressBook()); fail(); } catch (PojoRestException e) { assertEquals("Cannot overwrite root object", e.getLocalizedMessage()); } try { model.put(null, new AddressBook()); fail(); } catch (PojoRestException e) { assertEquals("Cannot overwrite root object", e.getLocalizedMessage()); } try { model.put("/", new AddressBook()); fail(); } catch (PojoRestException e) { assertEquals("Cannot overwrite root object", e.getLocalizedMessage()); } }
new Address("street B", "city B", "state B", 12345, false) ); model.put("/person1", p); model.put("addresses/0", new Address("street D", "city D", "state D", 12345, false)); model.put("addresses/1", new Address("street E", "city E", "state E", 12345, false)); model.put("addresses/2", new Address("street F", "city F", "state F", 12345, false)); serializer = JsonSerializer.create().ssq().build(); s = serializer.serialize(p); model.put("addresses/2", new Address("street A", "city A", "state A", 12345, true)); s = serializer.serialize(p); expectedValue = "{name:'some name',age:123,addresses:[null,null,{street:'street A',city:'city A',state:'state A',zip:12345,isCurrent:true}]}"; model.put("addresses/1", m); s = ((Address)model.get("addresses/1")).toString(); expectedValue = "Address(street=street D,city=city D,state=state D,zip=12345,isCurrent=false)"; model.put("addresses/1/zip", new Integer(99999)); s = model.get("addresses/1/zip").toString(); expectedValue = "99999"; model.put("", m2); s = model.get("").toString(); assertEquals("{x:1}", s); m2 = new ObjectMap("{x:2}"); model.put("/", m2); s = model.get("").toString(); assertEquals("{x:2}", s);