From 2bf63fc8d2a4b84e051d28e670c7c74b039e2545 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 25 Apr 2018 13:54:25 +0300 Subject: www: no more xmpp --- juick-www/src/main/java/com/juick/Application.java | 1 + .../juick/www/configuration/EmbeddedAPIConfig.java | 19 +++ .../www/configuration/EmbeddedXMPPConfig.java | 22 --- .../www/configuration/WwwAppConfiguration.java | 2 +- .../juick/www/configuration/XMPPConfiguration.java | 43 ------ .../java/com/juick/www/controllers/NewMessage.java | 147 ++++++++++----------- 6 files changed, 88 insertions(+), 146 deletions(-) create mode 100644 juick-www/src/main/java/com/juick/www/configuration/EmbeddedAPIConfig.java delete mode 100644 juick-www/src/main/java/com/juick/www/configuration/EmbeddedXMPPConfig.java delete mode 100644 juick-www/src/main/java/com/juick/www/configuration/XMPPConfiguration.java (limited to 'juick-www/src/main') diff --git a/juick-www/src/main/java/com/juick/Application.java b/juick-www/src/main/java/com/juick/Application.java index cb132ae9..47c16754 100644 --- a/juick-www/src/main/java/com/juick/Application.java +++ b/juick-www/src/main/java/com/juick/Application.java @@ -5,6 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.PropertySource; import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication diff --git a/juick-www/src/main/java/com/juick/www/configuration/EmbeddedAPIConfig.java b/juick-www/src/main/java/com/juick/www/configuration/EmbeddedAPIConfig.java new file mode 100644 index 00000000..6ceb140b --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/configuration/EmbeddedAPIConfig.java @@ -0,0 +1,19 @@ +package com.juick.www.configuration; + +import com.juick.server.configuration.PostConfig; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConditionalOnClass(name = "com.juick.server.api.Post") +public class EmbeddedAPIConfig { + @Bean + public ConfigurableApplicationContext apiContext() { + return new SpringApplicationBuilder() + .sources(PostConfig.class) + .run("--server.port=8081"); + } +} diff --git a/juick-www/src/main/java/com/juick/www/configuration/EmbeddedXMPPConfig.java b/juick-www/src/main/java/com/juick/www/configuration/EmbeddedXMPPConfig.java deleted file mode 100644 index a54b76a1..00000000 --- a/juick-www/src/main/java/com/juick/www/configuration/EmbeddedXMPPConfig.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.juick.www.configuration; - -import com.juick.server.XMPPConnection; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import rocks.xmpp.extensions.component.accept.ExternalComponent; - -import javax.inject.Inject; - -@Configuration -@ConditionalOnClass(name = "com.juick.server.xmpp.router.XMPPRouter") -@ComponentScan(basePackages = "com.juick.server") -public class EmbeddedXMPPConfig { - @Inject - private XMPPConnection xmppConnection; - @Bean - public ExternalComponent xmpp() { - return xmppConnection.getRouter(); - } -} diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java index 13a394cb..edee3b87 100644 --- a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java +++ b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java @@ -51,7 +51,7 @@ import java.util.Collections; @Configuration @EnableCaching @Import({ BaseWebConfiguration.class, WebSecurityConfig.class, SapeConfiguration.class, - StorageConfiguration.class, XMPPConfiguration.class}) + StorageConfiguration.class}) public class WwwAppConfiguration implements WebMvcConfigurer { @Inject private UserService userService; diff --git a/juick-www/src/main/java/com/juick/www/configuration/XMPPConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/XMPPConfiguration.java deleted file mode 100644 index 8afddf2d..00000000 --- a/juick-www/src/main/java/com/juick/www/configuration/XMPPConfiguration.java +++ /dev/null @@ -1,43 +0,0 @@ -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.boot.autoconfigure.condition.ConditionalOnMissingBean; -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; - @Bean - @ConditionalOnMissingBean(type = "rocks.xmpp.extensions.component.accept.ExternalComponent") - 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())); - try { - xmpp.connect(); - } catch (XmppException e) { - logger.error("xmpp extension", e); - } - return xmpp; - } -} diff --git a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java index c26d7c0c..3d6bc8cf 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java @@ -16,8 +16,11 @@ */ package com.juick.www.controllers; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.User; import com.juick.server.helpers.AnonymousUser; +import com.juick.server.helpers.CommandResult; import com.juick.server.util.*; import com.juick.service.*; import com.juick.www.WebApp; @@ -26,23 +29,29 @@ import org.apache.commons.text.StringEscapeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.FormHttpMessageConverter; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; -import rocks.xmpp.addr.Jid; -import rocks.xmpp.core.stanza.model.Message; -import rocks.xmpp.extensions.component.accept.ExternalComponent; -import rocks.xmpp.extensions.nick.model.Nickname; -import rocks.xmpp.extensions.oob.model.x.OobX; import javax.inject.Inject; import java.io.IOException; import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.stream.Collectors; /** @@ -66,20 +75,19 @@ public class NewMessage { @Inject private WebApp webApp; @Inject - private ExternalComponent xmpp; + private ObjectMapper jsonMapper; @Inject private ImagesService imagesService; @Value("${img_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}") private String imgDir; @Value("${upload_tmp_dir:#{systemEnvironment['TEMP'] ?: '/tmp'}}") private String tmpDir; - @Value("${xmppbot_jid:juick@localhost}") - private Jid botJid; + private RestTemplate rest = new RestTemplate(); private static final Logger logger = LoggerFactory.getLogger(NewMessage.class); @GetMapping("/post") - protected String postAction(@RequestParam(required = false) String body, ModelMap model) throws IOException { + protected String postAction(@RequestParam(required = false) String body, ModelMap model) { com.juick.User visitor = UserUtils.getCurrentUser(); model.addAttribute("title", "Написать"); model.addAttribute("headers", ""); @@ -146,51 +154,22 @@ public class NewMessage { } } - String attachmentType = StringUtils.isNotEmpty(attachmentFName.toString()) ? attachmentFName.toString().substring(attachmentFName.toString().length() - 3) : null; - int ridnew = messagesService.createReply(mid, rid, visitor, body, attachmentType); - subscriptionService.subscribeMessage(msg, visitor); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + MultiValueMap params = new LinkedMultiValueMap<>(); + HttpEntity> request = new HttpEntity<>(params, headers); - Message xmsg = new Message(); - xmsg.setFrom(botJid); - xmsg.setType(Message.Type.CHAT); - xmsg.setThread("juick-" + mid); - - com.juick.Message jmsg = messagesService.getReply(mid, ridnew); - xmsg.addExtension(jmsg); - - String quote = reply != null ? StringUtils.defaultString(reply.getText()) - : StringUtils.defaultString(msg.getText()); - if (quote.length() >= 50) { - quote = quote.substring(0, 47) + "..."; - } - xmsg.addExtension(new Nickname("@" + jmsg.getUser().getName())); + params.add("body", rid == 0 ? String.format("#%d %s", mid, body) : String.format("#%d/%d %s", mid, rid, body)); + params.add("hash", userService.getHashByUID(visitor.getUid())); if (StringUtils.isNotEmpty(attachmentFName.toString())) { - String fname = mid + "-" + ridnew + "." + attachmentType; - String attachmentURL = "http://i.juick.com/photos-1024/" + fname; - - imagesService.saveImageWithPreviews(attachmentFName.getHost(), fname); - - body = attachmentURL + "\n" + body; - try { - xmsg.addExtension(new OobX(new URI(attachmentURL))); - } catch (URISyntaxException e) { - logger.warn("invalid uri: {}, exception {}", attachmentURL, e); - } - } - - if (xmpp.isConnected()) { - - xmsg.setBody("Reply by @" + jmsg.getUser().getName() + ":\n>" + quote + "\n" + body + "\n\n#" + - mid + "/" + ridnew + " http://juick.com/" + mid + "#" + ridnew); - - xmsg.setTo(botJid); - xmpp.send(xmsg); - } else { - logger.warn("XMPP unavailable"); + params.add("img", attachmentFName.toASCIIString()); } - - return "redirect:/" + msg.getUser().getName() + "/" + mid + "#" + ridnew; + ResponseEntity result = rest.postForEntity( + "http://localhost:8081/post", + request, CommandResult.class); + logger.info("/comment: {}", jsonMapper.writeValueAsString(result.getBody())); + return "redirect:/" + msg.getUser().getName() + "/" + mid + "#" + result.getBody().getNewMessage().get().getRid(); } @PostMapping("/pm/send") @@ -217,30 +196,25 @@ public class NewMessage { throw new HttpForbiddenException(); } - if (pmQueriesService.createPM(visitor.getUid(), userTo.getUid(), body)) { - if (xmpp.isConnected()) { - Message msg = new Message(); - msg.setFrom(botJid); - msg.setTo(botJid.withLocal("pm")); - com.juick.Message jmsg = new com.juick.Message(); - jmsg.setUser(visitor); - jmsg.setText(body); - jmsg.setTo(userTo); - msg.addExtension(jmsg); - xmpp.send(msg); - } else { - logger.warn("XMPP unavailable"); - } - return "redirect:/pm/sent"; - } else { - throw new HttpBadRequestException(); - } + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + MultiValueMap params = new LinkedMultiValueMap<>(); + HttpEntity> request = new HttpEntity<>(params, headers); + + params.add("body", String.format("@%s %s", userTo.getName(), body)); + params.add("hash", userService.getHashByUID(visitor.getUid())); + ResponseEntity result = rest.postForEntity( + "http://localhost:8081/post", + request, CommandResult.class); + logger.info("/pm: {}", jsonMapper.writeValueAsString(result.getBody())); + return "redirect:/pm/sent"; + } @PostMapping("/post2") public String doPostMessage(@RequestParam(name = "body", required = false) String bodyParam, @RequestParam(required = false) String img, @RequestParam(required = false) String referer, - @RequestParam(required = false) MultipartFile attach) throws IOException { + @RequestParam(required = false) MultipartFile attach) throws JsonProcessingException { com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.getUid() == 0 || visitor.isBanned()) { @@ -259,18 +233,31 @@ public class NewMessage { throw new HttpBadRequestException(); } } - Message msg = new Message(); - msg.setType(Message.Type.CHAT); - msg.setFrom(Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "perl")); - msg.setTo(botJid); - msg.setBody(body); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + MultiValueMap params = new LinkedMultiValueMap<>(); + HttpEntity> request = new HttpEntity<>(params, headers); + + params.add("body", body); + params.add("hash", userService.getHashByUID(visitor.getUid())); if (StringUtils.isNotEmpty(attachmentFName.toString())) { - msg.addExtension(new OobX(attachmentFName, "!!!!Juick!!")); + params.add("img", attachmentFName.toASCIIString()); } - xmpp.sendMessage(msg); - if (StringUtils.isBlank(referer) || referer.substring(0, 21).equals("http://juick.com/post") - || referer.substring(0, 22).equals("https://juick.com/post")) { - return "redirect:/?show=my"; + try { + ResponseEntity result = rest.postForEntity( + "http://localhost:8081/post", + request, CommandResult.class); + if (result.getBody().getNewMessage().isPresent()) { + logger.info("/post: {}", jsonMapper.writeValueAsString(result.getBody())); + } else { + logger.info("{} : {}", body, result.getBody().getText()); + } + if (StringUtils.isBlank(referer) || referer.substring(0, 21).equals("http://juick.com/post") + || referer.substring(0, 22).equals("https://juick.com/post")) { + return "redirect:/?show=my"; + } + } catch (HttpClientErrorException e) { + logger.error("post error", e); } return "redirect:" + referer; } -- cgit v1.2.3