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.TInterface;
26 import org.eclipse.winery.repository.backend.BackendUtils;
27 import org.eclipse.winery.repository.resources._support.IPersistable;
28 import org.eclipse.winery.repository.resources._support.collections.withid.EntityWithIdCollectionResource;
29 import org.eclipse.winery.repository.resources.entitytypes.TopologyGraphElementEntityTypeResource;
30 import org.eclipse.winery.repository.resources.entitytypes.relationshiptypes.RelationshipTypeResource;
31 import org.restdoc.annotations.RestDoc;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.sun.jersey.api.view.Viewable;
37 public class InterfacesResource extends EntityWithIdCollectionResource<InterfaceResource, TInterface> {
39 private static final Logger logger = LoggerFactory.getLogger(InterfacesResource.class);
41 private TopologyGraphElementEntityTypeResource typeResource;
43 private String urlPrefix;
46 public InterfacesResource(IPersistable res, List<TInterface> list) {
47 super(InterfaceResource.class, TInterface.class, list, res);
51 * @param urlPrefix prefix to be prepended to the URL.
52 * "source"|"target"|null. E.g., "source" for "sourceinterfaces"
54 public InterfacesResource(String urlPrefix, List<TInterface> list, IPersistable typeResource) {
55 super(InterfaceResource.class, TInterface.class, list, typeResource);
56 this.urlPrefix = urlPrefix;
57 this.typeResource = (TopologyGraphElementEntityTypeResource) typeResource;
61 public Viewable getHTML() {
62 return new Viewable("/jsp/interfaces/interfaces.jsp", this);
66 * Implementation base: <br />
67 * {@link org.eclipse.winery.repository.resources.AbstractComponentResource.
70 * @return entity: id of the stored interface
73 @RestDoc(methodDescription = "Creates a new interface. Returns conflict if interface already exists")
74 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
75 @Produces(MediaType.TEXT_PLAIN)
76 public Response onPost(@FormParam("interfaceName") String interfaceName) {
77 if (StringUtils.isEmpty(interfaceName)) {
78 return Response.status(Status.BAD_REQUEST).entity("null interfaceName").build();
81 TInterface iface = new TInterface();
82 iface.setName(interfaceName);
84 // check for duplicates
85 // return "conflict" if interface already exists
86 if (this.alreadyContains(iface)) {
87 return Response.status(Status.CONFLICT).build();
91 return BackendUtils.persist(this.res);
95 * Required by interfaces.jsp
97 public String getUrlPrefix() {
98 return this.urlPrefix;
102 public String getId(TInterface entity) {
103 return entity.getName();
107 * @return the namespace of the node/relationship type
109 public String getNamespace() {
110 return this.typeResource.getId().getNamespace().getDecoded();
114 * @return the name of the node/relationship type
116 public String getName() {
117 return this.typeResource.getName();
120 public String getRelationshipTypeOrNodeTypeURLFragment() {
121 if (this.typeResource instanceof RelationshipTypeResource) {
122 return "relationshiptype";
128 public String getRelationshipTypeOrNodeType() {
129 if (this.typeResource instanceof RelationshipTypeResource) {
130 return "Relationship Type";
136 public String getTypeQName() {
137 String res = this.typeResource.getId().getQName().toString();