daa22d7ffba529ab5acbee1b84d03fcedb62b93c
[policy/apex-pdp.git] / core / core-infrastructure / src / main / java / org / onap / policy / apex / core / infrastructure / messaging / impl / ws / client / WebSocketClientImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.infrastructure.messaging.impl.ws.client;
22
23 import java.net.URI;
24
25 import org.java_websocket.client.WebSocketClient;
26 import org.java_websocket.handshake.ServerHandshake;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
29
30 /**
31  * This class implements {@link WebSocketClient} specific methods in order to act as a Java Web Socket client.
32  *
33  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
34  */
35 abstract class WebSocketClientImpl extends WebSocketClient {
36     // The logger for this class
37     private static final XLogger LOGGER = XLoggerFactory.getXLogger(WebSocketClientImpl.class);
38
39     /**
40      * Constructs a WebSocketClient instance and sets it to the connect to the specified URI. The channel does not
41      * attempt to connect automatically. You must call {@link connect} first to initiate the socket connection.
42      *
43      * @param serverUri the URI of the web socket server to connect to
44      */
45     WebSocketClientImpl(final URI serverUri) {
46         super(serverUri);
47     }
48
49     /**
50      * {@inheritDoc}.
51      */
52     @Override
53     public void onOpen(final ServerHandshake handshakedata) {
54         if (LOGGER.isDebugEnabled()) {
55             LOGGER.debug("Connection opened to server {} --> {}", this.getURI(), handshakedata.getHttpStatusMessage());
56         }
57     }
58
59     /**
60      * {@inheritDoc}.
61      */
62     @Override
63     public void onClose(final int code, final String reason, final boolean remote) {
64         if (LOGGER.isDebugEnabled()) {
65             LOGGER.debug("Connection closed to server {} --> code \"{}\", reason \"{}\"", this.getURI(), code, reason);
66         }
67     }
68
69     /**
70      * {@inheritDoc}.
71      */
72     @Override
73     public void onError(final Exception ex) {
74         LOGGER.info("Failed to make a connection to the server {} ", getURI());
75         LOGGER.catching(ex);
76     }
77 }