Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / servicetemplates / plans / PlanFileResource.java
1 /*******************************************************************************
2  * Copyright (c) 2014-2015 University of Stuttgart.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.repository.resources.servicetemplates.plans;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.io.InputStream;
17
18 import javax.ws.rs.Consumes;
19 import javax.ws.rs.GET;
20 import javax.ws.rs.HeaderParam;
21 import javax.ws.rs.PUT;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
24 import javax.ws.rs.core.Response.Status;
25
26 import org.eclipse.winery.common.RepositoryFileReference;
27 import org.eclipse.winery.common.ids.elements.PlanId;
28 import org.eclipse.winery.model.tosca.TPlan;
29 import org.eclipse.winery.repository.backend.BackendUtils;
30 import org.eclipse.winery.repository.backend.Repository;
31 import org.eclipse.winery.repository.resources.servicetemplates.ServiceTemplateResource;
32 import org.restdoc.annotations.RestDoc;
33
34 import com.sun.jersey.core.header.FormDataContentDisposition;
35 import com.sun.jersey.multipart.FormDataBodyPart;
36 import com.sun.jersey.multipart.FormDataParam;
37
38 public class PlanFileResource {
39         
40         private final PlanId planId;
41         private TPlan plan;
42         private ServiceTemplateResource res;
43         
44         
45         public PlanFileResource(ServiceTemplateResource res, PlanId planId, TPlan plan) {
46                 this.res = res;
47                 this.planId = planId;
48                 this.plan = plan;
49         }
50         
51         /**
52          * Extracts the file reference from plan's planModelReference
53          */
54         private RepositoryFileReference getFileRef() {
55                 String reference = this.plan.getPlanModelReference().getReference();
56                 File f = new File(reference);
57                 return new RepositoryFileReference(this.planId, f.getName());
58         }
59         
60         @PUT
61         @Consumes({MediaType.MULTIPART_FORM_DATA})
62         @RestDoc(methodDescription = "Resource currently works for BPMN4TOSCA plans only")
63         // @formatter:off
64         public Response onPutFile(
65                 @FormDataParam("file") InputStream uploadedInputStream,
66                 @FormDataParam("file") FormDataContentDisposition fileDetail,
67                 @FormDataParam("file") FormDataBodyPart body
68         ) {
69         // @formatter:on
70                 
71                 String fileName = fileDetail.getFileName();
72                 RepositoryFileReference ref = new RepositoryFileReference(this.planId, fileName);
73                 RepositoryFileReference oldRef = this.getFileRef();
74                 boolean persistanceNecessary;
75                 if (ref.equals(oldRef)) {
76                         // nothing todo, file will be replaced
77                         persistanceNecessary = false;
78                 } else {
79                         // new filename sent
80                         BackendUtils.delete(oldRef);
81                         PlansResource.setPlanModelReference(this.plan, this.planId, fileName);
82                         persistanceNecessary = true;
83                 }
84                 
85                 // Really store it
86                 try {
87                         Repository.INSTANCE.putContentToFile(ref, uploadedInputStream, body.getMediaType());
88                 } catch (IOException e1) {
89                         return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Could not store plan. " + e1.getMessage()).build();
90                 }
91                 
92                 if (persistanceNecessary) {
93                         return BackendUtils.persist(this.res);
94                 } else {
95                         return Response.noContent().build();
96                 }
97         }
98         
99         @PUT
100         @Consumes({MediaType.APPLICATION_JSON})
101         // @formatter:off
102         public Response onPutJSON(InputStream is) {
103                 RepositoryFileReference ref = this.getFileRef();
104                 return BackendUtils.putContentToFile(ref, is, MediaType.APPLICATION_JSON_TYPE);
105         }
106
107         /**
108          * Returns the stored file.
109          */
110         @GET
111         public Response getFile(@HeaderParam("If-Modified-Since") String modified) {
112                 RepositoryFileReference ref = this.getFileRef();
113                 return BackendUtils.returnRepoPath(ref, modified);
114         }
115 }