Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

Commit

Permalink
feat(rest): list all transitive releases for a specific project
Browse files Browse the repository at this point in the history
- new optional request parameter for project controller (transitive)
  • Loading branch information
maierthomas committed Feb 7, 2018
1 parent a41a66e commit 44a4f0b
Show file tree
Hide file tree
Showing 21 changed files with 237 additions and 291 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright Siemens AG, 2017. Part of the SW360 Portal Project.
* Copyright Siemens AG, 2017-2018. Part of the SW360 Portal Project.
*
* SPDX-License-Identifier: EPL-1.0
*
Expand All @@ -14,6 +14,7 @@
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.thrift.attachments.Attachment;
import org.eclipse.sw360.datahandler.thrift.components.Release;
import org.eclipse.sw360.datahandler.thrift.users.User;
Expand Down Expand Up @@ -52,40 +53,32 @@ public class AttachmentController implements ResourceProcessor<RepositoryLinksRe
@RequestMapping(value = ATTACHMENTS_URL, params = "sha1", method = RequestMethod.GET)
public ResponseEntity<Resource<Attachment>> getAttachmentForSha1(
OAuth2Authentication oAuth2Authentication,
@RequestParam String sha1) {
try {
User sw360User = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
AttachmentInfo attachmentInfo = attachmentService.getAttachmentBySha1ForUser(sha1, sw360User);
HalResource<Attachment> attachmentResource =
createHalAttachment(
attachmentInfo.getAttachment(),
attachmentInfo.getRelease(),
sw360User);
return new ResponseEntity<>(attachmentResource, HttpStatus.OK);
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
@RequestParam String sha1) throws TException {

User sw360User = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
AttachmentInfo attachmentInfo = attachmentService.getAttachmentBySha1ForUser(sha1, sw360User);
HalResource<Attachment> attachmentResource =
createHalAttachment(
attachmentInfo.getAttachment(),
attachmentInfo.getRelease(),
sw360User);
return new ResponseEntity<>(attachmentResource, HttpStatus.OK);
}

@RequestMapping(value = ATTACHMENTS_URL + "/{id}", method = RequestMethod.GET)
public ResponseEntity<Resource<Attachment>> getAttachmentForId(
@PathVariable("id") String id,
OAuth2Authentication oAuth2Authentication) {
try {
User sw360User = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
AttachmentInfo attachmentInfo =
attachmentService.getAttachmentByIdForUser(id, sw360User);

HalResource<Attachment> attachmentResource =
createHalAttachment(attachmentInfo.getAttachment(),
attachmentInfo.getRelease(),
sw360User);
return new ResponseEntity<>(attachmentResource, HttpStatus.OK);
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
OAuth2Authentication oAuth2Authentication) throws TException {

User sw360User = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
AttachmentInfo attachmentInfo =
attachmentService.getAttachmentByIdForUser(id, sw360User);

HalResource<Attachment> attachmentResource =
createHalAttachment(attachmentInfo.getAttachment(),
attachmentInfo.getRelease(),
sw360User);
return new ResponseEntity<>(attachmentResource, HttpStatus.OK);
}

private HalResource<Attachment> createHalAttachment(
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright Siemens AG, 2017. Part of the SW360 Portal Project.
* Copyright Siemens AG, 2017-2018. Part of the SW360 Portal Project.
*
* SPDX-License-Identifier: EPL-1.0
*
Expand Down Expand Up @@ -36,42 +36,34 @@ public class Sw360AttachmentService {
@Value("${sw360.thrift-server-url:http://localhost:8080}")
private String thriftServerUrl;

public AttachmentInfo getAttachmentBySha1ForUser(String sha1, User sw360User) {
try {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
List<Release> releases = sw360ComponentClient.getReleaseSummary(sw360User);
for (Release release : releases) {
final Set<Attachment> attachments = release.getAttachments();
if (attachments != null && attachments.size() > 0) {
for (Attachment attachment : attachments) {
if (sha1.equals(attachment.getSha1())) {
return new AttachmentInfo(attachment, release);
}
public AttachmentInfo getAttachmentBySha1ForUser(String sha1, User sw360User) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
List<Release> releases = sw360ComponentClient.getReleaseSummary(sw360User);
for (Release release : releases) {
final Set<Attachment> attachments = release.getAttachments();
if (attachments != null && attachments.size() > 0) {
for (Attachment attachment : attachments) {
if (sha1.equals(attachment.getSha1())) {
return new AttachmentInfo(attachment, release);
}
}
}
} catch (TException e) {
log.error("Cannot get attachment from sw360 with sha1: " + sha1);
}
return null;
}

public AttachmentInfo getAttachmentByIdForUser(String id, User sw360User) {
try {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
List<Release> releases = sw360ComponentClient.getReleaseSummary(sw360User);
for (Release release : releases) {
final Set<Attachment> attachments = release.getAttachments();
if (attachments != null && attachments.size() > 0) {
for (Attachment attachment : attachments) {
if (id.equals(attachment.getAttachmentContentId())) {
return new AttachmentInfo(attachment, release);
}
public AttachmentInfo getAttachmentByIdForUser(String id, User sw360User) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
List<Release> releases = sw360ComponentClient.getReleaseSummary(sw360User);
for (Release release : releases) {
final Set<Attachment> attachments = release.getAttachments();
if (attachments != null && attachments.size() > 0) {
for (Attachment attachment : attachments) {
if (id.equals(attachment.getAttachmentContentId())) {
return new AttachmentInfo(attachment, release);
}
}
}
} catch (TException e) {
log.error("Cannot get attachment from sw360 with id: " + id);
}
return null;
}
Expand Down
Expand Up @@ -16,6 +16,7 @@
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.thrift.components.Component;
import org.eclipse.sw360.datahandler.thrift.components.Release;
import org.eclipse.sw360.datahandler.thrift.users.User;
Expand Down Expand Up @@ -64,7 +65,7 @@ public class ComponentController implements ResourceProcessor<RepositoryLinksRes
@RequestMapping(value = COMPONENTS_URL, method = RequestMethod.GET)
public ResponseEntity<Resources<Resource<Component>>> getComponents(@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "type", required = false) String componentType,
OAuth2Authentication oAuth2Authentication) {
OAuth2Authentication oAuth2Authentication) throws TException {

User sw360User = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
List<Component> sw360Components = new ArrayList<>();
Expand Down Expand Up @@ -92,7 +93,7 @@ public ResponseEntity<Resources<Resource<Component>>> getComponents(@RequestPara

@RequestMapping(value = COMPONENTS_URL + "/{id}", method = RequestMethod.GET)
public ResponseEntity<Resource<Component>> getComponent(
@PathVariable("id") String id, OAuth2Authentication oAuth2Authentication) {
@PathVariable("id") String id, OAuth2Authentication oAuth2Authentication) throws TException {
User user = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
Component sw360Component = componentService.getComponentForUserById(id, user);
HalResource<Component> userHalResource = createHalComponent(sw360Component, user);
Expand All @@ -103,7 +104,7 @@ public ResponseEntity<Resource<Component>> getComponent(
@RequestMapping(value = COMPONENTS_URL, method = RequestMethod.POST)
public ResponseEntity<Resource<Component>> createComponent(
OAuth2Authentication oAuth2Authentication,
@RequestBody Component component) throws URISyntaxException {
@RequestBody Component component) throws URISyntaxException, TException {

User user = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);

Expand Down Expand Up @@ -136,7 +137,7 @@ public RepositoryLinksResource process(RepositoryLinksResource resource) {
return resource;
}

private HalResource<Component> createHalComponent(Component sw360Component, User user) {
private HalResource<Component> createHalComponent(Component sw360Component, User user) throws TException {
HalResource<Component> halComponent = new HalResource<>(sw360Component);

if (sw360Component.getReleaseIds() != null) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright Siemens AG, 2017.
* Copyright Siemens AG, 2017-2018.
* Copyright Bosch Software Innovations GmbH, 2017.
* Part of the SW360 Portal Project.
*
Expand Down Expand Up @@ -37,47 +37,31 @@ public class Sw360ComponentService {
@Value("${sw360.thrift-server-url:http://localhost:8080}")
private String thriftServerUrl;

public List<Component> getComponentsForUser(User sw360User) {
try {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
return sw360ComponentClient.getComponentSummary(sw360User);
} catch (TException e) {
throw new RuntimeException(e);
}
public List<Component> getComponentsForUser(User sw360User) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
return sw360ComponentClient.getComponentSummary(sw360User);
}

public Component getComponentForUserById(String componentId, User sw360User) {
try {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
return sw360ComponentClient.getComponentById(componentId, sw360User);
} catch (TException e) {
throw new RuntimeException(e);
}
public Component getComponentForUserById(String componentId, User sw360User) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
return sw360ComponentClient.getComponentById(componentId, sw360User);
}

public Component createComponent(Component component, User sw360User) {
try {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
AddDocumentRequestSummary documentRequestSummary = sw360ComponentClient.addComponent(component, sw360User);
if (documentRequestSummary.getRequestStatus() == AddDocumentRequestStatus.SUCCESS) {
component.setId(documentRequestSummary.getId());
return component;
} else if (documentRequestSummary.getRequestStatus() == AddDocumentRequestStatus.DUPLICATE) {
throw new DataIntegrityViolationException("sw360 component with name '" + component.getName() + "' already exists.");
}
} catch (TException e) {
throw new RuntimeException(e);
public Component createComponent(Component component, User sw360User) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
AddDocumentRequestSummary documentRequestSummary = sw360ComponentClient.addComponent(component, sw360User);
if (documentRequestSummary.getRequestStatus() == AddDocumentRequestStatus.SUCCESS) {
component.setId(documentRequestSummary.getId());
return component;
} else if (documentRequestSummary.getRequestStatus() == AddDocumentRequestStatus.DUPLICATE) {
throw new DataIntegrityViolationException("sw360 component with name '" + component.getName() + "' already exists.");
}
return null;
}

public List<Component> searchComponentByName(String name) {
try {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
return sw360ComponentClient.searchComponentForExport(name);
} catch (TException e) {
throw new RuntimeException(e);
}
public List<Component> searchComponentByName(String name) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
return sw360ComponentClient.searchComponentForExport(name);
}

private ComponentService.Iface getThriftComponentClient() throws TTransportException {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright Siemens AG, 2017. Part of the SW360 Portal Project.
* Copyright Siemens AG, 2017-2018. Part of the SW360 Portal Project.
*
* SPDX-License-Identifier: EPL-1.0
*
Expand All @@ -14,6 +14,7 @@
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.thrift.attachments.Attachment;
import org.eclipse.sw360.datahandler.thrift.components.Component;
import org.eclipse.sw360.datahandler.thrift.components.Release;
Expand Down Expand Up @@ -75,8 +76,12 @@ public void addEmbeddedReleases(
User user,
String linkRelation) {
for (String releaseId : releases) {
final Release release = sw360ReleaseService.getReleaseForUserById(releaseId, user);
addEmbeddedRelease(halResource, release, linkRelation);
try {
final Release release = sw360ReleaseService.getReleaseForUserById(releaseId, user);
addEmbeddedRelease(halResource, release, linkRelation);
} catch (TException e) {
log.error("cannot create embedded releases");
}
}
}

Expand Down Expand Up @@ -110,7 +115,6 @@ public void addEmbeddedVendors(HalResource<Component> halComponent, Set<String>
HalResource<Vendor> vendorHalResource = addEmbeddedVendor(vendorFullName);
halComponent.addEmbeddedResource("vendors", vendorHalResource);
}

}

private HalResource<Vendor> addEmbeddedVendor(String vendorFullName) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright Siemens AG, 2017. Part of the SW360 Portal Project.
* Copyright Siemens AG, 2017-2018. Part of the SW360 Portal Project.
*
* SPDX-License-Identifier: EPL-1.0
*
Expand All @@ -14,6 +14,7 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.apache.thrift.TException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
Expand All @@ -31,33 +32,33 @@
@ControllerAdvice
public class RestExceptionHandler {

@ExceptionHandler(Exception.class)
@ExceptionHandler({Exception.class, TException.class})
public ResponseEntity<ErrorMessage> handleException(Exception e) {
return new ResponseEntity<>(new ErrorMessage(e, HttpStatus.INTERNAL_SERVER_ERROR), HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<ErrorMessage> handleAccessDeniedException(Exception e) {
public ResponseEntity<ErrorMessage> handleAccessDeniedException(AccessDeniedException e) {
return new ResponseEntity<>(new ErrorMessage(e, HttpStatus.FORBIDDEN), HttpStatus.FORBIDDEN);
}

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ErrorMessage> handleResourceNotFound(Exception e) {
public ResponseEntity<ErrorMessage> handleResourceNotFound(ResourceNotFoundException e) {
return new ResponseEntity<>(new ErrorMessage(e, HttpStatus.NOT_FOUND), HttpStatus.NOT_FOUND);
}

@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<ErrorMessage> handleMessageNotReadableException(Exception e) {
public ResponseEntity<ErrorMessage> handleMessageNotReadableException(HttpMessageNotReadableException e) {
return new ResponseEntity<>(new ErrorMessage(e, HttpStatus.BAD_REQUEST), HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<ErrorMessage> handleRequestMethodNotSupported(Exception e) {
public ResponseEntity<ErrorMessage> handleRequestMethodNotSupported(HttpRequestMethodNotSupportedException e) {
return new ResponseEntity<>(new ErrorMessage(e, HttpStatus.METHOD_NOT_ALLOWED), HttpStatus.METHOD_NOT_ALLOWED);
}

@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ResponseEntity<ErrorMessage> handleMediaTypeNotSupportedException(Exception e) {
public ResponseEntity<ErrorMessage> handleMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
return new ResponseEntity<>(new ErrorMessage(e, HttpStatus.UNSUPPORTED_MEDIA_TYPE), HttpStatus.UNSUPPORTED_MEDIA_TYPE);
}

Expand Down

0 comments on commit 44a4f0b

Please sign in to comment.