Skip to content

Commit

Permalink
feat(REST): Added new Rest API endpoint for reading clearing request
Browse files Browse the repository at this point in the history
Signed-off-by: Abdul Kapti <abdul.kapti@siemens-healthineers.com>
  • Loading branch information
Abdul Kapti committed Mar 24, 2021
1 parent 958a8a7 commit 028ee60
Show file tree
Hide file tree
Showing 12 changed files with 382 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public RequestStatus addCommentToClearingRequest(String id, Comment comment, Use
public ClearingRequest getClearingRequestByProjectId(String projectId, User user) {
ModerationService.Iface client = thriftClients.makeModerationClient();
try {
return client.getClearingRequestByProjectId(projectId);
return client.getClearingRequestByProjectId(projectId, user);
} catch (TException e) {
log.error("Could not find CR for Project: " + projectId + " by User " + user.getEmail(), e);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,11 @@ public List<ModerationRequest> getRequestsByRequestingUser(User user) throws TEx
}

@Override
public ClearingRequest getClearingRequestByProjectId(String projectId) throws TException {
public ClearingRequest getClearingRequestByProjectId(String projectId, User user) throws TException {
assertId(projectId);
assertUser(user);

return handler.getClearingRequestByProjectId(projectId);
return handler.getClearingRequestByProjectId(projectId, user);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public List<ModerationRequest> getRequestsByRequestingUser(String user) {
return repository.getRequestsByRequestingUser(user);
}

public ClearingRequest getClearingRequestByProjectId(String projectId) {
public ClearingRequest getClearingRequestByProjectId(String projectId, User user) throws SW360Exception {
projectDatabaseHandler.getProjectById(projectId, user); // check if user have READ access to project.
return clearingRequestRepository.getClearingRequestByProjectId(projectId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ service ModerationService {
/**
* get clearing request by project Id
**/
ClearingRequest getClearingRequestByProjectId(1: string projectId);
ClearingRequest getClearingRequestByProjectId(1: string projectId, 2: User user);

/**
* update clearing request for associated project deletion
Expand Down
1 change: 1 addition & 0 deletions rest/resource-server/src/docs/asciidoc/api-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,4 @@ include::vulnerabilities.adoc[]
include::health.adoc[]
include::search.adoc[]
include::changeLogs.adoc[]
include::clearingRequest.adoc[]
44 changes: 44 additions & 0 deletions rest/resource-server/src/docs/asciidoc/clearingRequest.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Copyright Siemens AG, 2020. Part of the SW360 Portal Project.
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//

[[resources-clearingRequest]]
=== ClearingRequest

The ClearingRequest resource is used get the clearing request of a project


[[resources-clearingRequest-by-id]]
==== Get clearingRequest by id

A `GET` request will display the ClearingRequest by id

===== Response structure
include::{snippets}/should_document_get_clearingrequest/response-fields.adoc[]

===== Example request
include::{snippets}/should_document_get_clearingrequest/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_get_clearingrequest/http-response.adoc[]


[[resources-clearingRequest-by-project-id]]
==== Get clearingRequest by project id

A `GET` request will display the ClearingRequest based on the project id

===== Response structure
include::{snippets}/should_document_get_clearingrequest_by_projectid/response-fields.adoc[]

===== Example request
include::{snippets}/should_document_get_clearingrequest_by_projectid/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_get_clearingrequest_by_projectid/http-response.adoc[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright Siemens AG, 2020. Part of the SW360 Portal Project.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.sw360.rest.resourceserver.clearingrequest;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;

import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.thrift.ProjectReleaseRelationship;
import org.eclipse.sw360.datahandler.thrift.projects.ClearingRequest;
import org.eclipse.sw360.datahandler.thrift.projects.Project;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectRelationship;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.rest.resourceserver.changelog.ChangeLogController;
import org.eclipse.sw360.rest.resourceserver.core.HalResource;
import org.eclipse.sw360.rest.resourceserver.core.RestControllerHelper;
import org.eclipse.sw360.rest.resourceserver.project.Sw360ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.data.rest.webmvc.RepositoryLinksResource;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceProcessor;
import org.springframework.hateoas.UriTemplate;
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@BasePathAwareController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ClearingRequestController implements ResourceProcessor<RepositoryLinksResource> {

private static final Logger log = LogManager.getLogger(ClearingRequestController.class);

public static final String CLEARING_REQUEST_URL = "/clearingrequest";
@Autowired
private Sw360ClearingRequestService sw360ClearingRequestService;

@NonNull
private final RestControllerHelper restControllerHelper;

@NonNull
private final Sw360ProjectService projectService;

@NonNull
private final com.fasterxml.jackson.databind.Module sw360Module;


@RequestMapping(value = CLEARING_REQUEST_URL + "/{id}", method = RequestMethod.GET)
public ResponseEntity<Resource<ClearingRequest>> getClearingRequestById(Pageable pageable, @PathVariable("id") String docId,
HttpServletRequest request) throws TException, URISyntaxException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
ClearingRequest clearingRequest = sw360ClearingRequestService.getClearingRequestById(docId, sw360User);

HttpStatus status = clearingRequest == null ? HttpStatus.NO_CONTENT : HttpStatus.OK;
return new ResponseEntity(clearingRequest, status);
}

@RequestMapping(value = CLEARING_REQUEST_URL + "/project/{id}", method = RequestMethod.GET)
public ResponseEntity<Resource<ClearingRequest>> getClearingRequestByProjectId(Pageable pageable, @PathVariable("id") String projectId,
HttpServletRequest request) throws TException, URISyntaxException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
ClearingRequest clearingRequest = sw360ClearingRequestService.getClearingRequestByProjectId(projectId, sw360User);

HttpStatus status = clearingRequest == null ? HttpStatus.NO_CONTENT : HttpStatus.OK;
return new ResponseEntity(clearingRequest, status);
}

@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
final ControllerLinkBuilder controllerLinkBuilder = linkTo(ClearingRequestController.class);
final Link changelogLink = new Link(
new UriTemplate(controllerLinkBuilder.toUri().toString() + "/api" + CLEARING_REQUEST_URL + "/{id}"),
"clearingRequest");
resource.add(changelogLink);
return resource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright Siemens AG, 2020. Part of the SW360 Portal Project.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.sw360.rest.resourceserver.clearingrequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;
import org.apache.thrift.transport.TTransportException;
import org.eclipse.sw360.datahandler.thrift.moderation.ModerationService;
import org.eclipse.sw360.datahandler.thrift.projects.ClearingRequest;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class Sw360ClearingRequestService {
private static final Logger log = LogManager.getLogger(Sw360ClearingRequestService.class);

@Value("${sw360.thrift-server-url:http://localhost:8080}")
private String thriftServerUrl;

private ModerationService.Iface getThriftModerationClient() throws TTransportException {
THttpClient thriftClient = new THttpClient(thriftServerUrl + "/moderation/thrift");
TProtocol protocol = new TCompactProtocol(thriftClient);
return new ModerationService.Client(protocol);
}

public ClearingRequest getClearingRequestByProjectId(String projectId, User sw360User) throws TException {
return getThriftModerationClient().getClearingRequestByProjectId(projectId, sw360User);
}

public ClearingRequest getClearingRequestById(String id, User sw360User) throws TException {
return getThriftModerationClient().getClearingRequestById(id, sw360User);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.eclipse.sw360.datahandler.thrift.components.Component;
import org.eclipse.sw360.datahandler.thrift.components.Release;
import org.eclipse.sw360.datahandler.thrift.licenses.License;
import org.eclipse.sw360.datahandler.thrift.projects.ClearingRequest;
import org.eclipse.sw360.datahandler.thrift.projects.Project;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.vendors.Vendor;
Expand Down Expand Up @@ -50,6 +51,8 @@ public HalResource(T content, Link... links) {
((Vulnerability) content).setType(null);
} else if (content instanceof VulnerabilityDTO) {
((VulnerabilityDTO) content).setType(null);
} else if (content instanceof ClearingRequest) {
((ClearingRequest) content).setType(null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.module.SimpleModule;

import org.eclipse.sw360.datahandler.thrift.Comment;
import org.eclipse.sw360.datahandler.thrift.ProjectReleaseRelationship;
import org.eclipse.sw360.datahandler.thrift.Visibility;
import org.eclipse.sw360.datahandler.thrift.attachments.Attachment;
Expand All @@ -26,6 +27,7 @@
import org.eclipse.sw360.datahandler.thrift.changelogs.ReferenceDocData;
import org.eclipse.sw360.datahandler.thrift.components.*;
import org.eclipse.sw360.datahandler.thrift.licenses.License;
import org.eclipse.sw360.datahandler.thrift.projects.ClearingRequest;
import org.eclipse.sw360.datahandler.thrift.projects.Project;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectRelationship;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectState;
Expand Down Expand Up @@ -76,6 +78,8 @@ public Sw360Module() {
setMixInAnnotation(ChangeLogs.class, Sw360Module.ChangeLogsMixin.class);
setMixInAnnotation(ChangedFields.class, Sw360Module.ChangedFieldsMixin.class);
setMixInAnnotation(ReferenceDocData.class, Sw360Module.ReferenceDocDataMixin.class);
setMixInAnnotation(ClearingRequest.class, Sw360Module.ClearingRequestMixin.class);
setMixInAnnotation(Comment.class, Sw360Module.CommentMixin.class);
}

@JsonInclude(JsonInclude.Include.NON_NULL)
Expand Down Expand Up @@ -869,5 +873,49 @@ public static abstract class ChangedFieldsMixin extends ChangedFields {
})
public static abstract class ReferenceDocDataMixin extends ReferenceDocData {
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties({
"revision",
"setId",
"type",
"setType",
"timestamp",
"timestampOfDecision",
"reOpenOnSize",
"reOpenOnIterator",
"setReOpenOn",
"setClearingState",
"setRevision",
"setProjectId",
"setClearingTeam",
"setTimestamp",
"setTimestampOfDecision",
"setRequestingUser",
"setComments",
"setModifiedOn",
"setRequestedClearingDate",
"setProjectBU",
"setRequestingUserComment",
"setAgreedClearingDate",
"commentsIterator",
"modifiedOn",
"commentsSize"
})
@JsonRootName(value = "clearingRequest")
public static abstract class ClearingRequestMixin extends ClearingRequest {
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties({
"setText",
"setCommentedBy",
"commentedOn",
"setCommentedOn",
"autoGenerated",
"setAutoGenerated"
})
public static abstract class CommentMixin extends Comment {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public void should_document_index() throws Exception {
linkWithRel("sw360:vulnerabilities").description("The <<resources-vulnerabilities,Vulnerabilities resource>>"),
linkWithRel("sw360:searchs").description("The <<resources-search,Vulnerabilities resource>>"),
linkWithRel("sw360:changeLogs").description("The <<resources-changelog,Changelog resource>>"),
linkWithRel("sw360:clearingRequest").description("The <<resources-clearingRequest,ClearingRequest resource>>"),
linkWithRel("curies").description("The Curies for documentation"),
linkWithRel("profile").description("The profiles of the REST resources")
),
Expand Down
Loading

0 comments on commit 028ee60

Please sign in to comment.