-
-
Notifications
You must be signed in to change notification settings - Fork 607
/
Copy pathProjectResource.java
674 lines (651 loc) · 35.5 KB
/
ProjectResource.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*
* This file is part of Dependency-Track.
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) Steve Springett. All Rights Reserved.
*/
package org.dependencytrack.resources.v1;
import alpine.common.logging.Logger;
import alpine.event.framework.Event;
import alpine.persistence.PaginatedResult;
import alpine.server.auth.PermissionRequired;
import alpine.server.resources.AlpineResource;
import io.jsonwebtoken.lang.Collections;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Authorization;
import io.swagger.annotations.ResponseHeader;
import org.apache.commons.lang3.StringUtils;
import org.dependencytrack.auth.Permissions;
import org.dependencytrack.event.CloneProjectEvent;
import org.dependencytrack.model.Classifier;
import org.dependencytrack.model.Project;
import org.dependencytrack.model.Tag;
import org.dependencytrack.persistence.QueryManager;
import org.dependencytrack.resources.v1.vo.CloneProjectRequest;
import javax.jdo.FetchGroup;
import javax.validation.Validator;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PATCH;
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.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.security.Principal;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
/**
* JAX-RS resources for processing projects.
*
* @author Steve Springett
* @since 3.0.0
*/
@Path("/v1/project")
@Api(value = "project", authorizations = @Authorization(value = "X-Api-Key"))
public class ProjectResource extends AlpineResource {
private static final Logger LOGGER = Logger.getLogger(ProjectResource.class);
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Returns a list of all projects",
response = Project.class,
responseContainer = "List",
responseHeaders = @ResponseHeader(name = TOTAL_COUNT_HEADER, response = Long.class, description = "The total number of projects")
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized")
})
@PermissionRequired(Permissions.Constants.VIEW_PORTFOLIO)
public Response getProjects(@ApiParam(value = "The optional name of the project to query on", required = false)
@QueryParam("name") String name,
@ApiParam(value = "Optionally excludes inactive projects from being returned", required = false)
@QueryParam("excludeInactive") boolean excludeInactive,
@ApiParam(value = "Optionally excludes children projects from being returned", required = false)
@QueryParam("onlyRoot") boolean onlyRoot) {
try (QueryManager qm = new QueryManager(getAlpineRequest())) {
final PaginatedResult result = (name != null) ? qm.getProjects(name, excludeInactive, onlyRoot) : qm.getProjects(true, excludeInactive, onlyRoot);
return Response.ok(result.getObjects()).header(TOTAL_COUNT_HEADER, result.getTotal()).build();
}
}
@GET
@Path("/{uuid}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Returns a specific project",
response = Project.class
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Access to the specified project is forbidden"),
@ApiResponse(code = 404, message = "The project could not be found")
})
@PermissionRequired(Permissions.Constants.VIEW_PORTFOLIO)
public Response getProject(
@ApiParam(value = "The UUID of the project to retrieve", required = true)
@PathParam("uuid") String uuid) {
try (QueryManager qm = new QueryManager()) {
final Project project = qm.getProject(uuid);
if (project != null) {
if (qm.hasAccess(super.getPrincipal(), project)) {
return Response.ok(project).build();
} else {
return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified project is forbidden").build();
}
} else {
return Response.status(Response.Status.NOT_FOUND).entity("The project could not be found.").build();
}
}
}
@GET
@Path("/lookup")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Returns a specific project by its name and version", response = Project.class, nickname = "getProjectByNameAndVersion")
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Access to the specified project is forbidden"),
@ApiResponse(code = 404, message = "The project could not be found")
})
@PermissionRequired(Permissions.Constants.VIEW_PORTFOLIO)
public Response getProject(
@ApiParam(value = "The name of the project to query on", required = true)
@QueryParam("name") String name,
@ApiParam(value = "The version of the project to query on", required = true)
@QueryParam("version") String version) {
try (QueryManager qm = new QueryManager()) {
final Project project = qm.getProject(name, version);
if (project != null) {
if (qm.hasAccess(super.getPrincipal(), project)) {
return Response.ok(project).build();
} else {
return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified project is forbidden").build();
}
} else {
return Response.status(Response.Status.NOT_FOUND).entity("The project could not be found.").build();
}
}
}
@GET
@Path("/tag/{tag}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Returns a list of all projects by tag",
response = Project.class,
responseContainer = "List",
responseHeaders = @ResponseHeader(name = TOTAL_COUNT_HEADER, response = Long.class, description = "The total number of projects with the tag")
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized")
})
@PermissionRequired(Permissions.Constants.VIEW_PORTFOLIO)
public Response getProjectsByTag(
@ApiParam(value = "The tag to query on", required = true)
@PathParam("tag") String tagString,
@ApiParam(value = "Optionally excludes inactive projects from being returned", required = false)
@QueryParam("excludeInactive") boolean excludeInactive,
@ApiParam(value = "Optionally excludes children projects from being returned", required = false)
@QueryParam("onlyRoot") boolean onlyRoot) {
try (QueryManager qm = new QueryManager(getAlpineRequest())) {
final Tag tag = qm.getTagByName(tagString);
final PaginatedResult result = qm.getProjects(tag, true, excludeInactive, onlyRoot);
return Response.ok(result.getObjects()).header(TOTAL_COUNT_HEADER, result.getTotal()).build();
}
}
@GET
@Path("/classifier/{classifier}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Returns a list of all projects by classifier",
response = Project.class,
responseContainer = "List",
responseHeaders = @ResponseHeader(name = TOTAL_COUNT_HEADER, response = Long.class, description = "The total number of projects of the specified classifier")
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized")
})
@PermissionRequired(Permissions.Constants.VIEW_PORTFOLIO)
public Response getProjectsByClassifier(
@ApiParam(value = "The classifier to query on", required = true)
@PathParam("classifier") String classifierString,
@ApiParam(value = "Optionally excludes inactive projects from being returned", required = false)
@QueryParam("excludeInactive") boolean excludeInactive,
@ApiParam(value = "Optionally excludes children projects from being returned", required = false)
@QueryParam("onlyRoot") boolean onlyRoot) {
try (QueryManager qm = new QueryManager(getAlpineRequest())) {
final Classifier classifier = Classifier.valueOf(classifierString);
final PaginatedResult result = qm.getProjects(classifier, true, excludeInactive, onlyRoot);
return Response.ok(result.getObjects()).header(TOTAL_COUNT_HEADER, result.getTotal()).build();
} catch (IllegalArgumentException e) {
return Response.status(Response.Status.BAD_REQUEST).entity("The classifier type specified is not valid.").build();
}
}
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Creates a new project",
notes = "If a parent project exists, the UUID of the parent project is required ",
response = Project.class,
code = 201
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 409, message = "- An inactive Parent cannot be selected as parent\n- A project with the specified name already exists"),
})
@PermissionRequired(Permissions.Constants.PORTFOLIO_MANAGEMENT)
public Response createProject(Project jsonProject) {
final Validator validator = super.getValidator();
failOnValidationError(
validator.validateProperty(jsonProject, "author"),
validator.validateProperty(jsonProject, "publisher"),
validator.validateProperty(jsonProject, "group"),
validator.validateProperty(jsonProject, "name"),
validator.validateProperty(jsonProject, "description"),
validator.validateProperty(jsonProject, "version"),
validator.validateProperty(jsonProject, "classifier"),
validator.validateProperty(jsonProject, "cpe"),
validator.validateProperty(jsonProject, "purl"),
validator.validateProperty(jsonProject, "swidTagId")
);
if (jsonProject.getClassifier() == null) {
jsonProject.setClassifier(Classifier.APPLICATION);
}
try (QueryManager qm = new QueryManager()) {
if (jsonProject.getParent() != null && jsonProject.getParent().getUuid() != null) {
Project parent = qm.getObjectByUuid(Project.class, jsonProject.getParent().getUuid());
jsonProject.setParent(parent);
}
if (!qm.doesProjectExist(StringUtils.trimToNull(jsonProject.getName()), StringUtils.trimToNull(jsonProject.getVersion()))) {
final Project project;
try {
project = qm.createProject(jsonProject, jsonProject.getTags(), true);
} catch (IllegalArgumentException e){
LOGGER.debug(e.getMessage());
return Response.status(Response.Status.CONFLICT).entity("An inactive Parent cannot be selected as parent").build();
}
Principal principal = getPrincipal();
qm.updateNewProjectACL(project, principal);
LOGGER.info("Project " + project.toString() + " created by " + super.getPrincipal().getName());
return Response.status(Response.Status.CREATED).entity(project).build();
} else {
return Response.status(Response.Status.CONFLICT).entity("A project with the specified name already exists.").build();
}
}
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Updates a project",
response = Project.class
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 404, message = "The UUID of the project could not be found"),
@ApiResponse(code = 409, message = "- An inactive Parent cannot be selected as parent\n- Project cannot be set to inactive if active children are present\n- A project with the specified name already exists\n- A project cannot select itself as a parent")
})
@PermissionRequired(Permissions.Constants.PORTFOLIO_MANAGEMENT)
public Response updateProject(Project jsonProject) {
final Validator validator = super.getValidator();
failOnValidationError(
validator.validateProperty(jsonProject, "author"),
validator.validateProperty(jsonProject, "publisher"),
validator.validateProperty(jsonProject, "group"),
validator.validateProperty(jsonProject, "name"),
validator.validateProperty(jsonProject, "description"),
validator.validateProperty(jsonProject, "version"),
validator.validateProperty(jsonProject, "classifier"),
validator.validateProperty(jsonProject, "cpe"),
validator.validateProperty(jsonProject, "purl"),
validator.validateProperty(jsonProject, "swidTagId")
);
if (jsonProject.getClassifier() == null) {
jsonProject.setClassifier(Classifier.APPLICATION);
}
try (QueryManager qm = new QueryManager()) {
Project project = qm.getObjectByUuid(Project.class, jsonProject.getUuid());
if (project != null) {
if (!qm.hasAccess(super.getPrincipal(), project)) {
return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified project is forbidden").build();
}
final String name = StringUtils.trimToNull(jsonProject.getName());
final String version = StringUtils.trimToNull(jsonProject.getVersion());
final Project tmpProject = qm.getProject(name, version);
if (tmpProject == null || (tmpProject.getUuid().equals(project.getUuid()))) {
// Name cannot be empty or null - prevent it
if (name == null) {
jsonProject.setName(project.getName());
}
try {
project = qm.updateProject(jsonProject, true);
} catch (IllegalArgumentException e){
LOGGER.debug(e.getMessage());
return Response.status(Response.Status.CONFLICT).entity(e.getMessage()).build();
}
LOGGER.info("Project " + project.toString() + " updated by " + super.getPrincipal().getName());
return Response.ok(project).build();
} else {
return Response.status(Response.Status.CONFLICT).entity("A project with the specified name and version already exists.").build();
}
} else {
return Response.status(Response.Status.NOT_FOUND).entity("The UUID of the project could not be found.").build();
}
}
}
@PATCH
@Path("/{uuid}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Partially updates a project",
response = Project.class
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 404, message = "The UUID of the project could not be found"),
@ApiResponse(code = 409, message = "- An inactive Parent cannot be selected as parent\n- Project cannot be set to inactive if active children are present\n- A project with the specified name already exists\n- A project cannot select itself as a parent")
})
@PermissionRequired(Permissions.Constants.PORTFOLIO_MANAGEMENT)
public Response patchProject(
@ApiParam(value = "The UUID of the project to modify", required = true)
@PathParam("uuid") String uuid,
Project jsonProject) {
final Validator validator = getValidator();
failOnValidationError(
validator.validateProperty(jsonProject, "author"),
validator.validateProperty(jsonProject, "publisher"),
validator.validateProperty(jsonProject, "group"),
jsonProject.getName() != null ? validator.validateProperty(jsonProject, "name") : Set.of(),
validator.validateProperty(jsonProject, "description"),
validator.validateProperty(jsonProject, "version"),
validator.validateProperty(jsonProject, "classifier"),
validator.validateProperty(jsonProject, "cpe"),
validator.validateProperty(jsonProject, "purl"),
validator.validateProperty(jsonProject, "swidTagId")
);
try (QueryManager qm = new QueryManager()) {
Project project = qm.getObjectByUuid(Project.class, uuid);
if (project != null) {
if (!qm.hasAccess(super.getPrincipal(), project)) {
return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified project is forbidden").build();
}
var modified = false;
project = qm.detachWithGroups(project, List.of(FetchGroup.DEFAULT, Project.FetchGroup.PARENT.name()));
modified |= setIfDifferent(jsonProject, project, Project::getName, Project::setName);
modified |= setIfDifferent(jsonProject, project, Project::getVersion, Project::setVersion);
// if either name or version has been changed, verify that this new combination does not already exist
if (modified && qm.doesProjectExist(project.getName(), project.getVersion())) {
return Response.status(Response.Status.CONFLICT).entity("A project with the specified name and version already exists.").build();
}
modified |= setIfDifferent(jsonProject, project, Project::getAuthor, Project::setAuthor);
modified |= setIfDifferent(jsonProject, project, Project::getPublisher, Project::setPublisher);
modified |= setIfDifferent(jsonProject, project, Project::getGroup, Project::setGroup);
modified |= setIfDifferent(jsonProject, project, Project::getDescription, Project::setDescription);
modified |= setIfDifferent(jsonProject, project, Project::getClassifier, Project::setClassifier);
modified |= setIfDifferent(jsonProject, project, Project::getCpe, Project::setCpe);
modified |= setIfDifferent(jsonProject, project, Project::getPurl, Project::setPurl);
modified |= setIfDifferent(jsonProject, project, Project::getSwidTagId, Project::setSwidTagId);
modified |= setIfDifferent(jsonProject, project, Project::isActive, Project::setActive);
modified |= setIfDifferent(jsonProject, project, Project::getManufacturer, Project::setManufacturer);
modified |= setIfDifferent(jsonProject, project, Project::getSupplier, Project::setSupplier);
if (jsonProject.getParent() != null && jsonProject.getParent().getUuid() != null) {
final Project parent = qm.getObjectByUuid(Project.class, jsonProject.getParent().getUuid());
if (parent == null) {
return Response.status(Response.Status.NOT_FOUND).entity("The UUID of the parent project could not be found.").build();
}
if (!qm.hasAccess(getPrincipal(), parent)) {
return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified parent project is forbidden").build();
}
modified |= project.getParent() == null || !parent.getUuid().equals(project.getParent().getUuid());
project.setParent(parent);
}
if (isCollectionModified(jsonProject.getTags(), project.getTags())) {
modified = true;
project.setTags(jsonProject.getTags());
}
if (isCollectionModified(jsonProject.getExternalReferences(), project.getExternalReferences())) {
modified = true;
project.setExternalReferences(jsonProject.getExternalReferences());
}
if (modified) {
try {
project = qm.updateProject(project, true);
} catch (IllegalArgumentException e){
LOGGER.debug(e.getMessage());
return Response.status(Response.Status.CONFLICT).entity(e.getMessage()).build();
}
LOGGER.info("Project " + project.toString() + " updated by " + super.getPrincipal().getName());
return Response.ok(project).build();
} else {
return Response.notModified().build();
}
} else {
return Response.status(Response.Status.NOT_FOUND).entity("The UUID of the project could not be found.").build();
}
}
}
/**
* returns `true` if the given [updated] collection should be considered an update of the [original] collection.
*/
private static <T> boolean isCollectionModified(Collection<T> updated, Collection<T> original) {
return updated != null && (!Collections.isEmpty(updated) || !Collections.isEmpty(original));
}
/**
* updates the given target object using the supplied setter method with the
* new value from the source object using the supplied getter method. But
* only if the new value is not {@code null} and it is not
* {@link Object#equals(java.lang.Object) equal to} the old value.
*
* @param <T> the type of the old and new value
* @param source the source object that contains the new value
* @param target the target object that should be updated
* @param getter the method to retrieve the new value from {@code source}
* and the old value from {@code target}
* @param setter the method to set the new value on {@code target}
* @return {@code true} if {@code target} has been changed, else
* {@code false}
*/
private <T> boolean setIfDifferent(final Project source, final Project target, final Function<Project, T> getter, final BiConsumer<Project, T> setter) {
final T newValue = getter.apply(source);
if (newValue != null && !newValue.equals(getter.apply(target))) {
setter.accept(target, newValue);
return true;
} else {
return false;
}
}
@DELETE
@Path("/{uuid}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Deletes a project",
code = 204
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Access to the specified project is forbidden"),
@ApiResponse(code = 404, message = "The UUID of the project could not be found")
})
@PermissionRequired(Permissions.Constants.PORTFOLIO_MANAGEMENT)
public Response deleteProject(
@ApiParam(value = "The UUID of the project to delete", required = true)
@PathParam("uuid") String uuid) {
try (QueryManager qm = new QueryManager()) {
final Project project = qm.getObjectByUuid(Project.class, uuid, Project.FetchGroup.ALL.name());
if (project != null) {
if (qm.hasAccess(super.getPrincipal(), project)) {
LOGGER.info("Project " + project + " deletion request by " + super.getPrincipal().getName());
qm.recursivelyDelete(project, true);
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified project is forbidden").build();
}
} else {
return Response.status(Response.Status.NOT_FOUND).entity("The UUID of the project could not be found.").build();
}
}
}
@PUT
@Path("/clone")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Clones a project",
response = Project.class
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 404, message = "The UUID of the project could not be found")
})
@PermissionRequired(Permissions.Constants.PORTFOLIO_MANAGEMENT)
public Response cloneProject(CloneProjectRequest jsonRequest) {
final Validator validator = super.getValidator();
failOnValidationError(
validator.validateProperty(jsonRequest, "project"),
validator.validateProperty(jsonRequest, "version")
);
try (QueryManager qm = new QueryManager()) {
final Project sourceProject = qm.getObjectByUuid(Project.class, jsonRequest.getProject(), Project.FetchGroup.ALL.name());
if (sourceProject != null) {
if (!qm.hasAccess(super.getPrincipal(), sourceProject)) {
return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified project is forbidden").build();
}
if (qm.doesProjectExist(sourceProject.getName(), StringUtils.trimToNull(jsonRequest.getVersion()))) {
return Response.status(Response.Status.CONFLICT).entity("A project with the specified name and version already exists.").build();
}
LOGGER.info("Project " + sourceProject + " is being cloned by " + super.getPrincipal().getName());
Event.dispatch(new CloneProjectEvent(jsonRequest));
return Response.ok().build();
} else {
return Response.status(Response.Status.NOT_FOUND).entity("The UUID of the project could not be found.").build();
}
}
}
@GET
@Path("/{uuid}/children")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Returns a list of all children for a project",
response = Project.class,
responseContainer = "List",
responseHeaders = @ResponseHeader(name = TOTAL_COUNT_HEADER, response = Long.class, description = "The total number of projects")
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Access to the specified project is forbidden"),
@ApiResponse(code = 404, message = "The UUID of the project could not be found")
})
@PermissionRequired(Permissions.Constants.VIEW_PORTFOLIO)
public Response getChildrenProjects(@ApiParam(value = "The UUID of the project to get the children from", required = true)
@PathParam("uuid") String uuid,
@ApiParam(value = "Optionally excludes inactive projects from being returned", required = false)
@QueryParam("excludeInactive") boolean excludeInactive) {
try (QueryManager qm = new QueryManager(getAlpineRequest())) {
final Project project = qm.getObjectByUuid(Project.class, uuid);
if (project != null) {
final PaginatedResult result = qm.getChildrenProjects(project.getUuid(), true, excludeInactive);
if (qm.hasAccess(super.getPrincipal(), project)) {
return Response.ok(result.getObjects()).header(TOTAL_COUNT_HEADER, result.getTotal()).build();
} else {
return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified project is forbidden").build();
}
} else {
return Response.status(Response.Status.NOT_FOUND).entity("The UUID of the project could not be found.").build();
}
}
}
@GET
@Path("/{uuid}/children/classifier/{classifier}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Returns a list of all children for a project by classifier",
response = Project.class,
responseContainer = "List",
responseHeaders = @ResponseHeader(name = TOTAL_COUNT_HEADER, response = Long.class, description = "The total number of projects")
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Access to the specified project is forbidden"),
@ApiResponse(code = 404, message = "The UUID of the project could not be found")
})
@PermissionRequired(Permissions.Constants.VIEW_PORTFOLIO)
public Response getChildrenProjectsByClassifier(
@ApiParam(value = "The classifier to query on", required = true)
@PathParam("classifier") String classifierString,
@ApiParam(value = "The UUID of the project to get the children from", required = true)
@PathParam("uuid") String uuid,
@ApiParam(value = "Optionally excludes inactive projects from being returned", required = false)
@QueryParam("excludeInactive") boolean excludeInactive) {
try (QueryManager qm = new QueryManager(getAlpineRequest())) {
final Project project = qm.getObjectByUuid(Project.class, uuid);
if (project != null) {
final Classifier classifier = Classifier.valueOf(classifierString);
final PaginatedResult result = qm.getChildrenProjects(classifier, project.getUuid(), true, excludeInactive);
if (qm.hasAccess(super.getPrincipal(), project)) {
return Response.ok(result.getObjects()).header(TOTAL_COUNT_HEADER, result.getTotal()).build();
} else {
return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified project is forbidden").build();
}
} else {
return Response.status(Response.Status.NOT_FOUND).entity("The UUID of the project could not be found.").build();
}
}
}
@GET
@Path("/{uuid}/children/tag/{tag}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Returns a list of all children for a project by tag",
response = Project.class,
responseContainer = "List",
responseHeaders = @ResponseHeader(name = TOTAL_COUNT_HEADER, response = Long.class, description = "The total number of projects")
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Access to the specified project is forbidden"),
@ApiResponse(code = 404, message = "The UUID of the project could not be found")
})
@PermissionRequired(Permissions.Constants.VIEW_PORTFOLIO)
public Response getChildrenProjectsByTag(
@ApiParam(value = "The tag to query on", required = true)
@PathParam("tag") String tagString,
@ApiParam(value = "The UUID of the project to get the children from", required = true)
@PathParam("uuid") String uuid,
@ApiParam(value = "Optionally excludes inactive projects from being returned", required = false)
@QueryParam("excludeInactive") boolean excludeInactive) {
try (QueryManager qm = new QueryManager(getAlpineRequest())) {
final Project project = qm.getObjectByUuid(Project.class, uuid);
if (project != null) {
final Tag tag = qm.getTagByName(tagString);
final PaginatedResult result = qm.getChildrenProjects(tag, project.getUuid(), true, excludeInactive);
if (qm.hasAccess(super.getPrincipal(), project)) {
return Response.ok(result.getObjects()).header(TOTAL_COUNT_HEADER, result.getTotal()).build();
} else {
return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified project is forbidden").build();
}
} else {
return Response.status(Response.Status.NOT_FOUND).entity("The UUID of the project could not be found.").build();
}
}
}
@GET
@Path("/withoutDescendantsOf/{uuid}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Returns a list of all projects without the descendants of the selected project",
response = Project.class,
responseContainer = "List",
responseHeaders = @ResponseHeader(name = TOTAL_COUNT_HEADER, response = Long.class, description = "The total number of projects")
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Access to the specified project is forbidden"),
@ApiResponse(code = 404, message = "The UUID of the project could not be found")
})
@PermissionRequired(Permissions.Constants.VIEW_PORTFOLIO)
public Response getProjectsWithoutDescendantsOf(
@ApiParam(value = "The UUID of the project which descendants will be excluded", required = true)
@PathParam("uuid") String uuid,
@ApiParam(value = "The optional name of the project to query on", required = false)
@QueryParam("name") String name,
@ApiParam(value = "Optionally excludes inactive projects from being returned", required = false)
@QueryParam("excludeInactive") boolean excludeInactive) {
try (QueryManager qm = new QueryManager(getAlpineRequest())) {
final Project project = qm.getObjectByUuid(Project.class, uuid);
if (project != null) {
if (qm.hasAccess(super.getPrincipal(), project)) {
final PaginatedResult result = (name != null) ? qm.getProjectsWithoutDescendantsOf(name, excludeInactive, project) : qm.getProjectsWithoutDescendantsOf(excludeInactive, project);
return Response.ok(result.getObjects()).header(TOTAL_COUNT_HEADER, result.getTotal()).build();
} else{
return Response.status(Response.Status.FORBIDDEN).entity("Access to the specified project is forbidden").build();
}
} else {
return Response.status(Response.Status.NOT_FOUND).entity("The UUID of the project could not be found.").build();
}
}
}
}