Tomcat 8和非Atmosphere客户端的气氛+泽西错误

时间:2017-01-20 17:19:04

标签: tomcat websocket tomcat8 atmosphere tyrus

我运行一个使用Atmosphere作为websocket支持框架的聊天服务器。 Atmosphere使用泽西岛,因为我使用大气球衣maven依赖。

聊天服务器在Tomcat 7上运行,我们正在升级到Tomcat 8。

我们的一个客户使用Tyrus作为他们的Java websocket API。

当我们的聊天服务器部署在Tomcat 7上时(使用Java 6,7或8),没有问题。

使用Tomcat 8(Java 8)上的聊天服务器,我们使用Atmosphere的测试客户端工作正常,但使用Tyrus的客户端却没有。我们看到的错误是:

2017-01-17 02:36:51,114 ERROR () [http-nio-8443-exec-11] ReflectorServletProcessor (?) - onRequest()
java.lang.IllegalArgumentException: Schema specific part is opaque
        at com.sun.jersey.api.uri.UriBuilderImpl.checkSsp(UriBuilderImpl.java:529)
        at com.sun.jersey.api.uri.UriBuilderImpl.replacePath(UriBuilderImpl.java:256)
        at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:703)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.atmosphere.util.AtmosphereFilterChain.doFilter(AtmosphereFilterChain.java:135)
        at org.atmosphere.util.AtmosphereFilterChain.invokeFilterChain(AtmosphereFilterChain.java:96)
        at org.atmosphere.handler.ReflectorServletProcessor$FilterChainServletWrapper.service(ReflectorServletProcessor.java:317)
        at org.atmosphere.handler.ReflectorServletProcessor.onRequest(ReflectorServletProcessor.java:160)
        at org.atmosphere.cpr.AsynchronousProcessor.action(AsynchronousProcessor.java:199)
        at org.atmosphere.cpr.AsynchronousProcessor.suspended(AsynchronousProcessor.java:107)
        at org.atmosphere.container.Servlet30CometSupport.service(Servlet30CometSupport.java:66)
        at org.atmosphere.cpr.AtmosphereFramework.doCometSupport(AtmosphereFramework.java:2078)
        at org.atmosphere.websocket.DefaultWebSocketProcessor.dispatch(DefaultWebSocketProcessor.java:571)
        at org.atmosphere.websocket.DefaultWebSocketProcessor$3.run(DefaultWebSocketProcessor.java:333)
        at org.atmosphere.util.VoidExecutorService.execute(VoidExecutorService.java:101)
        at org.atmosphere.websocket.DefaultWebSocketProcessor.dispatch(DefaultWebSocketProcessor.java:328)
        at org.atmosphere.websocket.DefaultWebSocketProcessor.invokeWebSocketProtocol(DefaultWebSocketProcessor.java:525)
        at org.atmosphere.websocket.DefaultWebSocketProcessor.invokeWebSocketProtocol(DefaultWebSocketProcessor.java:428)
        at org.atmosphere.container.JSR356Endpoint$1.onMessage(JSR356Endpoint.java:213)
        at org.atmosphere.container.JSR356Endpoint$1.onMessage(JSR356Endpoint.java:210)
        at org.apache.tomcat.websocket.WsFrameBase.sendMessageText(WsFrameBase.java:393)
        at org.apache.tomcat.websocket.WsFrameBase.processDataText(WsFrameBase.java:494)
        at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:289)
        at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:130)
        at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:60)
        at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onDataAvailable(WsHttpUpgradeHandler.java:183)
        at org.apache.coyote.http11.upgrade.AbstractServletInputStream.onDataAvailable(AbstractServletInputStream.java:198)
        at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(AbstractProcessor.java:96)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:669)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:745)

这是因为使用Tomcat 7,来自websocket请求的请求URL被视为类似于" http:// localhost:8080 / chat /" (请求是对ws:// localhost:8080 / chat /,但它已改为http://某处)。但是对于Tomcat 8,来自websocket请求的请求URL类似于" / chat /"并且架构不存在,Jersey会抛出错误。

为了解决这个问题,Atmosphere寻找标题"起源"当Atmosphere客户端发出请求时,它具有模式。但是当一个Tyrus客户提出请求时,那个"起源" header不包含http模式,导致上面显示的错误。

我想知道是否有人对此的简单解决方案有任何想法或遇到过类似的问题。我已经做了很多寻找一个简单的解决方案,但还没有找到一个。没有一个,我们正在考虑不得不停止使用Atmosphere并使用其他很多代码重构的东西。

这些是关于此问题的故障单/错误的一些链接,似乎已经解决了这个但我有正确的版本但仍有问题。

https://bz.apache.org/bugzilla/show_bug.cgi?id=56573 https://github.com/Atmosphere/atmosphere/issues/1839

大气版本:2.4.8

泽西岛版(通过大气层):1.19

Tyrus版本:1.13

1 个答案:

答案 0 :(得分:0)

解决方法是通过Filter更改“origin”标头,并添加http:// schema(如果它不存在)。这篇文章有助于弄清楚:Modify request parameter with servlet filter

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import javax.servlet.*;

public class FixSchemaFilter implements Filter {

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

    @Override
    public void destroy() {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (request instanceof HttpServletRequest) {
            chain.doFilter(new FixSchemaHttpServletRequest((HttpServletRequest) request), response);
        } else {
            chain.doFilter(request, response);
        }
    }

    public class FixSchemaHttpServletRequest extends HttpServletRequestWrapper {

        public FixSchemaHttpServletRequest(HttpServletRequest request) {
            super(request);
        }

        @Override
        public String getHeader(String header) {
            String value = super.getHeader(header);
            if (header.equalsIgnoreCase("origin") && !value.startsWith("http://")) {
                return "http://" + value;
            }
            return value;
        }

        @Override
        public Enumeration getHeaders(String header) {
            Enumeration enumeration = super.getHeaders(header);
            if (header.equalsIgnoreCase("origin") && super.getHeader(header) != null && !super.getHeader(header).startsWith("http://")) {
                return Collections.enumeration(Collections.singleton(getHeader(header)));
            }
            return enumeration;
        }
    }

}

然后将过滤器添加到web.xml

<filter>
        <filter-name>FixSchemaFilter</filter-name>
        <filter-class>com.example.FixSchemaFilter</filter-class>
</filter>
<filter-mapping>
        <filter-name>FixSchemaFilter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>