aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/configuration/XMPPConfiguration.java
blob: 0fc86e2a690d3a4392f1559c89e826c661d3a8c9 (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
package com.juick.www.configuration;

import com.juick.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import rocks.xmpp.core.XmppException;
import rocks.xmpp.core.session.Extension;
import rocks.xmpp.core.session.XmppSessionConfiguration;
import rocks.xmpp.core.session.debug.LogbackDebugger;
import rocks.xmpp.extensions.component.accept.ExternalComponent;

@Configuration
public class XMPPConfiguration {
    private static Logger logger = LoggerFactory.getLogger(XMPPConfiguration.class);
    @Value("${xmpp_host:localhost}")
    private String xmppHost;
    @Value("${xmpp_password:secret}")
    private String xmppPassword;
    @Value("${www_xmpp_jid:www.juick.local}")
    private String xmppJid;
    @Value("${xmpp_port:5347}")
    private int xmppPort;
    @Value("${xmpp_disabled:false}")
    private boolean isXmppDisabled;
    @Bean
    public ExternalComponent xmpp() {
        XmppSessionConfiguration configuration = XmppSessionConfiguration.builder()
                .extensions(Extension.of(Message.class))
                .debugger(LogbackDebugger.class)
                .build();
        ExternalComponent xmpp = ExternalComponent.create(xmppJid, xmppPassword, configuration, xmppHost, xmppPort);
        xmpp.addConnectionListener(e -> logger.error(e.toString(), e.getCause()));
        if (!isXmppDisabled) try {
            Thread.sleep(3000);
            xmpp.connect();
        } catch (XmppException | InterruptedException e) {
            logger.error("xmpp extension", e);
        }
        return xmpp;
    }
}