001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.activemq.transport.ws.jetty9;
019
020import java.io.IOException;
021
022import javax.servlet.ServletException;
023import javax.servlet.http.HttpServletRequest;
024import javax.servlet.http.HttpServletResponse;
025
026import org.apache.activemq.transport.TransportAcceptListener;
027import org.eclipse.jetty.websocket.api.WebSocketListener;
028import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
029import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
030import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
031import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
032import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
033
034/**
035 * Handle connection upgrade requests and creates web sockets
036 */
037public class WSServlet extends WebSocketServlet {
038    private static final long serialVersionUID = -4716657876092884139L;
039
040    private TransportAcceptListener listener;
041
042    public void init() throws ServletException {
043        super.init();
044        listener = (TransportAcceptListener)getServletContext().getAttribute("acceptListener");
045        if (listener == null) {
046            throw new ServletException("No such attribute 'acceptListener' available in the ServletContext");
047        }
048    }
049
050    protected void doGet(HttpServletRequest request, HttpServletResponse response)
051        throws ServletException ,IOException  {
052        getServletContext().getNamedDispatcher("default").forward(request,response);
053    }
054
055    
056    public void configure(WebSocketServletFactory factory) {
057        factory.setCreator(new WebSocketCreator() {
058            @Override
059            public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
060                WebSocketListener socket;
061                if (req.getSubProtocols().contains("mqtt")) {
062                    socket = new MQTTSocket();
063                } else {
064                    socket = new StompSocket();
065                }
066                return socket;
067            }
068        });
069        
070    }
071}
072