LinkHeaderResponseFilter implements ContainerResponseFilter { @SuppressWarnings("rawtypes") public void filter(ContainerRequestContext reqContext, ContainerResponseContext respContext) { Page page = ResteasyContext.getContextData(Page.class); if (page == null) { return; } if (page.getPageRequest() == null || !page.getPageRequest().isPaging()) { return; } UriBuilder builder = buildBaseUrl(reqContext); if (builder == null) { return; } MultivaluedMap<String, String> params = null; params = reqContext.getUriInfo().getQueryParameters(); builder = addUnchangingQueryParams(builder, params); try { LinkHeader header = new LinkHeader(); Integer next = getNextPage(page); if (next != null) { header.addLink("next", "next", buildPageLink(builder, next), LINK_TYPE); } Integer prev = getPrevPage(page); if (prev != null) { header.addLink("prev", "prev", buildPageLink(builder, prev), LINK_TYPE); } header.addLink("first", "first", buildPageLink(builder, 1), LINK_TYPE); header.addLink("last", "last", buildPageLink(builder, getLastPage(page)), LINK_TYPE); respContext.getHeaders().add(LINK_HEADER, header.toString()); } catch (LinkTooLongException e) { log.warn("Link length exceeded maximum length ({}). " + "Link headers will be omitted from this response.", MAX_LINK_LENGTH, e); } respContext.getHeaders().add(TOTAL_RECORDS_COUNT, page.getMaxRecords()); } @Inject  LinkHeaderResponseFilter(Configuration config,
        @Named("PREFIX_APIURL_KEY") String apiUrlPrefixKey); @SuppressWarnings("rawtypes") void filter(ContainerRequestContext reqContext, ContainerResponseContext respContext); static final int MAX_LINK_LENGTH; static final String LINK_HEADER; static final String TOTAL_RECORDS_COUNT; static final String LINK_TYPE; }
@Test public void testPostProcessWithNullPage() { interceptor.filter(mockRequestContext, mockResponseContext); }
@Test public void testPostProcessWithNullPageRequest() { ResteasyContext.pushContext(Page.class, page); when(page.getPageRequest()).thenReturn(null); interceptor.filter(mockRequestContext, mockResponseContext); verify(page).getPageRequest(); }
@Test public void testPostProcessWithNonPagingPresentation() { when(page.getPageRequest()).thenReturn(pageRequest); when(pageRequest.isPaging()).thenReturn(false); ResteasyContext.pushContext(Page.class, page); interceptor.filter(mockRequestContext, mockResponseContext); verify(page, times(2)).getPageRequest(); verify(pageRequest).isPaging(); }
@Test public void testPostProcessWithPaging() throws Exception { when(page.getPageRequest()).thenReturn(pageRequest); when(page.getMaxRecords()).thenReturn(15); when(pageRequest.isPaging()).thenReturn(true); when(pageRequest.getPage()).thenReturn(2); when(pageRequest.getPerPage()).thenReturn(5); when(config.containsKey(eq(this.apiUrlPrefixKey))).thenReturn(false); MultivaluedMap<String, Object> map = new MultivaluedMapImpl<>(); ResteasyContext.pushContext(Page.class, page); mockReq = MockHttpRequest.create("GET", new URI("/candlepin/resource?order=asc&page=1&per_page=10"), new URI("https: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); when(mockResponseContext.getHeaders()).thenReturn(map); interceptor.filter(mockRequestContext, mockResponseContext); String header = (String) map.getFirst(LinkHeaderResponseFilter.LINK_HEADER); assertTrue(header.contains("rel=\"first\"")); assertTrue(header.contains("rel=\"last\"")); assertTrue(header.contains("rel=\"next\"")); assertTrue(header.contains("rel=\"prev\"")); assertEquals(4, StringUtils.countMatches(header, "&page=")); }
@Test public void testPostProcessWithPagingAndCustomURIBuilding() throws Exception { when(page.getPageRequest()).thenReturn(pageRequest); when(page.getMaxRecords()).thenReturn(15); when(pageRequest.isPaging()).thenReturn(true); when(pageRequest.getPage()).thenReturn(2); when(pageRequest.getPerPage()).thenReturn(5); when(config.containsKey(eq(this.apiUrlPrefixKey))).thenReturn(true); when(config.getString(eq(this.apiUrlPrefixKey))).thenReturn("localhost:8443/candlepin"); when(mockServletContext.getContextPath()).thenReturn("/candlepin"); MultivaluedMap<String, Object> map = new MultivaluedMapImpl<>(); ResteasyContext.pushContext(Page.class, page); mockReq = MockHttpRequest.create("GET", new URI("/candlepin/resource?order=asc&page=2&per_page=10"), new URI("https: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); when(mockResponseContext.getHeaders()).thenReturn(map); interceptor.filter(mockRequestContext, mockResponseContext); String header = (String) map.getFirst(LinkHeaderResponseFilter.LINK_HEADER); assertTrue(header.contains("rel=\"first\"")); assertTrue(header.contains("rel=\"last\"")); assertTrue(header.contains("rel=\"next\"")); assertTrue(header.contains("rel=\"prev\"")); assertEquals(4, StringUtils.countMatches(header, "&page=")); }