fed18994d9e92d23729b5c6b810d2cc6d969a203
[policy/apex-pdp.git] / core / core-infrastructure / src / main / java / org / onap / policy / apex / core / infrastructure / messaging / impl / ws / server / WebSocketServerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.infrastructure.messaging.impl.ws.server;
23
24 import java.net.InetSocketAddress;
25
26 import org.java_websocket.WebSocket;
27 import org.java_websocket.handshake.ClientHandshake;
28 import org.java_websocket.server.WebSocketServer;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
31
32 /**
33  * This class is the web socket server specific implementation for Apex.
34  *
35  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
36  */
37 abstract class WebSocketServerImpl extends WebSocketServer {
38     // The logger for this class
39     private static final XLogger LOGGER = XLoggerFactory.getXLogger(MessageServerImpl.class);
40
41     /**
42      * Constructor of this class.
43      *
44      * @param address host address of the local machine.
45      */
46     protected WebSocketServerImpl(final InetSocketAddress address) {
47         super(address);
48         LOGGER.entry(address.getAddress().getHostAddress() + ":" + address.getPort());
49         LOGGER.exit();
50     }
51
52     /**
53      * {@inheritDoc}.
54      */
55     @Override
56     public void onOpen(final WebSocket conn, final ClientHandshake handshake) {
57         LOGGER.entry(conn, handshake);
58         LOGGER.debug("A client connection opened from machine {}.",
59                 conn.getRemoteSocketAddress().getAddress().getHostAddress());
60         LOGGER.exit();
61     }
62
63     /**
64      * {@inheritDoc}.
65      */
66     @Override
67     public void onClose(final WebSocket conn, final int code, final String reason, final boolean remote) {
68         LOGGER.entry(conn, code, remote);
69         LOGGER.debug("A client  connection from machine {} closing with code {}.", super.getAddress(), code);
70         LOGGER.exit();
71     }
72
73     /**
74      * {@inheritDoc}.
75      */
76     @Override
77     public void onError(final WebSocket conn, final Exception ex) {
78         // some errors like port binding failed may not be assignable to a specific web socket
79         LOGGER.error("server error occurred", ex);
80     }
81 }