2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.adaptors.resource.mdsal;
24 import org.apache.commons.codec.binary.Base64;
25 import org.onap.ccsdk.sli.core.utils.common.AcceptIpAddressHostNameVerifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.w3c.dom.Document;
30 import javax.net.ssl.HostnameVerifier;
31 import javax.net.ssl.HttpsURLConnection;
32 import javax.xml.XMLConstants;
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
36 import java.net.Authenticator;
37 import java.net.HttpURLConnection;
38 import java.net.PasswordAuthentication;
41 public class RestService {
43 private static final Logger LOG = LoggerFactory.getLogger(ConfigResource.class);
45 private String passwd;
46 private String contentType;
47 private String accept;
48 private String protocol;
52 public RestService(String protocol, String host, String port, String user, String passwd, String accept, String contentType) {
53 this.protocol = protocol;
59 this.contentType = contentType;
62 private HttpURLConnection getRestConnection(String urlString, String method) throws IOException {
63 URL sdncUrl = new URL(urlString);
64 Authenticator.setDefault(new SdncAuthenticator(user, passwd));
66 String authStr = user + ":" + passwd;
67 String encodedAuthStr = new String(Base64.encodeBase64(authStr.getBytes()));
69 HttpURLConnection conn = (HttpURLConnection) sdncUrl.openConnection();
70 conn.addRequestProperty("Authentication", "Basic " + encodedAuthStr);
71 conn.setRequestMethod(method);
72 conn.setDoInput(true);
73 conn.setDoOutput(true);
74 conn.setUseCaches(false);
76 //Setting Accept header (doesn't dependent on Msg Body if present or not)
77 if ("XML".equalsIgnoreCase(accept)) {
78 conn.setRequestProperty("Accept", "application/xml");
80 conn.setRequestProperty("Accept", "application/json");
86 private Document send(String urlString, byte[] msgBytes, String method) {
87 Document response = null;
88 String fullUrl = protocol + "://" + host + ":" + port + "/" + urlString;
89 LOG.info("Sending REST {} to {}", method, fullUrl);
92 HttpURLConnection conn = getRestConnection(fullUrl, method);
93 if (conn instanceof HttpsURLConnection) {
94 // Safely disable host name verification if host is an IP address or 'localhost'
95 ((HttpsURLConnection) conn).setHostnameVerifier(new AcceptIpAddressHostNameVerifier());
99 if (msgBytes != null) {
100 LOG.info("Message body:\n{}", msgBytes);
101 conn.setRequestProperty("Content-Length", "" + msgBytes.length);
103 // Setting Content-Type header only if Msg Body is present
104 if ("XML".equalsIgnoreCase(contentType)) {
105 conn.setRequestProperty("Content-Type", "application/xml");
107 conn.setRequestProperty("Content-Type", "application/json");
110 DataOutputStream outStr = new DataOutputStream(conn.getOutputStream());
111 outStr.write(msgBytes);
114 conn.setRequestProperty("Content-Length", "0");
118 LOG.info("Response: {} {}", conn.getResponseCode(), conn.getResponseMessage());
120 BufferedReader respRdr;
121 if (conn.getResponseCode() < 300) {
122 respRdr = new BufferedReader(new InputStreamReader(conn.getInputStream()));
124 respRdr = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
127 StringBuffer respBuff = new StringBuffer();
129 while ((respLn = respRdr.readLine()) != null) {
130 respBuff.append(respLn + "\n");
134 String respString = respBuff.toString();
135 LOG.info("Response body :\n{}", respString);
137 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
138 dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
139 dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
140 dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
141 DocumentBuilder db = dbf.newDocumentBuilder();
143 response = db.parse(new ByteArrayInputStream(respString.getBytes()));
145 } catch (Exception e) {
146 LOG.error("Caught exception executing REST command", e);
152 public Document get(String urlString) {
153 return (send(urlString, null, "GET"));
156 public Document delete(String urlString) {
157 return (send(urlString, null, "DELETE"));
160 public Document post(String urlString, byte[] msgBytes) {
161 return (send(urlString, msgBytes, "POST"));
164 public Document put(String urlString, byte[] msgBytes) {
165 return (send(urlString, msgBytes, "PUT"));
169 private class SdncAuthenticator extends Authenticator {
171 private String passwd;
173 SdncAuthenticator(String user, String passwd) {
175 this.passwd = passwd;
179 protected PasswordAuthentication getPasswordAuthentication() {
180 return new PasswordAuthentication(user, passwd.toCharArray());