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
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.repository.resources.servicetemplates.plans;
15 import java.io.IOException;
16 import java.io.InputStream;
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;
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;
34 import com.sun.jersey.core.header.FormDataContentDisposition;
35 import com.sun.jersey.multipart.FormDataBodyPart;
36 import com.sun.jersey.multipart.FormDataParam;
38 public class PlanFileResource {
40 private final PlanId planId;
42 private ServiceTemplateResource res;
45 public PlanFileResource(ServiceTemplateResource res, PlanId planId, TPlan plan) {
52 * Extracts the file reference from plan's planModelReference
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());
61 @Consumes({MediaType.MULTIPART_FORM_DATA})
62 @RestDoc(methodDescription = "Resource currently works for BPMN4TOSCA plans only")
64 public Response onPutFile(
65 @FormDataParam("file") InputStream uploadedInputStream,
66 @FormDataParam("file") FormDataContentDisposition fileDetail,
67 @FormDataParam("file") FormDataBodyPart body
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;
80 BackendUtils.delete(oldRef);
81 PlansResource.setPlanModelReference(this.plan, this.planId, fileName);
82 persistanceNecessary = true;
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();
92 if (persistanceNecessary) {
93 return BackendUtils.persist(this.res);
95 return Response.noContent().build();
100 @Consumes({MediaType.APPLICATION_JSON})
102 public Response onPutJSON(InputStream is) {
103 RepositoryFileReference ref = this.getFileRef();
104 return BackendUtils.putContentToFile(ref, is, MediaType.APPLICATION_JSON_TYPE);
108 * Returns the stored file.
111 public Response getFile(@HeaderParam("If-Modified-Since") String modified) {
112 RepositoryFileReference ref = this.getFileRef();
113 return BackendUtils.returnRepoPath(ref, modified);