From 3382ed01dde0adebb4ef834a0dfd0baac392c040 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 28 Mar 2017 23:52:19 +0300 Subject: juick-api: inject properties with @Value --- .../src/main/java/com/juick/api/ApiServer.java | 24 ++++++++++------------ .../api/configuration/ApiAppConfiguration.java | 20 +++++------------- .../juick/api/configuration/ApiInitializer.java | 5 ++++- .../juick/api/configuration/ApiSecurityConfig.java | 9 ++++---- .../api/configuration/MessengerConfiguration.java | 22 ++++++++++++++++++++ .../api/configuration/TelegramConfiguration.java | 19 +++++++++++++++++ 6 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 juick-api/src/main/java/com/juick/api/configuration/MessengerConfiguration.java create mode 100644 juick-api/src/main/java/com/juick/api/configuration/TelegramConfiguration.java (limited to 'juick-api/src/main/java/com') diff --git a/juick-api/src/main/java/com/juick/api/ApiServer.java b/juick-api/src/main/java/com/juick/api/ApiServer.java index 4d260df8..7f4042af 100644 --- a/juick-api/src/main/java/com/juick/api/ApiServer.java +++ b/juick-api/src/main/java/com/juick/api/ApiServer.java @@ -21,6 +21,7 @@ import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import rocks.xmpp.core.XmppException; import rocks.xmpp.core.session.Extension; @@ -37,24 +38,21 @@ public class ApiServer implements AutoCloseable { private ExternalComponent xmpp; + @Value("${upload_tmp_dir:'/var/www/juick.com/i/tmp/'}") public String tmpDir; + @Value("${img_path:'/var/www/juick.com/i/'}") public String imgDir; - - private String xmppHost, xmppPassword, xmppJid; + @Value("${xmpp_host:'localhost'}") + private String xmppHost; + @Value("${xmpp_password:'secret'}") + private String xmppPassword; + @Value("${xmpp_jid:'api.localhost'}") + private String xmppJid; + @Value("${xmpp_port:5347}") private int xmppPort; + @Value("${xmpp_disabled:false}") private boolean isXmppDisabled; - - public ApiServer(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", "api.localhost"); - xmppPassword = conf.getProperty("xmpp_password"); - } - @PostConstruct public void init() { if (!isXmppDisabled) { diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java b/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java index 382b3df5..e987893d 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java @@ -3,7 +3,9 @@ package com.juick.api.configuration; import com.juick.api.ApiServer; import com.juick.api.MessengerBot; import com.juick.api.TGBot; +import com.pengrad.telegrambot.TelegramBot; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @@ -22,30 +24,18 @@ import javax.inject.Inject; @PropertySource("classpath:juick.conf") public class ApiAppConfiguration { @Inject - private Environment env; + TGBot tgBot; @Bean public ApiServer apiServer() { - return new ApiServer(env); - } - - @Bean - public TGBot tgBot() { - return new TGBot(env.getProperty("telegram_token", StringUtils.EMPTY)); - } - - @Bean - public MessengerBot fbBot() { - MessengerBot bot = new MessengerBot(); - bot.setVerifyToken(env.getProperty("fb_verify_token", StringUtils.EMPTY)); - return bot; + return new ApiServer(); } private static final String WS_URI = "wss://ws.juick.com/"; @Bean public WebSocketConnectionManager connectionManager() { - WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), tgBot(), WS_URI); + WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), tgBot, WS_URI); manager.setAutoStartup(true); return manager; } diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java b/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java index 41fe8b2d..bc97d587 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java @@ -14,7 +14,10 @@ public class ApiInitializer extends AbstractAnnotationConfigDispatcherServletIni @Override protected Class[] getRootConfigClasses() { - return new Class[]{ApiAppConfiguration.class, ApiSecurityConfig.class, DataConfiguration.class}; + return new Class[]{ + ApiAppConfiguration.class, ApiSecurityConfig.class, DataConfiguration.class, + TelegramConfiguration.class, MessengerConfiguration.class + }; } @Override diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java b/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java index cb50ddab..f1dcf30a 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java @@ -4,6 +4,7 @@ import com.juick.server.security.NotAuthorizedAuthenticationEntryPoint; import com.juick.service.UserService; import com.juick.service.security.JuickUserDetailsService; import com.juick.service.security.deprecated.RequestParamHashRememberMeServices; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @@ -31,8 +32,8 @@ import java.util.concurrent.TimeUnit; @EnableWebSecurity @PropertySource("classpath:juick.conf") public class ApiSecurityConfig extends WebSecurityConfigurerAdapter { - @Inject - private Environment env; + @Value("${auth_remember_me_key}") + private String rememberMeKey; @Inject private UserService userService; @@ -58,7 +59,7 @@ public class ApiSecurityConfig extends WebSecurityConfigurerAdapter { .alwaysRemember(true) .tokenValiditySeconds((int) TimeUnit.DAYS.toSeconds(6 * 30)) .rememberMeServices(rememberMeServices()) - .key(env.getProperty("auth_remember_me_key")) + .key(rememberMeKey) .and().authenticationProvider(authenticationProvider()) .headers().defaultsDisabled().cacheControl(); } @@ -79,7 +80,7 @@ public class ApiSecurityConfig extends WebSecurityConfigurerAdapter { @Bean public RememberMeServices rememberMeServices() throws Exception { - return new RequestParamHashRememberMeServices(env.getProperty("auth_remember_me_key"), userService); + return new RequestParamHashRememberMeServices(rememberMeKey, userService); } @Bean diff --git a/juick-api/src/main/java/com/juick/api/configuration/MessengerConfiguration.java b/juick-api/src/main/java/com/juick/api/configuration/MessengerConfiguration.java new file mode 100644 index 00000000..c3c8f3e6 --- /dev/null +++ b/juick-api/src/main/java/com/juick/api/configuration/MessengerConfiguration.java @@ -0,0 +1,22 @@ +package com.juick.api.configuration; + +import com.juick.api.MessengerBot; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Created by vital on 28.03.2017. + */ +@Configuration +public class MessengerConfiguration { + @Value("${fb_verify_token}") + private String messengerBotVerifyToken; + + @Bean + public MessengerBot fbBot() { + MessengerBot bot = new MessengerBot(); + bot.setVerifyToken(messengerBotVerifyToken); + return bot; + } +} diff --git a/juick-api/src/main/java/com/juick/api/configuration/TelegramConfiguration.java b/juick-api/src/main/java/com/juick/api/configuration/TelegramConfiguration.java new file mode 100644 index 00000000..03dec4e9 --- /dev/null +++ b/juick-api/src/main/java/com/juick/api/configuration/TelegramConfiguration.java @@ -0,0 +1,19 @@ +package com.juick.api.configuration; + +import com.juick.api.TGBot; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Created by vital on 28.03.2017. + */ +@Configuration +public class TelegramConfiguration { + @Value("${telegram_token}") + private String telegramToken; + @Bean + public TGBot tgBot() { + return new TGBot(telegramToken); + } +} -- cgit v1.2.3