Search in sources :

Example 1 with Page

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));
}
Also used : Collections.emptyMap(java.util.Collections.emptyMap) Page(org.eclipse.che.api.core.Page) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) HashMap(java.util.HashMap) Pair(org.eclipse.che.commons.lang.Pair) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) ArrayList(java.util.ArrayList) Matcher(java.util.regex.Matcher) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) Page(org.eclipse.che.api.core.Page) UriBuilder(javax.ws.rs.core.UriBuilder) Pair(org.eclipse.che.commons.lang.Pair)

Example 2 with Page

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);
}
Also used : Page(org.eclipse.che.api.core.Page) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 3 with Page

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);
}
Also used : UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl) Page(org.eclipse.che.api.core.Page) Test(org.testng.annotations.Test)

Example 4 with Page

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);
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl) Page(org.eclipse.che.api.core.Page) Transactional(com.google.inject.persist.Transactional)

Aggregations

Page (org.eclipse.che.api.core.Page)4 URI (java.net.URI)2 UserImpl (org.eclipse.che.api.user.server.model.impl.UserImpl)2 Test (org.testng.annotations.Test)2 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)1 Transactional (com.google.inject.persist.Transactional)1 String.format (java.lang.String.format)1 ArrayList (java.util.ArrayList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors.joining (java.util.stream.Collectors.joining)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 ServerException (org.eclipse.che.api.core.ServerException)1 Pair (org.eclipse.che.commons.lang.Pair)1