1 /*******************************************************************************
2 * Copyright (c) 2013 Pascal Hirmer.
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 * Pascal Hirmer - initial API and implementation
11 *******************************************************************************/
13 package org.eclipse.winery.topologymodeler.resources;
15 import java.io.StringReader;
17 import javax.ws.rs.FormParam;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.QueryParam;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25 import javax.xml.bind.JAXBContext;
26 import javax.xml.bind.JAXBException;
27 import javax.xml.bind.Unmarshaller;
29 import org.eclipse.winery.model.tosca.Definitions;
30 import org.eclipse.winery.model.tosca.TNodeTemplate;
31 import org.eclipse.winery.model.tosca.TRelationshipTemplate;
32 import org.eclipse.winery.model.tosca.TServiceTemplate;
33 import org.eclipse.winery.model.tosca.TTopologyTemplate;
34 import org.eclipse.winery.topologymodeler.addons.topologycompleter.helper.JAXBHelper;
35 import org.eclipse.winery.topologymodeler.addons.topologycompleter.helper.RESTHelper;
38 * This class contains resources used for the topology completion.
42 public class TopologyCompletionResource {
45 * Adds selected {@link TNodeTemplate}s and {@link TRelationshipTemplate}s
49 * the {@link TTopologyTemplate} as XML string
51 * all possible choices as XML
52 * @param selectedNodeTemplates
53 * the selected {@link TNodeTemplate}s as JSON array
54 * @param selectedRelationshipTemplates
55 * the selected {@link TRelationshipTemplate}s as JSON array
56 * @return the enhanced {@link TTopologyTemplate}
58 @Path("selectionhandler/")
60 @Produces(MediaType.TEXT_PLAIN)
61 public Response handleSelection(
62 @QueryParam(value = "topology") String topology,
63 @QueryParam(value = "allChoices") String allChoices,
64 @QueryParam(value = "selectedNodeTemplates") String selectedNodeTemplates,
65 @QueryParam(value = "selectedRelationshipTemplates") String selectedRelationshipTemplates) {
68 .entity(JAXBHelper.addTemplatesToTopology(topology, allChoices,
69 selectedNodeTemplates, selectedRelationshipTemplates))
74 * This resource is used to save a {@link TTopologyTemplate} to the repository.
77 * the topology to be saved
79 * the URL the {@link TTopologyTemplate} of the topology template
80 * @param repositoryURL
81 * the URL of the repository
83 * the name of the saved {@link TTopologyTemplate}
84 * @param topologyNamespace
85 * the namespace of the saved {@link TTopologyTemplate}
86 * @param overwriteTopology
87 * whether the {@link TTopologyTemplate} should be overwritten or not
89 * @return whether the save operation has been successful or not
91 @Path("topologysaver/")
93 public Response saveTopology(@FormParam("topology") String topology,
94 @FormParam(value = "templateURL") String templateURL,
95 @FormParam(value = "repositoryURL") String repositoryURL,
96 @FormParam(value = "topologyName") String topologyName,
97 @FormParam(value = "topologyNamespace") String topologyNamespace,
98 @FormParam(value = "overwriteTopology") String overwriteTopology) {
101 boolean overwrite = Boolean.parseBoolean(overwriteTopology);
103 // initiate JaxB context
105 context = JAXBContext.newInstance(Definitions.class);
106 StringReader reader = new StringReader(topology);
108 // unmarshall the topology XML string
111 um = context.createUnmarshaller();
113 Definitions jaxBDefinitions = (Definitions) um.unmarshal(reader);
114 TServiceTemplate st = (TServiceTemplate) jaxBDefinitions
115 .getServiceTemplateOrNodeTypeOrNodeTypeImplementation()
117 TTopologyTemplate toBeSaved = st.getTopologyTemplate();
119 // depending on the selected save method (overwrite or create new)
120 // the save method is called
122 RESTHelper.saveCompleteTopology(toBeSaved, templateURL, true,
123 "", "", repositoryURL);
125 RESTHelper.saveCompleteTopology(toBeSaved, templateURL, false,
126 topologyName, topologyNamespace, repositoryURL);
129 return Response.ok().build();
131 } catch (JAXBException e) {
133 return Response.serverError().build();