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 java.io.BufferedReader;
25 import java.io.ByteArrayInputStream;
26 import java.io.DataOutputStream;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.net.Authenticator;
30 import java.net.HttpURLConnection;
31 import java.net.PasswordAuthentication;
34 import javax.net.ssl.HostnameVerifier;
35 import javax.net.ssl.HttpsURLConnection;
36 import javax.net.ssl.SSLSession;
37 import javax.xml.parsers.DocumentBuilder;
38 import javax.xml.parsers.DocumentBuilderFactory;
40 import org.apache.commons.codec.binary.Base64;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.w3c.dom.Document;
48 public class RestService {
50 private static final Logger LOG = LoggerFactory.getLogger(ConfigResource.class);
52 public enum PayloadType {
57 private class SdncAuthenticator extends Authenticator {
60 private String passwd;
62 SdncAuthenticator(String user, String passwd) {
67 protected PasswordAuthentication getPasswordAuthentication() {
68 return new PasswordAuthentication(user, passwd.toCharArray());
74 private String passwd;
75 private PayloadType payloadType;
77 private String protocol;
81 public RestService(String protocol, String host, String port, String user, String passwd, PayloadType payloadType) {
82 this.protocol = protocol;
87 this.payloadType = payloadType;
90 private HttpURLConnection getRestConnection(String urlString, String method) throws IOException
93 URL sdncUrl = new URL(urlString);
94 Authenticator.setDefault(new SdncAuthenticator(user, passwd));
96 HttpURLConnection conn = (HttpURLConnection) sdncUrl.openConnection();
98 String authStr = user+":"+passwd;
99 String encodedAuthStr = new String(Base64.encodeBase64(authStr.getBytes()));
101 conn.addRequestProperty("Authentication", "Basic "+encodedAuthStr);
103 conn.setRequestMethod(method);
105 if (payloadType == PayloadType.XML) {
106 conn.setRequestProperty("Content-Type", "application/xml");
107 conn.setRequestProperty("Accept", "application/xml");
110 conn.setRequestProperty("Content-Type", "application/json");
111 conn.setRequestProperty("Accept", "application/json");
114 conn.setDoInput(true);
115 conn.setDoOutput(true);
116 conn.setUseCaches(false);
123 private Document send(String urlString, byte[] msgBytes, String method) {
124 Document response = null;
125 String fullUrl = protocol + "://" + host + ":" + port + "/" + urlString;
126 LOG.info("Sending REST "+method +" to "+fullUrl);
128 if (msgBytes != null) {
129 LOG.info("Message body:\n"+msgBytes);
133 HttpURLConnection conn = getRestConnection(fullUrl, method);
135 if (conn instanceof HttpsURLConnection) {
136 HostnameVerifier hostnameVerifier = new HostnameVerifier() {
138 public boolean verify(String hostname, SSLSession session) {
142 ((HttpsURLConnection)conn).setHostnameVerifier(hostnameVerifier);
146 if (msgBytes != null) {
147 conn.setRequestProperty("Content-Length", ""+msgBytes.length);
148 DataOutputStream outStr = new DataOutputStream(conn.getOutputStream());
149 outStr.write(msgBytes);
152 conn.setRequestProperty("Content-Length", "0");
157 BufferedReader respRdr;
159 LOG.info("Response: "+conn.getResponseCode()+" "+conn.getResponseMessage());
162 if (conn.getResponseCode() < 300) {
164 respRdr = new BufferedReader(new InputStreamReader(conn.getInputStream()));
166 respRdr = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
169 StringBuffer respBuff = new StringBuffer();
173 while ((respLn = respRdr.readLine()) != null) {
174 respBuff.append(respLn+"\n");
178 String respString = respBuff.toString();
180 LOG.info("Response body :\n"+respString);
182 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
183 DocumentBuilder db = dbf.newDocumentBuilder();
186 response = db.parse(new ByteArrayInputStream(respString.getBytes()));
188 } catch (Exception e) {
190 LOG.error("Caught exception executing REST command", e);
197 public Document get(String urlString) {
198 return(send(urlString, null, "GET"));
201 public Document delete(String urlString) {
202 return(send(urlString, null, "DELETE"));
205 public Document post(String urlString, byte[] msgBytes) {
206 return(send(urlString, msgBytes, "POST"));
209 public Document put(String urlString, byte[] msgBytes) {
210 return(send(urlString, msgBytes, "PUT"));