/**
 * 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()
    {
    }

}

@@ -682,16 +682,74 @@
 jaxrs;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import j
@@ -876,14 +876,124 @@
 java
-.io.IO
+x.servlet.http.HttpServletRequestWrapper;%0Aimport java.io.IOException;%0Aimport java.net.URI;%0Aimport java.net.URISyntax
 Exce
@@ -1082,16 +1082,91 @@
 ilter%0A%7B%0A
+%0A    private final Logger logger = LoggerFactory.getLogger( getClass() );%0A%0A
     @Ove
@@ -1230,28 +1230,8 @@
 ig )
-%0A                   
  thr
@@ -1525,24 +1525,25 @@
 equest;%0A
+%0A
         
 String n
@@ -1538,143 +1538,1036 @@
     
-String newURI = hsr.getRequestURI().replaceAll( %22/+%22, %22/%22 );%0A        servletRequest.getRequestDispatcher(newURI).forward(servletRequest
+final HttpServletRequestWrapper wrapped = new HttpServletRequestWrapper(hsr) %7B%0A            @Override%0A            public StringBuffer getRequestURL() %7B%0A                final StringBuffer originalUrl = hsr.getRequestURL();%0A                try%0A                %7B%0A                    return new StringBuffer( new URI( originalUrl.toString()).normalize().toString() );%0A                %7D%0A                catch ( URISyntaxException e )%0A                %7B%0A                    logger.error(%22URL rewriting error.%22, e);%0A                %7D%0A                return originalUrl;%0A            %7D%0A            @Override%0A            public String getRequestURI()%7B%0A                try%0A                %7B%0A                    return new URI( hsr.getRequestURI() ).normalize().toString();%0A                %7D%0A                catch ( URISyntaxException e )%0A                %7B%0A                    logger.error(%22URL rewriting error.%22, e);%0A                %7D%0A                return hsr.getRequestURI();%0A            %7D%0A%0A        %7D;%0A        filterChain.doFilter(wrapped
 , se

Normalize the request url (#1989)