Search in sources :

Example 11 with HalResource

use of org.eclipse.sw360.rest.resourceserver.core.HalResource in project sw360portal by sw360.

the class AttachmentController method getAttachmentForSha1.

@RequestMapping(value = ATTACHMENTS_URL, params = "sha1", method = RequestMethod.GET)
public ResponseEntity<Resource<Attachment>> getAttachmentForSha1(OAuth2Authentication oAuth2Authentication, @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);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) User(org.eclipse.sw360.datahandler.thrift.users.User) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with HalResource

use of org.eclipse.sw360.rest.resourceserver.core.HalResource in project sw360portal by sw360.

the class AttachmentController method createHalAttachment.

private HalResource<Attachment> createHalAttachment(Attachment sw360Attachment, Release sw360Release, User sw360User) {
    HalResource<Attachment> halAttachment = new HalResource<>(sw360Attachment);
    String componentUUID = sw360Attachment.getAttachmentContentId();
    Link releaseLink = linkTo(AttachmentController.class).slash("api/releases/" + sw360Release.getId()).withRel("release");
    halAttachment.add(releaseLink);
    restControllerHelper.addEmbeddedRelease(halAttachment, sw360Release);
    restControllerHelper.addEmbeddedUser(halAttachment, sw360User, "createdBy");
    return halAttachment;
}
Also used : Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) HalResource(org.eclipse.sw360.rest.resourceserver.core.HalResource) Link(org.springframework.hateoas.Link)

Example 13 with HalResource

use of org.eclipse.sw360.rest.resourceserver.core.HalResource in project sw360portal by sw360.

the class ComponentController method addAttachmentToComponent.

@RequestMapping(value = COMPONENTS_URL + "/{componentId}/attachments", method = RequestMethod.POST, consumes = { "multipart/mixed", "multipart/form-data" })
public ResponseEntity<HalResource> addAttachmentToComponent(@PathVariable("componentId") String componentId, OAuth2Authentication oAuth2Authentication, @RequestPart("file") MultipartFile file, @RequestPart("attachment") Attachment newAttachment) throws TException {
    final User sw360User = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
    Attachment attachment;
    try {
        attachment = attachmentService.uploadAttachment(file, newAttachment, sw360User);
    } catch (IOException e) {
        log.error("failed to upload attachment", e);
        throw new RuntimeException("failed to upload attachment", e);
    }
    final Component component = componentService.getComponentForUserById(componentId, sw360User);
    component.addToAttachments(attachment);
    componentService.updateComponent(component, sw360User);
    final HalResource halRelease = createHalComponent(component, sw360User);
    return new ResponseEntity<>(halRelease, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) User(org.eclipse.sw360.datahandler.thrift.users.User) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) HalResource(org.eclipse.sw360.rest.resourceserver.core.HalResource) IOException(java.io.IOException) Component(org.eclipse.sw360.datahandler.thrift.components.Component)

Example 14 with HalResource

use of org.eclipse.sw360.rest.resourceserver.core.HalResource in project sw360portal by sw360.

the class ComponentController method createComponent.

@PreAuthorize("hasAuthority('WRITE')")
@RequestMapping(value = COMPONENTS_URL, method = RequestMethod.POST)
public ResponseEntity<Resource<Component>> createComponent(OAuth2Authentication oAuth2Authentication, @RequestBody Component component) throws URISyntaxException, TException {
    User user = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
    if (component.getVendorNames() != null) {
        Set<String> vendors = new HashSet<>();
        for (String vendorUriString : component.getVendorNames()) {
            URI vendorURI = new URI(vendorUriString);
            String path = vendorURI.getPath();
            String vendorId = path.substring(path.lastIndexOf('/') + 1);
            Vendor vendor = vendorService.getVendorById(vendorId);
            String vendorFullName = vendor.getFullname();
            vendors.add(vendorFullName);
        }
        component.setVendorNames(vendors);
    }
    Component sw360Component = componentService.createComponent(component, user);
    HalResource<Component> halResource = createHalComponent(sw360Component, user);
    URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(sw360Component.getId()).toUri();
    return ResponseEntity.created(location).body(halResource);
}
Also used : User(org.eclipse.sw360.datahandler.thrift.users.User) Vendor(org.eclipse.sw360.datahandler.thrift.vendors.Vendor) Component(org.eclipse.sw360.datahandler.thrift.components.Component) URI(java.net.URI) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 15 with HalResource

use of org.eclipse.sw360.rest.resourceserver.core.HalResource in project sw360portal by sw360.

the class ProjectController method getProject.

@RequestMapping(value = PROJECTS_URL + "/{id}", method = RequestMethod.GET)
public ResponseEntity<Resource<Project>> getProject(@PathVariable("id") String id, OAuth2Authentication oAuth2Authentication) throws TException {
    User sw360User = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
    Project sw360Project = projectService.getProjectForUserById(id, sw360User);
    HalResource<Project> userHalResource = createHalProject(sw360Project, sw360User);
    return new ResponseEntity<>(userHalResource, HttpStatus.OK);
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) ResponseEntity(org.springframework.http.ResponseEntity) User(org.eclipse.sw360.datahandler.thrift.users.User)

Aggregations

User (org.eclipse.sw360.datahandler.thrift.users.User)16 ResponseEntity (org.springframework.http.ResponseEntity)12 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)8 Link (org.springframework.hateoas.Link)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 Release (org.eclipse.sw360.datahandler.thrift.components.Release)6 URI (java.net.URI)5 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)5 Vendor (org.eclipse.sw360.datahandler.thrift.vendors.Vendor)5 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 HalResource (org.eclipse.sw360.rest.resourceserver.core.HalResource)4 IOException (java.io.IOException)3 TException (org.apache.thrift.TException)3 Component (org.eclipse.sw360.datahandler.thrift.components.Component)3 License (org.eclipse.sw360.datahandler.thrift.licenses.License)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ProjectReleaseRelationship (org.eclipse.sw360.datahandler.thrift.ProjectReleaseRelationship)1 Vulnerability (org.eclipse.sw360.datahandler.thrift.vulnerabilities.Vulnerability)1 Resource (org.springframework.hateoas.Resource)1