From b75b5a3d1a3bf001923d49923dcebee95ba2327e Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 29 Jun 2017 17:09:49 +0300 Subject: notifications and configurations refactoring --- .../api/configuration/ApiAppConfiguration.java | 2 + .../juick/api/configuration/ApiInitializer.java | 3 +- .../api/configuration/ApiMvcConfiguration.java | 51 ---------- .../com/juick/api/controllers/Notifications.java | 112 ++++++++++----------- 4 files changed, 55 insertions(+), 113 deletions(-) delete mode 100644 juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java (limited to 'juick-api/src/main/java/com/juick/api') 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> 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> doGet( + public ResponseEntity> 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 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 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 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 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; } -- cgit v1.2.3