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    
018    package org.apache.activemq.transport.ws;
019    
020    import java.io.IOException;
021    import javax.servlet.ServletException;
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.http.HttpServletResponse;
024    
025    import org.apache.activemq.transport.TransportAcceptListener;
026    import org.eclipse.jetty.websocket.WebSocket;
027    import org.eclipse.jetty.websocket.WebSocketServlet;
028    
029    /**
030     * Handle connection upgrade requests and creates web sockets
031     */
032    public class StompServlet extends WebSocketServlet {
033        private static final long serialVersionUID = -4716657876092884139L;
034    
035        private TransportAcceptListener listener;
036    
037        public void init() throws ServletException {
038            super.init();
039            listener = (TransportAcceptListener)getServletContext().getAttribute("acceptListener");
040            if (listener == null) {
041                throw new ServletException("No such attribute 'acceptListener' available in the ServletContext");
042            }
043        }
044    
045        protected void doGet(HttpServletRequest request, HttpServletResponse response)
046            throws ServletException ,IOException  {
047            getServletContext().getNamedDispatcher("default").forward(request,response);
048        }
049    
050        @Override
051        public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
052            StompSocket socket = new StompSocket();
053            listener.onAccept(socket);
054            return socket;
055        }
056    }