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.util.List;
18 import javax.ws.rs.Consumes;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.Response;
22 import javax.ws.rs.core.Response.Status;
24 import org.apache.commons.lang3.StringUtils;
25 import org.eclipse.winery.model.selfservice.ApplicationOption;
26 import org.eclipse.winery.common.RepositoryFileReference;
27 import org.eclipse.winery.repository.Utils;
28 import org.eclipse.winery.repository.backend.BackendUtils;
29 import org.eclipse.winery.repository.backend.Repository;
30 import org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId;
31 import org.eclipse.winery.repository.resources._support.collections.withid.EntityWithIdCollectionResource;
32 import org.restdoc.annotations.RestDoc;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.sun.jersey.api.view.Viewable;
37 import com.sun.jersey.core.header.FormDataContentDisposition;
38 import com.sun.jersey.multipart.FormDataBodyPart;
39 import com.sun.jersey.multipart.FormDataParam;
41 public class OptionsResource extends EntityWithIdCollectionResource<OptionResource, ApplicationOption> {
43 private static final Logger logger = LoggerFactory.getLogger(OptionsResource.class);
46 public OptionsResource(List<ApplicationOption> list, SelfServicePortalResource res) {
47 super(OptionResource.class, ApplicationOption.class, list, res);
51 public String getId(ApplicationOption entity) {
52 return entity.getId();
56 public Viewable getHTML() {
57 throw new IllegalStateException("Not yet implemented.");
61 @RestDoc(methodDescription = "Adds a new option<p>TODO: @return JSON with .tableData: Array with row data for dataTable</p>")
62 @Consumes(MediaType.MULTIPART_FORM_DATA)
64 public Response onPost(
65 @FormDataParam("name") String name,
66 @FormDataParam("description") String description,
67 @FormDataParam("planServiceName") String planServiceName,
68 @FormDataParam("planInputMessage") String planInputMessage,
69 @FormDataParam("file") InputStream uploadedInputStream,
70 @FormDataParam("file") FormDataContentDisposition fileDetail,
71 @FormDataParam("file") FormDataBodyPart body
74 if (StringUtils.isEmpty(name)) {
75 return Response.status(Status.BAD_REQUEST).entity("planName must be given").build();
77 if (StringUtils.isEmpty(description)) {
78 return Response.status(Status.BAD_REQUEST).entity("description must be given").build();
80 if (StringUtils.isEmpty(planServiceName)) {
81 return Response.status(Status.BAD_REQUEST).entity("planServiceName must be given").build();
83 if (StringUtils.isEmpty(planInputMessage)) {
84 return Response.status(Status.BAD_REQUEST).entity("planInputMessage must be given").build();
86 if (uploadedInputStream == null) {
87 return Response.status(Status.BAD_REQUEST).entity("file has to be provided").build();
89 ApplicationOption option = new ApplicationOption();
91 String id = Utils.createXMLidAsString(name);
93 String fileNamePrefix = OptionResource.getFileNamePrefix(id);
94 String iconFileName = fileNamePrefix + OptionResource.ICON_JPG;
95 String planInputMessageFileName = fileNamePrefix + OptionResource.PLAN_INPUT_XML;
100 option.setDescription(description);
101 option.setIconUrl(iconFileName);
102 option.setPlanInputMessageUrl(planInputMessageFileName);
103 option.setPlanServiceName(planServiceName);
105 // BEGIN: store icon and planInputMessage
107 SelfServiceMetaDataId ssmdId = ((SelfServicePortalResource) this.res).getId();
109 RepositoryFileReference iconRef = new RepositoryFileReference(ssmdId, iconFileName);
111 Repository.INSTANCE.putContentToFile(iconRef, uploadedInputStream, body.getMediaType());
112 } catch (IOException e) {
113 OptionsResource.logger.error(e.getMessage(), e);
114 return Response.serverError().entity(e.getMessage()).build();
117 RepositoryFileReference planInputMessageRef = new RepositoryFileReference(ssmdId, planInputMessageFileName);
119 Repository.INSTANCE.putContentToFile(planInputMessageRef, planInputMessage, MediaType.TEXT_XML_TYPE);
120 } catch (IOException e) {
121 OptionsResource.logger.error(e.getMessage(), e);
122 return Response.serverError().entity(e.getMessage()).build();
125 // END: store icon and planInputMessage
127 this.list.add(option);
128 Response response = BackendUtils.persist(this.res);