11package org .opentosca .csarrepo .rest .resource ;
22
3+ import java .io .InputStream ;
4+ import java .net .URI ;
35import java .util .LinkedList ;
46import java .util .List ;
57
8+ import javax .ws .rs .Consumes ;
69import javax .ws .rs .DELETE ;
710import javax .ws .rs .GET ;
11+ import javax .ws .rs .POST ;
812import javax .ws .rs .Path ;
913import javax .ws .rs .PathParam ;
1014import javax .ws .rs .Produces ;
1620
1721import org .apache .logging .log4j .LogManager ;
1822import org .apache .logging .log4j .Logger ;
23+ import org .glassfish .jersey .media .multipart .FormDataContentDisposition ;
24+ import org .glassfish .jersey .media .multipart .FormDataParam ;
1925import org .opentosca .csarrepo .model .Csar ;
2026import org .opentosca .csarrepo .model .CsarFile ;
2127import org .opentosca .csarrepo .rest .model .CsarEntry ;
2228import org .opentosca .csarrepo .rest .model .SimpleXLink ;
2329import org .opentosca .csarrepo .rest .util .LinkBuilder ;
2430import org .opentosca .csarrepo .service .DeleteCsarService ;
2531import org .opentosca .csarrepo .service .ShowCsarService ;
32+ import org .opentosca .csarrepo .service .UploadCsarFileService ;
2633
2734public 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