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.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22 import org.eclipse.jetty.server.Request;
23 import org.eclipse.jetty.server.handler.HandlerWrapper;
24
25 public abstract class WebSocketHandler extends HandlerWrapper implements WebSocketFactory.Acceptor
26 {
27 private WebSocketFactory _webSocketFactory;
28 private int _bufferSize=64*1024;
29 private int _maxIdleTime=-1;
30
31 /* ------------------------------------------------------------ */
32 /** Get the bufferSize.
33 * @return the bufferSize
34 */
35 public int getBufferSize()
36 {
37 return _bufferSize;
38 }
39
40 /* ------------------------------------------------------------ */
41 /** Set the bufferSize.
42 * @param bufferSize the bufferSize to set
43 */
44 public void setBufferSize(int bufferSize)
45 {
46 _bufferSize = bufferSize;
47 }
48
49 /* ------------------------------------------------------------ */
50 /** Get the maxIdleTime.
51 * @return the maxIdleTime
52 */
53 public int getMaxIdleTime()
54 {
55 return (int)(_webSocketFactory==null?_maxIdleTime:_webSocketFactory.getMaxIdleTime());
56 }
57
58 /* ------------------------------------------------------------ */
59 /** Set the maxIdleTime.
60 * @param maxIdleTime the maxIdleTime to set
61 */
62 public void setMaxIdleTime(int maxIdleTime)
63 {
64 _maxIdleTime = maxIdleTime;
65 if (_webSocketFactory!=null)
66 _webSocketFactory.setMaxIdleTime(maxIdleTime);
67 }
68
69 /* ------------------------------------------------------------ */
70 /**
71 * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStart()
72 */
73 @Override
74 protected void doStart() throws Exception
75 {
76 _webSocketFactory=new WebSocketFactory(this,_bufferSize);
77 if (_maxIdleTime>=0)
78 _webSocketFactory.setMaxIdleTime(_maxIdleTime);
79 super.doStart();
80 }
81
82 /* ------------------------------------------------------------ */
83 /**
84 * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStop()
85 */
86 @Override
87 protected void doStop() throws Exception
88 {
89 super.doStop();
90 _webSocketFactory=null;
91 }
92
93 /* ------------------------------------------------------------ */
94 @Override
95 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
96 {
97 if (_webSocketFactory.acceptWebSocket(request,response) || response.isCommitted())
98 return;
99 super.handle(target,baseRequest,request,response);
100 }
101
102 /* ------------------------------------------------------------ */
103 public String checkOrigin(HttpServletRequest request, String host, String origin)
104 {
105 if (origin==null)
106 origin=host;
107 return origin;
108 }
109
110 }