@Test @Issue("issues/272") public void testGetBestRelativeURIForPathMappingWithoutTrailingSlash() { HttpServletRequest req = mock(HttpServletRequest.class); when(req.getRequestURI()).thenReturn("/application/bar"); when(req.getContextPath()).thenReturn("/application"); when(req.getServletPath()).thenReturn("/bar"); when(req.getPathInfo()).thenReturn(null); ServletContext sc = mock(ServletContext.class); when(req.getServletContext()).thenReturn(sc); Map<String,ServletRegistration> servletRegistrations = new HashMap<>(); when(sc.getServletRegistrations()).thenAnswer(inv -> servletRegistrations); ServletRegistration sr = mock(ServletRegistration.class); when(sr.getMappings()).thenReturn(Collections.singletonList("/bar/*")); servletRegistrations.put("ServletName", sr); assertEquals("", SniffyRequestProcessor.getBestRelativeURI(req)); }
public static String getBestRelativeURI(HttpServletRequest httpServletRequest) { String requestURI = httpServletRequest.getRequestURI(); if (null == requestURI) return null; int bestBaseURILength = httpServletRequest.getContextPath().length(); String servletPath = httpServletRequest.getServletPath(); if (null != servletPath) { String pathInfo = httpServletRequest.getPathInfo(); if (null != pathInfo && !pathInfo.isEmpty()) { bestBaseURILength += servletPath.length(); if (servletPath.endsWith("/")) bestBaseURILength--; } else { ServletContext servletContext = httpServletRequest.getServletContext(); for (String mapping : ServletRegistrationUtil.getServletMappings(servletContext)) { if (mapping.equals(servletPath) || (mapping.endsWith("/*") && mapping.substring(0, mapping.length() - 2).equals(servletPath))) { bestBaseURILength += servletPath.length(); if (servletPath.endsWith("/")) bestBaseURILength--; break; } } } } return requestURI.substring(bestBaseURILength); }
SniffyRequestProcessor implements BufferedServletResponseListener { public static String getBestRelativeURI(HttpServletRequest httpServletRequest) { String requestURI = httpServletRequest.getRequestURI(); if (null == requestURI) return null; int bestBaseURILength = httpServletRequest.getContextPath().length(); String servletPath = httpServletRequest.getServletPath(); if (null != servletPath) { String pathInfo = httpServletRequest.getPathInfo(); if (null != pathInfo && !pathInfo.isEmpty()) { bestBaseURILength += servletPath.length(); if (servletPath.endsWith("/")) bestBaseURILength--; } else { ServletContext servletContext = httpServletRequest.getServletContext(); for (String mapping : ServletRegistrationUtil.getServletMappings(servletContext)) { if (mapping.equals(servletPath) || (mapping.endsWith("/*") && mapping.substring(0, mapping.length() - 2).equals(servletPath))) { bestBaseURILength += servletPath.length(); if (servletPath.endsWith("/")) bestBaseURILength--; break; } } } } return requestURI.substring(bestBaseURILength); } }
SniffyRequestProcessor implements BufferedServletResponseListener { public static String getBestRelativeURI(HttpServletRequest httpServletRequest) { String requestURI = httpServletRequest.getRequestURI(); if (null == requestURI) return null; int bestBaseURILength = httpServletRequest.getContextPath().length(); String servletPath = httpServletRequest.getServletPath(); if (null != servletPath) { String pathInfo = httpServletRequest.getPathInfo(); if (null != pathInfo && !pathInfo.isEmpty()) { bestBaseURILength += servletPath.length(); if (servletPath.endsWith("/")) bestBaseURILength--; } else { ServletContext servletContext = httpServletRequest.getServletContext(); for (String mapping : ServletRegistrationUtil.getServletMappings(servletContext)) { if (mapping.equals(servletPath) || (mapping.endsWith("/*") && mapping.substring(0, mapping.length() - 2).equals(servletPath))) { bestBaseURILength += servletPath.length(); if (servletPath.endsWith("/")) bestBaseURILength--; break; } } } } return requestURI.substring(bestBaseURILength); } SniffyRequestProcessor(
            SniffyFilter sniffyFilter,
            HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse); }
SniffyRequestProcessor implements BufferedServletResponseListener { public static String getBestRelativeURI(HttpServletRequest httpServletRequest) { String requestURI = httpServletRequest.getRequestURI(); if (null == requestURI) return null; int bestBaseURILength = httpServletRequest.getContextPath().length(); String servletPath = httpServletRequest.getServletPath(); if (null != servletPath) { String pathInfo = httpServletRequest.getPathInfo(); if (null != pathInfo && !pathInfo.isEmpty()) { bestBaseURILength += servletPath.length(); if (servletPath.endsWith("/")) bestBaseURILength--; } else { ServletContext servletContext = httpServletRequest.getServletContext(); for (String mapping : ServletRegistrationUtil.getServletMappings(servletContext)) { if (mapping.equals(servletPath) || (mapping.endsWith("/*") && mapping.substring(0, mapping.length() - 2).equals(servletPath))) { bestBaseURILength += servletPath.length(); if (servletPath.endsWith("/")) bestBaseURILength--; break; } } } } return requestURI.substring(bestBaseURILength); } SniffyRequestProcessor(
            SniffyFilter sniffyFilter,
            HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse); void initStartMillis(); long getTimeToFirstByte(); long getElapsedTime(); static String getBestRelativeURI(HttpServletRequest httpServletRequest); void process(FilterChain chain); @Override void onBeforeCommit(BufferedServletResponseWrapper wrapper, Buffer buffer); @Override void beforeClose(BufferedServletResponseWrapper wrapper, Buffer buffer); }
SniffyRequestProcessor implements BufferedServletResponseListener { public static String getBestRelativeURI(HttpServletRequest httpServletRequest) { String requestURI = httpServletRequest.getRequestURI(); if (null == requestURI) return null; int bestBaseURILength = httpServletRequest.getContextPath().length(); String servletPath = httpServletRequest.getServletPath(); if (null != servletPath) { String pathInfo = httpServletRequest.getPathInfo(); if (null != pathInfo && !pathInfo.isEmpty()) { bestBaseURILength += servletPath.length(); if (servletPath.endsWith("/")) bestBaseURILength--; } else { ServletContext servletContext = httpServletRequest.getServletContext(); for (String mapping : ServletRegistrationUtil.getServletMappings(servletContext)) { if (mapping.equals(servletPath) || (mapping.endsWith("/*") && mapping.substring(0, mapping.length() - 2).equals(servletPath))) { bestBaseURILength += servletPath.length(); if (servletPath.endsWith("/")) bestBaseURILength--; break; } } } } return requestURI.substring(bestBaseURILength); } SniffyRequestProcessor(
            SniffyFilter sniffyFilter,
            HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse); void initStartMillis(); long getTimeToFirstByte(); long getElapsedTime(); static String getBestRelativeURI(HttpServletRequest httpServletRequest); void process(FilterChain chain); @Override void onBeforeCommit(BufferedServletResponseWrapper wrapper, Buffer buffer); @Override void beforeClose(BufferedServletResponseWrapper wrapper, Buffer buffer); final static String SNIFFY_REQUEST_PROCESSOR_REQUEST_ATTRIBUTE_NAME; final static String SNIFFY_REQUEST_ID_REQUEST_ATTRIBUTE_NAME; }