Introduce restconf.server.{api,spi,mdsal}
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / WebSocketInitializer.java
1 /*
2  * Copyright © 2019 FRINX s.r.o. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.nb.rfc8040.streams;
9
10 import java.io.IOException;
11 import java.io.NotSerializableException;
12 import java.io.ObjectInputStream;
13 import java.io.ObjectOutputStream;
14 import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
15 import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
16 import org.opendaylight.restconf.server.spi.RestconfStream;
17
18 /**
19  * Web-socket servlet listening on ws or wss schemas for created data-change-event or notification streams.
20  */
21 final class WebSocketInitializer extends WebSocketServlet {
22     @java.io.Serial
23     private static final long serialVersionUID = 1L;
24
25     private final transient WebSocketFactory creator;
26     private final int idleTimeoutMillis;
27
28     WebSocketInitializer(final RestconfStream.Registry streamRegistry, final PingExecutor pingExecutor,
29             final StreamsConfiguration configuration) {
30         creator = new WebSocketFactory(streamRegistry, pingExecutor,
31             configuration.maximumFragmentLength(), configuration.heartbeatInterval());
32         idleTimeoutMillis = configuration.idleTimeout();
33     }
34
35     /**
36      * Configuration of the web-socket factory - idle timeout and specified factory object.
37      *
38      * @param factory Configurable web-socket factory.
39      */
40     @Override
41     public void configure(final WebSocketServletFactory factory) {
42         factory.getPolicy().setIdleTimeout(idleTimeoutMillis);
43         factory.setCreator(creator);
44     }
45
46     @java.io.Serial
47     @SuppressWarnings("static-method")
48     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
49         throwNSE();
50     }
51
52     @java.io.Serial
53     @SuppressWarnings("static-method")
54     private void writeObject(final ObjectOutputStream out) throws IOException {
55         throwNSE();
56     }
57
58     private static void throwNSE() throws NotSerializableException {
59         throw new NotSerializableException(WebSocketInitializer.class.getName());
60     }
61 }