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.interfaces;
14 import java.util.List;
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.FormParam;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.Produces;
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.tosca.TBoolean;
26 import org.eclipse.winery.model.tosca.TParameter;
27 import org.eclipse.winery.repository.backend.BackendUtils;
28 import org.eclipse.winery.repository.resources._support.IPersistable;
29 import org.eclipse.winery.repository.resources._support.collections.withid.EntityWithIdCollectionResource;
30 import org.restdoc.annotations.RestDocParam;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.sun.jersey.api.view.Viewable;
36 public class ParametersResource extends EntityWithIdCollectionResource<ParameterResource, TParameter> {
38 private static final Logger logger = LoggerFactory.getLogger(ParametersResource.class);
41 public ParametersResource(List<TParameter> parameters, IPersistable typeResource) {
42 super(ParameterResource.class, TParameter.class, parameters, typeResource);
46 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
47 @Produces(MediaType.TEXT_PLAIN)
49 public Response createParamter(
50 @FormParam("name") String name,
51 @FormParam("type") String type,
52 @FormParam("required") @RestDocParam(description="type tYesNo, not Boolean. For convenience, on/off is also supported. In case this parameter is not provided, 'off' is assumed. This is in contrast to the specification, but it eases implementing the UI") String required) {
54 if (StringUtils.isEmpty(name)) {
55 return Response.status(Status.BAD_REQUEST).entity("name must not be null").build();
57 if (StringUtils.isEmpty(type)) {
58 return Response.status(Status.BAD_REQUEST).entity("type must not be null").build();
61 TParameter param = new TParameter();
65 if (required == null) {
66 // The specification states that the default value is "yes"
67 // We assume "no", because Chrome does not send the checkbox data if a checkbox is not checked
70 if (required.equalsIgnoreCase("on")) {
72 } else if (required.equalsIgnoreCase("off")) {
76 tb = TBoolean.valueOf(required);
77 } catch (java.lang.IllegalArgumentException e) {
78 return Response.status(Status.BAD_REQUEST).entity("Wrong format of required").build();
82 param.setRequired(tb);
86 return BackendUtils.persist(this.res);
90 public String getId(TParameter entity) {
91 return entity.getName();
95 public Viewable getHTML() {
96 throw new IllegalStateException("Not yet implemented.");