Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / AbstractComponentsWithTypeReferenceResource.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;
13
14 import javax.ws.rs.Consumes;
15 import javax.ws.rs.FormParam;
16 import javax.ws.rs.POST;
17 import javax.ws.rs.Produces;
18 import javax.ws.rs.core.MediaType;
19 import javax.ws.rs.core.Response;
20 import javax.ws.rs.core.Response.Status;
21
22 import org.apache.commons.lang3.StringUtils;
23 import org.eclipse.winery.common.ids.definitions.TOSCAComponentId;
24 import org.eclipse.winery.repository.backend.ResourceCreationResult;
25 import org.restdoc.annotations.RestDocParam;
26
27 /**
28  * This class does NOT inherit from TEntityTemplatesResource<ArtifactTemplate>
29  * as these templates are directly nested in a TDefinitionsElement
30  */
31 public abstract class AbstractComponentsWithTypeReferenceResource<T extends AbstractComponentInstanceResource> extends AbstractComponentsResource<T> {
32         
33         /**
34          * Creates the resource and sets the specified type
35          * 
36          * In contrast to the other component instances in this package, we
37          * additionally need the parameter "type" to set the type of the artifact
38          * template.
39          * 
40          * @param namespace Namespace of the template
41          * @param name name attribute of the template
42          * @param type: QName of the type, format: {namespace}localname is retrieved
43          *            from namespace manager
44          * 
45          * @return URI of the created Resource, null if resource already exists,
46          *         URI_internalServerError if an internal server error occurred
47          */
48         @POST
49         @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
50         @Produces(MediaType.TEXT_PLAIN)
51         @Override
52         public Response onPost(@RestDocParam(description = "Namespace of the component") @FormParam("namespace") String namespace, @RestDocParam(description = "name attribute of the component") @FormParam("name") String name, @RestDocParam(description = "QName of the type, format: {namespace}localname") @FormParam("type") String type) {
53                 // only check for type parameter as namespace and name are checked in super.onPost
54                 if (StringUtils.isEmpty(type)) {
55                         return Response.status(Status.BAD_REQUEST).build();
56                 }
57                 ResourceCreationResult creationResult = super.onPost(namespace, name);
58                 if (!creationResult.isSuccess()) {
59                         return creationResult.getResponse();
60                 }
61                 if (creationResult.getStatus().equals(Status.CREATED)) {
62                         IHasTypeReference resource = (IHasTypeReference) AbstractComponentsResource.getComponentInstaceResource((TOSCAComponentId) creationResult.getId());
63                         resource.setType(type);
64                         // we assume that setType succeeded and just return the result of the
65                         // creation of the artifact template resource
66                         // Thus, we do NOT change res
67                 }
68                 return creationResult.getResponse();
69         }
70         
71 }