aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-06-29 17:09:49 +0300
committerGravatar Vitaly Takmazov2017-06-29 17:13:29 +0300
commitb75b5a3d1a3bf001923d49923dcebee95ba2327e (patch)
tree5080eacdf348c9598885ad5d3b5c567632db3717 /juick-api/src
parent02723131139806c761539a42a5fa80b68ecadee8 (diff)
notifications and configurations refactoring
Diffstat (limited to 'juick-api/src')
-rw-r--r--juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java2
-rw-r--r--juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java3
-rw-r--r--juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java51
-rw-r--r--juick-api/src/main/java/com/juick/api/controllers/Notifications.java112
-rw-r--r--juick-api/src/test/java/com/juick/api/tests/MessagesTests.java6
5 files changed, 59 insertions, 115 deletions
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 cbf7d05c..7ef1cf45 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,6 +3,7 @@ package com.juick.api.configuration;
import com.juick.api.ApiServer;
import com.juick.api.TGBot;
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.scheduling.annotation.EnableAsync;
@@ -19,6 +20,7 @@ import javax.inject.Inject;
@EnableScheduling
@EnableAsync
@PropertySource("classpath:juick.conf")
+@ComponentScan(basePackages = "com.juick.api.controllers")
public class ApiAppConfiguration {
@Inject
TGBot tgBot;
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 965fd32b..d01df787 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
@@ -1,6 +1,7 @@
package com.juick.api.configuration;
import com.juick.configuration.DataConfiguration;
+import com.juick.server.configuration.BaseWebConfiguration;
import org.apache.commons.codec.CharEncoding;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@@ -22,7 +23,7 @@ public class ApiInitializer extends AbstractAnnotationConfigDispatcherServletIni
@Override
protected Class<?>[] getServletConfigClasses() {
- return new Class<?>[]{ApiMvcConfiguration.class};
+ return new Class<?>[]{BaseWebConfiguration.class};
}
@Override
diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java b/juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java
deleted file mode 100644
index 37c07434..00000000
--- a/juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java
+++ /dev/null
@@ -1,51 +0,0 @@
-package com.juick.api.configuration;
-
-import com.fasterxml.jackson.annotation.JsonInclude;
-import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
-import org.springframework.context.annotation.Bean;
-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.multipart.MultipartResolver;
-import org.springframework.web.multipart.commons.CommonsMultipartResolver;
-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.api.controllers"})
-public class ApiMvcConfiguration 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);
- }
-
- @Bean
- public MultipartResolver multipartResolver() {
- CommonsMultipartResolver resolver = new CommonsMultipartResolver();
- resolver.setMaxUploadSize(10000000);
- return resolver;
- }
-}
diff --git a/juick-api/src/main/java/com/juick/api/controllers/Notifications.java b/juick-api/src/main/java/com/juick/api/controllers/Notifications.java
index c121efb6..96edde51 100644
--- a/juick-api/src/main/java/com/juick/api/controllers/Notifications.java
+++ b/juick-api/src/main/java/com/juick/api/controllers/Notifications.java
@@ -2,14 +2,16 @@ package com.juick.api.controllers;
import com.juick.Message;
import com.juick.Status;
-import com.juick.TokensList;
+import com.juick.DeviceRegistration;
import com.juick.User;
+import com.juick.server.helpers.AnonymousUser;
import com.juick.server.util.HttpBadRequestException;
import com.juick.server.util.HttpForbiddenException;
import com.juick.service.MessagesService;
import com.juick.service.PushQueriesService;
import com.juick.service.SubscriptionService;
import com.juick.server.util.UserUtils;
+import com.juick.service.UserService;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@@ -17,7 +19,7 @@ import org.springframework.web.bind.annotation.*;
import javax.inject.Inject;
import java.io.IOException;
import java.security.Principal;
-import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@@ -33,9 +35,20 @@ public class Notifications {
MessagesService messagesService;
@Inject
SubscriptionService subscriptionService;
+ @Inject
+ UserService userService;
+
+
+ private User collectTokens(Integer uid) {
+ User user = userService.getUserByUID(uid).orElse(AnonymousUser.INSTANCE);
+ pushQueriesService.getGCMRegID(uid).forEach(t -> user.getDevices().add(new DeviceRegistration("gcm", t)));
+ pushQueriesService.getAPNSToken(uid).forEach(t -> user.getDevices().add(new DeviceRegistration("apns", t)));
+ pushQueriesService.getMPNSURL(uid).forEach(t -> user.getDevices().add(new DeviceRegistration("mpns", t)));
+ return user;
+ }
@RequestMapping(value = "/notifications", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
- public ResponseEntity<List<TokensList>> doGet(
+ public ResponseEntity<List<User>> doGet(
@RequestParam(required = false, defaultValue = "0") int uid,
@RequestParam(required = false, defaultValue = "0") int mid,
@RequestParam(required = false, defaultValue = "0") int rid) {
@@ -43,23 +56,9 @@ public class Notifications {
if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) {
throw new HttpForbiddenException();
}
- List<TokensList> tokensLists = new ArrayList<>();
if (uid > 0 && mid == 0) {
// PM
- TokensList gcmTokens = new TokensList();
- gcmTokens.setType("gcm");
- gcmTokens.setTokens(pushQueriesService.getGCMRegID(uid));
- tokensLists.add(gcmTokens);
- TokensList apnsTokens = new TokensList();
- apnsTokens.setType("apns");
- apnsTokens.setTokens(pushQueriesService.getAPNSToken(uid));
- tokensLists.add(apnsTokens);
- TokensList mpnsTokens = new TokensList();
- mpnsTokens.setType("mpns");
- mpnsTokens.setTokens(pushQueriesService.getMPNSURL(uid));
- tokensLists.add(mpnsTokens);
- return ResponseEntity.ok(tokensLists);
-
+ return ResponseEntity.ok(Collections.singletonList(collectTokens(uid)));
} else {
if (mid > 0) {
Message msg = messagesService.getMessage(mid);
@@ -72,21 +71,8 @@ public class Notifications {
users = subscriptionService.getSubscribedUsers(msg.getUser().getUid(), mid);
}
- List<Integer> uids = users.stream().map(User::getUid).collect(Collectors.toList());
-
- TokensList gcmTokens = new TokensList();
- gcmTokens.setType("gcm");
- gcmTokens.setTokens(pushQueriesService.getGCMTokens(uids));
- tokensLists.add(gcmTokens);
- TokensList apnsTokens = new TokensList();
- apnsTokens.setType("apns");
- apnsTokens.setTokens(pushQueriesService.getAPNSTokens(uids));
- tokensLists.add(apnsTokens);
- TokensList mpnsTokens = new TokensList();
- mpnsTokens.setType("mpns");
- mpnsTokens.setTokens(pushQueriesService.getMPNSTokens(uids));
- tokensLists.add(mpnsTokens);
- return ResponseEntity.ok(tokensLists);
+ return ResponseEntity.ok(users.stream().map(User::getUid)
+ .map(this::collectTokens).collect(Collectors.toList()));
}
}
}
@@ -95,49 +81,53 @@ public class Notifications {
@RequestMapping(value = "/notifications", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Status doDelete(
- @RequestBody TokensList list) throws IOException {
+ @RequestBody List<DeviceRegistration> list) throws IOException {
User visitor = UserUtils.getCurrentUser();
// FIXME: it is possible to delete other user's tokens
if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) {
throw new HttpForbiddenException();
}
- switch (list.getType()) {
- case "gcm":
- list.getTokens().forEach(t -> pushQueriesService.deleteGCMToken(t));
- break;
- case "apns":
- list.getTokens().forEach(t -> pushQueriesService.deleteAPNSToken(t));
- break;
- case "mpns":
- list.getTokens().forEach(t -> pushQueriesService.deleteMPNSToken(t));
- break;
- default:
- throw new HttpBadRequestException();
- }
+ list.forEach(t -> {
+ switch (t.getType()) {
+ case "gcm":
+ pushQueriesService.deleteGCMToken(t.getToken());
+ break;
+ case "apns":
+ pushQueriesService.deleteAPNSToken(t.getToken());
+ break;
+ case "mpns":
+ pushQueriesService.deleteMPNSToken(t.getToken());
+ break;
+ default:
+ throw new HttpBadRequestException();
+ }
+ });
return Status.OK;
}
@RequestMapping(value = "/notifications", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Status doPut(
- @RequestBody TokensList list) throws IOException {
+ @RequestBody List<DeviceRegistration> list) throws IOException {
User visitor = UserUtils.getCurrentUser();
if (visitor.getUid() == 0) {
throw new HttpForbiddenException();
}
- switch (list.getType()) {
- case "gcm":
- list.getTokens().forEach(t -> pushQueriesService.addGCMToken(visitor.getUid(), t));
- break;
- case "apns":
- list.getTokens().forEach(t -> pushQueriesService.addAPNSToken(visitor.getUid(), t));
- break;
- case "mpns":
- list.getTokens().forEach(t -> pushQueriesService.addMPNSToken(visitor.getUid(), t));
- break;
- default:
- throw new HttpBadRequestException();
- }
+ list.forEach(t -> {
+ switch (t.getType()) {
+ case "gcm":
+ pushQueriesService.addGCMToken(visitor.getUid(), t.getToken());
+ break;
+ case "apns":
+ pushQueriesService.addAPNSToken(visitor.getUid(), t.getToken());
+ break;
+ case "mpns":
+ pushQueriesService.addMPNSToken(visitor.getUid(), t.getToken());
+ break;
+ default:
+ throw new HttpBadRequestException();
+ }
+ });
return Status.OK;
}
diff --git a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java
index a9705aee..e8fcc86f 100644
--- a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java
+++ b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java
@@ -5,7 +5,7 @@ import com.juick.Tag;
import com.juick.User;
import com.juick.api.ApiServer;
import com.juick.api.TGBot;
-import com.juick.api.configuration.ApiMvcConfiguration;
+import com.juick.server.configuration.BaseWebConfiguration;
import com.juick.api.configuration.ApiSecurityConfig;
import com.juick.api.configuration.MessengerConfiguration;
import com.juick.api.configuration.TelegramConfiguration;
@@ -20,6 +20,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
@@ -50,7 +51,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@WebAppConfiguration
public class MessagesTests {
@Configuration
- @Import(value = {ApiMvcConfiguration.class, ApiSecurityConfig.class,
+ @ComponentScan(basePackages = "com.juick.api.controllers")
+ @Import(value = {BaseWebConfiguration.class, ApiSecurityConfig.class,
MockDataConfiguration.class, MessengerConfiguration.class, TelegramConfiguration.class})
static class Config {
@Bean