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.addons.topologycompleter.helper;
15 import java.io.IOException;
16 import java.io.OutputStreamWriter;
17 import java.net.HttpURLConnection;
21 import org.eclipse.winery.common.Util;
22 import org.eclipse.winery.model.tosca.TTopologyTemplate;
23 import org.slf4j.LoggerFactory;
26 * This class contains helper methods to call the REST API and PUT/POST information to it.
28 public class RESTHelper {
30 private static final org.slf4j.Logger logger = LoggerFactory.getLogger(RESTHelper.class.getName());
33 * This method uses a REST call to save the completed {@link TTopologyTemplate} to the repository.
36 * the {@link TTopologyTemplate} to be saved
37 * @param topologyTemplateURL
38 * the URL the {@link TTopologyTemplate} is saved to
39 * @param overwriteTopology
40 * whether the topology is overwritten or a new topology shall be created
42 * the name of the newly created topology to build the URL if a new topology shall be created
43 * @param topologyNamespace
44 * the name space of the newly created topology to build the URL if a new topology shall be created
45 * @param repositoryURL
46 * the URL to the repository to build the URL if a new topology shall be created
48 public static void saveCompleteTopology(TTopologyTemplate topology, String topologyTemplateURL, boolean overwriteTopology, String topologyName, String topologyNamespace, String repositoryURL) {
53 if (overwriteTopology) {
54 url = new URL(topologyTemplateURL);
56 // this is necessary to avoid encoding issues
57 topologyNamespace = Util.DoubleURLencode(topologyNamespace);
58 // build the URL with the repositoryURL, the topology namespace and the topology name
59 url = new URL(repositoryURL + "/servicetemplates/" + topologyNamespace + "/" + topologyName + "/topologytemplate/");
61 logger.info("The URL the topology is saved to: " + url);
65 System.setProperty("javax.net.ssl.trustStore", "jssecacerts.cert");
68 HttpURLConnection urlConn;
69 urlConn = (HttpURLConnection) url.openConnection();
71 logger.info("Sending HTTP request...");
73 urlConn.setDoOutput(true);
74 urlConn.setRequestMethod("PUT");
75 urlConn.setRequestProperty("Content-type", "text/xml");
76 OutputStreamWriter out = new OutputStreamWriter(urlConn.getOutputStream());
78 // build the XML string to be saved
79 TTopologyTemplate outputTopology = JAXBHelper.buildXML(topology);
80 String outputString = JAXBHelper.getXMLAsString(outputTopology.getClass(), outputTopology);
82 logger.info(outputString);
83 logger.info("Sending output to Winery.");
85 out.write(outputString);
87 urlConn.getOutputStream().close();
88 logger.info("Output sent, waiting for response...");
89 urlConn.getInputStream();
91 logger.info("HTTP Response Code is: " + urlConn.getResponseCode());
93 } catch (IOException e) {
94 logger.error(e.getLocalizedMessage());