Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] SPDYClient within a Servlet

Hi

Have been toying around with SPDYClient to make a simple spdy based call
to a public Google REST API. The code is based upon examples found in
http://wiki.eclipse.org/Jetty/Feature/SPDY and
SSLExternalServerTest.java (spdy-http-server).

The code runs just fine in a main() method, but when trying the same
code wrapped in a simple Servlet, StreamFrameListenere.onData() is never
called:

@WebServlet(urlPatterns = {"/rest"})
public class SPDYBasedRestCallServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private SPDYClient client;

    @Override
    public void init() throws ServletException {

        QueuedThreadPool threadPool = new QueuedThreadPool();
        SslContextFactory sslContextFactory = new SslContextFactory();
        sslContextFactory.setIncludeProtocols("TLSv1");
        sslContextFactory.setEndpointIdentificationAlgorithm("");
        SPDYClient.Factory clientFactory = new
SPDYClient.Factory(threadPool, null, sslContextFactory, 30000);

        try {
            clientFactory.start();
        } catch (Exception e) {
            throw new ServletException(e);
        }
        client = clientFactory.newSPDYClient(SPDY.V3);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws IOException {

        final PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");

        StreamFrameListener streamListener = new
StreamFrameListener.Adapter()
        {
            public void onData(Stream stream, DataInfo dataInfo)
            {
                // Data received from server
                String content = dataInfo.asString("UTF-8", true);
                out.append(content);
            }
        };

        try {
            Session session = client.connect(new
InetSocketAddress("www.googleapis.com", 443), null).get(5,
TimeUnit.SECONDS);

            Fields headers = new Fields();
            headers.put(HTTPSPDYHeader.SCHEME.name(SPDY.V3), "https");
            headers.put(HTTPSPDYHeader.HOST.name(SPDY.V3),
"www.googleapis.com" + ":" + "443");
            headers.put(HTTPSPDYHeader.METHOD.name(SPDY.V3), "GET");
            headers.put(HTTPSPDYHeader.URI.name(SPDY.V3),
"/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS";);
            headers.put(HTTPSPDYHeader.VERSION.name(SPDY.V3), "HTTP/1.1");

            // Start a new session, and configure the stream listener
            session.syn(new SynInfo(headers, true), streamListener);
        } catch (ExecutionException | InterruptedException |
TimeoutException e) {
            e.printStackTrace();
        }
    }
}

The servlet return a page, but a TimeoutException is thrown in the logs,
which I guess is because the StreamFrameListenere.onData() is never called.

java.util.concurrent.TimeoutException
    at org.eclipse.jetty.util.FuturePromise.get(FuturePromise.java:130)
    at
com.test.SPDYBasedRestCallServlet.doGet(SPDYBasedRestCallServlet.java:65)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:735)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
    at
org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:698)
    at
org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:505)
    at
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:138)
    at
org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:564)
    at
org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:213)
    at
org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1096)
    at
org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:432)
    at
org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:175)
    at
org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1030)
    at
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
    at
org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:201)
    at
org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:109)
    at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:445)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:268)
    at
org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:229)
    at
org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)
    at
org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:601)
    at
org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:532)
    at java.lang.Thread.run(Thread.java:724)

While the webapp runs using jetty-maven-plugin:run (9.0.5.v20130815),
both the main method test and webapp shares the same dependencies to:

* org.eclipse.jetty:jetty-server:9.0.5.v20130815
* org.eclipse.jetty.spdy:spdy-core:9.0.5.v20130815
* org.eclipse.jetty.spdy:spdy-client:9.0.5.v20130815
* org.eclipse.jetty.spdy:spdy-http-server:9.0.5.v20130815
* org.eclipse.jetty.npn:npn-api:1.1.0.v20120525

Any help would be appreciated.

Regards,
Espen



Back to the top