1 /******************************************************************************* 2 * Copyright (c) 2011 Intalio, Inc. 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 * 8 * The Eclipse Public License is available at 9 * http://www.eclipse.org/legal/epl-v10.html 10 * 11 * The Apache License v2.0 is available at 12 * http://www.opensource.org/licenses/apache2.0.php 13 * 14 * You may elect to redistribute this code under either of these licenses. 15 *******************************************************************************/ 16 // ======================================================================== 17 // Copyright (c) 2010 Mort Bay Consulting Pty. Ltd. 18 // ------------------------------------------------------------------------ 19 // All rights reserved. This program and the accompanying materials 20 // are made available under the terms of the Eclipse Public License v1.0 21 // and Apache License v2.0 which accompanies this distribution. 22 // The Eclipse Public License is available at 23 // http://www.eclipse.org/legal/epl-v10.html 24 // The Apache License v2.0 is available at 25 // http://www.opensource.org/licenses/apache2.0.php 26 // You may elect to redistribute this code under either of these licenses. 27 // ======================================================================== 28 29 package org.eclipse.jetty.websocket; 30 31 import java.io.IOException; 32 import javax.servlet.ServletException; 33 import javax.servlet.http.HttpServlet; 34 import javax.servlet.http.HttpServletRequest; 35 import javax.servlet.http.HttpServletResponse; 36 37 import org.eclipse.jetty.util.log.Log; 38 import org.eclipse.jetty.util.log.Logger; 39 40 /* ------------------------------------------------------------ */ 41 /** 42 * Servlet to upgrade connections to WebSocket 43 * <p/> 44 * The request must have the correct upgrade headers, else it is 45 * handled as a normal servlet request. 46 * <p/> 47 * The initParameter "bufferSize" can be used to set the buffer size, 48 * which is also the max frame byte size (default 8192). 49 * <p/> 50 * The initParameter "maxIdleTime" can be used to set the time in ms 51 * that a websocket may be idle before closing. 52 * <p/> 53 * The initParameter "maxTextMessagesSize" can be used to set the size in characters 54 * that a websocket may be accept before closing. 55 * <p/> 56 * The initParameter "maxBinaryMessagesSize" can be used to set the size in bytes 57 * that a websocket may be accept before closing. 58 */ 59 @SuppressWarnings("serial") 60 public abstract class WebSocketServlet extends HttpServlet implements WebSocketFactory.Acceptor 61 { 62 private final Logger LOG = Log.getLogger(getClass()); 63 private WebSocketFactory _webSocketFactory; 64 65 /* ------------------------------------------------------------ */ 66 /** 67 * @see javax.servlet.GenericServlet#init() 68 */ 69 @Override 70 public void init() throws ServletException 71 { 72 try 73 { 74 String bs = getInitParameter("bufferSize"); 75 _webSocketFactory = new WebSocketFactory(this, bs == null ? 8192 : Integer.parseInt(bs)); 76 _webSocketFactory.start(); 77 78 String max = getInitParameter("maxIdleTime"); 79 if (max != null) 80 _webSocketFactory.setMaxIdleTime(Integer.parseInt(max)); 81 82 max = getInitParameter("maxTextMessageSize"); 83 if (max != null) 84 _webSocketFactory.setMaxTextMessageSize(Integer.parseInt(max)); 85 86 max = getInitParameter("maxBinaryMessageSize"); 87 if (max != null) 88 _webSocketFactory.setMaxBinaryMessageSize(Integer.parseInt(max)); 89 } 90 catch (ServletException x) 91 { 92 throw x; 93 } 94 catch (Exception x) 95 { 96 throw new ServletException(x); 97 } 98 } 99 100 /* ------------------------------------------------------------ */ 101 /** 102 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 103 */ 104 @Override 105 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 106 { 107 if (_webSocketFactory.acceptWebSocket(request, response) || response.isCommitted()) 108 return; 109 super.service(request, response); 110 } 111 112 /* ------------------------------------------------------------ */ 113 public boolean checkOrigin(HttpServletRequest request, String origin) 114 { 115 return true; 116 } 117 118 /* ------------------------------------------------------------ */ 119 @Override 120 public void destroy() 121 { 122 try 123 { 124 _webSocketFactory.stop(); 125 } 126 catch (Exception x) 127 { 128 LOG.ignore(x); 129 } 130 } 131 }