diff options
author | 2018-03-21 16:33:12 +0300 | |
---|---|---|
committer | 2018-03-21 16:33:12 +0300 | |
commit | 01818b95286bc2bf96a4adad42c2194e487f373a (patch) | |
tree | 8d61ec1527dfd74f6cac6d560c44b20eb01d2a36 | |
parent | 77c7232a3ca354e009dfdd5a5eb0d390d7e627f5 (diff) |
drop xmpp-wip
-rw-r--r-- | juick-xmpp-wip/build.gradle | 10 | ||||
-rw-r--r-- | juick-xmpp-wip/src/main/java/com/juick/components/XMPPBot.java | 113 | ||||
-rw-r--r-- | juick-xmpp-wip/src/main/java/com/juick/components/XMPPRouter.java | 198 | ||||
-rw-r--r-- | juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotAppConfiguration.java | 41 | ||||
-rw-r--r-- | juick-xmpp-wip/src/main/java/com/juick/components/controllers/StatusController.java | 45 | ||||
-rw-r--r-- | juick-xmpp-wip/src/main/java/com/juick/components/controllers/helpers/RouterStatus.java | 17 | ||||
-rw-r--r-- | juick-xmpp-wip/src/main/resources/vCard.png | bin | 5558 -> 0 bytes | |||
-rw-r--r-- | juick-xmpp-wip/src/test/java/com/juick/xmpp/XMPPTests.java | 98 | ||||
-rw-r--r-- | settings.gradle | 2 |
9 files changed, 1 insertions, 523 deletions
diff --git a/juick-xmpp-wip/build.gradle b/juick-xmpp-wip/build.gradle deleted file mode 100644 index aab1b995..00000000 --- a/juick-xmpp-wip/build.gradle +++ /dev/null @@ -1,10 +0,0 @@ -apply plugin: 'java' -apply plugin: 'org.springframework.boot' -apply plugin: 'war' - -dependencies { - compile project(':juick-server-jdbc') - runtime "commons-fileupload:commons-fileupload:1.3.3" - providedRuntime 'com.h2database:h2:1.4.196' - testCompile("org.springframework.boot:spring-boot-starter-test") -} diff --git a/juick-xmpp-wip/src/main/java/com/juick/components/XMPPBot.java b/juick-xmpp-wip/src/main/java/com/juick/components/XMPPBot.java deleted file mode 100644 index 27206a32..00000000 --- a/juick-xmpp-wip/src/main/java/com/juick/components/XMPPBot.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -package com.juick.components; - -import com.juick.User; -import com.juick.server.helpers.UserInfo; -import com.juick.service.PMQueriesService; -import com.juick.service.UserService; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.math.NumberUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.core.env.Environment; -import rocks.xmpp.addr.Jid; -import rocks.xmpp.core.XmppException; -import rocks.xmpp.core.stanza.model.Message; -import rocks.xmpp.extensions.component.accept.ExternalComponent; -import rocks.xmpp.extensions.vcard.temp.VCardManager; -import rocks.xmpp.extensions.vcard.temp.model.VCard; - -import javax.annotation.PostConstruct; -import javax.inject.Inject; -import java.io.IOException; -import java.net.URL; - -/** - * Created by vt on 12/11/2016. - */ -@SpringBootApplication -public class XMPPBot implements AutoCloseable { - private static final Logger logger = LoggerFactory.getLogger(XMPPBot.class); - @Inject - private UserService userService; - @Inject - private PMQueriesService pmQueriesService; - @Inject - private Environment env; - - Jid juickJid; - - private ExternalComponent component; - - @PostConstruct - @Inject - public void init() { - component = ExternalComponent.create(env.getProperty("component_name", "juick.com"), - env.getProperty("component_password", "secret"), env.getProperty("component_host", "localhost"), - NumberUtils.toInt(env.getProperty("component_port", "5347"), 5347)); - juickJid = Jid.of(env.getProperty("xmppbot_jid", "juick@juick.com/Juick")); - try { - VCardManager vCardManager = component.getManager(VCardManager.class); - vCardManager.setEnabled(true); - VCard ownVCard = new VCard(); - ownVCard.setNickname("Juick"); - ownVCard.setUrl(new URL("http://juick.com/")); - ownVCard.setPhoto(new VCard.Image("image/png", IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream("vCard.png")))); - vCardManager.setVCard(ownVCard); - component.addInboundMessageListener(e -> { - Message message = e.getMessage(); - if (message.getType().equals(Message.Type.ERROR) || message.getType().equals(Message.Type.GROUPCHAT)) { - return; - } - String text = message.getBody().trim(); - String command = text.toUpperCase(); - User user = userService.getUserByJID(message.getFrom().asBareJid().toString()); - if (command.equals("VCARD")) { - try { - VCard vCard = vCardManager.getVCard(message.getFrom().asBareJid()).getResult(); - UserInfo info = new UserInfo(); - info.setFullName(vCard.getFormattedName()); - info.setCountry(vCard.getAddresses().get(0).getCountry()); - info.setUrl(vCard.getUrl().toString()); - userService.updateUserInfo(user, info); - component.sendMessage(new Message(message.getFrom(), Message.Type.CHAT, "vCard updated")); - } catch (XmppException vce) { - logger.warn("vcard exception", vce); - } - } - }); - component.connect(); - } catch (XmppException | IOException e) { - logger.error("bot connection error", e); - } - } - - @Override - public void close() throws Exception { - if (component != null) - component.close(); - - logger.info("ExternalComponent on xmpp-bot destroyed"); - } - public static void main(String[] args) { - SpringApplication.run(XMPPBot.class, args); - } -} diff --git a/juick-xmpp-wip/src/main/java/com/juick/components/XMPPRouter.java b/juick-xmpp-wip/src/main/java/com/juick/components/XMPPRouter.java deleted file mode 100644 index eb00229d..00000000 --- a/juick-xmpp-wip/src/main/java/com/juick/components/XMPPRouter.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -package com.juick.components; - -import com.juick.server.xmpp.s2s.BasicXmppSession; -import com.juick.xmpp.Message; -import com.juick.xmpp.StreamComponentServer; -import com.juick.xmpp.StreamListener; -import com.juick.server.xmpp.extensions.JuickMessage; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; -import org.xmlpull.v1.XmlPullParserException; -import rocks.xmpp.addr.Jid; -import rocks.xmpp.core.stanza.model.Stanza; -import rocks.xmpp.util.XmppUtils; - -import javax.annotation.PostConstruct; -import javax.inject.Inject; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamWriter; -import java.io.IOException; -import java.io.StringReader; -import java.io.StringWriter; -import java.net.ServerSocket; -import java.net.Socket; -import java.net.SocketException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.concurrent.ExecutorService; - -/** - * @author ugnich - */ -@Component -public class XMPPRouter implements Message.MessageListener, AutoCloseable, StreamListener { - private static final Logger logger = LoggerFactory.getLogger(XMPPRouter.class); - - @Inject - private ExecutorService service; - - private final List<StreamComponentServer> connections = Collections.synchronizedList(new ArrayList<>()); - - private ServerSocket listener; - - @Inject - private BasicXmppSession session; - - @Value("${router_port:5347}") - private int routerPort; - - @PostConstruct - public void init() { - - logger.info("component router initialized"); - service.submit(() -> { - try { - listener = new ServerSocket(routerPort); - logger.info("component router listening on {}", routerPort); - while (!listener.isClosed()) { - if (Thread.currentThread().isInterrupted()) break; - Socket socket = listener.accept(); - service.submit(() -> { - try { - StreamComponentServer client = new StreamComponentServer(socket.getInputStream(), socket.getOutputStream(), "secret"); - addConnectionIn(client); - client.addChildParser(new JuickMessage()); - client.addListener((Message.MessageListener) this); - client.addListener((StreamListener) this); - client.connect(); - } catch (IOException e) { - logger.error("component error", e); - } catch (XmlPullParserException e) { - e.printStackTrace(); - } - }); - } - } catch (SocketException e) { - // shutdown - } catch (IOException e) { - logger.warn("io exception", e); - } - }); - } - - @Override - public void close() throws Exception { - if (!listener.isClosed()) { - listener.close(); - } - synchronized (getConnections()) { - for (Iterator<StreamComponentServer> i = getConnections().iterator(); i.hasNext(); ) { - StreamComponentServer c = i.next(); - c.logoff(); - i.remove(); - } - } - service.shutdown(); - logger.info("XMPP router destroyed"); - } - - private void addConnectionIn(StreamComponentServer c) { - synchronized (getConnections()) { - getConnections().add(c); - } - } - - private void sendOut(Stanza s) { - try { - StringWriter stanzaWriter = new StringWriter(); - XMLStreamWriter xmppStreamWriter = XmppUtils.createXmppStreamWriter( - session.getConfiguration().getXmlOutputFactory().createXMLStreamWriter(stanzaWriter)); - session.createMarshaller().marshal(s, xmppStreamWriter); - xmppStreamWriter.flush(); - xmppStreamWriter.close(); - String xml = stanzaWriter.toString(); - logger.info("XMPPRouter (out): {}", xml); - sendOut(s.getTo().getDomain(), xml); - } catch (XMLStreamException | JAXBException e1) { - logger.info("jaxb exception", e1); - } - } - - private void sendOut(String hostname, String xml) { - boolean haveAnyConn = false; - - StreamComponentServer connOut = null; - synchronized (getConnections()) { - for (StreamComponentServer c : getConnections()) { - if (c.to != null && c.to.getDomain().equals(hostname)) { - if (c.isLoggedIn()) { - connOut = c; - break; - } - } - } - } - if (connOut != null) { - connOut.send(xml); - return; - } - logger.error("component unavailable: {}", hostname); - - } - - public List<StreamComponentServer> getConnections() { - return connections; - } - - private Stanza parse(String xml) { - try { - Unmarshaller unmarshaller = session.createUnmarshaller(); - return (Stanza)unmarshaller.unmarshal(new StringReader(xml)); - } catch (JAXBException e) { - logger.error("JAXB exception", e); - } - return null; - } - @Override - public void onMessage(Message message) { - sendOut(parse(message.toString())); - } - - @Override - public void ready() { - - } - - @Override - public void fail(Exception e) { - - } - - @Override - public boolean filter(Jid jid, Jid jid1) { - return false; - } -} diff --git a/juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotAppConfiguration.java b/juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotAppConfiguration.java deleted file mode 100644 index 7806347d..00000000 --- a/juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotAppConfiguration.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -package com.juick.components.configuration; - -import com.juick.components.XMPPBot; -import com.juick.server.configuration.BaseWebConfiguration; -import com.juick.server.protocol.JuickProtocol; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -/** - * Created by aalexeev on 11/12/16. - */ -@Configuration -@ComponentScan(basePackages = {"com.juick.components"}) -public class BotAppConfiguration extends BaseWebConfiguration { - @Bean - public XMPPBot xmpp() { - return new XMPPBot(); - } - @Bean - public JuickProtocol juickProtocol() { - return new JuickProtocol("http://juick.com"); - } -} diff --git a/juick-xmpp-wip/src/main/java/com/juick/components/controllers/StatusController.java b/juick-xmpp-wip/src/main/java/com/juick/components/controllers/StatusController.java deleted file mode 100644 index e57d0a82..00000000 --- a/juick-xmpp-wip/src/main/java/com/juick/components/controllers/StatusController.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -package com.juick.components.controllers; - -import com.juick.components.XMPPRouter; -import com.juick.components.controllers.helpers.RouterStatus; -import org.springframework.http.MediaType; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; - -import javax.inject.Inject; - -/** - * Created by vitalyster on 24.10.2016. - */ -@Controller -@ResponseBody -public class StatusController { - @Inject - private XMPPRouter router; - - @RequestMapping(method = RequestMethod.GET, value = "/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - public RouterStatus status() { - RouterStatus status = new RouterStatus(); - status.setConnections(router.getConnections()); - return status; - } -} diff --git a/juick-xmpp-wip/src/main/java/com/juick/components/controllers/helpers/RouterStatus.java b/juick-xmpp-wip/src/main/java/com/juick/components/controllers/helpers/RouterStatus.java deleted file mode 100644 index 11148dd7..00000000 --- a/juick-xmpp-wip/src/main/java/com/juick/components/controllers/helpers/RouterStatus.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.juick.components.controllers.helpers; - -import com.juick.xmpp.StreamComponentServer; - -import java.util.List; - -public class RouterStatus { - private List<StreamComponentServer> connections; - - public List<StreamComponentServer> getConnections() { - return connections; - } - - public void setConnections(List<StreamComponentServer> connections) { - this.connections = connections; - } -} diff --git a/juick-xmpp-wip/src/main/resources/vCard.png b/juick-xmpp-wip/src/main/resources/vCard.png Binary files differdeleted file mode 100644 index 5ed5336a..00000000 --- a/juick-xmpp-wip/src/main/resources/vCard.png +++ /dev/null diff --git a/juick-xmpp-wip/src/test/java/com/juick/xmpp/XMPPTests.java b/juick-xmpp-wip/src/test/java/com/juick/xmpp/XMPPTests.java deleted file mode 100644 index a9c6904c..00000000 --- a/juick-xmpp-wip/src/test/java/com/juick/xmpp/XMPPTests.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -package com.juick.xmpp; - -import com.juick.components.XMPPRouter; -import com.juick.components.configuration.BotAppConfiguration; -import com.juick.configuration.DataConfiguration; -import com.juick.service.ImagesService; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.context.junit4.SpringRunner; -import rocks.xmpp.addr.Jid; -import rocks.xmpp.core.XmppException; -import rocks.xmpp.core.session.ConnectionEvent; -import rocks.xmpp.core.session.Extension; -import rocks.xmpp.core.session.XmppSessionConfiguration; -import rocks.xmpp.core.session.debug.LogbackDebugger; -import rocks.xmpp.core.stanza.model.Message; -import rocks.xmpp.extensions.component.accept.ExternalComponent; - -import javax.inject.Inject; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutorService; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; - -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, - classes = { BotAppConfiguration.class, DataConfiguration.class}) -public class XMPPTests { - @MockBean - ImagesService imagesService; - @Inject - XMPPRouter router; - @Inject - ExecutorService executorService; - @Test - public void routerHandshake() throws InterruptedException { - XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() - .extensions(Extension.of(com.juick.Message.class)) - .debugger(LogbackDebugger.class) - .build(); - ExternalComponent routerClient = ExternalComponent.create("test.localhost", - "secret", configuration, "localhost", 5347); - CountDownLatch lock = new CountDownLatch(1); - routerClient.addConnectionListener(connectionEvent -> { - if (connectionEvent.equals(ConnectionEvent.Type.RECONNECTION_SUCCEEDED)) { - lock.countDown(); - } - }); - ExternalComponent routerClient2 = ExternalComponent.create("bot.localhost", - "secret", configuration, "localhost", 5347); - CountDownLatch lock2 = new CountDownLatch(1); - routerClient2.addConnectionListener(connectionEvent -> { - if (connectionEvent.equals(ConnectionEvent.Type.RECONNECTION_SUCCEEDED)) { - lock2.countDown(); - } - }); - executorService.submit(() -> { - try { - Thread.sleep(3000); - routerClient.connect(); - routerClient2.connect(); - } catch (XmppException | InterruptedException e) { - e.printStackTrace(); - } - lock.countDown(); - lock2.countDown(); - }); - lock.await(); - CountDownLatch lock3 = new CountDownLatch(2); - routerClient2.sendMessage(new Message(Jid.of("yoyo@test.localhost"), Message.Type.CHAT, "test" )); - routerClient2.sendMessage(new Message(Jid.of("yoyo@test.localhost"), Message.Type.CHAT, "test" )); - routerClient.addInboundMessageListener(messageEvent -> { - assertThat(messageEvent.getMessage().getBody(), equalTo("test")); - lock3.countDown(); - }); - lock3.await(); - } -} diff --git a/settings.gradle b/settings.gradle index 59ca2236..766c8337 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,4 @@ rootProject.name = "Juick" -include ':juick-common', ':juick-server-jdbc', ':juick-server', ':juick-www', ':juick-notifications', ':juick-xmpp-wip' +include ':juick-common', ':juick-server-jdbc', ':juick-server', ':juick-www', ':juick-notifications' |