Rework push server so it holds the subscriptions in a DB
This commit is contained in:
@@ -5,6 +5,7 @@ import org.springframework.http.ResponseEntity;
|
||||
|
||||
public enum ResponseReason {
|
||||
OK(HttpStatus.OK),
|
||||
EMPTY_SCOPE(HttpStatus.BAD_REQUEST),
|
||||
UNKNOWN_ERROR(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
|
||||
private final HttpStatus httpStatus;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package de.pushservice.client.controller;
|
||||
|
||||
import de.pushservice.client.ResponseReason;
|
||||
import de.pushservice.client.dto.SubscriptionDto;
|
||||
import de.pushservice.client.service.SubscriptionService;
|
||||
import org.slf4j.Logger;
|
||||
@@ -22,8 +21,6 @@ public class SubscriptionController {
|
||||
public ResponseEntity subscribe(@RequestBody SubscriptionDto subscriptionDto) {
|
||||
LOGGER.debug(String.format("Received subscription for endpoint %s", subscriptionDto.getEndpoint()));
|
||||
|
||||
this.subscriptionService.subscribe(subscriptionDto);
|
||||
|
||||
return ResponseReason.OK.toResponseEntity();
|
||||
return this.subscriptionService.subscribe(subscriptionDto).toResponseEntity();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
package de.pushservice.client.dto;
|
||||
|
||||
public class NotificationRequestDto {
|
||||
private SubscriptionDto subscriptionDto;
|
||||
private String scope;
|
||||
private MessageDto messageDto;
|
||||
|
||||
public NotificationRequestDto(SubscriptionDto subscriptionDto, MessageDto messageDto) {
|
||||
this.subscriptionDto = subscriptionDto;
|
||||
public NotificationRequestDto(String scope, MessageDto messageDto) {
|
||||
this.scope = scope;
|
||||
this.messageDto = messageDto;
|
||||
}
|
||||
|
||||
public SubscriptionDto getSubscriptionDto() {
|
||||
return subscriptionDto;
|
||||
public String getScope() {
|
||||
return scope;
|
||||
}
|
||||
|
||||
public void setSubscriptionDto(SubscriptionDto subscriptionDto) {
|
||||
this.subscriptionDto = subscriptionDto;
|
||||
public void setScope(String scope) {
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
public MessageDto getMessageDto() {
|
||||
|
||||
@@ -4,6 +4,7 @@ public class SubscriptionDto {
|
||||
private String auth;
|
||||
private String endpoint;
|
||||
private String key;
|
||||
private String scope;
|
||||
|
||||
public void setAuth(String auth) {
|
||||
this.auth = auth;
|
||||
@@ -28,4 +29,12 @@ public class SubscriptionDto {
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public String getScope() {
|
||||
return scope;
|
||||
}
|
||||
|
||||
public void setScope(String scope) {
|
||||
this.scope = scope;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import de.pushservice.client.dto.NotificationRequestDto;
|
||||
import de.pushservice.client.dto.PayloadDto;
|
||||
import de.pushservice.client.dto.SubscriptionDto;
|
||||
import de.pushservice.client.model.Urgency;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -30,37 +31,41 @@ public class SubscriptionService {
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private Set<SubscriptionDto> subscriptions = new HashSet<>();
|
||||
private String scope;
|
||||
|
||||
public void subscribe(SubscriptionDto subscriptionDto) {
|
||||
this.subscriptions.add(subscriptionDto);
|
||||
public boolean isInitialized() {
|
||||
return StringUtils.isNotEmpty(this.scope);
|
||||
}
|
||||
|
||||
public int notifyAll(PayloadDto payload, String topic, Urgency urgency) throws JsonProcessingException {
|
||||
int notificationsSend = 0;
|
||||
final Urgency tmpUrgency = Optional.ofNullable(urgency).orElse(Urgency.NORMAL);
|
||||
|
||||
final MessageDto messageDto = new MessageDto(objectMapper.writeValueAsString(payload), topic, tmpUrgency);
|
||||
|
||||
for (SubscriptionDto subscription : this.subscriptions) {
|
||||
final NotificationRequestDto requestDto = new NotificationRequestDto(subscription, messageDto);
|
||||
|
||||
LOGGER.debug(String.format("Sending notification for endpoint %s", subscription.getEndpoint()));
|
||||
|
||||
final ResponseEntity<String> responseEntity = new RestTemplate()
|
||||
.exchange(this.config.getServerUrl(), HttpMethod.POST, new HttpEntity<>(requestDto), String.class);
|
||||
|
||||
final ResponseReason responseReason = ResponseReason.fromResponseEntity(responseEntity);
|
||||
|
||||
if (ResponseReason.OK == responseReason) {
|
||||
notificationsSend++;
|
||||
}
|
||||
else {
|
||||
// Well, nothing we can do about it
|
||||
LOGGER.error("Sending notification to endpoint failed! %s", responseReason);
|
||||
public ResponseReason subscribe(SubscriptionDto subscriptionDto) {
|
||||
if (StringUtils.isEmpty(this.scope)) {
|
||||
this.scope = subscriptionDto.getScope();
|
||||
}
|
||||
else {
|
||||
if(!this.scope.equals(subscriptionDto.getScope())) {
|
||||
// Should not happen since the scope is the host + context path of the hosting app
|
||||
LOGGER.warn(String.format("Scope changed! Old: %s, new %s", this.scope, subscriptionDto.getScope()));
|
||||
}
|
||||
}
|
||||
|
||||
return notificationsSend;
|
||||
final ResponseEntity<String> responseEntity = new RestTemplate()
|
||||
.exchange(this.config.getServerUrl() + "subscribe", HttpMethod.POST, new HttpEntity<>(subscriptionDto), String.class);
|
||||
|
||||
return ResponseReason.fromResponseEntity(responseEntity);
|
||||
}
|
||||
|
||||
public void notify(PayloadDto payload, String topic, Urgency urgency) throws JsonProcessingException {
|
||||
final Urgency tmpUrgency = Optional.ofNullable(urgency).orElse(Urgency.NORMAL);
|
||||
final MessageDto messageDto = new MessageDto(objectMapper.writeValueAsString(payload), topic, tmpUrgency);
|
||||
final NotificationRequestDto requestDto = new NotificationRequestDto(this.scope, messageDto);
|
||||
|
||||
final ResponseEntity<String> responseEntity = new RestTemplate()
|
||||
.exchange(this.config.getServerUrl() + "notify", HttpMethod.POST, new HttpEntity<>(requestDto), String.class);
|
||||
|
||||
final ResponseReason responseReason = ResponseReason.fromResponseEntity(responseEntity);
|
||||
|
||||
if (ResponseReason.OK != responseReason) {
|
||||
LOGGER.error(String.format("Could not send notification! %s", responseReason));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user