/**
 * 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 javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;

@ApplicationScoped
public class SlashTolerationFilter
        implements Filter
{

    private static final String PROTOCOL_HTTP = "http:";
    private static final String PROTOCOL_HTTPS = "https:";

    @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;

        final HttpServletRequestWrapper wrapped = new HttpServletRequestWrapper(hsr) {
            @Override
            public StringBuffer getRequestURL() {
                final StringBuffer originalUrl = ((HttpServletRequest) getRequest()).getRequestURL();
                if ( originalUrl.indexOf( PROTOCOL_HTTP ) != -1 )
                {
                    String temp = originalUrl.toString().substring( PROTOCOL_HTTP.length() + 2 );
                    return new StringBuffer( PROTOCOL_HTTP ).append( "//" ).append( handleSlash( temp ) );
                }
                else if ( originalUrl.indexOf( PROTOCOL_HTTPS ) != -1 )
                {
                    String temp = originalUrl.toString().substring( PROTOCOL_HTTPS.length() + 2 );
                    return new StringBuffer( PROTOCOL_HTTPS ).append( "//" ).append( handleSlash( temp ) );
                }
                else
                {
                    return originalUrl;
                }
            }
            @Override
            public String getRequestURI(){
                return handleSlash( hsr.getRequestURI() );
            }

            private String handleSlash( String url ){
                return url.replaceAll( "/+", "/" );
            }
        };
        filterChain.doFilter(wrapped, servletResponse);
    }

    @Override
    public void destroy()
    {
    }

}

@@ -682,16 +682,74 @@
 jaxrs;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import j
@@ -941,16 +941,73 @@
 ception;
+%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException;
 %0A%0A@Appli
@@ -1099,238 +1099,68 @@
 ate 
-static final String PROTOCOL_HTTP = %22http:%22;%0A    private static final String PROTOCOL_HTTPS = %22https:%22;%0A%0A    @Override%0A    public void init( final FilterConfig filterConfig )%0A                    throws ServletException%0A    %7B%0A    %7D
+final Logger logger = LoggerFactory.getLogger( getClass() );
 %0A%0A  
@@ -1631,239 +1631,167 @@
 l = 
-((HttpServletRequest) getRequest()).getRequestURL();%0A                if ( originalUrl.indexOf( PROTOCOL_HTTP ) != -1 )%0A                %7B%0A                    String temp = originalUrl.toString().substring( PROTOCOL_HTTP.length() + 2
+hsr.getRequestURL();%0A                try%0A                %7B%0A                    return new StringBuffer( new URI( originalUrl.toString()).normalize().toString()
  );%0A
@@ -1810,98 +1810,56 @@
     
+%7D%0A
     
-return new StringBuffer( PROTOCOL_HTTP ).append( %22//%22 ).append( handleSlash( temp )
+            catch ( URISyntaxException e
  )
-;
 %0A   
@@ -1863,33 +1863,33 @@
                 
-%7D
+%7B
 %0A               
@@ -1893,63 +1893,52 @@
     
-else if ( originalUrl.indexOf( PROTOCOL_HTTPS ) != -1 )
+    logger.error(%22URL rewriting error.%22, e);
 %0A   
@@ -1942,33 +1942,33 @@
                 
-%7B
+%7D
 %0A               
@@ -1972,99 +1972,40 @@
     
-    String temp = originalUrl.toString().substring( PROTOCOL_HTTPS.length() + 2 );%0A
+return originalUrl;%0A    
         
     
@@ -1992,32 +1992,34 @@
 rl;%0A            
+%7D%0A
             retu
@@ -2018,113 +2018,60 @@
     
-return new StringBuffer( PROTOCOL_HTTPS ).append( %22//%22 ).append( handleSlash( temp ) );%0A                %7D
+@Override%0A            public String getRequestURI()%7B
 %0A   
@@ -2087,12 +2087,11 @@
     
-else
+try
 %0A   
@@ -2128,35 +2128,77 @@
      return 
-originalUrl
+new URI( hsr.getRequestURI() ).normalize().toString()
 ;%0A          
@@ -2221,252 +2221,181 @@
     
-%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 
+catch ( URISyntaxException e )%0A                %7B%0A                    logger.error(%22URL rewriting error.%22, e);%0A                %7D%0A                return hsr.getRequestURI(
 );%0A 
@@ -2399,32 +2399,33 @@
 ;%0A            %7D%0A
+%0A
         %7D;%0A     
@@ -2486,59 +2486,6 @@
  %7D%0A%0A
-    @Override%0A    public void destroy()%0A    %7B%0A    %7D%0A%0A
 %7D%0A

Normalize the request url