@Test public void testList() throws Exception { MockGeoServer.get().catalog() .workspace("foo", "http: .layer("one").style().point().layer() .featureType().defaults().store("one").workspace() .layer("two").style().point().layer() .featureType().defaults().store("two").workspace() .geoServer().build(geoServer); Catalog catalog = geoServer.getCatalog(); final List<LayerInfo> layers = catalog.getLayers(); Answer<CloseableIteratorAdapter<LayerInfo>> a = new Answer<CloseableIteratorAdapter<LayerInfo>>() { @Override public CloseableIteratorAdapter<LayerInfo> answer(InvocationOnMock invocation) throws Throwable { return new CloseableIteratorAdapter<LayerInfo>(layers.iterator()); } }; when(catalog.list(eq(LayerInfo.class), eq(Predicates.and(Predicates.equal("resource.namespace.prefix", "foo"), Predicates.fullTextSearch("o"))), isA(Integer.class), isA(Integer.class), isA(SortBy.class))).thenAnswer(a); when(catalog.count(eq(LayerInfo.class), eq(Predicates.and(Predicates.equal("resource.namespace.prefix", "foo"), Predicates.fullTextSearch(""))))).thenReturn(layers.size()); MvcResult result = mvc.perform(get("/app/api/layers/foo")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andReturn(); JSONObj obj = JSONWrapper.read(result.getResponse().getContentAsString()).toObject(); assertEquals(2, obj.integer("total").intValue()); assertEquals(2, obj.integer("count").intValue()); assertEquals(0, obj.integer("page").intValue()); JSONArr arr = obj.array("layers"); assertEquals(arr.size(), 2); Iterables.find(arr, new Predicate<Object>() { @Override public boolean apply(@Nullable Object o) { return "one".equals(JSONWrapper.wrap(o).toObject().str("name")); } }); Iterables.find(arr, new Predicate<Object>() { @Override public boolean apply(@Nullable Object o) { return "two".equals(JSONWrapper.wrap(o).toObject().str("name")); } }); result = mvc.perform(get("/app/api/layers/foo?page=0&count=10&sort=name:asc&filter=o")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andReturn(); result = mvc.perform(get("/app/api/layers/foo?page=0&count=10&sort=invalidparameter:badrequest&filter=o")) .andExpect(status().isBadRequest()) .andReturn(); }
@RequestMapping(value="/{wsName:.+}", method = RequestMethod.GET) public @ResponseBody JSONObj list(@PathVariable String wsName, @RequestParam(value="page", required=false) Integer page, @RequestParam(value="count", required=false, defaultValue=""+DEFAULT_PAGESIZE) Integer count, @RequestParam(value="sort", required=false) String sort, @RequestParam(value="filter", required=false) String textFilter, HttpServletRequest req) { JSONObj obj = new JSONObj(); Catalog cat = geoServer.getCatalog(); if ("default".equals(wsName)) { WorkspaceInfo def = cat.getDefaultWorkspace(); if (def != null) { wsName = def.getName(); } } Filter filter = equal("resource.namespace.prefix", wsName); if (textFilter != null) { filter = Predicates.and(filter, Predicates.fullTextSearch(textFilter)); } Integer total = cat.count(LayerInfo.class, filter); SortBy sortBy = parseSort(sort); obj.put("total", total); obj.put("page", page != null ? page : 0); obj.put("count", Math.min(total, count != null ? count : total)); JSONArr arr = obj.putArray("layers"); try ( CloseableIterator<LayerInfo> it = cat.list(LayerInfo.class, filter, offset(page, count), count, sortBy); ) { while (it.hasNext()) { layer(arr.addObject(), it.next(), req); } } return obj; }
LayerController extends ApiController { @RequestMapping(value="/{wsName:.+}", method = RequestMethod.GET) public @ResponseBody JSONObj list(@PathVariable String wsName, @RequestParam(value="page", required=false) Integer page, @RequestParam(value="count", required=false, defaultValue=""+DEFAULT_PAGESIZE) Integer count, @RequestParam(value="sort", required=false) String sort, @RequestParam(value="filter", required=false) String textFilter, HttpServletRequest req) { JSONObj obj = new JSONObj(); Catalog cat = geoServer.getCatalog(); if ("default".equals(wsName)) { WorkspaceInfo def = cat.getDefaultWorkspace(); if (def != null) { wsName = def.getName(); } } Filter filter = equal("resource.namespace.prefix", wsName); if (textFilter != null) { filter = Predicates.and(filter, Predicates.fullTextSearch(textFilter)); } Integer total = cat.count(LayerInfo.class, filter); SortBy sortBy = parseSort(sort); obj.put("total", total); obj.put("page", page != null ? page : 0); obj.put("count", Math.min(total, count != null ? count : total)); JSONArr arr = obj.putArray("layers"); try ( CloseableIterator<LayerInfo> it = cat.list(LayerInfo.class, filter, offset(page, count), count, sortBy); ) { while (it.hasNext()) { layer(arr.addObject(), it.next(), req); } } return obj; } }
LayerController extends ApiController { @RequestMapping(value="/{wsName:.+}", method = RequestMethod.GET) public @ResponseBody JSONObj list(@PathVariable String wsName, @RequestParam(value="page", required=false) Integer page, @RequestParam(value="count", required=false, defaultValue=""+DEFAULT_PAGESIZE) Integer count, @RequestParam(value="sort", required=false) String sort, @RequestParam(value="filter", required=false) String textFilter, HttpServletRequest req) { JSONObj obj = new JSONObj(); Catalog cat = geoServer.getCatalog(); if ("default".equals(wsName)) { WorkspaceInfo def = cat.getDefaultWorkspace(); if (def != null) { wsName = def.getName(); } } Filter filter = equal("resource.namespace.prefix", wsName); if (textFilter != null) { filter = Predicates.and(filter, Predicates.fullTextSearch(textFilter)); } Integer total = cat.count(LayerInfo.class, filter); SortBy sortBy = parseSort(sort); obj.put("total", total); obj.put("page", page != null ? page : 0); obj.put("count", Math.min(total, count != null ? count : total)); JSONArr arr = obj.putArray("layers"); try ( CloseableIterator<LayerInfo> it = cat.list(LayerInfo.class, filter, offset(page, count), count, sortBy); ) { while (it.hasNext()) { layer(arr.addObject(), it.next(), req); } } return obj; } @Autowired  LayerController(GeoServer geoServer, RecentObjectCache recentCache, Importer importer); }
LayerController extends ApiController { @RequestMapping(value="/{wsName:.+}", method = RequestMethod.GET) public @ResponseBody JSONObj list(@PathVariable String wsName, @RequestParam(value="page", required=false) Integer page, @RequestParam(value="count", required=false, defaultValue=""+DEFAULT_PAGESIZE) Integer count, @RequestParam(value="sort", required=false) String sort, @RequestParam(value="filter", required=false) String textFilter, HttpServletRequest req) { JSONObj obj = new JSONObj(); Catalog cat = geoServer.getCatalog(); if ("default".equals(wsName)) { WorkspaceInfo def = cat.getDefaultWorkspace(); if (def != null) { wsName = def.getName(); } } Filter filter = equal("resource.namespace.prefix", wsName); if (textFilter != null) { filter = Predicates.and(filter, Predicates.fullTextSearch(textFilter)); } Integer total = cat.count(LayerInfo.class, filter); SortBy sortBy = parseSort(sort); obj.put("total", total); obj.put("page", page != null ? page : 0); obj.put("count", Math.min(total, count != null ? count : total)); JSONArr arr = obj.putArray("layers"); try ( CloseableIterator<LayerInfo> it = cat.list(LayerInfo.class, filter, offset(page, count), count, sortBy); ) { while (it.hasNext()) { layer(arr.addObject(), it.next(), req); } } return obj; } @Autowired  LayerController(GeoServer geoServer, RecentObjectCache recentCache, Importer importer); @RequestMapping(value="/{wsName:.+}", method = RequestMethod.GET) @ResponseBody JSONObj list(@PathVariable String wsName, 
            @RequestParam(value="page", required=false) Integer page,
            @RequestParam(value="count", required=false, defaultValue=""+DEFAULT_PAGESIZE) Integer count,
            @RequestParam(value="sort", required=false) String sort, 
            @RequestParam(value="filter", required=false) String textFilter, 
            HttpServletRequest req); @RequestMapping(value = "/{wsName:.+}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.CREATED) @ResponseBody JSONObj create(@PathVariable String wsName, @RequestBody JSONObj obj, HttpServletRequest req); @RequestMapping(value="/{wsName}/{name:.+}", method = RequestMethod.GET) @ResponseBody JSONObj get(@PathVariable String wsName, @PathVariable String name, HttpServletRequest req); @RequestMapping(value="/{wsName}/{name:.+}", method = RequestMethod.DELETE) @ResponseStatus(value = HttpStatus.NO_CONTENT) void delete(@PathVariable String wsName, @PathVariable String name); @RequestMapping(value="/{wsName}/{name:.+}", method = RequestMethod.PATCH) @ResponseBody JSONObj patch(@PathVariable String wsName, @PathVariable String name, @RequestBody JSONObj obj, HttpServletRequest req); @RequestMapping(value="/{wsName}/{name:.+}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseBody JSONObj put(@PathVariable String wsName, @PathVariable String name, @RequestBody JSONObj obj, HttpServletRequest req); void computeNativeBounds(ResourceInfo r, Catalog catalog); void computeLatLonBounds(ResourceInfo r, Catalog catalog); @RequestMapping(value="/{wsName}/{name}/style", method = RequestMethod.PUT, consumes = YsldHandler.MIMETYPE) @ResponseStatus(value = HttpStatus.OK) void style(@RequestBody byte[] rawStyle, @PathVariable String wsName,
        @PathVariable String name, @RequestParam(value="map", required=false) String mapName); @RequestMapping(value="/{wsName}/{name}/style", method = RequestMethod.GET, produces = YsldHandler.MIMETYPE) @ResponseBody Object style(@PathVariable String wsName, @PathVariable String name); @ExceptionHandler(InvalidYsldException.class) @ResponseBody JSONObj error(InvalidYsldException e, HttpServletResponse response); @RequestMapping(value="/recent", method = RequestMethod.GET) @ResponseBody JSONArr recent(HttpServletRequest req); }
LayerController extends ApiController { @RequestMapping(value="/{wsName:.+}", method = RequestMethod.GET) public @ResponseBody JSONObj list(@PathVariable String wsName, @RequestParam(value="page", required=false) Integer page, @RequestParam(value="count", required=false, defaultValue=""+DEFAULT_PAGESIZE) Integer count, @RequestParam(value="sort", required=false) String sort, @RequestParam(value="filter", required=false) String textFilter, HttpServletRequest req) { JSONObj obj = new JSONObj(); Catalog cat = geoServer.getCatalog(); if ("default".equals(wsName)) { WorkspaceInfo def = cat.getDefaultWorkspace(); if (def != null) { wsName = def.getName(); } } Filter filter = equal("resource.namespace.prefix", wsName); if (textFilter != null) { filter = Predicates.and(filter, Predicates.fullTextSearch(textFilter)); } Integer total = cat.count(LayerInfo.class, filter); SortBy sortBy = parseSort(sort); obj.put("total", total); obj.put("page", page != null ? page : 0); obj.put("count", Math.min(total, count != null ? count : total)); JSONArr arr = obj.putArray("layers"); try ( CloseableIterator<LayerInfo> it = cat.list(LayerInfo.class, filter, offset(page, count), count, sortBy); ) { while (it.hasNext()) { layer(arr.addObject(), it.next(), req); } } return obj; } @Autowired  LayerController(GeoServer geoServer, RecentObjectCache recentCache, Importer importer); @RequestMapping(value="/{wsName:.+}", method = RequestMethod.GET) @ResponseBody JSONObj list(@PathVariable String wsName, 
            @RequestParam(value="page", required=false) Integer page,
            @RequestParam(value="count", required=false, defaultValue=""+DEFAULT_PAGESIZE) Integer count,
            @RequestParam(value="sort", required=false) String sort, 
            @RequestParam(value="filter", required=false) String textFilter, 
            HttpServletRequest req); @RequestMapping(value = "/{wsName:.+}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.CREATED) @ResponseBody JSONObj create(@PathVariable String wsName, @RequestBody JSONObj obj, HttpServletRequest req); @RequestMapping(value="/{wsName}/{name:.+}", method = RequestMethod.GET) @ResponseBody JSONObj get(@PathVariable String wsName, @PathVariable String name, HttpServletRequest req); @RequestMapping(value="/{wsName}/{name:.+}", method = RequestMethod.DELETE) @ResponseStatus(value = HttpStatus.NO_CONTENT) void delete(@PathVariable String wsName, @PathVariable String name); @RequestMapping(value="/{wsName}/{name:.+}", method = RequestMethod.PATCH) @ResponseBody JSONObj patch(@PathVariable String wsName, @PathVariable String name, @RequestBody JSONObj obj, HttpServletRequest req); @RequestMapping(value="/{wsName}/{name:.+}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseBody JSONObj put(@PathVariable String wsName, @PathVariable String name, @RequestBody JSONObj obj, HttpServletRequest req); void computeNativeBounds(ResourceInfo r, Catalog catalog); void computeLatLonBounds(ResourceInfo r, Catalog catalog); @RequestMapping(value="/{wsName}/{name}/style", method = RequestMethod.PUT, consumes = YsldHandler.MIMETYPE) @ResponseStatus(value = HttpStatus.OK) void style(@RequestBody byte[] rawStyle, @PathVariable String wsName,
        @PathVariable String name, @RequestParam(value="map", required=false) String mapName); @RequestMapping(value="/{wsName}/{name}/style", method = RequestMethod.GET, produces = YsldHandler.MIMETYPE) @ResponseBody Object style(@PathVariable String wsName, @PathVariable String name); @ExceptionHandler(InvalidYsldException.class) @ResponseBody JSONObj error(InvalidYsldException e, HttpServletResponse response); @RequestMapping(value="/recent", method = RequestMethod.GET) @ResponseBody JSONArr recent(HttpServletRequest req);  }