LinkHeaderResponseFilter implements ContainerResponseFilter { protected UriBuilder buildBaseUrl(ContainerRequestContext reqContext) { if (config.containsKey(this.apiUrlPrefixKey) && !"".equals(config.getString(this.apiUrlPrefixKey))) { ServletContext servletContext = ResteasyContext.getContextData(ServletContext.class); contextPath = servletContext.getContextPath(); StringBuilder url = new StringBuilder(config.getString(this.apiUrlPrefixKey)); if (url.indexOf(": url = new StringBuilder("https: } String requestUri = reqContext.getUriInfo().getRequestUri().toString(); int resourceStartOffset = requestUri.lastIndexOf(contextPath); int queryParamsStartOffset = requestUri.lastIndexOf("?"); if (resourceStartOffset >= 0) { url.append(requestUri, resourceStartOffset + contextPath.length(), queryParamsStartOffset); } else { log.warn("Could not find servlet context in {}", requestUri); return null; } try { return UriBuilder.fromUri(url.toString()); } catch (IllegalArgumentException e) { log.warn("Couldn't build URI for link header using {}", url, e); return null; } } else { return reqContext.getUriInfo().getAbsolutePathBuilder(); } } @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 testBuildBaseUrlWithConfigPropertyEmpty() throws Exception { when(config.containsKey(eq(this.apiUrlPrefixKey))).thenReturn(true); when(config.getString(eq(this.apiUrlPrefixKey))).thenReturn(""); mockReq = MockHttpRequest.get("https: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); UriBuilder builder = interceptor.buildBaseUrl(mockRequestContext); assertEquals("https: }
@Test public void testBuildBaseUrlWithNoConfigProperty() throws Exception { when(config.containsKey(eq(this.apiUrlPrefixKey))).thenReturn(false); mockReq = MockHttpRequest.get("https: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); UriBuilder builder = interceptor.buildBaseUrl(mockRequestContext); assertEquals("https: }
@Test public void testBuildBaseUrlWithConfigDefault() throws Exception { when(mockServletContext.getContextPath()).thenReturn("/candlepin"); mockReq = MockHttpRequest.get("https: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); LinkHeaderResponseFilter interceptorWithDefault = new LinkHeaderResponseFilter(new ConfigForTesting(), this.apiUrlPrefixKey); UriBuilder builder = interceptorWithDefault.buildBaseUrl(mockRequestContext); assertEquals("https: }
@Test public void testBuildBaseUrlWithNoSchemeProvided() throws Exception { when(mockServletContext.getContextPath()).thenReturn("/candlepin"); when(config.containsKey(eq(this.apiUrlPrefixKey))).thenReturn(true); when(config.getString(eq(this.apiUrlPrefixKey))).thenReturn( "localhost:8443/candlepin"); mockReq = MockHttpRequest.get("https: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); UriBuilder builder = interceptor.buildBaseUrl(mockRequestContext); assertEquals("https: }
@Test public void testBuildBaseUrlWithBadConfigReturnsNull() throws Exception { when(config.containsKey(eq(this.apiUrlPrefixKey))).thenReturn(true); when(config.getString(eq(this.apiUrlPrefixKey))).thenReturn("localhost:8443/subscriptions"); mockReq = MockHttpRequest.get("https: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); when(mockServletContext.getContextPath()).thenReturn("/subscriptions"); interceptor = new LinkHeaderResponseFilter(config, apiUrlPrefixKey); UriBuilder builder = interceptor.buildBaseUrl(mockRequestContext); assertNull(builder); }