Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Cannot get org.eclipse.jetty.server.Request instance

On Wed, Sep 11, 2019 at 6:57 PM Joakim Erdfelt <joakim@xxxxxxxxxxx> wrote:
Can you change your implementation a bit so that we can understand what's going on?

(the below code can be found at https://gist.github.com/joakime/ca3bf453cd946e47a8a09181ef3a0dd7 as well, and might be slightly more up to date)

package jetty.http2;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestWrapper;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.PushBuilder;
import org.eclipse.jetty.server.Request;

public class Http2PushBuilderServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        dumpRequestDetail(request);
        Request req = Request.getBaseRequest(request);
        if (req != null)
        {
            System.out.printf("req: (%s) %s%n", req.getClass().getName(), req);
            System.out.printf("req.isPushSupported() = %b%n", req.isPushSupported());
            PushBuilder pushBuilder = req.getPushBuilder();
            pushBuilder.path("/res").push();
        }
    }

    private void dumpRequestDetail(HttpServletRequest request)
    {
        System.out.printf("HttpServletRequest is implemented by %s%n", request.getClass().getName());
        System.out.printf("HttpServletRequest is instanceof Request = %b%n", request instanceof Request);

        // some relevant virtual attributes
        dumpAttribute(request, "org.eclipse.jetty.server.Server");
        dumpAttribute(request, "org.eclipse.jetty.server.HttpChannel");
        dumpAttribute(request, "org.eclipse.jetty.server.HttpConnection");

        // the state of the wrapping of the request
        if (request instanceof ServletRequestWrapper)
        {
            System.out.printf("  + is a HttpServletRequestWrapper%n");

            ServletRequest servletRequest = request;

            while (servletRequest instanceof ServletRequestWrapper)
            {
                servletRequest = ((ServletRequestWrapper)servletRequest).getRequest();
                System.out.printf("  + Wrapped by %s%n", servletRequest.getClass().getName());
            }
            System.out.printf("Unwrapped ServletRequest is instanceof Request = %b%n", servletRequest instanceof Request);
        }
    }

    private void dumpAttribute(HttpServletRequest request, String attrName)
    {
        System.out.printf(".getAttribute(%s) = ", attrName);
        Object value = request.getAttribute(attrName);
        if (value == null)
        {
            System.out.println("<null>");
        }
        else
        {
            System.out.printf("(%s) %s%n", value.getClass().getName(), value);
        }
    }
}
The req is null, and the server output is the below,
HttpServletRequest is implemented by org.eclipse.jetty.server.Request
HttpServletRequest is instanceof Request = false
.getAttribute(org.eclipse.jetty.server.Server) = (org.eclipse.jetty.server.Server) Server@16eb3ea3{STARTED}[9.4.20.v20190813]
.getAttribute(org.eclipse.jetty.server.HttpChannel) = (org.eclipse.jetty.server.HttpChannelOverHttp) HttpChannelOverHttp@40ed3e95{r=1,c=false,c=false/false,a=DISPATCHED,uri=//localhost:9020/test,age=22}
.getAttribute(org.eclipse.jetty.server.HttpConnection) = (org.eclipse.jetty.server.HttpConnection) HttpConnection@48b3d482::SocketChannelEndPoint@6e894f22{/0:0:0:0:0:0:0:1:63409<->/0:0:0:0:0:0:0:1:9020,OPEN,fill=-,flush=-,to=31/30000}{io=0/0,kio=0,kro=1}->HttpConnection@48b3d482[p=HttpParser{s=END,0 of -1},g=HttpGenerator@4f7a01ad{s=START}]=>HttpChannelOverHttp@40ed3e95{r=1,c=false,c=false/false,a=DISPATCHED,uri=//localhost:9020/test,age=25}

Back to the top