Search code examples
javatomcatwebsocketcatalina

Javax WebSockets won't work with programmattic Tomcat 7 instantiation


Here's what I'm trying to do:

  • Set up a Tomcat 7 server programmatically - that works with a simple servlet. I'm defining Tomcat's endpoints from within the code only, there are no settings nor routers on the file system (this is a constraint).
  • Have the said Tomcat server include a websocket server endpoint using Javax Websockets API (1.1) - that doesn't work.

My app/server entry point:

package com.myapp;

import java.io.File;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.deploy.FilterDef;
import org.apache.catalina.deploy.FilterMap;
import org.apache.catalina.startup.Tomcat;

public class App 
{
    public static void main(String[] args) throws LifecycleException
    {
        int port = 8080;

        Tomcat webServer = new Tomcat();

        webServer.setPort(port);
        webServer.setHostname("localhost");
        String appBase = ".";
        webServer.getHost().setAppBase(appBase);
        File docBase = new File(System.getProperty("java.io.tmpdir"));
        Context context = webServer.addContext("", docBase.getAbsolutePath());

        // both MyServlet and MyFilter exist and work.
        Class servletClass = MyServlet.class;

        Tomcat.addServlet(context, servletClass.getSimpleName(), servletClass.getName());
        context.addServletMapping("/my-servlet/*", servletClass.getSimpleName());

        Class filterClass = MyFilter.class;
        FilterDef myFilterDef = new FilterDef();
        myFilterDef.setFilterClass(filterClass.getName());
        myFilterDef.setFilterName(filterClass.getSimpleName());
        context.addFilterDef(myFilterDef);

        FilterMap myFilterMap = new FilterMap();
        myFilterMap.setFilterName(filterClass.getSimpleName());
        myFilterMap.addURLPattern("/my-servlet/*");
        context.addFilterMap(myFilterMap);

        webServer.start();
        webServer.getServer().await();
    }
}

My websocket server endpoint class:

package com.myapp;

import java.nio.ByteBuffer;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.PongMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/web-socket/")
public class WebSocket
{

    @OnOpen
    public void onOpen()
    {
        System.out.println("Open Connection ...");
    }

     @OnMessage
    public static  void onTextMessage(Session session, String msg) {
        System.out.println("On Message for Web Socket");
    }   
    @OnMessage
    public void onBinaryMessage(Session session, ByteBuffer msg){
        System.out.println("On Message for Web Socket");
    }   
    @OnMessage
    public void onPongMessage(Session session, PongMessage pMsg) {
        System.out.println("On Message for Web Socket");
    }
    @OnClose
    public void onClose(Session session) {
        System.out.println("Connection Close for Web Socket");
    }

    @OnError
    public void onError(Throwable e)
    {
        e.printStackTrace();
    }
}

I'm using Maven, and I'm including the WebSocket API with <scope>provided</scope>, as I've seen in other questions that this was the root of other problems.

When I run the server, I can hit the my-servlet endpoint successfully (as a web page, of course), but when I try to create a WS object, simply within the browser's dev console using var webSocket = new WebSocket("ws://localhost:8080/web-socket"), it says that it can't hit the endpoint:

WebSocket connection to 'ws://localhost:8080/web-socket' failed: Error during WebSocket handshake: Unexpected response code: 404

My question: What am I missing? The error message (from Chrome) suggests that the endpoint isn't registered (hence the 404). How do I register the websocket ServerEndpoint? Thanks!

EDIT:

Here's my implementation of MyServlet class:

package com.myapp;

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

public class MyServlet extends HttpServlet
{

    @Override
    protected void doGet(
            HttpServletRequest req,
            HttpServletResponse resp) throws IOException
    {

        resp.setStatus(HttpServletResponse.SC_OK);
        resp.getWriter().write("Works...");
        resp.getWriter().flush();
        resp.getWriter().close();
    }

}

And here's my implementation of MyFilter:

package com.myapp;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class MyFilter implements Filter
{

    @Override
    public void init(FilterConfig filterConfig)
    {
        // ...
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {

        HttpServletResponse httpResponse = (HttpServletResponse) response;
        httpResponse.addHeader("myHeader", "myHeaderValue");
        chain.doFilter(request, httpResponse);
    }

    @Override
    public void destroy()
    {
        // ...
    }
}

Solution

  • Try this to manually add web socket endpoint:

    
        String serverContainerClass = ServerContainer.class.getName();
        //should be "javax.websocket.server.ServerContainer", if not, some external package could have hogged the implementation
        final ServerContainer serverContainer = (ServerContainer) context.getServletContext().getAttribute(serverContainerClass);
        try 
        {
            serverContainer.addEndpoint(WebSocket.class);
        } 
        catch (DeploymentException e)
        {
            TraceWriter.Error(this, "Failed to initialize websocket", e);
        }
    
    

    You should put this code after you add servlet to context