package org.projectodd.wunderboss.web.async.websocket;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;

/**
 * This filter is used to force the JsrWebSocketFilter in
 * Wildfly to find the endpoint registered at the servlet path.
 * Endpoints are looked up for servletPath + pathInfo, but we can't
 * know all the pathInfos we need to register the endpoint under,
 * because they are handled by frameworks in ruby/clojure at the app-level.
 *
 * So, this filter sends a request down the chain that doesn't have a pathInfo
 * if the request is a websocket upgrade request.
 *
 * It also grabs the original ServletRequest in a ThreadLocal for the
 * downstream handshake to use.
 */
public class WebSocketHelpyHelpersonFilter implements Filter {

    public static final ThreadLocal<HttpServletRequest> requestTL = new ThreadLocal<>();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (request instanceof HttpServletRequest &&
                !(request instanceof PathInfoRemovingRequestJacket) && // this filter has already been applied
                "websocket".equalsIgnoreCase(((HttpServletRequest) request).getHeader("Upgrade"))) {
            requestTL.set((HttpServletRequest)request);
            try {
                chain.doFilter(new PathInfoRemovingRequestJacket((HttpServletRequest) request), response);
            } finally {
                requestTL.remove();
            }
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {

    }

    class PathInfoRemovingRequestJacket extends HttpServletRequestWrapper {
        public PathInfoRemovingRequestJacket(HttpServletRequest request) {
            super(request);
        }

        @Override
        public String getPathInfo() {
            return null;
        }
    }
}

@@ -1,8 +1,636 @@
+/*%0A * Copyright 2014-2015 Red Hat, Inc, and individual contributors.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 

Add missing license header.