@Test public void testLimits() throws Exception { JAXBContext context = JAXBContext.newInstance(OnmsNodeList.class); Unmarshaller unmarshaller = context.createUnmarshaller(); for (m_nodeCounter = 0; m_nodeCounter < 20; m_nodeCounter++) { createNode(); } String url = "/nodes"; Map<String,String> parameters = new HashMap<String,String>(); parameters.put("limit", "10"); parameters.put("orderBy", "id"); String xml = sendRequest(GET, url, parameters, 200); assertTrue(xml, xml.contains("Darwin TestMachine 9.4.0 Darwin Kernel Version 9.4.0")); Pattern p = Pattern.compile("<node [^>]*\\s*id=", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); Matcher m = p.matcher(xml); int count = 0; while (m.find()) { count++; } assertEquals("should get 10 nodes back", 10, count); OnmsNodeList list = (OnmsNodeList)unmarshaller.unmarshal(new StringReader(xml)); assertEquals(10, list.getCount()); assertEquals(10, list.getNodes().size()); assertEquals(20, list.getTotalCount()); int i = 0; Set<OnmsNode> sortedNodes = new TreeSet<OnmsNode>(new Comparator<OnmsNode>() { public int compare(OnmsNode o1, OnmsNode o2) { if (o1 == null && o2 == null) { return 0; } else if (o1 == null) { return 1; } else if (o2 == null) { return -1; } else { if (o1.getId() == null) { throw new IllegalStateException("Null ID on node: " + o1.toString()); } return o1.getId().compareTo(o2.getId()); } } }); sortedNodes.addAll(list.getNodes()); for (OnmsNode node : sortedNodes) { assertEquals(node.toString(), "TestMachine" + i++, node.getLabel()); } }
@GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML}) public OnmsNodeList getNodes() { readLock(); try { final CriteriaBuilder builder = new CriteriaBuilder(OnmsNode.class); builder.alias("snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN); builder.alias("ipInterfaces", "ipInterface", JoinType.LEFT_JOIN); builder.alias("categories", "category", JoinType.LEFT_JOIN); final MultivaluedMap<String, String> params = m_uriInfo.getQueryParameters(); final String type = params.getFirst("type"); applyQueryFilters(params, builder); builder.orderBy("label").asc(); final Criteria crit = builder.toCriteria(); if (type == null) { final List<Restriction> restrictions = new ArrayList<Restriction>(crit.getRestrictions()); restrictions.add(Restrictions.ne("type", "D")); crit.setRestrictions(restrictions); } final OnmsNodeList coll = new OnmsNodeList(m_nodeDao.findMatching(crit)); crit.setLimit(null); crit.setOffset(null); crit.setOrders(new ArrayList<Order>()); coll.setTotalCount(m_nodeDao.countMatching(crit)); return coll; } finally { readUnlock(); } }
NodeRestService extends OnmsRestService { @GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML}) public OnmsNodeList getNodes() { readLock(); try { final CriteriaBuilder builder = new CriteriaBuilder(OnmsNode.class); builder.alias("snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN); builder.alias("ipInterfaces", "ipInterface", JoinType.LEFT_JOIN); builder.alias("categories", "category", JoinType.LEFT_JOIN); final MultivaluedMap<String, String> params = m_uriInfo.getQueryParameters(); final String type = params.getFirst("type"); applyQueryFilters(params, builder); builder.orderBy("label").asc(); final Criteria crit = builder.toCriteria(); if (type == null) { final List<Restriction> restrictions = new ArrayList<Restriction>(crit.getRestrictions()); restrictions.add(Restrictions.ne("type", "D")); crit.setRestrictions(restrictions); } final OnmsNodeList coll = new OnmsNodeList(m_nodeDao.findMatching(crit)); crit.setLimit(null); crit.setOffset(null); crit.setOrders(new ArrayList<Order>()); coll.setTotalCount(m_nodeDao.countMatching(crit)); return coll; } finally { readUnlock(); } } }
NodeRestService extends OnmsRestService { @GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML}) public OnmsNodeList getNodes() { readLock(); try { final CriteriaBuilder builder = new CriteriaBuilder(OnmsNode.class); builder.alias("snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN); builder.alias("ipInterfaces", "ipInterface", JoinType.LEFT_JOIN); builder.alias("categories", "category", JoinType.LEFT_JOIN); final MultivaluedMap<String, String> params = m_uriInfo.getQueryParameters(); final String type = params.getFirst("type"); applyQueryFilters(params, builder); builder.orderBy("label").asc(); final Criteria crit = builder.toCriteria(); if (type == null) { final List<Restriction> restrictions = new ArrayList<Restriction>(crit.getRestrictions()); restrictions.add(Restrictions.ne("type", "D")); crit.setRestrictions(restrictions); } final OnmsNodeList coll = new OnmsNodeList(m_nodeDao.findMatching(crit)); crit.setLimit(null); crit.setOffset(null); crit.setOrders(new ArrayList<Order>()); coll.setTotalCount(m_nodeDao.countMatching(crit)); return coll; } finally { readUnlock(); } }  }
NodeRestService extends OnmsRestService { @GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML}) public OnmsNodeList getNodes() { readLock(); try { final CriteriaBuilder builder = new CriteriaBuilder(OnmsNode.class); builder.alias("snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN); builder.alias("ipInterfaces", "ipInterface", JoinType.LEFT_JOIN); builder.alias("categories", "category", JoinType.LEFT_JOIN); final MultivaluedMap<String, String> params = m_uriInfo.getQueryParameters(); final String type = params.getFirst("type"); applyQueryFilters(params, builder); builder.orderBy("label").asc(); final Criteria crit = builder.toCriteria(); if (type == null) { final List<Restriction> restrictions = new ArrayList<Restriction>(crit.getRestrictions()); restrictions.add(Restrictions.ne("type", "D")); crit.setRestrictions(restrictions); } final OnmsNodeList coll = new OnmsNodeList(m_nodeDao.findMatching(crit)); crit.setLimit(null); crit.setOffset(null); crit.setOrders(new ArrayList<Order>()); coll.setTotalCount(m_nodeDao.countMatching(crit)); return coll; } finally { readUnlock(); } }  @GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML}) OnmsNodeList getNodes(); @GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML}) @Path("{nodeCriteria}") OnmsNode getNode(@PathParam("nodeCriteria") final String nodeCriteria); @POST @Consumes(MediaType.APPLICATION_XML) Response addNode(final OnmsNode node); @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Path("{nodeCriteria}") Response updateNode(@PathParam("nodeCriteria") final String nodeCriteria, final MultivaluedMapImpl params); @DELETE @Path("{nodeCriteria}") Response deleteNode(@PathParam("nodeCriteria") final String nodeCriteria); @Path("{nodeCriteria}/ipinterfaces") OnmsIpInterfaceResource getIpInterfaceResource(); @Path("{nodeCriteria}/snmpinterfaces") OnmsSnmpInterfaceResource getSnmpInterfaceResource(); @Path("{nodeCriteria}/categories") OnmsCategoryResource getCategoryResource(); @Path("{nodeCriteria}/assetRecord") AssetRecordResource getAssetRecordResource(); }
NodeRestService extends OnmsRestService { @GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML}) public OnmsNodeList getNodes() { readLock(); try { final CriteriaBuilder builder = new CriteriaBuilder(OnmsNode.class); builder.alias("snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN); builder.alias("ipInterfaces", "ipInterface", JoinType.LEFT_JOIN); builder.alias("categories", "category", JoinType.LEFT_JOIN); final MultivaluedMap<String, String> params = m_uriInfo.getQueryParameters(); final String type = params.getFirst("type"); applyQueryFilters(params, builder); builder.orderBy("label").asc(); final Criteria crit = builder.toCriteria(); if (type == null) { final List<Restriction> restrictions = new ArrayList<Restriction>(crit.getRestrictions()); restrictions.add(Restrictions.ne("type", "D")); crit.setRestrictions(restrictions); } final OnmsNodeList coll = new OnmsNodeList(m_nodeDao.findMatching(crit)); crit.setLimit(null); crit.setOffset(null); crit.setOrders(new ArrayList<Order>()); coll.setTotalCount(m_nodeDao.countMatching(crit)); return coll; } finally { readUnlock(); } }  @GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML}) OnmsNodeList getNodes(); @GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML}) @Path("{nodeCriteria}") OnmsNode getNode(@PathParam("nodeCriteria") final String nodeCriteria); @POST @Consumes(MediaType.APPLICATION_XML) Response addNode(final OnmsNode node); @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Path("{nodeCriteria}") Response updateNode(@PathParam("nodeCriteria") final String nodeCriteria, final MultivaluedMapImpl params); @DELETE @Path("{nodeCriteria}") Response deleteNode(@PathParam("nodeCriteria") final String nodeCriteria); @Path("{nodeCriteria}/ipinterfaces") OnmsIpInterfaceResource getIpInterfaceResource(); @Path("{nodeCriteria}/snmpinterfaces") OnmsSnmpInterfaceResource getSnmpInterfaceResource(); @Path("{nodeCriteria}/categories") OnmsCategoryResource getCategoryResource(); @Path("{nodeCriteria}/assetRecord") AssetRecordResource getAssetRecordResource();  }