9ce11f9fae2f3da7ce0a960f679a6063cafef310
[vfc/nfvo/wfengine.git] /
1 /*******************************************************************************
2  * Copyright (c) 2012-2013 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.selfserviceportal;
13
14 import java.io.IOException;
15 import java.util.List;
16
17 import javax.ws.rs.GET;
18 import javax.ws.rs.HeaderParam;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.core.Response;
21 import javax.ws.rs.core.Response.Status;
22
23 import org.eclipse.winery.model.selfservice.ApplicationOption;
24 import org.eclipse.winery.common.RepositoryFileReference;
25 import org.eclipse.winery.repository.backend.BackendUtils;
26 import org.eclipse.winery.repository.backend.Repository;
27 import org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId;
28 import org.eclipse.winery.repository.resources._support.collections.IIdDetermination;
29 import org.eclipse.winery.repository.resources._support.collections.withid.EntityWithIdResource;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class OptionResource extends EntityWithIdResource<ApplicationOption> {
34         
35         private static final Logger logger = LoggerFactory.getLogger(OptionResource.class);
36         
37         static final String ICON_JPG = "icon.jpg";
38         static final String PLAN_INPUT_XML = "plan.input.xml";
39         private SelfServiceMetaDataId ssmdId;
40         
41         
42         public OptionResource(IIdDetermination<ApplicationOption> idDetermination, ApplicationOption o, int idx, List<ApplicationOption> list, SelfServicePortalResource res) {
43                 super(idDetermination, o, idx, list, res);
44                 this.ssmdId = ((SelfServicePortalResource) this.res).getId();
45         }
46         
47         private String getFileNamePrefix() {
48                 return OptionResource.getFileNamePrefix(this.o.getId());
49         }
50         
51         public static String getFileNamePrefix(String id) {
52                 return "option_" + id + "_";
53         }
54         
55         @Path(OptionResource.ICON_JPG)
56         @GET
57         public Response getIcon(@HeaderParam("If-Modified-Since") String modified) {
58                 RepositoryFileReference ref = new RepositoryFileReference(this.ssmdId, this.getFileNamePrefix() + OptionResource.ICON_JPG);
59                 return BackendUtils.returnRepoPath(ref, modified);
60         }
61         
62         @Path("planinputmessage")
63         @GET
64         public Response getPlanInputMessage(@HeaderParam("If-Modified-Since") String modified) {
65                 RepositoryFileReference ref = new RepositoryFileReference(this.ssmdId, this.getFileNamePrefix() + OptionResource.PLAN_INPUT_XML);
66                 return BackendUtils.returnRepoPath(ref, modified);
67         }
68         
69         @Override
70         public Response onDelete() {
71                 // delete icon and plan model reference ...
72                 
73                 // delete icon
74                 // we use the URL stored in the data instead of the generated URL to be compatible with manually edits
75                 RepositoryFileReference ref = new RepositoryFileReference(this.ssmdId, this.o.getIconUrl());
76                 try {
77                         Repository.INSTANCE.forceDelete(ref);
78                 } catch (IOException e) {
79                         OptionResource.logger.error("Could not remove file", e);
80                         return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
81                 }
82                 
83                 // delete plan input
84                 // we use the URL stored in the data instead of the generated URL to be compatible with manually edits
85                 ref = new RepositoryFileReference(this.ssmdId, this.o.getPlanInputMessageUrl());
86                 try {
87                         Repository.INSTANCE.forceDelete(ref);
88                 } catch (IOException e) {
89                         OptionResource.logger.error("Could not remove file", e);
90                         return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
91                 }
92                 
93                 // after deleting files, continue with list deletion
94                 return super.onDelete();
95         }
96 }