/**
 * Copyright (C) 2011-2020 Red Hat, Inc. (https://github.com/Commonjava/indy)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.commonjava.indy.bind.jaxrs;

import javax.enterprise.context.ApplicationScoped;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@ApplicationScoped
public class SlashTolerationFilter
        implements Filter
{
    @Override
    public void init( final FilterConfig filterConfig )
                    throws ServletException
    {
    }

    @Override
    public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain )
            throws IOException, ServletException
    {
        final HttpServletRequest hsr = (HttpServletRequest) servletRequest;
        String newURI = hsr.getRequestURI().replaceAll( "/+", "/" );
        servletRequest.getRequestDispatcher(newURI).forward(servletRequest, servletResponse);
    }

    @Override
    public void destroy()
    {
    }

}

@@ -803,16 +803,69 @@
 equest;%0A
+import javax.servlet.http.HttpServletRequestWrapper;%0A
 import j
@@ -884,16 +884,16 @@
 eption;%0A
-
 %0A@Applic
@@ -967,16 +967,134 @@
 ilter%0A%7B%0A
+%0A    private static final String PROTOCOL_HTTP = %22http:%22;%0A    private static final String PROTOCOL_HTTPS = %22https:%22;%0A%0A
     @Ove
@@ -1473,24 +1473,25 @@
 equest;%0A
+%0A
         
 String n
@@ -1486,27 +1486,243 @@
     
-String newURI = hsr
+final HttpServletRequestWrapper wrapped = new HttpServletRequestWrapper(hsr) %7B%0A            @Override%0A            public StringBuffer getRequestURL() %7B%0A                final StringBuffer originalUrl = ((HttpServletRequest) getRequest())
 .get
@@ -1734,111 +1734,1052 @@
 stUR
-I().replaceAll( %22/+%22,
+L();%0A                if ( originalUrl.indexOf( PROTOCOL_HTTP ) != -1 )%0A                %7B%0A                    String temp = originalUrl.toString().substring( PROTOCOL_HTTP.length() + 2 );%0A                    return new StringBuffer( PROTOCOL_HTTP ).append( %22//%22 ).append( handleSlash( temp ) );%0A                %7D%0A                else if ( originalUrl.indexOf( PROTOCOL_HTTPS ) != -1 )%0A                %7B%0A                    String temp = originalUrl.toString().substring( PROTOCOL_HTTPS.length() + 2 );%0A                    return new StringBuffer( PROTOCOL_HTTPS ).append(
  %22/
+/
 %22 )
-;%0A        servletRequest.getRequestDispatcher(newURI).forward(servletRequest
+.append( handleSlash( temp ) );%0A                %7D%0A                else%0A                %7B%0A                    return originalUrl;%0A                %7D%0A            %7D%0A            @Override%0A            public String getRequestURI()%7B%0A                return handleSlash( hsr.getRequestURI() );%0A            %7D%0A%0A            private String handleSlash( String url )%7B%0A                return url.replaceAll( %22/+%22, %22/%22 );%0A            %7D%0A        %7D;%0A        filterChain.doFilter(wrapped
 , se

Use the filterChain instead of forward