Commit 378348b9 by Johannes Edmeier

Remove class level @RequestMapping

... to prevent registration of the controller from standard spring mechanisms
parent 8a497e65
...@@ -26,9 +26,10 @@ import java.util.Collections; ...@@ -26,9 +26,10 @@ import java.util.Collections;
import java.util.Map; import java.util.Map;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.util.MimeTypeUtils; import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
...@@ -41,7 +42,6 @@ import static org.springframework.util.StringUtils.hasText; ...@@ -41,7 +42,6 @@ import static org.springframework.util.StringUtils.hasText;
*/ */
@AdminController @AdminController
@ResponseBody @ResponseBody
@RequestMapping("/notifications/filters")
public class NotificationFilterController { public class NotificationFilterController {
private FilteringNotifier filteringNotifier; private FilteringNotifier filteringNotifier;
...@@ -49,12 +49,12 @@ public class NotificationFilterController { ...@@ -49,12 +49,12 @@ public class NotificationFilterController {
this.filteringNotifier = filteringNotifier; this.filteringNotifier = filteringNotifier;
} }
@RequestMapping(method = {RequestMethod.GET}, produces = MimeTypeUtils.APPLICATION_JSON_VALUE) @GetMapping(path = "/notifications/filters", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
public Map<String, NotificationFilter> getFilters() { public Map<String, NotificationFilter> getFilters() {
return filteringNotifier.getNotificationFilters(); return filteringNotifier.getNotificationFilters();
} }
@RequestMapping(method = {RequestMethod.POST}, produces = MimeTypeUtils.APPLICATION_JSON_VALUE) @PostMapping(path = "/notifications/filters", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
public ResponseEntity<?> addFilter(@RequestParam(name = "id", required = false) String id, public ResponseEntity<?> addFilter(@RequestParam(name = "id", required = false) String id,
@RequestParam(name = "name", required = false) String name, @RequestParam(name = "name", required = false) String name,
@RequestParam(name = "ttl", required = false, defaultValue = "-1") long ttl) { @RequestParam(name = "ttl", required = false, defaultValue = "-1") long ttl) {
...@@ -67,7 +67,7 @@ public class NotificationFilterController { ...@@ -67,7 +67,7 @@ public class NotificationFilterController {
} }
} }
@RequestMapping(path = "/{id}", method = {RequestMethod.DELETE}) @DeleteMapping(path = "/notifications/filters/{id}")
public ResponseEntity<?> deleteFilter(@PathVariable("id") String id) { public ResponseEntity<?> deleteFilter(@PathVariable("id") String id) {
NotificationFilter deleted = filteringNotifier.removeFilter(id); NotificationFilter deleted = filteringNotifier.removeFilter(id);
if (deleted != null) { if (deleted != null) {
......
...@@ -37,7 +37,6 @@ import org.springframework.util.StringUtils; ...@@ -37,7 +37,6 @@ import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import static java.util.Comparator.comparing; import static java.util.Comparator.comparing;
...@@ -50,22 +49,21 @@ import static java.util.stream.Collectors.toMap; ...@@ -50,22 +49,21 @@ import static java.util.stream.Collectors.toMap;
*/ */
@AdminController @AdminController
@ResponseBody @ResponseBody
@RequestMapping("/applications")
public class ApplicationsController { public class ApplicationsController {
private static final Logger log = LoggerFactory.getLogger(ApplicationsController.class); private static final Logger log = LoggerFactory.getLogger(ApplicationsController.class);
private final InstanceRegistry registry; private final InstanceRegistry registry;
private final InstanceEventPublisher eventPublisher; private final InstanceEventPublisher eventPublisher;
private static final ServerSentEvent<Application> PING = ServerSentEvent.<Application>builder().comment("ping") private static final ServerSentEvent<Application> PING = ServerSentEvent.<Application>builder().comment("ping")
.build(); .build();
private static final Flux<ServerSentEvent<Application>> PING_FLUX = Flux.interval(Duration.ofSeconds(10L)) private static final Flux<ServerSentEvent<Application>> PING_FLUX = Flux.interval(Duration.ZERO,
.map(tick -> PING); Duration.ofSeconds(10L)).map(tick -> PING);
public ApplicationsController(InstanceRegistry registry, InstanceEventPublisher eventPublisher) { public ApplicationsController(InstanceRegistry registry, InstanceEventPublisher eventPublisher) {
this.registry = registry; this.registry = registry;
this.eventPublisher = eventPublisher; this.eventPublisher = eventPublisher;
} }
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(path = "/applications", produces = MediaType.APPLICATION_JSON_VALUE)
public Flux<Application> applications() { public Flux<Application> applications() {
return registry.getInstances() return registry.getInstances()
.filter(Instance::isRegistered) .filter(Instance::isRegistered)
...@@ -73,7 +71,7 @@ public class ApplicationsController { ...@@ -73,7 +71,7 @@ public class ApplicationsController {
.flatMap(grouped -> toApplication(grouped.key(), grouped)); .flatMap(grouped -> toApplication(grouped.key(), grouped));
} }
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE) @GetMapping(path = "/applications", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<Application>> applicationsStream() { public Flux<ServerSentEvent<Application>> applicationsStream() {
return Flux.from(eventPublisher) return Flux.from(eventPublisher)
.flatMap(event -> registry.getInstance(event.getInstance())) .flatMap(event -> registry.getInstance(event.getInstance()))
...@@ -83,7 +81,7 @@ public class ApplicationsController { ...@@ -83,7 +81,7 @@ public class ApplicationsController {
.mergeWith(PING_FLUX); .mergeWith(PING_FLUX);
} }
@DeleteMapping(path = "/{name}") @DeleteMapping(path = "/applications/{name}")
public Mono<ResponseEntity<Void>> unregister(@PathVariable("name") String name) { public Mono<ResponseEntity<Void>> unregister(@PathVariable("name") String name) {
log.debug("Unregister application with name '{}'", name); log.debug("Unregister application with name '{}'", name);
return registry.getInstancesByApplication(name) return registry.getInstancesByApplication(name)
......
...@@ -25,18 +25,19 @@ import reactor.core.publisher.Flux; ...@@ -25,18 +25,19 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.net.URI; import java.net.URI;
import java.time.Duration;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
...@@ -46,11 +47,14 @@ import org.springframework.web.util.UriComponentsBuilder; ...@@ -46,11 +47,14 @@ import org.springframework.web.util.UriComponentsBuilder;
*/ */
@AdminController @AdminController
@ResponseBody @ResponseBody
@RequestMapping("/instances")
public class InstancesController { public class InstancesController {
private static final Logger LOGGER = LoggerFactory.getLogger(InstancesController.class); private static final Logger LOGGER = LoggerFactory.getLogger(InstancesController.class);
private final InstanceRegistry registry; private final InstanceRegistry registry;
private final InstanceEventStore eventStore; private final InstanceEventStore eventStore;
private static final ServerSentEvent<InstanceEvent> PING = ServerSentEvent.<InstanceEvent>builder().comment("ping")
.build();
private static final Flux<ServerSentEvent<InstanceEvent>> PING_FLUX = Flux.interval(Duration.ZERO,
Duration.ofSeconds(10L)).map(tick -> PING);
public InstancesController(InstanceRegistry registry, InstanceEventStore eventStore) { public InstancesController(InstanceRegistry registry, InstanceEventStore eventStore) {
this.registry = registry; this.registry = registry;
...@@ -63,7 +67,7 @@ public class InstancesController { ...@@ -63,7 +67,7 @@ public class InstancesController {
* @param registration registration info * @param registration registration info
* @return The registered instance id; * @return The registered instance id;
*/ */
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) @PostMapping(path = "/instances", consumes = MediaType.APPLICATION_JSON_VALUE)
public Mono<ResponseEntity<Map<String, InstanceId>>> register(@RequestBody Registration registration, public Mono<ResponseEntity<Map<String, InstanceId>>> register(@RequestBody Registration registration,
UriComponentsBuilder builder) { UriComponentsBuilder builder) {
Registration withSource = Registration.copyOf(registration).source("http-api").build(); Registration withSource = Registration.copyOf(registration).source("http-api").build();
...@@ -80,7 +84,7 @@ public class InstancesController { ...@@ -80,7 +84,7 @@ public class InstancesController {
* @param name the name to search for * @param name the name to search for
* @return application list * @return application list
*/ */
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(path = "/instances", produces = MediaType.APPLICATION_JSON_VALUE)
public Flux<Instance> instances(@RequestParam(value = "name", required = false) String name) { public Flux<Instance> instances(@RequestParam(value = "name", required = false) String name) {
LOGGER.debug("Deliver registered instances with name={}", name); LOGGER.debug("Deliver registered instances with name={}", name);
if (name == null || name.isEmpty()) { if (name == null || name.isEmpty()) {
...@@ -96,7 +100,7 @@ public class InstancesController { ...@@ -96,7 +100,7 @@ public class InstancesController {
* @param id The application identifier. * @param id The application identifier.
* @return The registered application. * @return The registered application.
*/ */
@GetMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(path = "/instances/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<ResponseEntity<Instance>> instance(@PathVariable String id) { public Mono<ResponseEntity<Instance>> instance(@PathVariable String id) {
LOGGER.debug("Deliver registered instance with ID '{}'", id); LOGGER.debug("Deliver registered instance with ID '{}'", id);
return registry.getInstance(InstanceId.of(id)) return registry.getInstance(InstanceId.of(id))
...@@ -110,7 +114,7 @@ public class InstancesController { ...@@ -110,7 +114,7 @@ public class InstancesController {
* *
* @param id The instance id. * @param id The instance id.
*/ */
@DeleteMapping(path = "/{id}") @DeleteMapping(path = "/instances/{id}")
public Mono<ResponseEntity<Void>> unregister(@PathVariable String id) { public Mono<ResponseEntity<Void>> unregister(@PathVariable String id) {
LOGGER.debug("Unregister instance with ID '{}'", id); LOGGER.debug("Unregister instance with ID '{}'", id);
return registry.deregister(InstanceId.of(id)) return registry.deregister(InstanceId.of(id))
...@@ -118,14 +122,14 @@ public class InstancesController { ...@@ -118,14 +122,14 @@ public class InstancesController {
.defaultIfEmpty(ResponseEntity.notFound().build()); .defaultIfEmpty(ResponseEntity.notFound().build());
} }
@GetMapping(path = "/events", produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(path = "/instances/events", produces = MediaType.APPLICATION_JSON_VALUE)
public Flux<InstanceEvent> events() { public Flux<InstanceEvent> events() {
return eventStore.findAll(); return eventStore.findAll();
} }
@GetMapping(path = "/events", produces = MediaType.APPLICATION_STREAM_JSON_VALUE) @GetMapping(path = "/instances/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<? extends InstanceEvent> eventStream() { public Flux<ServerSentEvent<InstanceEvent>> eventStream() {
return Flux.from(eventStore); return Flux.from(eventStore).map(event -> ServerSentEvent.builder(event).build()).mergeWith(PING_FLUX);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment