Tomcat 8和非Atmosphere客户端出现Atmosphere+Jersey错误



我运行了一个聊天服务器,该服务器使用Atmosphere作为websocket支持的框架。Atmosphere使用Jersey是因为我使用了Atmosphere-Jersey maven依赖项。

聊天服务器在Tomcat7上运行,我们正在升级到Tomcat8。

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

当我们的聊天服务器部署在Tomcat7(使用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)

发生这种情况的原因是,对于Tomcat7,来自websocket请求的请求URL被视为类似于"http://localhost:8080/chat/"(请求被发送到ws://localhost:8080/chat/,但它被更改为http://somewhere)。但是对于Tomcat8,来自websocket请求的请求URL类似于"/chat/",并且不存在模式,Jersey抛出错误。

为了解决这个问题,Atmosphere会在Atmospheres客户端发出请求时查找具有模式的标头"origin"。但是,当Tyrus客户端发出请求时,"origin"标头不包括http模式,从而导致上面显示的错误。

我想知道是否有人对此有任何简单的解决方案,或者遇到过类似的问题。我已经搜索了很多简单的解决方案,但还没有找到。如果没有它,我们将不得不停止使用Atmosphere,而使用其他的东西,这是大量的代码重构。

这些是关于这个问题的票证/错误的一些链接,似乎已经解决了这个问题,但我有正确的版本,仍然存在问题。

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

大气版本:2.4.8

泽西版(通过大气):1.19

Tyrus版本:1.13

解决方法是通过Filter更改"origin"标头,如果它不存在,则添加http://schema。本文帮助解决了这个问题:使用servlet过滤器修改请求参数

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>

相关内容

  • 没有找到相关文章

最新更新