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