diff options
author | Vitaly Takmazov | 2018-03-16 10:46:23 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-03-16 10:46:23 +0300 |
commit | 9ef168a03b75aeca0c2f7dda9ce87d4014c703a9 (patch) | |
tree | 8ace0ada0f576d97592f8bc84803a2dcf3e7a244 /juick-common/src/main/java/com/juick/server/configuration | |
parent | 7cadae166b103182d7d1daaafe602cd8fb145c53 (diff) |
merge common projects
Diffstat (limited to 'juick-common/src/main/java/com/juick/server/configuration')
3 files changed, 200 insertions, 0 deletions
diff --git a/juick-common/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java b/juick-common/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java new file mode 100644 index 00000000..d9b842af --- /dev/null +++ b/juick-common/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2008-2017, Juick + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package com.juick.server.configuration; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.juick.server.xmpp.JidConverter; +import com.juick.server.xmpp.s2s.BasicXmppSession; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.convert.ConversionService; +import org.springframework.format.support.DefaultFormattingConversionService; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.scheduling.annotation.SchedulingConfigurer; +import org.springframework.scheduling.config.ScheduledTaskRegistrar; +import org.springframework.web.multipart.MultipartResolver; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; +import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import rocks.xmpp.core.session.Extension; +import rocks.xmpp.core.session.XmppSessionConfiguration; +import rocks.xmpp.core.session.debug.LogbackDebugger; + +import java.util.List; +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * Created by vitalyster on 28.06.2016. + */ +@Configuration +public class BaseWebConfiguration implements WebMvcConfigurer, SchedulingConfigurer { + + + @Override + public void configurePathMatch(PathMatchConfigurer configurer) { + configurer.setUseSuffixPatternMatch(false); + } + + @Override + public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { + MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(jsonMapper()); + converters.add(converter); + } + + @Bean + public ObjectMapper jsonMapper() { + ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + return mapper; + } + + @Bean + public MultipartResolver multipartResolver() { + CommonsMultipartResolver resolver = new CommonsMultipartResolver(); + resolver.setMaxUploadSize(10000000); + return resolver; + } + + @Override + public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { + taskRegistrar.setScheduler(taskExecutor()); + } + + @Bean(destroyMethod="shutdown") + public Executor taskExecutor() { + return Executors.newScheduledThreadPool(100); + } + @Value("${hostname:localhost}") + private String hostname; + + @Bean + public ExecutorService service() { + return Executors.newCachedThreadPool(); + } + @Bean + public BasicXmppSession session() { + XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() + .extensions(Extension.of(com.juick.Message.class)) + .debugger(LogbackDebugger.class) + .build(); + return BasicXmppSession.create(hostname, configuration); + } + @Bean + public static ConversionService conversionService() { + DefaultFormattingConversionService cs = new DefaultFormattingConversionService(); + cs.addConverter(new JidConverter()); + return cs; + } +} diff --git a/juick-common/src/main/java/com/juick/server/configuration/JuickServerWebsocketConfiguration.java b/juick-common/src/main/java/com/juick/server/configuration/JuickServerWebsocketConfiguration.java new file mode 100644 index 00000000..18501eaf --- /dev/null +++ b/juick-common/src/main/java/com/juick/server/configuration/JuickServerWebsocketConfiguration.java @@ -0,0 +1,72 @@ +package com.juick.server.configuration; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.juick.server.component.JuickServerComponent; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.InterceptingClientHttpRequestFactory; +import org.springframework.http.client.support.BasicAuthorizationInterceptor; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.socket.client.WebSocketConnectionManager; +import org.springframework.web.socket.client.standard.StandardWebSocketClient; +import org.springframework.web.util.UriComponentsBuilder; + +import javax.inject.Inject; +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +@Lazy +@Configuration +@EnableScheduling +public class JuickServerWebsocketConfiguration { + private static final Logger logger = LoggerFactory.getLogger(JuickServerWebsocketConfiguration.class); + @Value("${websocket_url:ws://localhost:8080/ws/}") + private String baseUri; + @Value("${api_user:juick}") + private String serviceUser; + @Value("${api_password:secret}") + private String servicePassword; + @Inject + ObjectMapper jsonMapper; + @Inject + private JuickServerComponent juickServerComponent; + @Bean + public RestTemplate rest() { + RestTemplate rest = new RestTemplate(); + List<ClientHttpRequestInterceptor> interceptors = Collections.singletonList( + new BasicAuthorizationInterceptor(serviceUser, servicePassword)); + + rest.setRequestFactory(new InterceptingClientHttpRequestFactory(rest.getRequestFactory(), interceptors)); + return rest; + } + @Bean + public WebSocketConnectionManager connectionManager() { + String hash = StringUtils.EMPTY; + try { + ResponseEntity<String> response = rest().exchange("https://api.juick.com/auth", + HttpMethod.GET, null, String.class); + hash = jsonMapper.readValue(response.getBody(), String.class); + } catch (HttpClientErrorException | IOException e) { + logger.warn("service component is not authenticated", e); + } + String websocketURI = UriComponentsBuilder.fromUriString(baseUri) + .queryParam("hash", hash).build().toUriString(); + WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), juickServerComponent, websocketURI); + return manager; + } + @Bean + public StandardWebSocketClient client() { + return new StandardWebSocketClient(); + } +} diff --git a/juick-common/src/main/java/com/juick/server/configuration/StorageConfiguration.java b/juick-common/src/main/java/com/juick/server/configuration/StorageConfiguration.java new file mode 100644 index 00000000..94b23037 --- /dev/null +++ b/juick-common/src/main/java/com/juick/server/configuration/StorageConfiguration.java @@ -0,0 +1,14 @@ +package com.juick.server.configuration; + +import com.juick.service.ImagesService; +import com.juick.service.ImagesServiceImpl; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class StorageConfiguration { + @Bean + public ImagesService imagesService() { + return new ImagesServiceImpl(); + } +} |