-
Notifications
You must be signed in to change notification settings - Fork 573
/
RestSpecManagementServiceImpl.java
339 lines (299 loc) · 19.3 KB
/
RestSpecManagementServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* 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.
*/
package org.kie.server.controller.rest;
import java.util.Collection;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.kie.server.controller.api.KieServerControllerException;
import org.kie.server.controller.api.KieServerControllerNotFoundException;
import org.kie.server.controller.api.model.spec.Capability;
import org.kie.server.controller.api.model.spec.ContainerConfig;
import org.kie.server.controller.api.model.spec.ContainerSpecKey;
import org.kie.server.controller.api.model.spec.ContainerSpecList;
import org.kie.server.controller.api.model.spec.RuleConfig;
import org.kie.server.controller.api.model.spec.ServerTemplateKey;
import org.kie.server.controller.api.model.spec.ServerTemplateList;
import org.kie.server.controller.impl.service.SpecManagementServiceImpl;
import org.kie.server.controller.api.model.spec.ContainerSpec;
import org.kie.server.controller.api.model.spec.ProcessConfig;
import org.kie.server.controller.api.model.spec.ServerTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.kie.server.controller.rest.ControllerUtils.*;
@Path("/controller/management")
public class RestSpecManagementServiceImpl extends SpecManagementServiceImpl {
private static final Logger logger = LoggerFactory.getLogger(RestSpecManagementServiceImpl.class);
@PUT
@Path("servers/{id}/containers/{containerId}")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response saveContainerSpec(@Context HttpHeaders headers, @PathParam("id") String serverTemplateId, @PathParam("containerId") String containerId, String containerSpecPayload) {
String contentType = getContentType(headers);
try {
logger.debug("Received save container spec request for server template with id {}", serverTemplateId);
ContainerSpec containerSpec = unmarshal(containerSpecPayload, contentType, ContainerSpec.class);
logger.debug("Container spec is {}", containerSpec);
super.saveContainerSpec(serverTemplateId, containerSpec);
logger.debug("Returning response for save container spec request for server template with id '{}': CREATED", serverTemplateId);
return createCorrectVariant("", headers, Response.Status.CREATED);
} catch (KieServerControllerNotFoundException e) {
return createCorrectVariant(e.getMessage(), headers, Response.Status.NOT_FOUND);
} catch (KieServerControllerException e){
return createCorrectVariant("Request failed to be processed due to" + e.getMessage(), headers, Response.Status.BAD_REQUEST);
} catch (Exception e) {
logger.error("Save container spec request for server template id {} failed due to {}", serverTemplateId, e.getMessage(), e);
return createCorrectVariant("Unknown error " + e.getMessage(), headers, Response.Status.INTERNAL_SERVER_ERROR);
}
}
@PUT
@Path("servers/{id}")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response saveServerTemplate(@Context HttpHeaders headers, @PathParam("id") String serverTemplateId, String serverTemplatePayload) {
String contentType = getContentType(headers);
try {
if (super.getServerTemplate(serverTemplateId) != null) {
return createCorrectVariant("Server template " + serverTemplateId + " already registered", headers, Response.Status.NOT_FOUND);
}
logger.debug("Received save server template with id {}", serverTemplateId);
ServerTemplate serverTemplate = unmarshal(serverTemplatePayload, contentType, ServerTemplate.class);
if (serverTemplate == null) {
return createCorrectVariant("Server template " + serverTemplateId + " not found", headers, Response.Status.NOT_FOUND);
}
logger.debug("Server template is {}", serverTemplate);
super.saveServerTemplate(serverTemplate);
logger.debug("Returning response for save server template with id '{}': CREATED", serverTemplateId);
return createCorrectVariant("", headers, Response.Status.CREATED);
} catch (KieServerControllerNotFoundException e) {
return createCorrectVariant(e.getMessage(), headers, Response.Status.NOT_FOUND);
} catch (KieServerControllerException e){
return createCorrectVariant("Request failed to be processed due to" + e.getMessage(), headers, Response.Status.BAD_REQUEST);
} catch (Exception e) {
logger.error("Save server template id {} failed due to {}", serverTemplateId, e.getMessage(), e);
return createCorrectVariant("Unknown error " + e.getMessage(), headers, Response.Status.INTERNAL_SERVER_ERROR);
}
}
@GET
@Path("servers/{id}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getServerTemplate(@Context HttpHeaders headers, @PathParam("id") String serverTemplateId) {
String contentType = getContentType(headers);
try {
logger.debug("Received get server template with id {}", serverTemplateId);
ServerTemplate serverTemplate = super.getServerTemplate(serverTemplateId);
if (serverTemplate == null) {
return createCorrectVariant("Server template " + serverTemplateId + " not found", headers, Response.Status.NOT_FOUND);
}
String response = marshal(contentType, serverTemplate);
logger.debug("Returning response for get server template with id '{}': {}", serverTemplateId, response);
return createCorrectVariant(response, headers, Response.Status.OK);
} catch (KieServerControllerNotFoundException e) {
return createCorrectVariant(e.getMessage(), headers, Response.Status.NOT_FOUND);
} catch (KieServerControllerException e){
return createCorrectVariant("Request failed to be processed due to" + e.getMessage(), headers, Response.Status.BAD_REQUEST);
} catch (Exception e) {
logger.error("Get server template id {} failed due to {}", serverTemplateId, e.getMessage(), e);
return createCorrectVariant("Unknown error " + e.getMessage(), headers, Response.Status.INTERNAL_SERVER_ERROR);
}
}
@GET
@Path("servers")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response listServerTemplates(@Context HttpHeaders headers) {
String contentType = getContentType(headers);
try {
logger.debug("Received get server templates");
Collection<ServerTemplate> servers = super.listServerTemplates();
String response = marshal(contentType, new ServerTemplateList(servers));
logger.debug("Returning response for get server templates: {}", response);
return createCorrectVariant(response, headers, Response.Status.OK);
} catch (KieServerControllerNotFoundException e) {
return createCorrectVariant(e.getMessage(), headers, Response.Status.NOT_FOUND);
} catch (KieServerControllerException e){
return createCorrectVariant("Request failed to be processed due to" + e.getMessage(), headers, Response.Status.BAD_REQUEST);
} catch (Exception e) {
logger.error("Get server templates failed due to {}", e.getMessage(), e);
return createCorrectVariant("Unknown error " + e.getMessage(), headers, Response.Status.INTERNAL_SERVER_ERROR);
}
}
@GET
@Path("servers/{id}/containers")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response listContainerSpec(@Context HttpHeaders headers, @PathParam("id") String serverTemplateId) {
String contentType = getContentType(headers);
try {
logger.debug("Received get containers for server template with id {}", serverTemplateId);
Collection<ContainerSpec> containerSpecs = super.listContainerSpec(serverTemplateId);
String response = marshal(contentType, new ContainerSpecList(containerSpecs));
logger.debug("Returning response for get containers for server templates with id {}: {}", serverTemplateId, response);
return createCorrectVariant(response, headers, Response.Status.OK);
} catch (KieServerControllerNotFoundException e) {
return createCorrectVariant(e.getMessage(), headers, Response.Status.NOT_FOUND);
} catch (KieServerControllerException e){
return createCorrectVariant("Request failed to be processed due to" + e.getMessage(), headers, Response.Status.BAD_REQUEST);
} catch (Exception e) {
logger.error("Get server templates failed due to {}", e.getMessage(), e);
return createCorrectVariant("Unknown error " + e.getMessage(), headers, Response.Status.INTERNAL_SERVER_ERROR);
}
}
@GET
@Path("servers/{id}/containers/{containerId}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getContainerSpec(@Context HttpHeaders headers, @PathParam("id") String serverTemplateId, @PathParam("containerId") String containerId) {
String contentType = getContentType(headers);
try {
logger.debug("Received get container {} for server template with id {}", containerId, serverTemplateId);
ServerTemplate serverTemplate = super.getServerTemplate(serverTemplateId);
if (serverTemplate == null) {
return createCorrectVariant("Server template " + serverTemplateId + " not found", headers, Response.Status.NOT_FOUND);
}
ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerId);
if (containerSpec == null) {
return createCorrectVariant("Server template " + serverTemplateId + " does not have container with id " + containerId, headers, Response.Status.NOT_FOUND);
}
// set it as server template key only to avoid cyclic references between containers and templates
containerSpec.setServerTemplateKey(new ServerTemplateKey(serverTemplate.getId(), serverTemplate.getName()));
String response = marshal(contentType, containerSpec);
logger.debug("Returning response for get container {} for server templates with id {}: {}", containerId, serverTemplateId, response);
return createCorrectVariant(response, headers, Response.Status.OK);
} catch (KieServerControllerNotFoundException e) {
return createCorrectVariant(e.getMessage(), headers, Response.Status.NOT_FOUND);
} catch (KieServerControllerException e){
return createCorrectVariant("Request failed to be processed due to" + e.getMessage(), headers, Response.Status.BAD_REQUEST);
} catch (Exception e) {
logger.error("Get server templates failed due to {}", e.getMessage(), e);
return createCorrectVariant("Unknown error " + e.getMessage(), headers, Response.Status.INTERNAL_SERVER_ERROR);
}
}
@DELETE
@Path("servers/{id}/containers/{containerId}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response deleteContainerSpec(@Context HttpHeaders headers, @PathParam("id") String serverTemplateId, @PathParam("containerId") String containerSpecId) {
try {
super.deleteContainerSpec(serverTemplateId, containerSpecId);
// return null to produce 204
return null;
} catch (KieServerControllerNotFoundException e) {
return createCorrectVariant(e.getMessage(), headers, Response.Status.NOT_FOUND);
} catch (KieServerControllerException e){
return createCorrectVariant("Request failed to be processed due to" + e.getMessage(), headers, Response.Status.BAD_REQUEST);
} catch (Exception e) {
logger.error("Remove container with id {} from server template with id {} failed due to {}", containerSpecId, serverTemplateId, e.getMessage(), e);
return createCorrectVariant("Unknown error " + e.getMessage(), headers, Response.Status.INTERNAL_SERVER_ERROR);
}
}
@DELETE
@Path("servers/{id}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response deleteServerTemplate(@Context HttpHeaders headers, @PathParam("id") String serverTemplateId) {
try {
super.deleteServerTemplate(serverTemplateId);
// return null to produce 204
return null;
} catch (KieServerControllerNotFoundException e) {
return createCorrectVariant(e.getMessage(), headers, Response.Status.NOT_FOUND);
} catch (KieServerControllerException e){
return createCorrectVariant("Request failed to be processed due to" + e.getMessage(), headers, Response.Status.BAD_REQUEST);
} catch (Exception e) {
logger.error("Remove server template with id {} failed due to {}", serverTemplateId, e.getMessage(), e);
return createCorrectVariant("Unknown error " + e.getMessage(), headers, Response.Status.INTERNAL_SERVER_ERROR);
}
}
@POST
@Path("servers/{id}/containers/{containerId}/config/{capability}")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response updateContainerConfig(@Context HttpHeaders headers, @PathParam("id") String serverTemplateId, @PathParam("containerId") String containerSpecId,
@PathParam("capability") String capabilityStr, String containerConfigPayload) {
String contentType = getContentType(headers);
try {
ContainerConfig containerConfig = null;
Capability capability = null;
if (capabilityStr.equals(Capability.PROCESS.name())) {
capability = Capability.PROCESS;
logger.debug("Received update container (with id {}) process config request for server template with id {}", containerSpecId , serverTemplateId);
containerConfig = unmarshal(containerConfigPayload, contentType, ProcessConfig.class);
} else if (capabilityStr.equals(Capability.RULE.name())) {
capability = Capability.RULE;
logger.debug("Received update container (with id {}) rule config request for server template with id {}", containerSpecId, serverTemplateId);
containerConfig = unmarshal(containerConfigPayload, contentType, RuleConfig.class);
} else {
logger.debug("Not supported configuration type {}, returning bad request response", capabilityStr);
return createCorrectVariant("Not supported configuration " + capabilityStr, headers, Response.Status.BAD_REQUEST);
}
logger.debug("Container configuration is {}", containerConfig);
super.updateContainerConfig(serverTemplateId, containerSpecId, capability, containerConfig);
logger.debug("Returning response for update container (with id {}) config '{}': CREATED", containerSpecId);
return createCorrectVariant("", headers, Response.Status.CREATED);
} catch (KieServerControllerNotFoundException e) {
return createCorrectVariant(e.getMessage(), headers, Response.Status.NOT_FOUND);
} catch (KieServerControllerException e){
return createCorrectVariant("Request failed to be processed due to" + e.getMessage(), headers, Response.Status.BAD_REQUEST);
} catch (Exception e) {
logger.error("Remove server template with id {} failed due to {}", serverTemplateId, e.getMessage(), e);
return createCorrectVariant("Unknown error " + e.getMessage(), headers, Response.Status.INTERNAL_SERVER_ERROR);
}
}
@POST
@Path("servers/{id}/containers/{containerId}/status/stopped")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response stopContainer(@Context HttpHeaders headers, @PathParam("id") String serverTemplateId, @PathParam("containerId") String containerId) {
logger.debug("Requesting stop container with id {} server instance: {}", containerId, serverTemplateId);
try {
ContainerSpecKey containerSpecKey = new ContainerSpecKey();
containerSpecKey.setId(containerId);
containerSpecKey.setServerTemplateKey(new ServerTemplateKey(serverTemplateId, ""));
super.stopContainer(containerSpecKey);
logger.debug("Returning response for stop container with id {} server instance: {}", containerId, serverTemplateId);
return createCorrectVariant("", headers, Response.Status.OK);
} catch (KieServerControllerNotFoundException e) {
return createCorrectVariant(e.getMessage(), headers, Response.Status.NOT_FOUND);
} catch (Exception e) {
logger.error("Stop container failed due to {}", e.getMessage(), e);
return createCorrectVariant("Unknown error " + e.getMessage(), headers, Response.Status.INTERNAL_SERVER_ERROR);
}
}
@POST
@Path("servers/{id}/containers/{containerId}/status/started")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response startContainer(@Context HttpHeaders headers, @PathParam("id") String serverTemplateId, @PathParam("containerId") String containerId) {
logger.debug("Requesting start container with id {} server instance: {}", containerId, serverTemplateId);
try {
ContainerSpecKey containerSpecKey = new ContainerSpecKey();
containerSpecKey.setId(containerId);
containerSpecKey.setServerTemplateKey(new ServerTemplateKey(serverTemplateId, ""));
super.startContainer(containerSpecKey);
logger.debug("Returning response for start container with id {} server instance: {}", containerId, serverTemplateId);
return createCorrectVariant("", headers, Response.Status.OK);
} catch (KieServerControllerNotFoundException e) {
return createCorrectVariant(e.getMessage(), headers, Response.Status.NOT_FOUND);
} catch (Exception e) {
logger.error("Start container failed due to {}", e.getMessage(), e);
return createCorrectVariant("Unknown error " + e.getMessage(), headers, Response.Status.INTERNAL_SERVER_ERROR);
}
}
}