Stuff all over the place
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package de.pushservice.client;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
|
||||
/**
|
||||
* Not used but required by the spring boot maven plugin during repackage
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class PushServiceClientApplication extends SpringBootServletInitializer {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PushServiceClientApplication.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||
return application.sources(PushServiceClientApplication.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package de.pushservice.client.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "push-service-client")
|
||||
public class PushServiceClientConfig {
|
||||
private String serverUrl;
|
||||
|
||||
public String getServerUrl() {
|
||||
return serverUrl;
|
||||
}
|
||||
|
||||
public void setServerUrl(String serverUrl) {
|
||||
this.serverUrl = serverUrl;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
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;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
@Controller
|
||||
public class SubscriptionController {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SubscriptionController.class);
|
||||
|
||||
@Autowired
|
||||
private SubscriptionService subscriptionService;
|
||||
|
||||
@PostMapping("/subscription/subscribe")
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,12 @@ public class MessageDto {
|
||||
private String topic;
|
||||
private Urgency urgency;
|
||||
|
||||
public MessageDto(String payload, String topic, Urgency urgency) {
|
||||
this.payload = payload;
|
||||
this.topic = topic;
|
||||
this.urgency = urgency;
|
||||
}
|
||||
|
||||
public String getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,11 @@ public class NotificationRequestDto {
|
||||
private SubscriptionDto subscriptionDto;
|
||||
private MessageDto messageDto;
|
||||
|
||||
public NotificationRequestDto(SubscriptionDto subscriptionDto, MessageDto messageDto) {
|
||||
this.subscriptionDto = subscriptionDto;
|
||||
this.messageDto = messageDto;
|
||||
}
|
||||
|
||||
public SubscriptionDto getSubscriptionDto() {
|
||||
return subscriptionDto;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package de.pushservice.client.dto;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Payload for a notification. Gets sent to the third party push service which forwards it to the user agent.
|
||||
* </p>
|
||||
* <p>
|
||||
* Evaluated after receival in the <code>push-service-client-service-worker.js</code> service worker.
|
||||
* </p>
|
||||
*
|
||||
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification">MDN spec for notification payload</a>
|
||||
*/
|
||||
public class PayloadDto {
|
||||
private String title;
|
||||
private String message;
|
||||
private String icon;
|
||||
private int[] vibration;
|
||||
private long timestamp;
|
||||
|
||||
public PayloadDto(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getIcon() {
|
||||
return icon;
|
||||
}
|
||||
|
||||
public void setIcon(String icon) {
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
public int[] getVibration() {
|
||||
return vibration;
|
||||
}
|
||||
|
||||
public void setVibration(int[] vibration) {
|
||||
this.vibration = vibration;
|
||||
}
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(long timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package de.pushservice.client.service;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.pushservice.client.ResponseReason;
|
||||
import de.pushservice.client.config.PushServiceClientConfig;
|
||||
import de.pushservice.client.dto.MessageDto;
|
||||
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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class SubscriptionService {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SubscriptionService.class);
|
||||
|
||||
@Autowired
|
||||
private PushServiceClientConfig config;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private Set<SubscriptionDto> subscriptions = new HashSet<>();
|
||||
|
||||
public void subscribe(SubscriptionDto subscriptionDto) {
|
||||
this.subscriptions.add(subscriptionDto);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return notificationsSend;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user