RegistryController.java 3.82 KB
Newer Older
Thomas Bosch committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Thomas Bosch committed
16
package de.codecentric.boot.admin.controller;
Thomas Bosch committed
17

18
import java.util.Collection;
Thomas Bosch committed
19

Thomas Bosch committed
20 21
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
22
import org.springframework.http.HttpStatus;
23
import org.springframework.http.ResponseEntity;
Thomas Bosch committed
24
import org.springframework.web.bind.annotation.PathVariable;
Thomas Bosch committed
25 26 27
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
28
import org.springframework.web.bind.annotation.RequestParam;
Dennis Schulte committed
29
import org.springframework.web.bind.annotation.RestController;
Thomas Bosch committed
30

Thomas Bosch committed
31
import de.codecentric.boot.admin.model.Application;
Johannes Stelzer committed
32
import de.codecentric.boot.admin.registry.ApplicationRegistry;
Thomas Bosch committed
33

Thomas Bosch committed
34 35 36
/**
 * REST controller for controlling registration of managed applications.
 */
Dennis Schulte committed
37
@RestController
38
@RequestMapping(value = RegistryController.PATH)
Thomas Bosch committed
39
public class RegistryController {
40
	public static final String PATH = "/api/applications";
Thomas Bosch committed
41

Thomas Bosch committed
42 43
	private static final Logger LOGGER = LoggerFactory.getLogger(RegistryController.class);

44 45 46 47 48
	private final ApplicationRegistry registry;

	public RegistryController(ApplicationRegistry registry) {
		this.registry = registry;
	}
Thomas Bosch committed
49

Thomas Bosch committed
50 51
	/**
	 * Register an application within this admin application.
52
	 *
53
	 * @param app The application infos.
Thomas Bosch committed
54 55
	 * @return The registered application.
	 */
56
	@RequestMapping(method = RequestMethod.POST)
57 58
	public ResponseEntity<Application> register(@RequestBody Application app) {
		LOGGER.debug("Register application {}", app.toString());
59 60
		Application registeredApp = registry.register(app);
		return new ResponseEntity<Application>(registeredApp, HttpStatus.CREATED);
Thomas Bosch committed
61 62
	}

63 64
	/**
	 * List all registered applications with name
65 66
	 * @param name the name to search for
	 * @return List
67 68 69 70 71 72 73 74 75 76 77 78
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Collection<Application> applications(@RequestParam(value = "name", required = false) String name) {
		LOGGER.debug("Deliver registered applications with name= {}", name);
		if (name == null || name.isEmpty()) {
			return registry.getApplications();
		}
		else {
			return registry.getApplicationsByName(name);
		}
	}

Thomas Bosch committed
79 80
	/**
	 * Get a single application out of the registry.
81
	 *
82
	 * @param id The application identifier.
Thomas Bosch committed
83 84
	 * @return The registered application.
	 */
85
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
86
	public ResponseEntity<Application> get(@PathVariable String id) {
Thomas Bosch committed
87
		LOGGER.debug("Deliver registered application with ID '{}'", id);
88 89 90
		Application application = registry.getApplication(id);
		if (application != null) {
			return new ResponseEntity<Application>(application, HttpStatus.OK);
Johannes Stelzer committed
91
		} else {
92 93
			return new ResponseEntity<Application>(application, HttpStatus.NOT_FOUND);
		}
Thomas Bosch committed
94 95
	}

96
	/**
97
	 * Unregister an application within this admin application.
98
	 *
99
	 * @param id The application id.
100
	 * @return the unregistered application.
101
	 */
102
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
103
	public ResponseEntity<Application> unregister(@PathVariable String id) {
Johannes Stelzer committed
104
		LOGGER.debug("Unregister application with ID '{}'", id);
105 106 107
		Application app = registry.unregister(id);
		if (app != null) {
			return new ResponseEntity<Application>(app, HttpStatus.NO_CONTENT);
Johannes Stelzer committed
108
		} else {
109 110
			return new ResponseEntity<Application>(HttpStatus.NOT_FOUND);
		}
111 112
	}

Thomas Bosch committed
113
}