From 428c748b25b156a973f508e82a1c53ab6b74bc2b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 7 Feb 2017 12:49:22 +0300 Subject: juick-xmpp: merge juick-xmpp-ft --- .../java/com/juick/components/XMPPConnection.java | 65 +++++++++++++++++++++- .../configuration/XmppAppConfiguration.java | 7 +-- 2 files changed, 65 insertions(+), 7 deletions(-) (limited to 'juick-xmpp') diff --git a/juick-xmpp/src/main/java/com/juick/components/XMPPConnection.java b/juick-xmpp/src/main/java/com/juick/components/XMPPConnection.java index bdec908f..48c67b25 100644 --- a/juick-xmpp/src/main/java/com/juick/components/XMPPConnection.java +++ b/juick-xmpp/src/main/java/com/juick/components/XMPPConnection.java @@ -1,8 +1,12 @@ package com.juick.components; import com.juick.User; +import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.core.env.Environment; import rocks.xmpp.addr.Jid; import rocks.xmpp.core.XmppException; import rocks.xmpp.core.stanza.model.IQ; @@ -10,20 +14,28 @@ import rocks.xmpp.core.stanza.model.Message; import rocks.xmpp.core.stanza.model.Stanza; import rocks.xmpp.core.stanza.model.client.ClientMessage; import rocks.xmpp.extensions.component.accept.ExternalComponent; +import rocks.xmpp.extensions.filetransfer.FileTransfer; +import rocks.xmpp.extensions.filetransfer.FileTransferManager; import rocks.xmpp.extensions.nick.model.Nickname; import rocks.xmpp.extensions.oob.model.x.OobX; import rocks.xmpp.extensions.ping.PingManager; import rocks.xmpp.extensions.receipts.MessageDeliveryReceiptsManager; import rocks.xmpp.util.XmppUtils; +import javax.annotation.PostConstruct; +import javax.inject.Inject; import javax.xml.bind.JAXBException; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; +import java.io.IOException; import java.io.StringWriter; import java.net.URI; import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ExecutionException; /** * @author ugnich @@ -33,11 +45,17 @@ public class XMPPConnection implements AutoCloseable { private static final Logger logger = LoggerFactory.getLogger(XMPPConnection.class); private ExternalComponent router; + @Inject private XMPPServer xmpp; + @Inject + private Environment env; - public XMPPConnection(XMPPServer s2s, String componentName, int componentPort, String password) { - this.xmpp = s2s; + @PostConstruct + public void init() { logger.info("stream router start"); + String componentName = env.getProperty("componentname"); + int componentPort = NumberUtils.toInt(env.getProperty("component_port"), 5347); + String password = env.getProperty("xmpp_password"); router = ExternalComponent.create(componentName, password, xmpp.getSession().getConfiguration(), "localhost", componentPort); PingManager pingManager = router.getManager(PingManager.class); pingManager.setEnabled(true); @@ -80,6 +98,49 @@ public class XMPPConnection implements AutoCloseable { route(jid.getDomain(), iq); } }); + String tmpDir = env.getProperty("upload_tmp_dir", "/tmp"); + FileTransferManager fileTransferManager = router.getManager(FileTransferManager.class); + fileTransferManager.addFileTransferOfferListener(e -> { + try { + List allowedTypes = new ArrayList() {{ + add("png"); + add("jpg"); + }}; + String attachmentExtension = FilenameUtils.getExtension(e.getName()).toLowerCase(); + String targetFilename = String.format("%s.%s", + DigestUtils.md5Hex(String.format("%s-%s", + e.getInitiator().toString(), e.getSessionId()).getBytes()), attachmentExtension); + if (allowedTypes.contains(attachmentExtension)) { + Path filePath = Paths.get(tmpDir, targetFilename); + FileTransfer ft = e.accept(filePath).get(); + ft.addFileTransferStatusListener(st -> { + logger.debug("{}: received {} of {}", e.getName(), st.getBytesTransferred(), e.getSize()); + if (ft.isDone()) { + logger.info("transfer completed"); + Message msg = new Message(); + msg.setType(Message.Type.CHAT); + msg.setFrom(e.getInitiator()); + msg.setTo(xmpp.getJid()); + msg.setBody(e.getDescription()); + try { + String attachmentUrl = String.format("juick://%s", targetFilename); + msg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!")); + router.sendMessage(msg); + } catch (URISyntaxException e1) { + logger.warn("attachment error", e1); + } + } + }); + ft.transfer(); + logger.info("transfer started"); + } else { + e.reject(); + logger.info("transfer rejected"); + } + } catch (IOException | InterruptedException | ExecutionException e1) { + logger.error("ft error", e1); + } + }); try { router.connect(); } catch (XmppException e) { diff --git a/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java index 22b4cc41..35de10f6 100644 --- a/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java +++ b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java @@ -4,13 +4,12 @@ package com.juick.components.configuration; * Created by aalexeev on 11/12/16. */ +import com.juick.components.XMPPConnection; import com.juick.components.XMPPServer; import com.juick.components.s2s.CleaningUp; -import com.juick.components.XMPPConnection; import com.juick.components.s2s.JuickBot; import com.juick.configuration.DataConfiguration; import org.apache.commons.lang3.BooleanUtils; -import org.apache.commons.lang3.math.NumberUtils; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -50,8 +49,6 @@ public class XmppAppConfiguration { @Bean public XMPPConnection router() { boolean disabled = BooleanUtils.toBoolean(env.getProperty("xmpp_disabled", "false")); - return disabled ? null : new XMPPConnection(xmpp(), env.getProperty("componentname"), - NumberUtils.toInt(env.getProperty("component_port"), 5347), - env.getProperty("xmpp_password")); + return disabled ? null : new XMPPConnection(); } } -- cgit v1.2.3