|
1
|
|
|
package com.hltech.judged.server.interfaces.rest.contracts; |
|
2
|
|
|
|
|
3
|
|
|
import com.hltech.judged.server.domain.ServiceId; |
|
4
|
|
|
import com.hltech.judged.server.domain.contracts.Contract; |
|
5
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContracts; |
|
6
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContractsRepository; |
|
7
|
|
|
import com.hltech.judged.server.interfaces.rest.ResourceNotFoundException; |
|
8
|
|
|
import io.swagger.annotations.ApiOperation; |
|
9
|
|
|
import io.swagger.annotations.ApiResponse; |
|
10
|
|
|
import io.swagger.annotations.ApiResponses; |
|
11
|
|
|
import lombok.RequiredArgsConstructor; |
|
12
|
|
|
import org.springframework.http.HttpStatus; |
|
13
|
|
|
import org.springframework.http.MediaType; |
|
14
|
|
|
import org.springframework.http.ResponseEntity; |
|
15
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
16
|
|
|
import org.springframework.web.bind.annotation.CrossOrigin; |
|
17
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler; |
|
18
|
|
|
import org.springframework.web.bind.annotation.GetMapping; |
|
19
|
|
|
import org.springframework.web.bind.annotation.PathVariable; |
|
20
|
|
|
import org.springframework.web.bind.annotation.PostMapping; |
|
21
|
|
|
import org.springframework.web.bind.annotation.RequestBody; |
|
22
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
23
|
|
|
import org.springframework.web.bind.annotation.ResponseStatus; |
|
24
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
25
|
|
|
import org.springframework.web.servlet.view.RedirectView; |
|
26
|
|
|
|
|
27
|
|
|
import javax.persistence.Entity; |
|
28
|
|
|
import javax.persistence.NoResultException; |
|
29
|
|
|
import java.util.List; |
|
30
|
|
|
|
|
31
|
|
|
import static java.util.stream.Collectors.toList; |
|
32
|
|
|
import static org.springframework.http.ResponseEntity.ok; |
|
33
|
|
|
|
|
34
|
|
|
@RestController |
|
35
|
|
|
@RequestMapping("/contracts") |
|
36
|
|
|
@Transactional |
|
37
|
|
|
@RequiredArgsConstructor |
|
38
|
|
|
public class ContractsController { |
|
39
|
|
|
|
|
40
|
|
|
private final ServiceContractsRepository serviceContractsRepository; |
|
41
|
|
|
private final ContractsMapper mapper; |
|
42
|
|
|
|
|
43
|
|
|
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) |
|
44
|
|
|
@ApiOperation(value = "Get registered contracts", nickname = "get names of services") |
|
45
|
|
|
@ApiResponses(value = { |
|
46
|
|
|
@ApiResponse(code = 302, message = "Found") |
|
47
|
|
|
}) |
|
48
|
|
|
public RedirectView getServicesEndpointDescription() { |
|
49
|
|
|
return new RedirectView("contracts/services", true); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
@GetMapping(value = "/services", produces = MediaType.APPLICATION_JSON_VALUE) |
|
53
|
|
|
@ApiOperation(value = "Get names of services with registered contracts", nickname = "get names of services") |
|
54
|
|
|
@ApiResponses(value = { |
|
55
|
|
|
@ApiResponse(code = 200, message = "Success", response = String.class, responseContainer = "list"), |
|
56
|
|
|
@ApiResponse(code = 400, message = "Bad Request"), |
|
57
|
|
|
@ApiResponse(code = 500, message = "Failure")}) |
|
58
|
|
|
public List<String> getAvailableServiceNames() { |
|
59
|
|
|
return serviceContractsRepository.getServiceNames(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* This endpoint is intended to server request checking if a service with given registered any contracts. |
|
64
|
|
|
* It will be extended to provide more detailed information on a service in the future. |
|
65
|
|
|
* |
|
66
|
|
|
* @param serviceName - name of the service that we are querying for |
|
67
|
|
|
* @return name of the service if contracts are present, NOT_FOUND (404) otherwise |
|
68
|
|
|
*/ |
|
69
|
|
|
@GetMapping(value = "/services/{serviceName}", produces = MediaType.TEXT_PLAIN_VALUE) |
|
70
|
|
|
@ApiOperation(value = "Get details of a services with registered contracts", nickname = "get service details") |
|
71
|
|
|
@ApiResponses(value = { |
|
72
|
|
|
@ApiResponse(code = 200, message = "Success", response = String.class), |
|
73
|
|
|
@ApiResponse(code = 400, message = "Bad Request"), |
|
74
|
|
|
@ApiResponse(code = 400, message = "Not found"), |
|
75
|
|
|
@ApiResponse(code = 500, message = "Failure")}) |
|
76
|
|
|
public String getAvailableServiceNames(@PathVariable(name = "serviceName") String serviceName) { |
|
77
|
|
|
return serviceContractsRepository.getService(serviceName); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
@ExceptionHandler(NoResultException.class) |
|
81
|
|
|
@ResponseStatus(HttpStatus.NOT_FOUND) |
|
82
|
|
|
void notFound() { } |
|
83
|
|
|
|
|
84
|
|
|
@GetMapping(value = "/services/{serviceName}/versions", produces = MediaType.APPLICATION_JSON_VALUE) |
|
85
|
|
|
@ApiOperation(value = "Get versions of a service with registered contracts", nickname = "get versions of a service") |
|
86
|
|
|
@ApiResponses(value = { |
|
87
|
|
|
@ApiResponse(code = 200, message = "Success", response = String.class, responseContainer = "list"), |
|
88
|
|
|
@ApiResponse(code = 400, message = "Bad Request"), |
|
89
|
|
|
@ApiResponse(code = 500, message = "Failure")}) |
|
90
|
|
|
public List<String> getAllServiceVersions(@PathVariable(name = "serviceName") String serviceName) { |
|
91
|
|
|
return serviceContractsRepository.findAllByServiceName(serviceName) |
|
92
|
|
|
.stream() |
|
93
|
|
|
.map(ServiceContracts::getVersion).sorted() |
|
94
|
|
|
.collect(toList()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
@GetMapping(value = "services/{serviceName}/versions/{version:.+}", produces = MediaType.APPLICATION_JSON_VALUE) |
|
98
|
|
|
@ApiOperation(value = "Get contracts for a version of a service", nickname = "get contracts") |
|
99
|
|
|
@ApiResponses(value = { |
|
100
|
|
|
@ApiResponse(code = 200, message = "Success", response = ServiceContractsDto.class), |
|
101
|
|
|
@ApiResponse(code = 400, message = "Bad Request"), |
|
102
|
|
|
@ApiResponse(code = 500, message = "Failure")}) |
|
103
|
|
|
public ServiceContractsDto getContracts(@PathVariable(name = "serviceName") String serviceName, @PathVariable(name = "version") String version) { |
|
104
|
|
|
return mapper.toDto(this.serviceContractsRepository.findOne(new ServiceId(serviceName, version)).orElseThrow(ResourceNotFoundException::new)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
@GetMapping(value = "services/{serviceName}/versions/{version:.+}/capabilities/{protocol}", produces = MediaType.ALL_VALUE) |
|
108
|
|
|
@ApiOperation(value = "Get capabilities of a version of a service for a protocol", nickname = "get capabilities by protocol") |
|
109
|
|
|
@ApiResponses(value = { |
|
110
|
|
|
@ApiResponse(code = 200, message = "Success", response = ServiceContractsDto.class), |
|
111
|
|
|
@ApiResponse(code = 400, message = "Bad Request"), |
|
112
|
|
|
@ApiResponse(code = 500, message = "Failure")}) |
|
113
|
|
|
public ResponseEntity<String> getCapabilities(@PathVariable String serviceName, @PathVariable String version, @PathVariable(name = "protocol") String protocol) { |
|
114
|
|
|
final Contract contract = this.serviceContractsRepository |
|
115
|
|
|
.findCapabilityByServiceIdProtocol(new ServiceId(serviceName, version), protocol) |
|
116
|
|
|
.orElseThrow(ResourceNotFoundException::new); |
|
117
|
|
|
return ResponseEntity.ok().contentType(MediaType.valueOf(contract.getMimeType())).body(contract.getValue()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
@PostMapping(value = "services/{serviceName}/versions/{version:.+}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) |
|
121
|
|
|
@ApiOperation(value = "Register contracts for a version of a service", nickname = "register contracts") |
|
122
|
|
|
@ApiResponses(value = { |
|
123
|
|
|
@ApiResponse(code = 200, message = "Success", response = ServiceContractsDto.class), |
|
124
|
|
|
@ApiResponse(code = 400, message = "Bad Request"), |
|
125
|
|
|
@ApiResponse(code = 500, message = "Failure")}) |
|
126
|
|
|
public ServiceContractsDto registerContract(@PathVariable(name = "serviceName") String serviceName, @PathVariable(name = "version") String version, @RequestBody ServiceContractsForm form) { |
|
127
|
|
|
return mapper.toDto(this.serviceContractsRepository.persist( |
|
128
|
|
|
new ServiceContracts( |
|
129
|
|
|
new ServiceId(serviceName, version), |
|
130
|
|
|
mapper.mapCapabilitiesForm(form.getCapabilities()), |
|
131
|
|
|
mapper.mapExpectationsForm(form.getExpectations()) |
|
132
|
|
|
) |
|
133
|
|
|
)); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|