Changes for checkstyle 8.32
[policy/apex-pdp.git] / core / core-infrastructure / src / main / java / org / onap / policy / apex / core / infrastructure / messaging / stringmessaging / WsStringMessageClient.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.stringmessaging;
23
24 import com.google.common.eventbus.Subscribe;
25 import java.net.URI;
26 import org.onap.policy.apex.core.infrastructure.messaging.MessageListener;
27 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
28 import org.onap.policy.apex.core.infrastructure.messaging.MessagingService;
29 import org.onap.policy.apex.core.infrastructure.messaging.MessagingServiceFactory;
30 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
33
34 /**
35  * This class uses a web socket client to send and receive strings over a web socket.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class WsStringMessageClient implements WsStringMessager {
40     private static final XLogger LOGGER = XLoggerFactory.getXLogger(WsStringMessageClient.class);
41
42     // Repeated string constants
43     private static final String MESSAGE_PREAMBLE = "web socket event consumer client to \"";
44
45     // Message service factory and the message service itself
46     private final MessagingServiceFactory<String> factory = new MessagingServiceFactory<>();
47     private MessagingService<String> service = null;
48
49     // The listener to use for reception of strings
50     private WsStringMessageListener wsStringMessageListener;
51
52     // Address of the server
53     private final String host;
54     private final int port;
55     private String uriString;
56
57     /**
58      * Constructor, define the host and port of the server to connect to.
59      *
60      * @param host the host of the server
61      * @param port the port of the server
62      */
63     public WsStringMessageClient(final String host, final int port) {
64         this.host = host;
65         this.port = port;
66     }
67
68     /**
69      * {@inheritDoc}.
70      */
71     @Override
72     public void start(final WsStringMessageListener newWsStringMessageListener) throws MessagingException {
73         this.wsStringMessageListener = newWsStringMessageListener;
74
75         uriString = "ws://" + host + ":" + port;
76         String messagePreamble = MESSAGE_PREAMBLE + uriString + "\" ";
77         LOGGER.entry(messagePreamble + "starting . . .");
78
79         try {
80             service = factory.createClient(new URI(uriString));
81             service.addMessageListener(new WsStringMessageClientListener());
82             service.startConnection();
83         } catch (final Exception e) {
84             String message = messagePreamble + "start failed";
85             LOGGER.warn(message, e);
86             throw new MessagingException(message, e);
87         }
88
89         LOGGER.exit(messagePreamble + "started");
90     }
91
92     /**
93      * {@inheritDoc}.
94      */
95     @Override
96     public void stop() {
97         LOGGER.entry(MESSAGE_PREAMBLE + uriString + "\" stopping . . .");
98         service.stopConnection();
99         LOGGER.exit(MESSAGE_PREAMBLE + uriString + "\" stopped");
100     }
101
102     /**
103      * {@inheritDoc}.
104      */
105     @Override
106     public void sendString(final String stringMessage) {
107         service.send(stringMessage);
108
109         if (LOGGER.isDebugEnabled()) {
110             String message = "message sent to server: " + stringMessage;
111             LOGGER.debug(message);
112         }
113     }
114
115     /**
116      * The Class WSStringMessageClientListener.
117      */
118     private class WsStringMessageClientListener implements MessageListener<String> {
119         /**
120          * {@inheritDoc}.
121          */
122         @Subscribe
123         @Override
124         public void onMessage(final MessageBlock<String> messageBlock) {
125             throw new UnsupportedOperationException("raw messages are not supported on string message clients");
126         }
127
128         /**
129          * {@inheritDoc}.
130          */
131         @Subscribe
132         @Override
133         public void onMessage(final String messageString) {
134             wsStringMessageListener.receiveString(messageString);
135         }
136     }
137
138     /**
139      * {@inheritDoc}.
140      */
141     @Override
142     public boolean isStarted() {
143         return service.isStarted();
144     }
145 }