1 // ======================================================================== 2 // Copyright (c) 2010 Mort Bay Consulting Pty. Ltd. 3 // ------------------------------------------------------------------------ 4 // All rights reserved. This program and the accompanying materials 5 // are made available under the terms of the Eclipse Public License v1.0 6 // and Apache License v2.0 which accompanies this distribution. 7 // The Eclipse Public License is available at 8 // http://www.eclipse.org/legal/epl-v10.html 9 // The Apache License v2.0 is available at 10 // http://www.opensource.org/licenses/apache2.0.php 11 // You may elect to redistribute this code under either of these licenses. 12 // ======================================================================== 13 14 package org.eclipse.jetty.websocket; 15 16 import java.io.IOException; 17 18 import javax.servlet.ServletException; 19 import javax.servlet.http.HttpServlet; 20 import javax.servlet.http.HttpServletRequest; 21 import javax.servlet.http.HttpServletResponse; 22 23 24 /* ------------------------------------------------------------ */ 25 /** 26 * Servlet to upgrade connections to WebSocket 27 * <p> 28 * The request must have the correct upgrade headers, else it is 29 * handled as a normal servlet request. 30 * <p> 31 * The initParameter "bufferSize" can be used to set the buffer size, 32 * which is also the max frame byte size (default 8192). 33 * <p> 34 * The initParameter "maxIdleTime" can be used to set the time in ms 35 * that a websocket may be idle before closing. 36 * <p> 37 * The initParameter "maxTextMessagesSize" can be used to set the size in characters 38 * that a websocket may be accept before closing. 39 * <p> 40 * The initParameter "maxBinaryMessagesSize" can be used to set the size in bytes 41 * that a websocket may be accept before closing. 42 * 43 */ 44 public abstract class WebSocketServlet extends HttpServlet implements WebSocketFactory.Acceptor 45 { 46 WebSocketFactory _webSocketFactory; 47 48 /* ------------------------------------------------------------ */ 49 /** 50 * @see javax.servlet.GenericServlet#init() 51 */ 52 @Override 53 public void init() throws ServletException 54 { 55 String bs=getInitParameter("bufferSize"); 56 _webSocketFactory = new WebSocketFactory(this,bs==null?8192:Integer.parseInt(bs)); 57 String max=getInitParameter("maxIdleTime"); 58 if (max!=null) 59 _webSocketFactory.setMaxIdleTime(Integer.parseInt(max)); 60 61 max=getInitParameter("maxTextMessageSize"); 62 if (max!=null) 63 _webSocketFactory.setMaxTextMessageSize(Integer.parseInt(max)); 64 65 max=getInitParameter("maxBinaryMessageSize"); 66 if (max!=null) 67 _webSocketFactory.setMaxBinaryMessageSize(Integer.parseInt(max)); 68 69 } 70 71 /* ------------------------------------------------------------ */ 72 /** 73 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 74 */ 75 @Override 76 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 77 { 78 if (_webSocketFactory.acceptWebSocket(request,response) || response.isCommitted()) 79 return; 80 super.service(request,response); 81 } 82 83 /* ------------------------------------------------------------ */ 84 public boolean checkOrigin(HttpServletRequest request, String origin) 85 { 86 return true; 87 } 88 89 90 91 }