aboutsummaryrefslogtreecommitdiff
path: root/juick-crosspost/src/main/java/com
diff options
context:
space:
mode:
authorGravatar Alexander Alexeev2016-11-12 01:34:07 +0700
committerGravatar Vitaly Takmazov2016-11-12 11:14:56 +0300
commit194a9a763c6b11d207e682b2f93de94475c473b4 (patch)
tree196317662017b716066eadfa413ecbdc7532c52d /juick-crosspost/src/main/java/com
parent7d86347a351aa5263c19cd03aa0bda4650c8bfca (diff)
extracted application configuration from Mvc configuration with application specific beans;
extracted bean initialization from constructor; force using properties;
Diffstat (limited to 'juick-crosspost/src/main/java/com')
-rw-r--r--juick-crosspost/src/main/java/com/juick/components/Crosspost.java86
-rw-r--r--juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java37
-rw-r--r--juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostConfiguration.java122
-rw-r--r--juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java10
-rw-r--r--juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostMvcConfiguration.java43
5 files changed, 138 insertions, 160 deletions
diff --git a/juick-crosspost/src/main/java/com/juick/components/Crosspost.java b/juick-crosspost/src/main/java/com/juick/components/Crosspost.java
index 35623c40..905816fd 100644
--- a/juick-crosspost/src/main/java/com/juick/components/Crosspost.java
+++ b/juick-crosspost/src/main/java/com/juick/components/Crosspost.java
@@ -18,6 +18,7 @@
package com.juick.components;
import com.juick.server.CrosspostQueries;
+import com.juick.util.ThreadHelper;
import com.juick.xmpp.JID;
import com.juick.xmpp.Message;
import com.juick.xmpp.Stream;
@@ -29,8 +30,10 @@ import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.util.Assert;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
@@ -46,36 +49,40 @@ import java.util.concurrent.ExecutorService;
/**
* @author Ugnich Anton
*/
-public class Crosspost implements DisposableBean, Stream.StreamListener, Message.MessageListener {
- public final static String TWITTERURL = "https://api.twitter.com/1.1/statuses/update.json";
- public final static String FBURL = "https://graph.facebook.com/me/feed";
- public final static String VKURL = "https://api.vk.com/method/wall.post";
+public class Crosspost implements InitializingBean, DisposableBean, Stream.StreamListener, Message.MessageListener {
+ final static String TWITTERURL = "https://api.twitter.com/1.1/statuses/update.json";
+ final static String FBURL = "https://graph.facebook.com/me/feed";
+ final static String VKURL = "https://api.vk.com/method/wall.post";
private static Logger logger = LoggerFactory.getLogger(Crosspost.class);
- JdbcTemplate jdbc;
- Stream xmpp;
- String twitter_consumer_key;
- String twitter_consumer_secret;
- ExecutorService service;
+ private final JdbcTemplate jdbc;
+ private final ExecutorService service;
+ private Stream xmpp;
+
+ private final String twitter_consumer_key;
+ private final String twitter_consumer_secret;
+ private final String crosspostJid;
+ private final String password;
+ private final int port;
+
+ public Crosspost(final Environment env, final ExecutorService service, final JdbcTemplate jdbc) {
+ Assert.notNull(env);
+ Assert.notNull(service);
+ Assert.notNull(jdbc);
- public Crosspost(Environment env, ExecutorService service, JdbcTemplate jdbc) {
this.service = service;
this.jdbc = jdbc;
- logger.info("component initialized");
- try {
- twitter_consumer_key = env.getProperty("twitter_consumer_key", "");
- twitter_consumer_secret = env.getProperty("twitter_consumer_secret", "");
- setupXmppComponent(env.getProperty("crosspost_jid", "crosspost.juick.local"),
- env.getProperty("xmpp_password", ""), NumberUtils.toInt(env.getProperty("xmpp_port", ""), 5347));
- service.submit(() -> xmpp.startParsing());
- } catch (Exception e) {
- logger.error("crosspost exception", e);
- }
+ twitter_consumer_key = env.getProperty("twitter_consumer_key", "");
+ twitter_consumer_secret = env.getProperty("twitter_consumer_secret", "");
+
+ crosspostJid = env.getProperty("crosspost_jid", "crosspost.juick.local");
+ password = env.getProperty("xmpp_password", "");
+ port = NumberUtils.toInt(env.getProperty("xmpp_port"), 5347);
}
- public static String percentEncode(String s) {
+ public static String percentEncode(final String s) {
String ret = "";
try {
ret = URLEncoder.encode(s, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
@@ -85,23 +92,30 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message
}
@Override
- public void destroy() {
- logger.info("component destroyed");
- }
-
- public void setupXmppComponent(String jid, String password, int port) {
+ public void afterPropertiesSet() throws Exception {
try {
Socket socket = new Socket("localhost", port);
- xmpp = new StreamComponent(new JID(jid), socket.getInputStream(), socket.getOutputStream(), password);
+ xmpp = new StreamComponent(new JID(crosspostJid), socket.getInputStream(), socket.getOutputStream(), password);
xmpp.addChildParser(new JuickMessage());
xmpp.addListener((Stream.StreamListener) this);
xmpp.addListener((Message.MessageListener) this);
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
+
+ service.submit(() -> xmpp.startParsing());
+
+ logger.info("Crosspost initialized");
+ } catch (Exception e) {
+ logger.error("Crosspost initialization exception", e);
}
}
@Override
+ public void destroy() throws Exception {
+ ThreadHelper.shutdownAndAwaitTermination(service);
+
+ logger.info("Crosspost destroyed");
+ }
+
+ @Override
public void onStreamReady() {
logger.info("XMPP STREAM READY");
}
@@ -112,7 +126,7 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message
}
@Override
- public void onMessage(com.juick.xmpp.Message msg) {
+ public void onMessage(final com.juick.xmpp.Message msg) {
JuickMessage jmsg = (JuickMessage) msg.getChild(JuickMessage.XMLNS);
if (msg.to != null && msg.to.Username != null && jmsg != null && jmsg.getRID() == 0) {
if (msg.to.Username.equals("twitter")) {
@@ -125,7 +139,7 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message
}
}
- public boolean facebookPost(com.juick.Message jmsg) {
+ public boolean facebookPost(final com.juick.Message jmsg) {
String token = CrosspostQueries.getFacebookToken(jdbc, jmsg.getUser().getUID()).orElse("");
if (token.isEmpty()) {
return false;
@@ -167,7 +181,7 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message
return ret;
}
- public boolean vkontaktePost(com.juick.Message jmsg) {
+ public boolean vkontaktePost(final com.juick.Message jmsg) {
Pair<String, String> tokens = CrosspostQueries.getVKTokens(jdbc, jmsg.getUser().getUID()).orElse(Pair.of("", ""));
if (tokens.getLeft().isEmpty() || tokens.getRight().isEmpty()) {
return false;
@@ -204,7 +218,7 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message
return ret;
}
- public boolean twitterPost(com.juick.Message jmsg) {
+ public boolean twitterPost(final com.juick.Message jmsg) {
Pair<String, String> tokens = CrosspostQueries.getTwitterTokens(jdbc, jmsg.getUser().getUID()).orElse(Pair.of("", ""));
if (tokens.getLeft().isEmpty() || tokens.getRight().isEmpty()) {
return false;
@@ -259,7 +273,7 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message
return ret;
}
- public String twitterSignature(String status, String nonce, String timestamp, String token, String token_secret) {
+ public String twitterSignature(final String status, final String nonce, final String timestamp, final String token, final String token_secret) {
try {
// ALPHABET-SORTED
String params = "oauth_consumer_key=" + twitter_consumer_key
@@ -285,7 +299,7 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message
return null;
}
- public String streamToString(InputStream is) {
+ public String streamToString(final InputStream is) {
try {
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
StringBuilder str = new StringBuilder();
@@ -301,7 +315,7 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message
return null;
}
- public String getMessageHashTags(com.juick.Message jmsg) {
+ public String getMessageHashTags(final com.juick.Message jmsg) {
String hashtags = "";
for (int i = 0; i < jmsg.Tags.size(); i++) {
hashtags += "#" + jmsg.Tags.get(i) + " ";
diff --git a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java
new file mode 100644
index 00000000..a7787b26
--- /dev/null
+++ b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java
@@ -0,0 +1,37 @@
+package com.juick.components.configuration;
+
+import com.juick.components.Crosspost;
+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 org.springframework.jdbc.core.JdbcTemplate;
+
+import javax.inject.Inject;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+/**
+ * Created by aalexeev on 11/12/16.
+ */
+@Configuration
+@PropertySource("classpath:juick.conf")
+@Import(DataConfiguration.class)
+public class CrosspostAppConfiguration {
+ @Inject
+ private Environment env;
+ @Inject
+ private JdbcTemplate jdbcTemplate;
+
+ @Bean
+ public Crosspost crosspost() {
+ return new Crosspost(env, service(), jdbcTemplate);
+ }
+
+ @Bean
+ public ExecutorService service() {
+ return Executors.newCachedThreadPool();
+ }
+}
diff --git a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostConfiguration.java b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostConfiguration.java
deleted file mode 100644
index 8ae511c4..00000000
--- a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostConfiguration.java
+++ /dev/null
@@ -1,122 +0,0 @@
-package com.juick.components.configuration;
-
-import com.fasterxml.jackson.annotation.JsonInclude;
-import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
-import com.juick.components.Crosspost;
-import com.mitchellbosecke.pebble.PebbleEngine;
-import com.mitchellbosecke.pebble.loader.Loader;
-import com.mitchellbosecke.pebble.loader.ServletLoader;
-import com.mitchellbosecke.pebble.spring4.PebbleViewResolver;
-import com.mitchellbosecke.pebble.spring4.extension.SpringExtension;
-import org.springframework.beans.factory.config.PlaceholderConfigurerSupport;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.ComponentScan;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.PropertySource;
-import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
-import org.springframework.core.env.Environment;
-import org.springframework.http.converter.HttpMessageConverter;
-import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
-import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
-import org.springframework.jdbc.core.JdbcTemplate;
-import org.springframework.web.servlet.ViewResolver;
-import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
-import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
-import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
-
-import javax.inject.Inject;
-import javax.servlet.ServletContext;
-import java.util.List;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-
-/**
- * Created by vitalyster on 28.06.2016.
- */
-@Configuration
-@ComponentScan(basePackages = {"com.juick"})
-@PropertySource(value = {"classpath:juick.conf", "file:${user.home}/juick.conf"})
-public class CrosspostConfiguration extends WebMvcConfigurationSupport {
- @Inject
- private Environment env;
- @Inject
- private ServletContext servletContext;
- @Inject
- private JdbcTemplate jdbc;
-
- @Bean
- public static PlaceholderConfigurerSupport propertySourcesPlaceholderConfigurer() {
- PlaceholderConfigurerSupport configurer = new PropertySourcesPlaceholderConfigurer();
-
- configurer.setFileEncoding("utf-8");
- configurer.setOrder(1);
- return configurer;
- }
-
- @Bean
- public Loader templateLoader() {
- return new ServletLoader(servletContext);
- }
-
- @Bean
- public SpringExtension springExtension() {
- return new SpringExtension();
- }
-
- @Bean
- public PebbleEngine pebbleEngine() {
- return new PebbleEngine.Builder()
- .loader(this.templateLoader())
- .extension(springExtension())
- .build();
- }
-
- @Bean
- public ViewResolver viewResolver() {
- PebbleViewResolver viewResolver = new PebbleViewResolver();
- viewResolver.setPrefix("/WEB-INF/templates/");
- viewResolver.setSuffix(".html");
- viewResolver.setPebbleEngine(pebbleEngine());
-
- return viewResolver;
- }
-
- @Bean
- public Crosspost crosspost() {
- return new Crosspost(env, service(), jdbc);
- }
-
- @Bean
- public ExecutorService service() {
- return Executors.newCachedThreadPool();
- }
-
- @Override
- public RequestMappingHandlerMapping requestMappingHandlerMapping() {
- RequestMappingHandlerMapping mapping = super.requestMappingHandlerMapping();
- mapping.setUseSuffixPatternMatch(false);
- return mapping;
- }
-
- @Override
- protected void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.setOrder(0);
- registry.addResourceHandler("/scripts.js").addResourceLocations("/");
- registry.addResourceHandler("/style.css").addResourceLocations("/");
- }
-
- @Override
- protected void configureMessageConverters(List<HttpMessageConverter<?>> 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-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java
index 0768f834..baedfa29 100644
--- a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java
+++ b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostInitializer.java
@@ -10,14 +10,15 @@ import javax.servlet.Filter;
* Created by vt on 09/02/16.
*/
public class CrosspostInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
+
@Override
protected Class<?>[] getRootConfigClasses() {
- return new Class<?>[]{DataConfiguration.class};
+ return new Class<?>[]{CrosspostAppConfiguration.class, DataConfiguration.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
- return new Class<?>[]{CrosspostConfiguration.class};
+ return new Class<?>[]{CrosspostMvcConfiguration.class};
}
@Override
@@ -32,4 +33,9 @@ public class CrosspostInitializer extends AbstractAnnotationConfigDispatcherServ
return new Filter[]{characterEncodingFilter};
}
+
+ @Override
+ protected String getServletName() {
+ return "Crosspost dispatcher servlet";
+ }
}
diff --git a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostMvcConfiguration.java b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostMvcConfiguration.java
new file mode 100644
index 00000000..619c3d78
--- /dev/null
+++ b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostMvcConfiguration.java
@@ -0,0 +1,43 @@
+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 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
+
+import java.util.List;
+
+/**
+ * Created by vitalyster on 28.06.2016.
+ */
+@Configuration
+@ComponentScan(basePackages = {"com.juick.components.controllers"})
+public class CrosspostMvcConfiguration extends WebMvcConfigurationSupport {
+
+ @Override
+ public RequestMappingHandlerMapping requestMappingHandlerMapping() {
+ RequestMappingHandlerMapping mapping = super.requestMappingHandlerMapping();
+ mapping.setUseSuffixPatternMatch(false);
+ return mapping;
+ }
+
+ @Override
+ protected void configureMessageConverters(List<HttpMessageConverter<?>> 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);
+ }
+}