All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.codearte.accurest.stubrunner.boot.TriggerController.groovy Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package io.codearte.accurest.stubrunner.boot

import groovy.util.logging.Slf4j
import io.codearte.accurest.stubrunner.StubFinder
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

import static org.springframework.web.bind.annotation.RequestMethod.GET
import static org.springframework.web.bind.annotation.RequestMethod.POST

/**
 * @author Marcin Grzejszczak
 */
@RestController
@RequestMapping("/triggers")
@Slf4j
class TriggerController {

	private final StubFinder stubFinder

	@Autowired
	TriggerController(StubFinder stubFinder) {
		this.stubFinder = stubFinder
	}

	@RequestMapping(method = POST, path = "/{label:.*}")
	ResponseEntity>> trigger(@PathVariable String label) {
		return respond(label) {
			stubFinder.trigger(label)
		}
	}

	@RequestMapping(method = POST, path = "/{ivyNotation:.*}/{label:.*}")
	ResponseEntity>> triggerByArtifact(@PathVariable String ivyNotation, @PathVariable String label) {
		return respond(label) {
			stubFinder.trigger(ivyNotation, label)
		}
	}

	@RequestMapping(method = GET)
	Map> labels() {
		return stubFinder.labels()
	}

	private ResponseEntity>> respond(String label, Closure closure) {
		try {
			closure()
			return ResponseEntity.ok().body()
		} catch (Exception e) {
			log.debug("Exception occurred while trying to return [$label] label", e)
			return new ResponseEntity>>(stubFinder.labels(), HttpStatus.NOT_FOUND)
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy