diff options
author | Vitaly Takmazov | 2016-02-03 13:54:12 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-02-03 13:54:12 +0300 |
commit | e80e13c27e6f06c52a3dc8602cd1f02abdfc3180 (patch) | |
tree | a309a04f63bd0e4eefae4b0583e88352f1f0e802 /src/main/java/com/juick/http/www | |
parent | 380018da475ff41d3375e7f2bea0a192a4d9b178 (diff) |
WIP
Diffstat (limited to 'src/main/java/com/juick/http/www')
-rw-r--r-- | src/main/java/com/juick/http/www/Main.java | 83 | ||||
-rw-r--r-- | src/main/java/com/juick/http/www/NewMessage.java | 47 |
2 files changed, 41 insertions, 89 deletions
diff --git a/src/main/java/com/juick/http/www/Main.java b/src/main/java/com/juick/http/www/Main.java index 36376d3c..08b92305 100644 --- a/src/main/java/com/juick/http/www/Main.java +++ b/src/main/java/com/juick/http/www/Main.java @@ -17,17 +17,10 @@ */ package com.juick.http.www; -import com.juick.CrosspostComponent; -import com.juick.JuickComponent; -import com.juick.JuickNotificator; -import com.juick.PushComponent; +import com.juick.JuickApplication; import com.juick.server.UserQueries; -import com.juick.xmpp.JID; import com.juick.xmpp.Stream; -import com.juick.xmpp.StreamComponent; -import com.juick.xmpp.s2s.S2SComponent; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DriverManagerDataSource; import ru.sape.Sape; import javax.servlet.ServletException; @@ -37,20 +30,13 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.net.InetSocketAddress; import java.net.URLEncoder; -import java.nio.channels.AsynchronousSocketChannel; -import java.nio.channels.Channels; -import java.nio.channels.CompletionHandler; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; -import java.util.ArrayList; import java.util.Enumeration; -import java.util.List; import java.util.Properties; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; /** * @@ -58,7 +44,7 @@ import java.util.concurrent.Executors; */ @WebServlet(name = "Main", urlPatterns = {"/"}) @MultipartConfig(fileSizeThreshold = 1024 * 1024, maxRequestSize = 1024 * 1024 * 10) -public class Main extends HttpServlet implements JuickNotificator, Stream.StreamListener { +public class Main extends HttpServlet implements Stream.StreamListener { static ExecutorService executorService; JdbcTemplate sql; JdbcTemplate sqlSearch; @@ -70,14 +56,14 @@ public class Main extends HttpServlet implements JuickNotificator, Stream.Stream Help help = new Help(); User pagesUser = new User(); UserThread pagesUserThread = new UserThread(); - NewMessage pagesNewMessage = new NewMessage(); + NewMessage pagesNewMessage; FacebookLogin loginFacebook = new FacebookLogin(); VKontakteLogin loginVK = new VKontakteLogin(); TwitterAuth twitterAuth; SignUp signup = new SignUp(); Settings settings = new Settings(); RSS rss = new RSS(); - static List<JuickComponent> components = new ArrayList<>(); + JuickApplication app; @Override public void init() throws ServletException { @@ -86,64 +72,19 @@ public class Main extends HttpServlet implements JuickNotificator, Stream.Stream try { Properties conf = new Properties(); conf.load(getServletContext().getResourceAsStream("WEB-INF/juick.conf")); - - executorService = Executors.newWorkStealingPool(); - getServletContext().setAttribute("es", executorService); - - DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); - dataSource.setUrl(conf.getProperty("datasource_url")); - sql = new JdbcTemplate(dataSource); - getServletContext().setAttribute("sql", sql); - DriverManagerDataSource searchDatasource = new DriverManagerDataSource(); - searchDatasource.setDriverClassName("com.mysql.jdbc.Driver"); - searchDatasource.setUrl("jdbc:mysql://127.0.0.1:9306?autoReconnect=true&useUnicode=yes&characterEncoding=utf8&maxAllowedPacket=512000"); - sqlSearch = new JdbcTemplate(searchDatasource); - getServletContext().setAttribute("sqlSearch", sqlSearch); - setupXmppComponent(conf.getProperty("xmpp_password")); twitterAuth = new TwitterAuth(conf.getProperty("twitter_consumer_key"), conf.getProperty("twitter_consumer_secret")); PageTemplates.sape = new Sape(conf.getProperty("sape_user"), "juick.com", 2000, 3600); - components.add(new S2SComponent(sql, executorService, conf)); - components.add(new CrosspostComponent(sql, conf)); - components.add(new PushComponent(sql, conf)); + app = new JuickApplication(conf); + sql = app.getSql(); + sqlSearch = app.getSqlSearch(); + pagesNewMessage = new NewMessage(app); } catch (Exception e) { log("www failed", e); } } @Override - public void push(com.juick.xmpp.Message msg) { - for (JuickComponent c : components) { - c.messageReceived(msg); - } - } - - public void setupXmppComponent(final String password) { - executorService.submit(() -> { - try { - AsynchronousSocketChannel socket = AsynchronousSocketChannel.open(); - socket.connect(new InetSocketAddress("localhost", 5347), socket, new CompletionHandler<Void, AsynchronousSocketChannel>() { - @Override - public void completed(Void result, AsynchronousSocketChannel attachment) { - xmpp = new StreamComponent(new JID("", "www.juick.com", ""), Channels.newInputStream(socket), - Channels.newOutputStream(socket), password); - xmpp.addListener(Main.this); - xmpp.startParsing(); - } - - @Override - public void failed(Throwable exc, AsynchronousSocketChannel attachment) { - log("www xmpp failed"); - } - }); - } catch (IOException e) { - log("xmpp exception", e); - } - }); - } - - @Override public void onStreamFail(Exception e) {log("XMPP STREAM FAIL", e);} @Override @@ -177,7 +118,7 @@ public class Main extends HttpServlet implements JuickNotificator, Stream.Stream } else if (uri.equals("/post")) { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor != null) { - pagesNewMessage.doGetNewMessage(sql, request, response, visitor); + pagesNewMessage.doGetNewMessage(request, response, visitor); } else { Utils.sendTemporaryRedirect(response, "/login"); } @@ -312,21 +253,21 @@ public class Main extends HttpServlet implements JuickNotificator, Stream.Stream if (uri.equals("/post")) { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor != null && !visitor.Banned) { - pagesNewMessage.doPostMessage(sql, request, response, xmpp, visitor); + pagesNewMessage.doPostMessage(request, response, visitor); } else { response.sendError(403); } } else if (uri.equals("/comment")) { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor != null && !visitor.Banned) { - pagesNewMessage.doPostComment(sql, request, response, xmpp, visitor); + pagesNewMessage.doPostComment(request, response, visitor); } else { response.sendError(403); } } else if (uri.equals("/like")) { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor != null && !visitor.Banned) { - pagesNewMessage.doPostRecomm(sql, request, response, xmpp, visitor); + pagesNewMessage.doPostRecomm(request, response, visitor); } else { response.sendError(403); } diff --git a/src/main/java/com/juick/http/www/NewMessage.java b/src/main/java/com/juick/http/www/NewMessage.java index b3b63b17..d3178c59 100644 --- a/src/main/java/com/juick/http/www/NewMessage.java +++ b/src/main/java/com/juick/http/www/NewMessage.java @@ -17,6 +17,7 @@ */ package com.juick.http.www; +import com.juick.JuickApplication; import com.juick.Tag; import com.juick.server.CrosspostQueries; import com.juick.server.MessagesQueries; @@ -54,10 +55,19 @@ public class NewMessage { private static final Logger logger = Logger.getLogger(NewMessage.class.getName()); - protected void doGetNewMessage(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException { + JuickApplication app; + JdbcTemplate sql; + Stream xmpp; + + public NewMessage(JuickApplication application) { + this.app = application; + this.sql = application.getSql(); + this.xmpp = application.getRouter(); + } + + protected void doGetNewMessage(HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { PageTemplates.pageHead(out, "Написать", "<script src=\"//maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAAVVtPtxkw4soCEHg44FsNChRB4OFYjAXt73He16Zkp6a_0tPs2RTU6i6UlcMs4QvPBYvIY8rWvcxqOg\" type=\"text/javascript\"></script>" + "<script src=\"//static.juick.com/mc.js\" type=\"text/javascript\" defer=\"defer\"></script>" + "<script src=\"//static.juick.com/maps.js?2010111500\" type=\"text/javascript\" defer=\"defer\"></script>" @@ -84,17 +94,15 @@ public class NewMessage { out.println("</form>"); out.println("<div id=\"geomap\"></div>"); out.println("<p style=\"text-align: left\"><b>Теги:</b></p>"); - printUserTags(sql, out, visitor); + printUserTags(out, visitor); out.println("</section>"); PageTemplates.pageFooter(request, out, visitor, false); PageTemplates.pageEnd(out); - } finally { - out.close(); } } - void printUserTags(JdbcTemplate sql, PrintWriter out, com.juick.User visitor) { + void printUserTags(PrintWriter out, com.juick.User visitor) { List<Tag> tags = TagQueries.getUserTagsAll(sql, visitor.getUID()); if (tags.isEmpty()) { @@ -140,7 +148,8 @@ public class NewMessage { out.println("</p>"); } - public void doPostMessage(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { + public void doPostMessage(HttpServletRequest request, HttpServletResponse response, + com.juick.User visitor) throws ServletException, IOException { String body = request.getParameter("body"); if (body == null || body.length() < 1 || body.length() > 4096) { response.sendError(400); @@ -222,20 +231,20 @@ public class NewMessage { xmsg.body = "@" + jmsg.getUser().getUName() + ":" + tagsStr2 + "\n" + body + "\n\n#" + mid + " http://juick.com/" + mid; xmsg.to = new JID("juick", "s2s.juick.com", null); - Main.push(xmsg); + app.push(xmsg); xmsg.to.Host = "push.juick.com"; - Main.push(xmsg); + app.push(xmsg); xmsg.to.Host = "crosspost.juick.com"; xmsg.to.Username = "twitter"; - Main.push(xmsg); + app.push(xmsg); xmsg.to.Username = "fb"; - Main.push(xmsg); + app.push(xmsg); xmsg.to.Host = "nologin.ru"; xmsg.to.Username = "jubo"; - Main.push(xmsg); + app.push(xmsg); if (xmpp != null) { xmsg.to.Host = "ws.juick.com"; xmpp.send(xmsg); @@ -288,7 +297,8 @@ public class NewMessage { } } - public void doPostComment(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { + public void doPostComment(HttpServletRequest request, HttpServletResponse response, + com.juick.User visitor) throws ServletException, IOException { int mid = Utils.parseInt(request.getParameter("mid"), 0); if (mid == 0) { response.sendError(400); @@ -379,9 +389,9 @@ public class NewMessage { xmsg.body = "Reply by @" + jmsg.getUser().getUName() + ":\n>" + quote + "\n" + body + "\n\n#" + mid + "/" + ridnew + " http://juick.com/" + mid + "#" + ridnew; xmsg.to = new JID("juick", "s2s.juick.com", null); - Main.push(xmsg); + app.push(xmsg); xmsg.to.Host = "push.juick.com"; - Main.push(xmsg); + app.push(xmsg); if (xmpp != null) { xmsg.to.Host = "ws.juick.com"; @@ -393,7 +403,8 @@ public class NewMessage { Utils.sendTemporaryRedirect(response, "/" + msg.getUser().getUName() + "/" + mid + "#" + ridnew); } - public void doPostRecomm(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { + public void doPostRecomm(HttpServletRequest request, HttpServletResponse response, + com.juick.User visitor) throws ServletException, IOException { int mid = Utils.parseInt(request.getParameter("mid"), 0); if (mid == 0) { response.sendError(400); @@ -418,7 +429,7 @@ public class NewMessage { jmsg.setMID(mid); jmsg.setUser(new JuickUser(visitor)); xmsg.addChild(jmsg); - Main.push(xmsg); + app.push(xmsg); Utils.replyJSON(request, response, "{\"status\":\"ok\"}"); } else { |