Skip to content
This repository was archived by the owner on Jun 17, 2020. It is now read-only.

Commit bb57fe2

Browse files
committed
Merge pull request #171 from eiselems/121
add upload file for REST fix #121
2 parents 665d4e4 + 7f282b3 commit bb57fe2

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

src/main/java/org/opentosca/csarrepo/rest/resource/CsarResource.java

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package org.opentosca.csarrepo.rest.resource;
22

3+
import java.io.InputStream;
4+
import java.net.URI;
35
import java.util.LinkedList;
46
import java.util.List;
57

8+
import javax.ws.rs.Consumes;
69
import javax.ws.rs.DELETE;
710
import javax.ws.rs.GET;
11+
import javax.ws.rs.POST;
812
import javax.ws.rs.Path;
913
import javax.ws.rs.PathParam;
1014
import javax.ws.rs.Produces;
@@ -16,13 +20,16 @@
1620

1721
import org.apache.logging.log4j.LogManager;
1822
import org.apache.logging.log4j.Logger;
23+
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
24+
import org.glassfish.jersey.media.multipart.FormDataParam;
1925
import org.opentosca.csarrepo.model.Csar;
2026
import org.opentosca.csarrepo.model.CsarFile;
2127
import org.opentosca.csarrepo.rest.model.CsarEntry;
2228
import org.opentosca.csarrepo.rest.model.SimpleXLink;
2329
import org.opentosca.csarrepo.rest.util.LinkBuilder;
2430
import org.opentosca.csarrepo.service.DeleteCsarService;
2531
import org.opentosca.csarrepo.service.ShowCsarService;
32+
import org.opentosca.csarrepo.service.UploadCsarFileService;
2633

2734
public class CsarResource {
2835

@@ -40,7 +47,6 @@ public CsarResource(UriInfo uriInfo, long id) {
4047
public Response getCsar() {
4148
// TODO: check if csar exists
4249

43-
// TODO: validate if id is really a long
4450
List<SimpleXLink> links = new LinkedList<SimpleXLink>();
4551
links.add(LinkBuilder.selfLink(uriInfo));
4652

@@ -65,22 +71,58 @@ public Response getCsar() {
6571
LOGGER.debug("Accessing csar<id:{},name:{}>", csar.getId(), csar.getName());
6672
return Response.ok(csarEntry).build();
6773
}
68-
74+
6975
@DELETE
7076
public Response deleteCsar() {
7177
DeleteCsarService service = new DeleteCsarService(0, this.id);
72-
73-
if(service.hasErrors()) {
74-
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(service.getErrors().get(0)).build();
78+
79+
if (service.hasErrors()) {
80+
String message = service.getErrors().get(0);
81+
LOGGER.error(message);
82+
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).build();
7583
}
76-
84+
7785
return Response.ok().build();
7886
}
7987

8088
// TODO: move id to constant class
8189
@Path("/{" + "id" + "}")
8290
public Object getCsarFile(@PathParam("id") long csarfileID, @Context UriInfo uriInfo) {
83-
// TODO: add warning if longID = -1;
8491
return new CsarFileResource(uriInfo, this.id, csarfileID);
8592
}
93+
94+
@POST
95+
@Consumes(MediaType.MULTIPART_FORM_DATA)
96+
// TODO: check how the occurring nullpointer-exc. can be handled
97+
public Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream,
98+
@FormDataParam("file") FormDataContentDisposition fileDetail) {
99+
100+
if (null == uploadedInputStream) {
101+
String message = "Couldn't read uploaded file - was it set?";
102+
LOGGER.error(message);
103+
return Response.serverError().entity(message).build();
104+
}
105+
106+
if (null == fileDetail) {
107+
String message = "The details of the file could not be read.";
108+
LOGGER.error(message);
109+
return Response.serverError().entity(message).build();
110+
}
111+
112+
String csarName = fileDetail.getFileName();
113+
UploadCsarFileService upService = new UploadCsarFileService(0L, this.id, uploadedInputStream, csarName);
114+
// TODO, think about better Exceptionhandling (currently we
115+
// just take first Exception)
116+
if (upService.hasErrors()) {
117+
return Response.serverError().entity("UploadCsarService has Errors: " + upService.getErrors().get(0))
118+
.build();
119+
}
120+
121+
CsarFile csarFile = upService.getResult();
122+
123+
LOGGER.info("Post for uploading a new CSAR as file with name \"" + fileDetail.getFileName());
124+
125+
URI linkToCsarFile = LinkBuilder.linkToCsarFile(uriInfo, this.id, csarFile.getId());
126+
return Response.created(linkToCsarFile).build();
127+
}
86128
}

0 commit comments

Comments
 (0)