private void fetchPage(long skip) { try { page = supplier.getPage(size, skip); delegate = page.getItems().iterator(); } catch (Exception x) { throw new RuntimeException(x.getMessage(), x); } } }
private static <E, X extends Exception> List<E> eagerFetch(PageSupplier<E, X> supplier, int size) throws X { Page<? extends E> page = supplier.getPage(size, 0); ArrayList<E> container = new ArrayList<>(page.hasNextPage() ? page.getItemsCount() * 2 : page.getItemsCount()); while (page.hasNextPage()) { container.addAll(page.getItems()); PageRef next = page.getNextPageRef(); page = supplier.getPage(next.getPageSize(), next.getItemsBefore()); } container.addAll(page.getItems()); return container; }
@GET @Produces(APPLICATION_JSON) @ApiOperation( value = "Get free resources limits", response = FreeResourcesLimitDto.class, responseContainer = "List") @ApiResponses({ @ApiResponse(code = 200, message = "The resources limits successfully fetched"), @ApiResponse(code = 500, message = "Internal server error occurred") }) public Response getFreeResourcesLimits( @ApiParam(value = "Max items") @QueryParam("maxItems") @DefaultValue("30") int maxItems, @ApiParam(value = "Skip count") @QueryParam("skipCount") @DefaultValue("0") int skipCount) throws ServerException { final Page<? extends FreeResourcesLimit> limitsPage = freeResourcesLimitManager.getAll(maxItems, skipCount); return Response.ok() .entity(limitsPage.getItems(DtoConverter::asDto)) .header("Link", createLinkHeader(limitsPage)) .build(); }
@GET @Produces(APPLICATION_JSON) @ApiOperation( value = "Get a collection of the available installers", response = Installer.class, responseContainer = "collection") @ApiResponses({ @ApiResponse(code = 200, message = "The response contains collection of available installers"), @ApiResponse(code = 400, message = "Bad Request"), @ApiResponse(code = 500, message = "Internal server error occurred") }) public Response getInstallers( @ApiParam(value = "Max items") @QueryParam("maxItems") @DefaultValue("30") int maxItems, @ApiParam(value = "Skip count") @QueryParam("skipCount") @DefaultValue("0") int skipCount) throws InstallerException, BadRequestException { try { Page<? extends Installer> installers = installerRegistry.getInstallers(maxItems, skipCount); return Response.ok() .entity( installers.getItems().stream().map(DtoConverter::asDto).collect(Collectors.toList())) .header("Link", createLinkHeader(installers)) .header(TOTAL_ITEMS_COUNT_HEADER, installers.getTotalItemsCount()) .build(); } catch (IllegalArgumentException e) { throw new BadRequestException(e.getMessage()); } }
.entity( workspacesPage .getItems() .stream() .filter(ws -> status == null || status.equalsIgnoreCase(ws.getStatus().toString()))
/** * Gets list of workspaces which user can read * * <p>Returned workspaces have either {@link WorkspaceStatus#STOPPED} status or status defined by * their runtime instances(if those exist). * * @param user the id of the user * @param includeRuntimes if <code>true</code>, will fetch runtime info for workspaces. If <code> * false</code>, will not fetch runtime info. * @return the list of workspaces or empty list if user can't read any workspace * @throws NullPointerException when {@code user} is null * @throws ServerException when any server error occurs while getting workspaces with {@link * WorkspaceDao#getWorkspaces(String, int, long)} */ public Page<WorkspaceImpl> getWorkspaces( String user, boolean includeRuntimes, int maxItems, long skipCount) throws ServerException { requireNonNull(user, "Required non-null user id"); final Page<WorkspaceImpl> workspaces = workspaceDao.getWorkspaces(user, maxItems, skipCount); for (WorkspaceImpl workspace : workspaces.getItems()) { normalizeState(workspace, includeRuntimes); } return workspaces; }
/** * Gets list of workspaces which has given namespace * * <p> * * <p>Returned workspaces have either {@link WorkspaceStatus#STOPPED} status or status defined by * their runtime instances(if those exist). * * @param namespace the namespace to find workspaces * @param includeRuntimes if <code>true</code>, will fetch runtime info for workspaces. If <code> * false</code>, will not fetch runtime info. * @return the list of workspaces or empty list if no matches * @throws NullPointerException when {@code namespace} is null * @throws ServerException when any server error occurs while getting workspaces with {@link * WorkspaceDao#getByNamespace(String, int, long)} */ public Page<WorkspaceImpl> getByNamespace( String namespace, boolean includeRuntimes, int maxItems, long skipCount) throws ServerException { requireNonNull(namespace, "Required non-null namespace"); final Page<WorkspaceImpl> workspaces = workspaceDao.getByNamespace(namespace, maxItems, skipCount); for (WorkspaceImpl workspace : workspaces.getItems()) { normalizeState(workspace, includeRuntimes); } return workspaces; }
/** * Determines whether stack is predefined. Note, that 'predefined' means public for all users (not * necessary provided with system from the box). * * @param stackId id of stack to test * @return true if stack is predefined, false otherwise * @throws ServerException when any error occurs during permissions fetching */ @VisibleForTesting boolean isStackPredefined(String stackId) throws ServerException { try { Page<AbstractPermissions> permissionsPage = permissionsManager.getByInstance(DOMAIN_ID, stackId, 25, 0); do { for (AbstractPermissions stackPermission : permissionsPage.getItems()) { if ("*".equals(stackPermission.getUserId())) { return true; } } } while ((permissionsPage = getNextPermissionsPage(stackId, permissionsPage)) != null); } catch (NotFoundException e) { // should never happen throw new ServerException(e); } return false; }
@VisibleForTesting void removeStackPermissions(String stackId, int pageSize) throws ServerException, NotFoundException { Page<StackPermissionsImpl> stacksPage; do { // skip count always equals to 0 because elements will be shifted after removing previous // items stacksPage = dao.getByInstance(stackId, pageSize, 0); for (StackPermissionsImpl stackPermissions : stacksPage.getItems()) { dao.remove(stackPermissions.getUserId(), stackPermissions.getInstanceId()); } } while (stacksPage.hasNextPage()); } }
@VisibleForTesting void removeWorkers(String workspaceId, int pageSize) throws ServerException { Page<WorkerImpl> workersPage; do { // skip count always equals to 0 because elements will be shifted after removing previous // items workersPage = workerDao.getWorkers(workspaceId, pageSize, 0); for (WorkerImpl worker : workersPage.getItems()) { workerDao.removeWorker(worker.getInstanceId(), worker.getUserId()); } } while (workersPage.hasNextPage()); } }