Jetty 9.3.5 ProxyServlet RewriteTarget方法和!asyncSupported

时间:2015-12-04 17:42:10

标签: jetty embedded-jetty jetty-9

我使用Jetty版本9.3.5创建ProxyServlet。

为此,我将从

扩展我的班级
org.eclipse.jetty.proxy.ProxyServlet;

与以前的版本不同,我没有得到覆盖方法受保护的URI rewriteURI(HttpServletRequest请求)。通过grepcode阅读后,我看到类似下面的内容并使用它:

@Override
protected String rewriteTarget(HttpServletRequest request) {
    if (!validateDestination(request.getServerName(), request.getServerPort()))
        return null;

    String roleAlias = (String)request.getSession().getAttribute(ATTR_PROXY_ROLE_ALIAS);        

    String uri =request.getRequestURI();
    String aliasedURI = getAliasedURI(request.getMethod(), uri, roleAlias);

    return aliasedURI ;
}

我不知道这是否是正确的覆盖方法。

在web.xml中我有几个过滤器并定义了这个servlet。对于Servlet,我已经定义如下:

<servlet>
    <servlet-name>Proxy</servlet-name>
    <servlet-class>com.company.MyProxyServlet</servlet-class>
    <async-supported>true</async-supported>
</servlet>

<servlet-mapping>
    <servlet-name>Proxy</servlet-name>
    <url-pattern>/proxy/*</url-pattern>
</servlet-mapping>

但我得到以下异常?任何的想法?我是否需要为过滤器做同样的事情?那怎么样?还有什么遗漏?

java.lang.IllegalStateException: !asyncSupported
            at org.eclipse.jetty.server.Request.startAsync(Request.java:2224)
            at org.eclipse.jetty.proxy.ProxyServlet.service(ProxyServlet.java:80)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
            at com.company.MyProxyServlet.service(MyProxyServlet.java:66)
            at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:821)
            at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1685)
            at com.netiq.sentinel.elasticsearch.proxy.AuditFilter.doFilter(AuditFilter.java:104)
            at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1668)
            at com.netiq.sentinel.elasticsearch.proxy.SecurityFilter.doFilter(SecurityFilter.java:160)
            at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1668)
            at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:581)
            at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
            at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
            at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226)
            at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1158)
            at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511)
            at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
            at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1090)
            at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
            at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)
            at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:109)
            at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119)
            at org.eclipse.jetty.server.Server.handle(Server.java:517)
            at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:306)
            at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242)
            at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:261)
            at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
            at org.eclipse.jetty.io.ssl.SslConnection.onFillable(SslConnection.java:192)
            at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:261)
            at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
            at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75)
            at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
            at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
            at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
            at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)

任何帮助?

注意: - 我发现很少有与此错误相关的其他帖子,但没有使用 rewriteTarget 方法。到处都使用了rewriteURI,我在Jetty 9.3.5中找不到它。

请帮助。

2 个答案:

答案 0 :(得分:1)

我找到了解决方案。

我们还需要为所有过滤器添加<async-supported>true</async-supported>

答案 1 :(得分:0)

对于版本9.3.7的嵌入式jetty,以下是注册AsyncProxyServlet的方法:

    ServletHandler proxyHandler = new ServletHandler();
    AsyncProxyServlet servlet = ...your instance of a proxy...;
    ServletHolder proxyServletHolder = new ServletHolder(servlet);
    proxyServletHolder.setAsyncSupported(true);
    proxyServletHolder.setInitParameter("maxThreads", "100");
    proxyHandler.addServletWithMapping(proxyServletHolder, "/*");

可以将该处理程序添加到HandlerList等。