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
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.repository.resources.servicetemplates.selfserviceportal;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.StringWriter;
17 import java.util.List;
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;
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;
48 import com.sun.jersey.api.view.Viewable;
49 import com.sun.jersey.multipart.FormDataBodyPart;
50 import com.sun.jersey.multipart.FormDataParam;
52 public class SelfServicePortalResource implements IPersistable {
54 private static final Logger logger = LoggerFactory.getLogger(SelfServicePortalResource.class);
56 private final ServiceTemplateResource serviceTemplateResource;
58 public final RepositoryFileReference data_xml_ref;
59 public final RepositoryFileReference icon_jpg_ref;
60 public final RepositoryFileReference image_jpg_ref;
62 private final Application application;
64 private final SelfServiceMetaDataId id;
67 public SelfServicePortalResource(ServiceTemplateId serviceTemplateId) {
68 this(null, serviceTemplateId);
71 public SelfServicePortalResource(ServiceTemplateResource serviceTemplateResource) {
72 this(serviceTemplateResource, (ServiceTemplateId) serviceTemplateResource.getId());
75 SelfServiceMetaDataId getId() {
80 * @param serviceTemplateResource may be null
81 * @param serviceTemplateId the id, must not be null
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();
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();
102 return this.getDefaultApplicationData();
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());
125 @Produces(MediaType.TEXT_HTML)
126 public Viewable getHTML() {
127 return new Viewable("/jsp/servicetemplates/selfservicemetadata/selfservicemetadata.jsp", this);
131 public void persist() throws IOException {
132 BackendUtils.persist(this.application, this.data_xml_ref, MediaType.TEXT_XML_TYPE);
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);
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);
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());
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);
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());
174 public Response onPutOnDisplayName(@FormParam("value") String value) {
175 this.application.setDisplayName(value);
176 return BackendUtils.persist(this);
181 public Response onPutOnDescription(@FormParam("value") String value) {
182 this.application.setDescription(value);
183 return BackendUtils.persist(this);
187 public OptionsResource getOptionsResource() {
188 Options options = this.application.getOptions();
189 if (options == null) {
190 options = new Options();
191 this.application.setOptions(options);
193 return new OptionsResource(options.getOption(), this);
197 * @return the internal application object. Used for the export.
199 public Application getApplication() {
200 return this.application;
206 public String getApplicationAsXMLStringEncoded() {
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);
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());
221 return Functions.escapeXml(res);