Gerb
Gerb

Reputation: 955

TomEE: Forwarding request to a different war

I'm having trouble getting TomEE to forward my request to a different webapp from the same ear file.

I updated the context.xml enabled 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>

The webapp that I'm forwarding from is a simple 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());
            }
    }

The webapp that it should be forwarded to is a Rest Resource that looks like this:

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";
    }
}

I did verify that webapps2 is working by curling the particular endpoint.

However, when I curl the webapp1 which should forward to webapp2 I get a 404. What is strange, is if I convert the Rest Resource to a plain servlet the forwarding works fine.

If you want to see the code I have it pushed to github: https://github.com/jstralko/tomee-fwd

I have a ReadMe explaining how to build and run this application. It runs in a docker container so everything is self-contained and super easy to build and run.

Upvotes: 0

Views: 134

Answers (1)

Gerb
Gerb

Reputation: 955

I decided to debug the TomEE and found this code that was causing issues in 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);
        }
    }

The line that was causing issues:

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

So I found the definition of TRY_STATIC_RESOURCES

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

So I updated the system.property of openejb.jaxrs.static-first to false and it worked.

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

I pushed up my changes to my github repo for those of us that are playing along.

Upvotes: 1

Related Questions