aboutsummaryrefslogtreecommitdiff
path: root/juick-spring-www/src/main/java/com/juick
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-12-09 17:35:49 +0300
committerGravatar Vitaly Takmazov2016-12-09 17:35:49 +0300
commite2a018e998e125ee2ec983962059c4d2b733a4b4 (patch)
tree2366ed5a465107c7acd539d22610462204f272c3 /juick-spring-www/src/main/java/com/juick
parent87d82a83c604b81abef8766a84f1c8b6f99add59 (diff)
juick-spring-www: WIP
Diffstat (limited to 'juick-spring-www/src/main/java/com/juick')
-rw-r--r--juick-spring-www/src/main/java/com/juick/www/WebApp.java79
-rw-r--r--juick-spring-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java5
-rw-r--r--juick-spring-www/src/main/java/com/juick/www/controllers/LoginController.java65
-rw-r--r--juick-spring-www/src/main/java/com/juick/www/controllers/PMController.java138
4 files changed, 287 insertions, 0 deletions
diff --git a/juick-spring-www/src/main/java/com/juick/www/WebApp.java b/juick-spring-www/src/main/java/com/juick/www/WebApp.java
new file mode 100644
index 00000000..6b26ec03
--- /dev/null
+++ b/juick-spring-www/src/main/java/com/juick/www/WebApp.java
@@ -0,0 +1,79 @@
+package com.juick.www;
+
+import org.apache.commons.lang3.BooleanUtils;
+import org.apache.commons.lang3.math.NumberUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.env.Environment;
+import rocks.xmpp.core.XmppException;
+import rocks.xmpp.core.session.Extension;
+import rocks.xmpp.core.session.XmppSessionConfiguration;
+import rocks.xmpp.extensions.component.accept.ExternalComponent;
+
+import javax.annotation.PostConstruct;
+
+/**
+ * Created by vitalyster on 09.12.2016.
+ */
+public class WebApp implements AutoCloseable {
+ private static Logger logger = LoggerFactory.getLogger(WebApp.class);
+
+ private ExternalComponent xmpp;
+
+ public String tmpDir;
+ public String imgDir;
+
+ private String xmppHost, xmppPassword, xmppJid;
+ private int xmppPort;
+ private boolean isXmppDisabled;
+
+
+ public WebApp(Environment conf) {
+ tmpDir = conf.getProperty("upload_tmp_dir", "/var/www/juick.com/i/tmp/");
+ imgDir = conf.getProperty("img_path", "/var/www/juick.com/i/");
+ isXmppDisabled = BooleanUtils.toBoolean(conf.getProperty("xmpp_disabled"));
+ xmppHost = conf.getProperty("xmpp_host", "localhost");
+ xmppPort = NumberUtils.toInt(conf.getProperty("xmpp_port", "5347"), 5347);
+ xmppJid = conf.getProperty("xmpp_jid", "www.localhost");
+ xmppPassword = conf.getProperty("xmpp_password");
+ }
+
+ @PostConstruct
+ public void init() {
+ if (!isXmppDisabled) {
+ setupXmppComponent(xmppHost, xmppPort, xmppJid, xmppPassword);
+ }
+ }
+
+ @Override
+ public void close() {
+ try {
+ if (getXmpp() != null)
+ getXmpp().close();
+
+ logger.info("ExternalComponent on juick-www destroyed");
+ } catch (Exception e) {
+ logger.warn("Exception occurs on juick-www destroy", e);
+ }
+ }
+
+ public void setupXmppComponent(final String host, final int port, final String jid, final String password) {
+ XmppSessionConfiguration configuration = XmppSessionConfiguration.builder()
+ .extensions(Extension.of(com.juick.Message.class))
+ .build();
+ setXmpp(ExternalComponent.create(jid, password, configuration, host, port));
+ try {
+ getXmpp().connect();
+ } catch (XmppException e) {
+ logger.warn("xmpp extension", e);
+ }
+ }
+
+ public ExternalComponent getXmpp() {
+ return xmpp;
+ }
+
+ public void setXmpp(ExternalComponent xmpp) {
+ this.xmpp = xmpp;
+ }
+}
diff --git a/juick-spring-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java b/juick-spring-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java
index 5fb7848d..eaed8ebd 100644
--- a/juick-spring-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java
+++ b/juick-spring-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java
@@ -1,5 +1,6 @@
package com.juick.www.configuration;
+import com.juick.www.WebApp;
import com.juick.www.settings.TemplateSettingsHolder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -34,4 +35,8 @@ public class WebAppConfiguration {
public TemplateSettingsHolder settingsHolder() {
return new TemplateSettingsHolder(env);
}
+ @Bean
+ public WebApp webApp() {
+ return new WebApp(env);
+ }
}
diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/LoginController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/LoginController.java
new file mode 100644
index 00000000..8a474c9b
--- /dev/null
+++ b/juick-spring-www/src/main/java/com/juick/www/controllers/LoginController.java
@@ -0,0 +1,65 @@
+package com.juick.www.controllers;
+
+import com.juick.User;
+import com.juick.server.util.HttpBadRequestException;
+import com.juick.server.util.HttpForbiddenException;
+import com.juick.service.UserService;
+import com.juick.util.UserUtils;
+import org.springframework.core.env.Environment;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+
+import javax.inject.Inject;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+import java.net.URI;
+import java.security.Principal;
+import java.util.Optional;
+
+/**
+ * Created by vitalyster on 09.12.2016.
+ */
+@Controller
+public class LoginController {
+ @Inject
+ UserService userService;
+ @Inject
+ Environment env;
+
+ @RequestMapping(value = "/login", method = RequestMethod.GET)
+ public String doGetLoginForm(Principal principal) {
+ String name = UserUtils.getUsername(principal, null);
+ User visitor = userService.getUserByName(name);
+ if (visitor.getUid() > 0) {
+ return "redirect:/login";
+ }
+ return "views/login";
+ }
+ @RequestMapping(value = "/login", method = RequestMethod.POST)
+ protected String doPostLogin(
+ @RequestParam("username") Optional<String> username,
+ @RequestParam("password") Optional<String> password,
+ @RequestHeader("Referer") Optional<String> referer,
+ HttpServletResponse response) {
+ if (!username.isPresent() && password.isPresent()) {
+ throw new HttpBadRequestException();
+ }
+
+ int uid = userService.checkPassword(username.get(), password.get());
+ if (uid > 0) {
+ if (referer.isPresent()) {
+ URI refererURI = URI.create(referer.get());
+ if (refererURI.getHost().equals(env.getProperty("web_domain"))
+ && !refererURI.getPath().equals("/login")) {
+ return "redirect:" + referer.get();
+ } else {
+ return "redirect:/";
+ }
+ }
+ }
+ throw new HttpForbiddenException();
+ }
+}
diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/PMController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/PMController.java
new file mode 100644
index 00000000..c3b37cf6
--- /dev/null
+++ b/juick-spring-www/src/main/java/com/juick/www/controllers/PMController.java
@@ -0,0 +1,138 @@
+package com.juick.www.controllers;
+
+import com.juick.Message;
+import com.juick.User;
+import com.juick.server.util.HttpBadRequestException;
+import com.juick.server.util.HttpForbiddenException;
+import com.juick.service.PMQueriesService;
+import com.juick.service.TagService;
+import com.juick.service.UserService;
+import com.juick.util.MessageUtils;
+import com.juick.util.UserUtils;
+import com.juick.www.WebApp;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.ModelMap;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import rocks.xmpp.addr.Jid;
+
+import javax.inject.Inject;
+import java.security.Principal;
+import java.util.List;
+
+/**
+ * Created by vitalyster on 09.12.2016.
+ */
+@Controller
+public class PMController {
+
+ private static final Logger logger = LoggerFactory.getLogger(PMController.class);
+
+ @Inject
+ PMQueriesService pmQueriesService;
+ @Inject
+ UserService userService;
+ @Inject
+ TagService tagService;
+ @Inject
+ WebApp webApp;
+
+ @RequestMapping("/pm/inbox")
+ public String doGetInbox(Principal principal, ModelMap context) {
+ String name = UserUtils.getUsername(principal, null);
+ User visitor = userService.getUserByName(name);
+ String title = "PM: Inbox";
+ List<Message> msgs = pmQueriesService.getLastPMInbox(visitor.getUid());
+ msgs.forEach(m -> m.setText(MessageUtils.formatMessage(m.getText())));
+ context.put("title", title);
+ context.put("visitor", visitor);
+ context.put("msgs", msgs);
+ context.put("tags", tagService.getPopularTags());
+ return "views/pm_inbox";
+ }
+
+ @RequestMapping(value = "/pm/sent", method = RequestMethod.GET)
+ public String doGetSent(
+ Principal principal,
+ @RequestParam String uname,
+ ModelMap context) {
+ String title = "PM: Sent";
+ String name = UserUtils.getUsername(principal, null);
+ User visitor = userService.getUserByName(name);
+ List<com.juick.Message> msgs = pmQueriesService.getLastPMSent(visitor.getUid());
+
+ if (!UserUtils.checkUserNameValid(uname)) {
+ uname = "";
+ }
+ context.put("title", title);
+ context.put("visitor", visitor);
+ context.put("msgs", msgs);
+ context.put("tags", tagService.getPopularTags());
+ context.put("uname", uname);
+ return "views/pm_sent";
+ }
+
+ @RequestMapping(value = "/pm/sent", method = RequestMethod.POST)
+ public String doPostPM(
+ Principal principal,
+ @RequestParam String uname,
+ @RequestParam String body,
+ ModelMap context) {
+ String name = UserUtils.getUsername(principal, null);
+ User visitor = userService.getUserByName(name);
+ if (uname.startsWith("@")) {
+ uname = uname.substring(1);
+ }
+ int uid = 0;
+ if (UserUtils.checkUserNameValid(uname)) {
+ uid = userService.getUIDbyName(uname);
+ }
+
+ if (uid == 0 || body == null || body.length() < 1 || body.length() > 10240) {
+ throw new HttpBadRequestException();
+ }
+
+ if (userService.isInBLAny(uid, visitor.getUid())) {
+ throw new HttpForbiddenException();
+ }
+
+ if (pmQueriesService.createPM(visitor.getUid(), uid, body)) {
+ if (webApp.getXmpp() != null) {
+ rocks.xmpp.core.stanza.model.Message msg = new rocks.xmpp.core.stanza.model.Message();
+ msg.setFrom(Jid.of("juick@juick.com"));
+ msg.setTo(Jid.of(String.format("%d@push.juick.com", uid)));
+ com.juick.Message jmsg = new com.juick.Message();
+ jmsg.setUser(visitor);
+ jmsg.setText(body);
+ msg.addExtension(jmsg);
+ webApp.getXmpp().send(msg);
+
+ msg.setTo(Jid.of(String.format("%d@ws.juick.com", uid)));
+ webApp.getXmpp().send(msg);
+
+ List<String> jids = userService.getJIDsbyUID(uid);
+ for (String jid : jids) {
+ rocks.xmpp.core.stanza.model.Message mm = new rocks.xmpp.core.stanza.model.Message();
+ mm.setTo(Jid.of(jid));
+ mm.setType(rocks.xmpp.core.stanza.model.Message.Type.CHAT);
+ if (pmQueriesService.havePMinRoster(visitor.getUid(), jid)) {
+ mm.setFrom(Jid.of(jmsg.getUser().getName(), "juick.com", "Juick"));
+ mm.setBody(body);
+ } else {
+ mm.setFrom(Jid.of("juick", "juick.com", "Juick"));
+ mm.setBody("Private message from @" + jmsg.getUser().getName() + ":\n" + body);
+ }
+ webApp.getXmpp().send(mm);
+ }
+ } else {
+ logger.warn("XMPP unavailable");
+ }
+ return "redirect:/pm/sent";
+ } else {
+ throw new HttpBadRequestException();
+ }
+ }
+}