98e1dcf9937a9a357f2a9aea41e79d8a9fd5e8f5
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / servicetemplates / selfserviceportal / SelfServicePortalResource.java
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.io.InputStream;
16 import java.io.StringWriter;
17 import java.util.List;
18
19 import javax.ws.rs.Consumes;
20 import javax.ws.rs.FormParam;
21 import javax.ws.rs.GET;
22 import javax.ws.rs.HeaderParam;
23 import javax.ws.rs.PUT;
24 import javax.ws.rs.Path;
25 import javax.ws.rs.Produces;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28 import javax.xml.bind.JAXBException;
29 import javax.xml.bind.Unmarshaller;
30
31 import org.apache.commons.io.IOUtils;
32 import org.apache.taglibs.standard.functions.Functions;
33 import org.eclipse.winery.common.RepositoryFileReference;
34 import org.eclipse.winery.common.ids.definitions.ServiceTemplateId;
35 import org.eclipse.winery.model.selfservice.Application;
36 import org.eclipse.winery.model.selfservice.Application.Options;
37 import org.eclipse.winery.model.tosca.TDocumentation;
38 import org.eclipse.winery.repository.JAXBSupport;
39 import org.eclipse.winery.repository.Utils;
40 import org.eclipse.winery.repository.backend.BackendUtils;
41 import org.eclipse.winery.repository.backend.Repository;
42 import org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId;
43 import org.eclipse.winery.repository.resources._support.IPersistable;
44 import org.eclipse.winery.repository.resources.servicetemplates.ServiceTemplateResource;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import com.sun.jersey.api.view.Viewable;
49 import com.sun.jersey.multipart.FormDataBodyPart;
50 import com.sun.jersey.multipart.FormDataParam;
51
52 public class SelfServicePortalResource implements IPersistable {
53         
54         private static final Logger logger = LoggerFactory.getLogger(SelfServicePortalResource.class);
55         
56         private final ServiceTemplateResource serviceTemplateResource;
57         
58         public final RepositoryFileReference data_xml_ref;
59         public final RepositoryFileReference icon_jpg_ref;
60         public final RepositoryFileReference image_jpg_ref;
61         
62         private final Application application;
63         
64         private final SelfServiceMetaDataId id;
65         
66         
67         public SelfServicePortalResource(ServiceTemplateId serviceTemplateId) {
68                 this(null, serviceTemplateId);
69         }
70         
71         public SelfServicePortalResource(ServiceTemplateResource serviceTemplateResource) {
72                 this(serviceTemplateResource, (ServiceTemplateId) serviceTemplateResource.getId());
73         }
74         
75         SelfServiceMetaDataId getId() {
76                 return this.id;
77         }
78         
79         /**
80          * @param serviceTemplateResource may be null
81          * @param serviceTemplateId the id, must not be null
82          */
83         private SelfServicePortalResource(ServiceTemplateResource serviceTemplateResource, ServiceTemplateId serviceTemplateId) {
84                 this.serviceTemplateResource = serviceTemplateResource;
85                 this.id = new SelfServiceMetaDataId(serviceTemplateId);
86                 this.data_xml_ref = new RepositoryFileReference(this.id, "data.xml");
87                 this.icon_jpg_ref = new RepositoryFileReference(this.id, "icon.jpg");
88                 this.image_jpg_ref = new RepositoryFileReference(this.id, "image.jpg");
89                 this.application = this.getData();
90         }
91         
92         private Application getData() {
93                 if (Repository.INSTANCE.exists(this.data_xml_ref)) {
94                         Unmarshaller u = JAXBSupport.createUnmarshaller();
95                         try (InputStream is = Repository.INSTANCE.newInputStream(this.data_xml_ref);) {
96                                 return (Application) u.unmarshal(is);
97                         } catch (IOException | JAXBException e) {
98                                 SelfServicePortalResource.logger.error("Could not read from " + this.data_xml_ref, e);
99                                 return new Application();
100                         }
101                 } else {
102                         return this.getDefaultApplicationData();
103                 }
104         }
105         
106         private Application getDefaultApplicationData() {
107                 Application app = new Application();
108                 app.setIconUrl("icon.jpg");
109                 app.setImageUrl("image.jpg");
110                 if (this.serviceTemplateResource != null) {
111                         app.setDisplayName(this.serviceTemplateResource.getName());
112                         List<TDocumentation> documentation = this.serviceTemplateResource.getServiceTemplate().getDocumentation();
113                         if ((documentation != null) && (!documentation.isEmpty())) {
114                                 TDocumentation doc = documentation.get(0);
115                                 List<Object> content = doc.getContent();
116                                 if ((content != null) && (!content.isEmpty())) {
117                                         app.setDescription(content.get(0).toString());
118                                 }
119                         }
120                 }
121                 return app;
122         }
123         
124         @GET
125         @Produces(MediaType.TEXT_HTML)
126         public Viewable getHTML() {
127                 return new Viewable("/jsp/servicetemplates/selfservicemetadata/selfservicemetadata.jsp", this);
128         }
129         
130         @Override
131         public void persist() throws IOException {
132                 BackendUtils.persist(this.application, this.data_xml_ref, MediaType.TEXT_XML_TYPE);
133         }
134         
135         @PUT
136         @Consumes(MediaType.TEXT_XML)
137         public Response onPutXML(Application data) {
138                 String content = Utils.getXMLAsString(data);
139                 return BackendUtils.putContentToFile(this.data_xml_ref, content, MediaType.TEXT_XML_TYPE);
140         }
141         
142         @Path("icon.jpg")
143         @GET
144         public Response getIcon(@HeaderParam("If-Modified-Since") String modified) {
145                 RepositoryFileReference ref = new RepositoryFileReference(this.id, "icon.jpg");
146                 return BackendUtils.returnRepoPath(ref, modified);
147         }
148         
149         @Path("icon.jpg")
150         @PUT
151         @Consumes(MediaType.MULTIPART_FORM_DATA)
152         public Response putIcon(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataBodyPart body) {
153                 RepositoryFileReference ref = new RepositoryFileReference(this.id, "icon.jpg");
154                 return BackendUtils.putContentToFile(ref, uploadedInputStream, body.getMediaType());
155         }
156         
157         @Path("image.jpg")
158         @GET
159         public Response getImage(@HeaderParam("If-Modified-Since") String modified) {
160                 RepositoryFileReference ref = new RepositoryFileReference(this.id, "image.jpg");
161                 return BackendUtils.returnRepoPath(ref, modified);
162         }
163         
164         @Path("image.jpg")
165         @PUT
166         @Consumes(MediaType.MULTIPART_FORM_DATA)
167         public Response putImage(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataBodyPart body) {
168                 RepositoryFileReference ref = new RepositoryFileReference(this.id, "image.jpg");
169                 return BackendUtils.putContentToFile(ref, uploadedInputStream, body.getMediaType());
170         }
171         
172         @Path("displayname")
173         @PUT
174         public Response onPutOnDisplayName(@FormParam("value") String value) {
175                 this.application.setDisplayName(value);
176                 return BackendUtils.persist(this);
177         }
178         
179         @Path("description")
180         @PUT
181         public Response onPutOnDescription(@FormParam("value") String value) {
182                 this.application.setDescription(value);
183                 return BackendUtils.persist(this);
184         }
185         
186         @Path("options/")
187         public OptionsResource getOptionsResource() {
188                 Options options = this.application.getOptions();
189                 if (options == null) {
190                         options = new Options();
191                         this.application.setOptions(options);
192                 }
193                 return new OptionsResource(options.getOption(), this);
194         }
195         
196         /**
197          * @return the internal application object. Used for the export.
198          */
199         public Application getApplication() {
200                 return this.application;
201         }
202         
203         /**
204          * Used in JSP only
205          */
206         public String getApplicationAsXMLStringEncoded() {
207                 String res;
208                 if (Repository.INSTANCE.exists(this.data_xml_ref)) {
209                         StringWriter sw = new StringWriter();
210                         try (InputStream is = Repository.INSTANCE.newInputStream(this.data_xml_ref);) {
211                                 IOUtils.copy(is, sw);
212                         } catch (IOException e) {
213                                 SelfServicePortalResource.logger.error("Could not read from file", e);
214                         }
215                         res = sw.toString();
216                 } else {
217                         // return skeleton for application
218                         // application object is already filled with default values if no file exists in repo
219                         res = Utils.getXMLAsString(this.getApplication());
220                 }
221                 return Functions.escapeXml(res);
222         }
223 }