private PojoRest getPojoRest() { if (pojoRest == null) pojoRest = new PojoRest(this); return pojoRest; }
/** * Returns the specified entry value converted to a {@link String}. * * <p> * Shortcut for <code>get(String.<jk>class</jk>, key)</code>. * * @param url The key. * @return The converted value, or <jk>null</jk> if the map contains no mapping for this key. */ public String getString(String url) { return get(url, String.class); }
/** * Retrieves the element addressed by the URL. * * @param url * The URL of the element to retrieve. * <br>If <jk>null</jk> or blank, returns the root. * @return The addressed element, or <jk>null</jk> if that element does not exist in the tree. */ public Object get(String url) { return getWithDefault(url, null); }
@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()); }
model = new PojoRest(new ObjectMap()); Person p = new Person("some name", 123, new Address("street A", "city A", "state A", 12345, true), new Address("street B", "city B", "state B", 12345, false) ); model.put("/person1", p); assertEquals("{person1:{name:'some name',age:123,addresses:[{street:'street A',city:'city A',state:'state A',zip:12345,isCurrent:true},{street:'street B',city:'city B',state:'state B',zip:12345,isCurrent:false}]}}", serializer.serialize(model.getRootObject())); p = (Person)model.get("/person1"); assertEquals("city B", p.addresses[1].city); Address a3 = (Address)model.get("/person1/addresses/1"); assertEquals("city B", a3.city); model = new PojoRest(p); model.post("addresses", new Address("street C", "city C", "state C", 12345, true)); s = ((Address)model.get("addresses/2")).toString(); expectedValue = "Address(street=street C,city=city C,state=state C,zip=12345,isCurrent=true)"; assertEquals(expectedValue, s); 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.delete("addresses/1"); s = serializer.serialize(p);
url = normalizeUrl(url); JsonNode p = getNode(url, root); return p == null ? null : p.o; JsonNode n = (parentUrl == null ? root : getNode(parentUrl, root)); if (n == null) throw new PojoRestException(HTTP_NOT_FOUND, "Node at URL ''{0}'' not found.", parentUrl); Object o = n.o; if (cm.isMap()) return ((Map)o).put(childKey, convert(val, cm.getValueType())); if (cm.isCollection() && o instanceof List) return ((List)o).set(parseInt(childKey), convert(val, cm.getElementType())); if (cm.isArray()) { o = setArrayEntry(n.o, parseInt(childKey), val, cm.getElementType()); ClassMeta pct = n.parent.cm; Object po = n.parent.o; if (cm.isCollection()) { Collection c = (Collection)o; c.add(convert(val, cm.getElementType())); return (c instanceof List ? url + "/" + (c.size()-1) : null); Object[] o2 = addArrayEntry(o, val, cm.getElementType()); root = new JsonNode(null, null, o2, null); return url + "/" + (o2.length-1); JsonNode n = getNode(url, root);
@Test public void testConstructors() throws Exception { PojoRest model = new PojoRest(new AddressBook(), JsonParser.DEFAULT); // Try adding a person to the address book. Person billClinton = new Person("Bill Clinton", 65, new Address("55W. 125th Street", "New York", "NY", 10027, true) ); model.post("/", billClinton); // Make sure we get the original person back. billClinton = (Person)model.get("/0"); }
/** * [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); } }
@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()); }
@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()); } }
@Test public void testGetClassMeta() throws Exception { PojoRest model = new PojoRest(new AddressBook().init()); assertEquals("Person", model.getClassMeta("0").getInnerClass().getSimpleName()); assertEquals("String", model.getClassMeta("0/addresses/0/state").getInnerClass().getSimpleName()); assertNull(model.getClassMeta("1")); assertNull(model.getClassMeta("0/addresses/1/state")); } }
@Test public void testInvokeMethod() throws Exception { PojoRest model = new PojoRest(new AddressBook().init()); assertEquals("Person(name=Bill Clinton,age=65)", model.invokeMethod("0", "toString", "")); model = new PojoRest(new AddressBook().init(), JsonParser.DEFAULT); assertEquals("Person(name=Bill Clinton,age=65)", model.invokeMethod("0", "toString", "")); assertEquals("NY", model.invokeMethod("0/addresses/0/state", "toString", "")); assertNull(model.invokeMethod("1", "toString", "")); }
@Test public void testGetPublicMethods() throws Exception { PojoRest model = new PojoRest(new AddressBook().init()); assertTrue(SimpleJsonSerializer.DEFAULT.toString(model.getPublicMethods("0")).contains("'toString'")); assertTrue(SimpleJsonSerializer.DEFAULT.toString(model.getPublicMethods("0/addresses/0/state")).contains("'toString'")); assertNull(model.getPublicMethods("1")); }
/** * 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); }
/** * Similar to {@link #remove(Object) remove(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(0).remove(<js>"baz"</js>); * * <jc>// Using this method</jc> * m.deleteAt(<js>"foo/bar/0/baz"</js>); * </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. * @return The previous value, or <jk>null</jk> if the entry doesn't exist. */ public Object deleteAt(String path) { return getPojoRest().delete(path); }
/** * Similar to {@link #putAt(String,Object) putAt(String,Object)}, but used to append to collections and arrays. * * <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>).append(123); * * <jc>// Using this method</jc> * m.postAt(<js>"foo/bar"</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 postAt(String path, Object o) { return getPojoRest().post(path, o); }
url = normalizeUrl(url); JsonNode p = getNode(url, root); return p == null ? null : p.o; JsonNode n = (parentUrl == null ? root : getNode(parentUrl, root)); if (n == null) throw new PojoRestException(HTTP_NOT_FOUND, "Node at URL ''{0}'' not found.", parentUrl); Object o = n.o; if (cm.isMap()) return ((Map)o).put(childKey, convert(val, cm.getValueType())); if (cm.isCollection() && o instanceof List) return ((List)o).set(parseInt(childKey), convert(val, cm.getElementType())); if (cm.isArray()) { o = setArrayEntry(n.o, parseInt(childKey), val, cm.getElementType()); ClassMeta pct = n.parent.cm; Object po = n.parent.o; if (cm.isCollection()) { Collection c = (Collection)o; c.add(convert(val, cm.getElementType())); return (c instanceof List ? url + "/" + (c.size()-1) : null); Object[] o2 = addArrayEntry(o, val, cm.getElementType()); root = new JsonNode(null, null, o2, null); return url + "/" + (o2.length-1); JsonNode n = getNode(url, root);
@Test public void testAddressBook() { PojoRest model; model = new PojoRest(new AddressBook()); // Try adding a person to the address book. Person billClinton = new Person("Bill Clinton", 65, new Address("55W. 125th Street", "New York", "NY", 10027, true) ); model.post("/", billClinton); // Make sure we get the original person back. billClinton = (Person)model.get("/0"); }