TomEE:将请求转发到另一场战争

时间:2019-07-03 18:15:51

标签: java war tomee

我很难让TomEE将我的请求从同一个耳朵文件转发到另一个Web应用程序。

我更新了启用context.xml的crossContext:

<Context antiResourceLocking="false" privileged="true" crossContext="true">
    <!--
      <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="172.17.0.1" />
    -->
    <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>

</Context>

我要转发的web应用是一个简单的Servlet:

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            RequestDispatcher dispatcher;
            dispatcher = getServletContext().getContext("/web2").getRequestDispatcher("/webapp2/hello");
            dispatcher.forward(req, resp);

        } catch (Exception e) {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw, true);
                e.printStackTrace(pw);
                resp.sendError(HttpServletResponse.SC_BAD_REQUEST, sw.toString());
            }
    }

应转发到的Web应用是一个如下所示的Rest资源:

package local.gerb;

import javax.ws.rs.*;

@Path("webapp2")
public class HelloResource {

    @GET
    @Path("hello")
    @Produces("text/plain")
    public String getMessage() {
        return "Rest Never Sleeps";
    }
}

我确实通过卷曲特定端点来验证webapps2是否正常工作。

但是,当我卷曲应转发到webapp2的webapp1时,会得到一个404。奇怪的是,如果我将Rest Resource转换为普通的servlet,转发就可以正常工作。

如果您想查看我将其推送到github的代码: https://github.com/jstralko/tomee-fwd

我有一个自述文件,解释了如何构建和运行此应用程序。它在docker容器中运行,因此所有内容都是独立的,并且超级易于构建和运行。

1 个答案:

答案 0 :(得分:1)

我决定调试TomEE,并在CXFJAXRSFilter.java中找到导致问题的代码:

    @Override
    public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
        if (!HttpServletRequest.class.isInstance(request)) {
            chain.doFilter(request, response);
            return;
        }

        final HttpServletRequest httpServletRequest = HttpServletRequest.class.cast(request);
        final HttpServletResponse httpServletResponse = HttpServletResponse.class.cast(response);

        if (CxfRsHttpListener.TRY_STATIC_RESOURCES) { // else 100% JAXRS
            if (servletMappingIsUnderRestPath(httpServletRequest)) {
                chain.doFilter(request, response);
                return;
            }
            final InputStream staticContent = delegate.findStaticContent(httpServletRequest, welcomeFiles);
            if (staticContent != null) {
                chain.doFilter(request, response);
                return;
            }
        }

        try {
            delegate.doInvoke(
                    new ServletRequestAdapter(httpServletRequest, httpServletResponse, request.getServletContext()),
                    new ServletResponseAdapter(httpServletResponse));
        } catch (final Exception e) {
            throw new ServletException("Error processing webservice request", e);
        }
    }

引起问题的行:

if (CxfRsHttpListener.TRY_STATIC_RESOURCES) { // else 100% JAXRS

所以我找到了TRY_STATIC_RESOURCES的定义

public static final boolean TRY_STATIC_RESOURCES = "true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.jaxrs.static-first", "true"));

所以我将openejb.jaxrs.static-first的system.property更新为false,并且可以正常工作。

$> curl 'http://localhost:8080/web1/webapp1?op=foo'
Hello From Webapp2 Rest Resource%

我为正在玩的人推高了对github存储库的更改。