Skip to main content
summaryrefslogtreecommitdiffstats
blob: 32813f5b68d9bb11e232588ed6299b3af20a76d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
//  ========================================================================
//  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.websocket;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

/* ------------------------------------------------------------ */
/**
 * Servlet to upgrade connections to WebSocket
 * <p/>
 * The request must have the correct upgrade headers, else it is
 * handled as a normal servlet request.
 * <p/>
 * The initParameter "bufferSize" can be used to set the buffer size,
 * which is also the max frame byte size (default 8192).
 * <p/>
 * The initParameter "maxIdleTime" can be used to set the time in ms
 * that a websocket may be idle before closing.
 * <p/>
 * The initParameter "maxTextMessagesSize" can be used to set the size in characters
 * that a websocket may be accept before closing.
 * <p/>
 * The initParameter "maxBinaryMessagesSize" can be used to set the size in bytes
 * that a websocket may be accept before closing.
 * <p/>
 * The initParameter "minVersion" can be used to set the minimum protocol version
 * accepted. Default is the RFC6455 version (13)
 */
@SuppressWarnings("serial")
public abstract class WebSocketServlet extends HttpServlet implements WebSocketFactory.Acceptor
{
    private final Logger LOG = Log.getLogger(getClass());
    private WebSocketFactory _webSocketFactory;

    /* ------------------------------------------------------------ */
    /**
     * @see javax.servlet.GenericServlet#init()
     */
    @Override
    public void init() throws ServletException
    {
        try
        {
            String bs = getInitParameter("bufferSize");
            _webSocketFactory = new WebSocketFactory(this, bs == null ? 8192 : Integer.parseInt(bs));
            _webSocketFactory.start();

            String max = getInitParameter("maxIdleTime");
            if (max != null)
                _webSocketFactory.setMaxIdleTime(Integer.parseInt(max));

            max = getInitParameter("maxTextMessageSize");
            if (max != null)
                _webSocketFactory.setMaxTextMessageSize(Integer.parseInt(max));

            max = getInitParameter("maxBinaryMessageSize");
            if (max != null)
                _webSocketFactory.setMaxBinaryMessageSize(Integer.parseInt(max));
            
            String min = getInitParameter("minVersion");
            if (min != null)
                _webSocketFactory.setMinVersion(Integer.parseInt(min));
        }
        catch (ServletException x)
        {
            throw x;
        }
        catch (Exception x)
        {
            throw new ServletException(x);
        }
    }

    /* ------------------------------------------------------------ */
    /**
     * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        if (_webSocketFactory.acceptWebSocket(request, response) || response.isCommitted())
            return;
        super.service(request, response);
    }

    /* ------------------------------------------------------------ */
    public boolean checkOrigin(HttpServletRequest request, String origin)
    {
        return true;
    }

    /* ------------------------------------------------------------ */
    @Override
    public void destroy()
    {
        try
        {
            _webSocketFactory.stop();
        }
        catch (Exception x)
        {
            LOG.ignore(x);
        }
    }
}

Back to the top