use of org.eclipse.che.api.core.Page in project che by eclipse.
the class PagingUtil method createLinkHeader.
/**
* Generates link header value from the page object and base uri.
* <a href="https://tools.ietf.org/html/rfc5988">The Link header spec</a>
*
* @param page
* the page used to generate link
* @param uri
* the uri which is used for adding {@code skipCount} & {@code maxItems} query parameters
* @return 'Link' header value
* @throws NullPointerException
* when either {@code page} or {@code uri} is null
*/
public static String createLinkHeader(Page<?> page, URI uri) {
requireNonNull(page, "Required non-null page");
requireNonNull(uri, "Required non-null uri");
final ArrayList<Pair<String, Page.PageRef>> pageRefs = new ArrayList<>(4);
pageRefs.add(Pair.of("first", page.getFirstPageRef()));
pageRefs.add(Pair.of("last", page.getLastPageRef()));
if (page.hasPreviousPage()) {
pageRefs.add(Pair.of("prev", page.getPreviousPageRef()));
}
if (page.hasNextPage()) {
pageRefs.add(Pair.of("next", page.getNextPageRef()));
}
final UriBuilder ub = UriBuilder.fromUri(uri);
return pageRefs.stream().map(refPair -> format("<%s>; rel=\"%s\"", ub.clone().replaceQueryParam("skipCount", refPair.second.getItemsBefore()).replaceQueryParam("maxItems", refPair.second.getPageSize()).build().toString(), refPair.first)).collect(joining(LINK_HEADER_SEPARATOR));
}
use of org.eclipse.che.api.core.Page in project che by eclipse.
the class PagingUtilTest method testCreatingLinksHeader.
@Test
public void testCreatingLinksHeader() throws Exception {
final Page<String> page = new Page<>(asList("item3", "item4", "item5"), 3, 3, 7);
final URI srcUri = URI.create("http://localhost:8080/path?qp=test");
final String linkHeader = createLinkHeader(page, srcUri);
final String[] expLinks = ("<http://localhost:8080/path?qp=test&skipCount=0&maxItems=3>; rel=\"first\", " + "<http://localhost:8080/path?qp=test&skipCount=6&maxItems=3>; rel=\"last\", " + "<http://localhost:8080/path?qp=test&skipCount=0&maxItems=3>; rel=\"prev\", " + "<http://localhost:8080/path?qp=test&skipCount=6&maxItems=3>; rel=\"next\"").split(", ");
assertEqualsNoOrder(linkHeader.split(", "), expLinks);
}
use of org.eclipse.che.api.core.Page in project che by eclipse.
the class UserManagerTest method shouldGetAllUsers.
@Test
public void shouldGetAllUsers() throws Exception {
final Page users = new Page(Arrays.asList(new UserImpl("identifier1", "test1@email.com", "testName1", "password", Collections.singletonList("alias1")), new UserImpl("identifier2", "test2@email.com", "testName2", "password", Collections.singletonList("alias2")), new UserImpl("identifier3", "test3@email.com", "testName3", "password", Collections.singletonList("alias3"))), 0, 30, 3);
when(userDao.getAll(30, 0)).thenReturn(users);
assertEquals(manager.getAll(30, 0), users);
verify(userDao).getAll(30, 0);
}
use of org.eclipse.che.api.core.Page in project che by eclipse.
the class JpaUserDao method getAll.
@Override
@Transactional
public Page<UserImpl> getAll(int maxItems, long skipCount) throws ServerException {
// TODO need to ensure that 'getAll' query works with same data as 'getTotalCount'
checkArgument(maxItems >= 0, "The number of items to return can't be negative.");
checkArgument(skipCount >= 0 && skipCount <= Integer.MAX_VALUE, "The number of items to skip can't be negative or greater than " + Integer.MAX_VALUE);
try {
final List<UserImpl> list = managerProvider.get().createNamedQuery("User.getAll", UserImpl.class).setMaxResults(maxItems).setFirstResult((int) skipCount).getResultList().stream().map(JpaUserDao::erasePassword).collect(toList());
return new Page<>(list, skipCount, maxItems, getTotalCount());
} catch (RuntimeException x) {
throw new ServerException(x.getLocalizedMessage(), x);
}
}
Aggregations