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 --- juick-xmpp-ft/build.gradle | 23 ----- .../java/com/juick/components/XMPPFTServer.java | 105 --------------------- .../FileTransferAppConfiguration.java | 26 ----- .../configuration/FileTransferInitializer.java | 40 -------- .../FileTransferMvcConfiguration.java | 33 ------- .../components/controllers/StatusController.java | 24 ----- juick-xmpp-ft/src/main/webapp/WEB-INF/web.xml | 7 -- .../java/com/juick/components/XMPPConnection.java | 65 ++++++++++++- .../configuration/XmppAppConfiguration.java | 7 +- settings.gradle | 2 +- 10 files changed, 66 insertions(+), 266 deletions(-) delete mode 100644 juick-xmpp-ft/build.gradle delete mode 100644 juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java delete mode 100644 juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferAppConfiguration.java delete mode 100644 juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferInitializer.java delete mode 100644 juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferMvcConfiguration.java delete mode 100644 juick-xmpp-ft/src/main/java/com/juick/components/controllers/StatusController.java delete mode 100644 juick-xmpp-ft/src/main/webapp/WEB-INF/web.xml diff --git a/juick-xmpp-ft/build.gradle b/juick-xmpp-ft/build.gradle deleted file mode 100644 index 2bef7852..00000000 --- a/juick-xmpp-ft/build.gradle +++ /dev/null @@ -1,23 +0,0 @@ -apply plugin: 'java' -apply plugin: 'war' -apply plugin: 'org.akhikhl.gretty' -apply plugin: 'com.github.ben-manes.versions' - -dependencies { - compile project(':juick-server') - compile "org.springframework:spring-webmvc:${rootProject.springFrameworkVersion}" - providedRuntime 'mysql:mysql-connector-java:5.1.40' -} - -compileJava.options.encoding = 'UTF-8' - -gretty { - httpPort = 8080 - contextPath = '' - servletContainer = 'tomcat8' -} - -configurations { - all*.exclude module: 'commons-logging' -} - diff --git a/juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java b/juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java deleted file mode 100644 index 236c4c0d..00000000 --- a/juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.juick.components; - -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.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 rocks.xmpp.extensions.filetransfer.FileTransfer; -import rocks.xmpp.extensions.filetransfer.FileTransferManager; -import rocks.xmpp.extensions.oob.model.x.OobX; -import rocks.xmpp.extensions.ping.PingManager; - -import java.io.IOException; -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; - -/** - * Created by vitalyster on 11.11.2016. - */ -public class XMPPFTServer implements AutoCloseable { - private static final Logger logger = LoggerFactory.getLogger(XMPPFTServer.class); - - private ExternalComponent component; - - public XMPPFTServer(Environment env) { - XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() - .debugger(LogbackDebugger.class) - .build(); - component = ExternalComponent.create(env.getProperty("component_name", "files"), - env.getProperty("component_password", "secret"), configuration, env.getProperty("component_host", "localhost"), - NumberUtils.toInt(env.getProperty("component_port", "5347"), 5347)); - String tmpDir = env.getProperty("upload_tmp_dir", "/tmp"); - String juickJid = env.getProperty("xmppbot_jid", "juick@juick.com/Juick"); - PingManager pingManager = component.getManager(PingManager.class); - pingManager.setEnabled(true); - FileTransferManager fileTransferManager = component.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(Jid.of(juickJid)); - msg.setBody(e.getDescription()); - try { - String attachmentUrl = String.format("juick://%s", targetFilename); - msg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!")); - component.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 { - component.connect(); - } catch (XmppException e) { - logger.error("ft initialization error", e); - } - } - - @Override - public void close() throws Exception { - if (component != null) - component.close(); - - logger.info("ExternalComponent on xmpp-ft destroyed"); - } -} diff --git a/juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferAppConfiguration.java b/juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferAppConfiguration.java deleted file mode 100644 index e52a604a..00000000 --- a/juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferAppConfiguration.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.juick.components.configuration; - -import com.juick.components.XMPPFTServer; -import com.juick.configuration.DataConfiguration; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; -import org.springframework.context.annotation.PropertySource; -import org.springframework.core.env.Environment; - -import javax.inject.Inject; - -/** - * Created by aalexeev on 11/12/16. - */ -@Configuration -@PropertySource("classpath:juick.conf") -public class FileTransferAppConfiguration { - @Inject - private Environment env; - - @Bean - public XMPPFTServer xmpp() { - return new XMPPFTServer(env); - } -} diff --git a/juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferInitializer.java b/juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferInitializer.java deleted file mode 100644 index 7fac43d4..00000000 --- a/juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferInitializer.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.juick.components.configuration; - -import com.juick.configuration.DataConfiguration; -import org.apache.commons.lang3.CharEncoding; -import org.springframework.web.filter.CharacterEncodingFilter; -import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; - -import javax.servlet.Filter; - -/** - * Created by vt on 09/02/16. - */ -public class FileTransferInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { - - @Override - protected Class[] getRootConfigClasses() { - return new Class[]{FileTransferAppConfiguration.class, DataConfiguration.class}; - } - - @Override - protected Class[] getServletConfigClasses() { - return new Class[]{FileTransferMvcConfiguration.class}; - } - - @Override - protected String[] getServletMappings() { - return new String[]{"/"}; - } - - @Override - protected Filter[] getServletFilters() { - CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(CharEncoding.UTF_8); - return new Filter[]{characterEncodingFilter}; - } - - @Override - protected String getServletName() { - return "File transfer dispantcher servlet"; - } -} diff --git a/juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferMvcConfiguration.java b/juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferMvcConfiguration.java deleted file mode 100644 index 9500f4e6..00000000 --- a/juick-xmpp-ft/src/main/java/com/juick/components/configuration/FileTransferMvcConfiguration.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.juick.components.configuration; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; - -import java.util.List; - -/** - * Created by vitalyster on 28.06.2016. - */ -@Configuration -@ComponentScan(basePackages = {"com.juick.components.controllers"}) -public class FileTransferMvcConfiguration extends WebMvcConfigurationSupport { - - @Override - protected void configureMessageConverters(List> converters) { - Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() - .serializationInclusion(JsonInclude.Include.NON_DEFAULT) - .serializationInclusion(JsonInclude.Include.NON_NULL) - .serializationInclusion(JsonInclude.Include.NON_ABSENT) - .serializationInclusion(JsonInclude.Include.NON_EMPTY); - MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(builder.build()); - converter.getObjectMapper().registerModule(new Jdk8Module()); - converters.add(converter); - super.configureMessageConverters(converters); - } -} diff --git a/juick-xmpp-ft/src/main/java/com/juick/components/controllers/StatusController.java b/juick-xmpp-ft/src/main/java/com/juick/components/controllers/StatusController.java deleted file mode 100644 index eeff36f8..00000000 --- a/juick-xmpp-ft/src/main/java/com/juick/components/controllers/StatusController.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.juick.components.controllers; - -import com.juick.components.XMPPFTServer; -import com.juick.Status; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -import javax.inject.Inject; - -/** - * Created by vitalyster on 24.10.2016. - */ -@RestController -public class StatusController { - @Inject - XMPPFTServer xmpp; - - @RequestMapping(method = RequestMethod.GET, value = "/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - public Status status() { - return xmpp != null ? Status.OK : Status.ERROR; - } -} diff --git a/juick-xmpp-ft/src/main/webapp/WEB-INF/web.xml b/juick-xmpp-ft/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index a57cceb9..00000000 --- a/juick-xmpp-ft/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - \ No newline at end of file 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(); } } diff --git a/settings.gradle b/settings.gradle index af94a9e0..fe2d7c3f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,4 @@ rootProject.name = "Juick" -include ':juick-core', 'juick-server', ':juick-api', ':juick-www', ':juick-rss', ':juick-ws', ':juick-notifications', ':juick-crosspost', ':juick-xmpp', ':juick-xmpp-ft', ':juick-xmpp-bot', ':juick-spring-www' +include ':juick-core', 'juick-server', ':juick-api', ':juick-www', ':juick-rss', ':juick-ws', ':juick-notifications', ':juick-crosspost', ':juick-xmpp', ':juick-xmpp-bot', ':juick-spring-www' -- cgit v1.2.3