Skip to content

Commit

Permalink
fix(rest): Show attachments as embedded resource list
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Maier <thomas.maier@evosoft.com>
  • Loading branch information
maierthomas committed Aug 29, 2018
1 parent 8e09b9b commit c2c0afe
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ private HalResource<Component> createHalComponent(Component sw360Component, User
sw360Component.setVendorNames(null);
}

if (sw360Component.getAttachments() != null) {
restControllerHelper.addEmbeddedAttachments(halComponent, sw360Component.getAttachments());
}

restControllerHelper.addEmbeddedUser(halComponent, user, "createdBy");

return halComponent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.net.URLEncoder;
import java.util.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.*;

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

Expand Down Expand Up @@ -202,7 +202,7 @@ public void addEmbeddedVendors(HalResource<Component> halComponent, Set<String>
}
}

private HalResource<Vendor> addEmbeddedVendor(String vendorFullName) {
public HalResource<Vendor> addEmbeddedVendor(String vendorFullName) {
Vendor embeddedVendor = convertToEmbeddedVendor(vendorFullName);
HalResource<Vendor> halVendor = new HalResource<>(embeddedVendor);
try {
Expand Down Expand Up @@ -241,33 +241,6 @@ private HalResource<License> addEmbeddedLicense(String licenseId) {
return null;
}

public HalResource<Release> createHalReleaseResource(Release release, boolean verbose) {
HalResource<Release> halRelease = new HalResource<>(release);
Link componentLink = linkTo(ReleaseController.class)
.slash("api" + ComponentController.COMPONENTS_URL + "/" + release.getComponentId()).withRel("component");
halRelease.add(componentLink);
release.setComponentId(null);

if (verbose) {
if (release.getModerators() != null) {
Set<String> moderators = release.getModerators();
this.addEmbeddedModerators(halRelease, moderators);
release.setModerators(null);
}
if (release.getVendor() != null) {
Vendor vendor = release.getVendor();
HalResource<Vendor> vendorHalResource = this.addEmbeddedVendor(vendor.getFullname());
halRelease.addEmbeddedResource("sw360:vendors", vendorHalResource);
release.setVendor(null);
}
if (release.getMainLicenseIds() != null) {
this.addEmbeddedLicenses(halRelease, release.getMainLicenseIds());
release.setMainLicenseIds(null);
}
}
return halRelease;
}

public void addEmbeddedRelease(HalResource halResource, Release release) {
Release embeddedRelease = convertToEmbeddedRelease(release);
HalResource<Release> halRelease = new HalResource<>(embeddedRelease);
Expand All @@ -277,7 +250,7 @@ public void addEmbeddedRelease(HalResource halResource, Release release) {
halResource.addEmbeddedResource("sw360:releases", halRelease);
}

private void addEmbeddedAttachments(
public void addEmbeddedAttachments(
HalResource halResource,
Set<Attachment> attachments) {
for (Attachment attachment : attachments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,10 @@ private HalResource<Project> createHalProject(Project sw360Project, User sw360Us
restControllerHelper.addEmbeddedModerators(halProject, moderators);
}

if (sw360Project.getAttachments() != null) {
restControllerHelper.addEmbeddedAttachments(halProject, sw360Project.getAttachments());
}

return halProject;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.attachments.Attachment;
import org.eclipse.sw360.datahandler.thrift.components.Release;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.vendors.Vendor;
import org.eclipse.sw360.rest.resourceserver.attachment.AttachmentInfo;
import org.eclipse.sw360.rest.resourceserver.attachment.Sw360AttachmentService;
import org.eclipse.sw360.rest.resourceserver.component.ComponentController;
import org.eclipse.sw360.rest.resourceserver.core.HalResource;
import org.eclipse.sw360.rest.resourceserver.core.MultiStatus;
import org.eclipse.sw360.rest.resourceserver.core.RestControllerHelper;
import org.springframework.beans.factory.annotation.Autowired;
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.Resources;
Expand Down Expand Up @@ -103,7 +105,7 @@ public ResponseEntity<Resource> getRelease(
@PathVariable("id") String id, OAuth2Authentication oAuth2Authentication) throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
Release sw360Release = releaseService.getReleaseForUserById(id, sw360User);
HalResource halRelease = restControllerHelper.createHalReleaseResource(sw360Release, true);
HalResource halRelease = createHalReleaseResource(sw360Release, true);
return new ResponseEntity<>(halRelease, HttpStatus.OK);
}

Expand Down Expand Up @@ -136,7 +138,7 @@ public ResponseEntity<Resource<Release>> patchComponent(
Release sw360Release = releaseService.getReleaseForUserById(id, user);
sw360Release = this.restControllerHelper.updateRelease(sw360Release, updateRelease);
releaseService.updateRelease(sw360Release, user);
HalResource<Release> halRelease = restControllerHelper.createHalReleaseResource(sw360Release, true);
HalResource<Release> halRelease = createHalReleaseResource(sw360Release, true);
return new ResponseEntity<>(halRelease, HttpStatus.OK);
}

Expand Down Expand Up @@ -173,7 +175,7 @@ public ResponseEntity<Resource<Release>> createRelease(
}

Release sw360Release = releaseService.createRelease(release, sw360User);
HalResource<Release> halResource = restControllerHelper.createHalReleaseResource(sw360Release, true);
HalResource<Release> halResource = createHalReleaseResource(sw360Release, true);

URI location = ServletUriComponentsBuilder
.fromCurrentRequest().path("/{id}")
Expand Down Expand Up @@ -210,7 +212,7 @@ public ResponseEntity<HalResource> addAttachmentToRelease(@PathVariable("release
release.addToAttachments(attachment);
releaseService.updateRelease(release, sw360User);

final HalResource halRelease = restControllerHelper.createHalReleaseResource(release, true);
final HalResource halRelease = createHalReleaseResource(release, true);

return new ResponseEntity<>(halRelease, HttpStatus.OK);
}
Expand All @@ -231,4 +233,36 @@ public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add(linkTo(ReleaseController.class).slash("api" + RELEASES_URL).withRel("releases"));
return resource;
}

private HalResource<Release> createHalReleaseResource(Release release, boolean verbose) {
HalResource<Release> halRelease = new HalResource<>(release);
Link componentLink = linkTo(ReleaseController.class)
.slash("api" + ComponentController.COMPONENTS_URL + "/" + release.getComponentId()).withRel("component");
halRelease.add(componentLink);
release.setComponentId(null);

if (verbose) {
if (release.getModerators() != null) {
Set<String> moderators = release.getModerators();
restControllerHelper.addEmbeddedModerators(halRelease, moderators);
release.setModerators(null);
}
if (release.getAttachments() != null) {
Set<Attachment> attachments = release.getAttachments();
restControllerHelper.addEmbeddedAttachments(halRelease, attachments);
release.setAttachments(null);
}
if (release.getVendor() != null) {
Vendor vendor = release.getVendor();
HalResource<Vendor> vendorHalResource = restControllerHelper.addEmbeddedVendor(vendor.getFullname());
halRelease.addEmbeddedResource("sw360:vendors", vendorHalResource);
release.setVendor(null);
}
if (release.getMainLicenseIds() != null) {
restControllerHelper.addEmbeddedLicenses(halRelease, release.getMainLicenseIds());
release.setMainLicenseIds(null);
}
}
return halRelease;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ public void should_document_get_component() throws Exception {
fieldWithPath("_embedded.createdBy").description("The user who created this component"),
fieldWithPath("_embedded.sw360:releases").description("An array of all component releases with version and link to their <<resources-releases,Releases resource>>"),
fieldWithPath("_embedded.sw360:moderators").description("An array of all component moderators with email and link to their <<resources-user-get,User resource>>"),
fieldWithPath("_embedded.sw360:vendors").description("An array of all component vendors with ful name and link to their <<resources-vendor-get,Vendor resource>>")
fieldWithPath("_embedded.sw360:vendors").description("An array of all component vendors with full name and link to their <<resources-vendor-get,Vendor resource>>"),
fieldWithPath("_embedded.sw360:attachments").description("An array of all component attachments and link to their <<resources-attachment-get,Attachment resource>>")
)));
}

Expand Down Expand Up @@ -416,7 +417,8 @@ public void should_document_update_component() throws Exception {
fieldWithPath("_embedded.createdBy").description("The user who created this component"),
fieldWithPath("_embedded.sw360:releases").description("An array of all component releases with version and link to their <<resources-releases,Releases resource>>"),
fieldWithPath("_embedded.sw360:moderators").description("An array of all component moderators with email and link to their <<resources-user-get,User resource>>"),
fieldWithPath("_embedded.sw360:vendors").description("An array of all component vendors with ful name and link to their <<resources-vendor-get,Vendor resource>>")
fieldWithPath("_embedded.sw360:vendors").description("An array of all component vendors with ful name and link to their <<resources-vendor-get,Vendor resource>>"),
fieldWithPath("_embedded.sw360:attachments").description("An array of all component attachments and link to their <<resources-attachment-get,Attachment resource>>")
)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ public void should_document_get_project() throws Exception {
fieldWithPath("_embedded.createdBy").description("The user who created this project"),
fieldWithPath("_embedded.sw360:projects").description("An array of <<resources-projects, Projects resources>>"),
fieldWithPath("_embedded.sw360:releases").description("An array of <<resources-releases, Releases resources>>"),
fieldWithPath("_embedded.sw360:moderators").description("An array of all project moderators with email and link to their <<resources-user-get,User resource>>")
fieldWithPath("_embedded.sw360:moderators").description("An array of all project moderators with email and link to their <<resources-user-get,User resource>>"),
fieldWithPath("_embedded.sw360:attachments").description("An array of all project attachments and link to their <<resources-attachment-get,Attachment resource>>")
)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public void should_document_get_release() throws Exception {
fieldWithPath("downloadurl").description("the download url of the release"),
fieldWithPath("externalIds").description("When releases are imported from other tools, the external ids can be stored here"),
fieldWithPath("_embedded.sw360:moderators").description("An array of all release moderators with email and link to their <<resources-user-get,User resource>>"),
fieldWithPath("_embedded.sw360:attachments").description("An array of all release attachments and link to their <<resources-attachment-get,Attachment resource>>"),
fieldWithPath("_links").description("<<resources-index-links,Links>> to other resources")
)));
}
Expand Down Expand Up @@ -252,6 +253,7 @@ public void should_document_update_release() throws Exception {
fieldWithPath("downloadurl").description("the download url of the release"),
fieldWithPath("externalIds").description("When releases are imported from other tools, the external ids can be stored here"),
fieldWithPath("_embedded.sw360:moderators").description("An array of all release moderators with email and link to their <<resources-user-get,User resource>>"),
fieldWithPath("_embedded.sw360:attachments").description("An array of all release attachments and link to their <<resources-attachment-get,Attachment resource>>"),
fieldWithPath("_links").description("<<resources-index-links,Links>> to other resources")
)
));
Expand Down

0 comments on commit c2c0afe

Please sign in to comment.