Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.topologymodeler / src / main / java / org / eclipse / winery / topologymodeler / addons / topologycompleter / helper / RESTHelper.java
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
8  *
9  * Contributors:
10  *    Pascal Hirmer - initial API and implementation
11  *******************************************************************************/
12
13 package org.eclipse.winery.topologymodeler.addons.topologycompleter.helper;
14
15 import java.io.IOException;
16 import java.io.OutputStreamWriter;
17 import java.net.HttpURLConnection;
18 import java.net.URL;
19
20
21 import org.eclipse.winery.common.Util;
22 import org.eclipse.winery.model.tosca.TTopologyTemplate;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * This class contains helper methods to call the REST API and PUT/POST information to it.
27  */
28 public class RESTHelper {
29
30         private static final org.slf4j.Logger logger = LoggerFactory.getLogger(RESTHelper.class.getName());
31
32         /**
33          * This method uses a REST call to save the completed {@link TTopologyTemplate} to the repository.
34          *
35          * @param topology
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
41          * @param topologyName
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
47          */
48         public static void saveCompleteTopology(TTopologyTemplate topology, String topologyTemplateURL, boolean overwriteTopology, String topologyName, String topologyNamespace, String repositoryURL) {
49                 try {
50
51                         URL url = null;
52
53                         if (overwriteTopology) {
54                                 url = new URL(topologyTemplateURL);
55                         } else {
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/");
60
61                                 logger.info("The URL the topology is saved to: " + url);
62                         }
63
64                         // using SSL
65                         System.setProperty("javax.net.ssl.trustStore", "jssecacerts.cert");
66
67                         // configure message
68                         HttpURLConnection urlConn;
69                         urlConn = (HttpURLConnection) url.openConnection();
70
71                         logger.info("Sending HTTP request...");
72
73                         urlConn.setDoOutput(true);
74                         urlConn.setRequestMethod("PUT");
75                         urlConn.setRequestProperty("Content-type", "text/xml");
76                         OutputStreamWriter out = new OutputStreamWriter(urlConn.getOutputStream());
77
78                         // build the XML string to be saved
79                         TTopologyTemplate outputTopology = JAXBHelper.buildXML(topology);
80                         String outputString = JAXBHelper.getXMLAsString(outputTopology.getClass(), outputTopology);
81
82                         logger.info(outputString);
83                         logger.info("Sending output to Winery.");
84
85                         out.write(outputString);
86                         out.close();
87                         urlConn.getOutputStream().close();
88                         logger.info("Output sent, waiting for response...");
89                         urlConn.getInputStream();
90
91                         logger.info("HTTP Response Code is: " + urlConn.getResponseCode());
92
93                 } catch (IOException e) {
94                         logger.error(e.getLocalizedMessage());
95                 }
96         }
97 }