summaryrefslogtreecommitdiffstats
path: root/mdsal-resource/provider/src/main/java/org
diff options
context:
space:
mode:
authorMarcus G K Williams <marcus.williams@intel.com>2017-08-02 15:09:24 -0700
committerMarcus G K Williams <marcus.williams@intel.com>2017-08-02 16:08:09 -0700
commit806052bf4df8bd023cfb391794bc5937f2624716 (patch)
tree166e4a28582d96eaf3a8dd69b484cf02076d634c /mdsal-resource/provider/src/main/java/org
parent16508ffde287f481791ae0b9c071f60a331c680d (diff)
Convert tabs to spaces
Per Java Code Style Guide: https://wiki.onap.org/display/DW/Java+code+style Converting tabs to 4 spaces. Issue-Id: SDNC-25 Change-Id: I99ce6c244df72f805b52d0d66403d8b76d9929ae Signed-off-by: Marcus G K Williams <marcus.williams@intel.com>
Diffstat (limited to 'mdsal-resource/provider/src/main/java/org')
-rw-r--r--mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/ConfigResource.java160
-rw-r--r--mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/MdsalResourceActivator.java90
-rw-r--r--mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/OperationalResource.java150
-rw-r--r--mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/RestService.java326
4 files changed, 363 insertions, 363 deletions
diff --git a/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/ConfigResource.java b/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/ConfigResource.java
index ed0ed0638..4a89ad8f6 100644
--- a/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/ConfigResource.java
+++ b/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/ConfigResource.java
@@ -33,118 +33,118 @@ import org.w3c.dom.Document;
public class ConfigResource implements SvcLogicResource {
- private static final Logger LOG = LoggerFactory.getLogger(ConfigResource.class);
+ private static final Logger LOG = LoggerFactory.getLogger(ConfigResource.class);
- private RestService restService;
+ private RestService restService;
- public ConfigResource(String sdncProtocol, String sdncHost, String sdncPort, String sdncUser, String sdncPasswd)
- {
- restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, RestService.PayloadType.XML);
- }
+ public ConfigResource(String sdncProtocol, String sdncHost, String sdncPort, String sdncUser, String sdncPasswd)
+ {
+ restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, RestService.PayloadType.XML);
+ }
- @Override
- public QueryStatus isAvailable(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException
- {
- return(query(resource, false, null, key, prefix, null, null));
- }
+ @Override
+ public QueryStatus isAvailable(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException
+ {
+ return(query(resource, false, null, key, prefix, null, null));
+ }
- @Override
- public QueryStatus exists(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException
- {
+ @Override
+ public QueryStatus exists(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException
+ {
- return(query(resource, false, null, key, prefix, null, null));
+ return(query(resource, false, null, key, prefix, null, null));
- }
+ }
- @Override
- public QueryStatus query(String resource, boolean localOnly, String select, String key, String prefix,
- String orderBy, SvcLogicContext ctx) throws SvcLogicException {
+ @Override
+ public QueryStatus query(String resource, boolean localOnly, String select, String key, String prefix,
+ String orderBy, SvcLogicContext ctx) throws SvcLogicException {
- String module = resource;
- StringBuffer restQuery = new StringBuffer();
+ String module = resource;
+ StringBuffer restQuery = new StringBuffer();
- String[] keyParts = key.split("/");
+ String[] keyParts = key.split("/");
- for (String keyPart : keyParts) {
- if (restQuery.length() > 0) {
- restQuery.append("/");
- }
- if (keyPart.startsWith("$")) {
+ for (String keyPart : keyParts) {
+ if (restQuery.length() > 0) {
+ restQuery.append("/");
+ }
+ if (keyPart.startsWith("$")) {
- restQuery.append(ctx.resolve(keyPart.substring(1)));
- } else {
- restQuery.append(keyPart);
- }
- }
+ restQuery.append(ctx.resolve(keyPart.substring(1)));
+ } else {
+ restQuery.append(keyPart);
+ }
+ }
- String restQueryStr = restQuery.toString();
- if ((restQueryStr.startsWith("'") && restQueryStr.endsWith("'")) ||
- (restQueryStr.startsWith("\"") && restQueryStr.endsWith("\""))) {
- restQueryStr = restQueryStr.substring(1, restQueryStr.length()-1);
- }
+ String restQueryStr = restQuery.toString();
+ if ((restQueryStr.startsWith("'") && restQueryStr.endsWith("'")) ||
+ (restQueryStr.startsWith("\"") && restQueryStr.endsWith("\""))) {
+ restQueryStr = restQueryStr.substring(1, restQueryStr.length()-1);
+ }
- String urlString = "restconf/config/" + module + ":" + restQueryStr;
+ String urlString = "restconf/config/" + module + ":" + restQueryStr;
LOG.info("Querying resource: " + resource + ". At URL: " + urlString);
- Document results = restService.get(urlString);
+ Document results = restService.get(urlString);
- if (results == null) {
- return(QueryStatus.NOT_FOUND);
- } else {
+ if (results == null) {
+ return(QueryStatus.NOT_FOUND);
+ } else {
- if (ctx != null) {
- ctx.mergeDocument(prefix, results);
- }
- return(QueryStatus.SUCCESS);
- }
+ if (ctx != null) {
+ ctx.mergeDocument(prefix, results);
+ }
+ return(QueryStatus.SUCCESS);
+ }
- }
+ }
- @Override
- public QueryStatus reserve(String resource, String select, String key, String prefix,
- SvcLogicContext ctx) throws SvcLogicException {
+ @Override
+ public QueryStatus reserve(String resource, String select, String key, String prefix,
+ SvcLogicContext ctx) throws SvcLogicException {
- return(QueryStatus.SUCCESS);
+ return(QueryStatus.SUCCESS);
- }
+ }
- @Override
- public QueryStatus release(String resource, String key, SvcLogicContext ctx) throws SvcLogicException {
+ @Override
+ public QueryStatus release(String resource, String key, SvcLogicContext ctx) throws SvcLogicException {
- return(QueryStatus.SUCCESS);
- }
+ return(QueryStatus.SUCCESS);
+ }
- @Override
- public QueryStatus delete(String arg0, String arg1, SvcLogicContext arg2)
- throws SvcLogicException {
- // TODO Auto-generated method stub
- return(QueryStatus.SUCCESS);
- }
+ @Override
+ public QueryStatus delete(String arg0, String arg1, SvcLogicContext arg2)
+ throws SvcLogicException {
+ // TODO Auto-generated method stub
+ return(QueryStatus.SUCCESS);
+ }
- @Override
- public QueryStatus save(String arg0, boolean arg1, boolean localOnly, String arg2,
- Map<String, String> arg3, String arg4, SvcLogicContext arg5)
- throws SvcLogicException {
- // TODO Auto-generated method stub
- return(QueryStatus.SUCCESS);
- }
+ @Override
+ public QueryStatus save(String arg0, boolean arg1, boolean localOnly, String arg2,
+ Map<String, String> arg3, String arg4, SvcLogicContext arg5)
+ throws SvcLogicException {
+ // TODO Auto-generated method stub
+ return(QueryStatus.SUCCESS);
+ }
- @Override
- public QueryStatus notify(String resource, String action, String key,
- SvcLogicContext ctx) throws SvcLogicException {
- return(QueryStatus.SUCCESS);
- }
+ @Override
+ public QueryStatus notify(String resource, String action, String key,
+ SvcLogicContext ctx) throws SvcLogicException {
+ return(QueryStatus.SUCCESS);
+ }
- public QueryStatus update(String resource, String key,
- Map<String, String> parms, String prefix, SvcLogicContext ctx)
- throws SvcLogicException {
- return(QueryStatus.SUCCESS);
- }
+ public QueryStatus update(String resource, String key,
+ Map<String, String> parms, String prefix, SvcLogicContext ctx)
+ throws SvcLogicException {
+ return(QueryStatus.SUCCESS);
+ }
diff --git a/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/MdsalResourceActivator.java b/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/MdsalResourceActivator.java
index ea0bab263..9be03e9d4 100644
--- a/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/MdsalResourceActivator.java
+++ b/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/MdsalResourceActivator.java
@@ -38,69 +38,69 @@ public class MdsalResourceActivator implements BundleActivator {
- private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
+ private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
- public LinkedList<ServiceRegistration> registrations = new LinkedList<ServiceRegistration>();
+ public LinkedList<ServiceRegistration> registrations = new LinkedList<ServiceRegistration>();
- private static final Logger LOG = LoggerFactory
- .getLogger(MdsalResourceActivator.class);
+ private static final Logger LOG = LoggerFactory
+ .getLogger(MdsalResourceActivator.class);
- @Override
- public void start(BundleContext ctx) throws Exception {
+ @Override
+ public void start(BundleContext ctx) throws Exception {
- // Read properties
- Properties props = new Properties();
+ // Read properties
+ Properties props = new Properties();
- String propDir = System.getenv(SDNC_CONFIG_DIR);
- if (propDir == null) {
+ String propDir = System.getenv(SDNC_CONFIG_DIR);
+ if (propDir == null) {
- propDir = "/opt/sdnc/data/properties";
- }
- String propPath = propDir + "/mdsal-resource.properties";
+ propDir = "/opt/sdnc/data/properties";
+ }
+ String propPath = propDir + "/mdsal-resource.properties";
- File propFile = new File(propPath);
+ File propFile = new File(propPath);
- if (!propFile.exists()) {
+ if (!propFile.exists()) {
- throw new ConfigurationException(
- "Missing configuration properties file : "
- + propFile);
- }
- try {
+ throw new ConfigurationException(
+ "Missing configuration properties file : "
+ + propFile);
+ }
+ try {
- props.load(new FileInputStream(propFile));
- } catch (Exception e) {
- throw new ConfigurationException(
- "Could not load properties file " + propPath, e);
+ props.load(new FileInputStream(propFile));
+ } catch (Exception e) {
+ throw new ConfigurationException(
+ "Could not load properties file " + propPath, e);
- }
+ }
- String sdncUser = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-user", "admin");
- String sdncPasswd = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-passwd", "admin");
- String sdncHost = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-host", "localhost");
- String sdncProtocol = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-protocol", "https");
- String sdncPort = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-port", "8443");
+ String sdncUser = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-user", "admin");
+ String sdncPasswd = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-passwd", "admin");
+ String sdncHost = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-host", "localhost");
+ String sdncProtocol = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-protocol", "https");
+ String sdncPort = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-port", "8443");
- // Advertise MD-SAL resource adaptors
- SvcLogicResource impl = new ConfigResource(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd);
+ // Advertise MD-SAL resource adaptors
+ SvcLogicResource impl = new ConfigResource(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd);
- LOG.debug("Registering MdsalResource service "+impl.getClass().getName());
- registrations.add(ctx.registerService(impl.getClass().getName(), impl, null));
+ LOG.debug("Registering MdsalResource service "+impl.getClass().getName());
+ registrations.add(ctx.registerService(impl.getClass().getName(), impl, null));
- impl = new OperationalResource(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd);
+ impl = new OperationalResource(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd);
- LOG.debug("Registering MdsalResource service "+impl.getClass().getName());
- registrations.add(ctx.registerService(impl.getClass().getName(), impl, null));
- }
+ LOG.debug("Registering MdsalResource service "+impl.getClass().getName());
+ registrations.add(ctx.registerService(impl.getClass().getName(), impl, null));
+ }
- @Override
- public void stop(BundleContext ctx) throws Exception {
+ @Override
+ public void stop(BundleContext ctx) throws Exception {
- for (ServiceRegistration registration : registrations)
- {
- registration.unregister();
- }
- }
+ for (ServiceRegistration registration : registrations)
+ {
+ registration.unregister();
+ }
+ }
}
diff --git a/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/OperationalResource.java b/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/OperationalResource.java
index 32323e3b1..9374d2ab4 100644
--- a/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/OperationalResource.java
+++ b/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/OperationalResource.java
@@ -33,52 +33,52 @@ import org.w3c.dom.Document;
public class OperationalResource implements SvcLogicResource {
- private static final Logger LOG = LoggerFactory.getLogger(OperationalResource.class);
+ private static final Logger LOG = LoggerFactory.getLogger(OperationalResource.class);
- private RestService restService;
+ private RestService restService;
- public OperationalResource(String sdncProtocol, String sdncHost, String sdncPort, String sdncUser, String sdncPasswd)
- {
- restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, RestService.PayloadType.XML);
+ public OperationalResource(String sdncProtocol, String sdncHost, String sdncPort, String sdncUser, String sdncPasswd)
+ {
+ restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, RestService.PayloadType.XML);
- }
+ }
- @Override
- public QueryStatus isAvailable(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException
- {
- return(query(resource, false, null, key, prefix, null, null));
- }
+ @Override
+ public QueryStatus isAvailable(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException
+ {
+ return(query(resource, false, null, key, prefix, null, null));
+ }
- @Override
- public QueryStatus exists(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException
- {
+ @Override
+ public QueryStatus exists(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException
+ {
- return(query(resource, false, null, key, prefix, null, null));
+ return(query(resource, false, null, key, prefix, null, null));
- }
+ }
- @Override
- public QueryStatus query(String resource, boolean localOnly, String select, String key, String prefix,
- String orderBy, SvcLogicContext ctx) throws SvcLogicException {
+ @Override
+ public QueryStatus query(String resource, boolean localOnly, String select, String key, String prefix,
+ String orderBy, SvcLogicContext ctx) throws SvcLogicException {
- String module = resource;
- StringBuffer restQuery = new StringBuffer();
+ String module = resource;
+ StringBuffer restQuery = new StringBuffer();
- String[] keyParts = key.split("/");
+ String[] keyParts = key.split("/");
- for (String keyPart : keyParts) {
- if (restQuery.length() > 0) {
- restQuery.append("/");
- }
- if (keyPart.startsWith("$")) {
+ for (String keyPart : keyParts) {
+ if (restQuery.length() > 0) {
+ restQuery.append("/");
+ }
+ if (keyPart.startsWith("$")) {
- restQuery.append(ctx.resolve(keyPart.substring(1)));
- } else {
- restQuery.append(keyPart);
- }
- }
+ restQuery.append(ctx.resolve(keyPart.substring(1)));
+ } else {
+ restQuery.append(keyPart);
+ }
+ }
String restQueryStr = restQuery.toString();
if ((restQueryStr.startsWith("'") && restQueryStr.endsWith("'")) ||
@@ -86,66 +86,66 @@ public class OperationalResource implements SvcLogicResource {
restQueryStr = restQueryStr.substring(1, restQueryStr.length()-1);
}
- String urlString = "restconf/operational/" + module + ":" + restQueryStr;
+ String urlString = "restconf/operational/" + module + ":" + restQueryStr;
LOG.info("Querying resource: " + resource + ". At URL: " + urlString);
- Document results = restService.get(urlString);
+ Document results = restService.get(urlString);
- if (results == null) {
- return(QueryStatus.NOT_FOUND);
- } else {
+ if (results == null) {
+ return(QueryStatus.NOT_FOUND);
+ } else {
- if (ctx != null) {
- ctx.mergeDocument(prefix, results);
- }
- return(QueryStatus.SUCCESS);
- }
+ if (ctx != null) {
+ ctx.mergeDocument(prefix, results);
+ }
+ return(QueryStatus.SUCCESS);
+ }
- }
+ }
- @Override
- public QueryStatus reserve(String resource, String select, String key, String prefix,
- SvcLogicContext ctx) throws SvcLogicException {
+ @Override
+ public QueryStatus reserve(String resource, String select, String key, String prefix,
+ SvcLogicContext ctx) throws SvcLogicException {
- return(QueryStatus.SUCCESS);
+ return(QueryStatus.SUCCESS);
- }
+ }
- @Override
- public QueryStatus release(String resource, String key, SvcLogicContext ctx) throws SvcLogicException {
+ @Override
+ public QueryStatus release(String resource, String key, SvcLogicContext ctx) throws SvcLogicException {
- return(QueryStatus.SUCCESS);
- }
+ return(QueryStatus.SUCCESS);
+ }
- @Override
- public QueryStatus delete(String arg0, String arg1, SvcLogicContext arg2)
- throws SvcLogicException {
- // TODO Auto-generated method stub
- return(QueryStatus.SUCCESS);
- }
+ @Override
+ public QueryStatus delete(String arg0, String arg1, SvcLogicContext arg2)
+ throws SvcLogicException {
+ // TODO Auto-generated method stub
+ return(QueryStatus.SUCCESS);
+ }
- @Override
- public QueryStatus save(String arg0, boolean arg1, boolean localOnly, String arg2,
- Map<String, String> arg3, String arg4, SvcLogicContext arg5)
- throws SvcLogicException {
- // TODO Auto-generated method stub
- return(QueryStatus.SUCCESS);
- }
+ @Override
+ public QueryStatus save(String arg0, boolean arg1, boolean localOnly, String arg2,
+ Map<String, String> arg3, String arg4, SvcLogicContext arg5)
+ throws SvcLogicException {
+ // TODO Auto-generated method stub
+ return(QueryStatus.SUCCESS);
+ }
- @Override
- public QueryStatus notify(String resource, String action, String key,
- SvcLogicContext ctx) throws SvcLogicException {
- return(QueryStatus.SUCCESS);
- }
+ @Override
+ public QueryStatus notify(String resource, String action, String key,
+ SvcLogicContext ctx) throws SvcLogicException {
+ return(QueryStatus.SUCCESS);
+ }
- public QueryStatus update(String resource, String key,
- Map<String, String> parms, String prefix, SvcLogicContext ctx)
- throws SvcLogicException {
- return(QueryStatus.SUCCESS);
- }
+ public QueryStatus update(String resource, String key,
+ Map<String, String> parms, String prefix, SvcLogicContext ctx)
+ throws SvcLogicException {
+ return(QueryStatus.SUCCESS);
+ }
}
diff --git a/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/RestService.java b/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/RestService.java
index 21d3439d4..a7b77e5bd 100644
--- a/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/RestService.java
+++ b/mdsal-resource/provider/src/main/java/org/openecomp/sdnc/sli/resource/mdsal/RestService.java
@@ -46,167 +46,167 @@ import org.w3c.dom.Document;
public class RestService {
-
- private static final Logger LOG = LoggerFactory.getLogger(ConfigResource.class);
-
- public enum PayloadType {
- XML,
- JSON
- }
-
- private class SdncAuthenticator extends Authenticator {
-
- private String user;
- private String passwd;
-
- SdncAuthenticator(String user, String passwd) {
- this.user = user;
- this.passwd = passwd;
- }
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(user, passwd.toCharArray());
- }
-
- }
-
- private String user;
- private String passwd;
- private PayloadType payloadType;
-
- private String protocol;
- private String host;
- private String port;
-
- public RestService(String protocol, String host, String port, String user, String passwd, PayloadType payloadType) {
- this.protocol = protocol;
- this.host = host;
- this.port = port;
- this.user = user;
- this.passwd = passwd;
- this.payloadType = payloadType;
- }
-
- private HttpURLConnection getRestConnection(String urlString, String method) throws IOException
- {
-
- URL sdncUrl = new URL(urlString);
- Authenticator.setDefault(new SdncAuthenticator(user, passwd));
-
- HttpURLConnection conn = (HttpURLConnection) sdncUrl.openConnection();
-
- String authStr = user+":"+passwd;
- String encodedAuthStr = new String(Base64.encodeBase64(authStr.getBytes()));
-
- conn.addRequestProperty("Authentication", "Basic "+encodedAuthStr);
-
- conn.setRequestMethod(method);
-
- if (payloadType == PayloadType.XML) {
- conn.setRequestProperty("Content-Type", "application/xml");
- conn.setRequestProperty("Accept", "application/xml");
- } else {
-
- conn.setRequestProperty("Content-Type", "application/json");
- conn.setRequestProperty("Accept", "application/json");
- }
-
- conn.setDoInput(true);
- conn.setDoOutput(true);
- conn.setUseCaches(false);
-
- return(conn);
-
- }
-
-
- private Document send(String urlString, byte[] msgBytes, String method) {
- Document response = null;
- String fullUrl = protocol + "://" + host + ":" + port + "/" + urlString;
- LOG.info("Sending REST "+method +" to "+fullUrl);
-
- if (msgBytes != null) {
- LOG.info("Message body:\n"+msgBytes);
- }
-
- try {
- HttpURLConnection conn = getRestConnection(fullUrl, method);
-
- if (conn instanceof HttpsURLConnection) {
- HostnameVerifier hostnameVerifier = new HostnameVerifier() {
- @Override
- public boolean verify(String hostname, SSLSession session) {
- return true;
- }
- };
- ((HttpsURLConnection)conn).setHostnameVerifier(hostnameVerifier);
- }
-
- // Write message
- if (msgBytes != null) {
- conn.setRequestProperty("Content-Length", ""+msgBytes.length);
- DataOutputStream outStr = new DataOutputStream(conn.getOutputStream());
- outStr.write(msgBytes);
- outStr.close();
- } else {
- conn.setRequestProperty("Content-Length", "0");
- }
-
-
- // Read response
- BufferedReader respRdr;
-
- LOG.info("Response: "+conn.getResponseCode()+" "+conn.getResponseMessage());
-
-
- if (conn.getResponseCode() < 300) {
-
- respRdr = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- } else {
- respRdr = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
- }
-
- StringBuffer respBuff = new StringBuffer();
-
- String respLn;
-
- while ((respLn = respRdr.readLine()) != null) {
- respBuff.append(respLn+"\n");
- }
- respRdr.close();
-
- String respString = respBuff.toString();
-
- LOG.info("Response body :\n"+respString);
-
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
-
-
- response = db.parse(new ByteArrayInputStream(respString.getBytes()));
-
- } catch (Exception e) {
-
- LOG.error("Caught exception executing REST command", e);
- }
-
- return (response);
- }
-
-
- public Document get(String urlString) {
- return(send(urlString, null, "GET"));
- }
-
- public Document delete(String urlString) {
- return(send(urlString, null, "DELETE"));
- }
-
- public Document post(String urlString, byte[] msgBytes) {
- return(send(urlString, msgBytes, "POST"));
- }
-
- public Document put(String urlString, byte[] msgBytes) {
- return(send(urlString, msgBytes, "PUT"));
- }
+
+ private static final Logger LOG = LoggerFactory.getLogger(ConfigResource.class);
+
+ public enum PayloadType {
+ XML,
+ JSON
+ }
+
+ private class SdncAuthenticator extends Authenticator {
+
+ private String user;
+ private String passwd;
+
+ SdncAuthenticator(String user, String passwd) {
+ this.user = user;
+ this.passwd = passwd;
+ }
+ @Override
+ protected PasswordAuthentication getPasswordAuthentication() {
+ return new PasswordAuthentication(user, passwd.toCharArray());
+ }
+
+ }
+
+ private String user;
+ private String passwd;
+ private PayloadType payloadType;
+
+ private String protocol;
+ private String host;
+ private String port;
+
+ public RestService(String protocol, String host, String port, String user, String passwd, PayloadType payloadType) {
+ this.protocol = protocol;
+ this.host = host;
+ this.port = port;
+ this.user = user;
+ this.passwd = passwd;
+ this.payloadType = payloadType;
+ }
+
+ private HttpURLConnection getRestConnection(String urlString, String method) throws IOException
+ {
+
+ URL sdncUrl = new URL(urlString);
+ Authenticator.setDefault(new SdncAuthenticator(user, passwd));
+
+ HttpURLConnection conn = (HttpURLConnection) sdncUrl.openConnection();
+
+ String authStr = user+":"+passwd;
+ String encodedAuthStr = new String(Base64.encodeBase64(authStr.getBytes()));
+
+ conn.addRequestProperty("Authentication", "Basic "+encodedAuthStr);
+
+ conn.setRequestMethod(method);
+
+ if (payloadType == PayloadType.XML) {
+ conn.setRequestProperty("Content-Type", "application/xml");
+ conn.setRequestProperty("Accept", "application/xml");
+ } else {
+
+ conn.setRequestProperty("Content-Type", "application/json");
+ conn.setRequestProperty("Accept", "application/json");
+ }
+
+ conn.setDoInput(true);
+ conn.setDoOutput(true);
+ conn.setUseCaches(false);
+
+ return(conn);
+
+ }
+
+
+ private Document send(String urlString, byte[] msgBytes, String method) {
+ Document response = null;
+ String fullUrl = protocol + "://" + host + ":" + port + "/" + urlString;
+ LOG.info("Sending REST "+method +" to "+fullUrl);
+
+ if (msgBytes != null) {
+ LOG.info("Message body:\n"+msgBytes);
+ }
+
+ try {
+ HttpURLConnection conn = getRestConnection(fullUrl, method);
+
+ if (conn instanceof HttpsURLConnection) {
+ HostnameVerifier hostnameVerifier = new HostnameVerifier() {
+ @Override
+ public boolean verify(String hostname, SSLSession session) {
+ return true;
+ }
+ };
+ ((HttpsURLConnection)conn).setHostnameVerifier(hostnameVerifier);
+ }
+
+ // Write message
+ if (msgBytes != null) {
+ conn.setRequestProperty("Content-Length", ""+msgBytes.length);
+ DataOutputStream outStr = new DataOutputStream(conn.getOutputStream());
+ outStr.write(msgBytes);
+ outStr.close();
+ } else {
+ conn.setRequestProperty("Content-Length", "0");
+ }
+
+
+ // Read response
+ BufferedReader respRdr;
+
+ LOG.info("Response: "+conn.getResponseCode()+" "+conn.getResponseMessage());
+
+
+ if (conn.getResponseCode() < 300) {
+
+ respRdr = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+ } else {
+ respRdr = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
+ }
+
+ StringBuffer respBuff = new StringBuffer();
+
+ String respLn;
+
+ while ((respLn = respRdr.readLine()) != null) {
+ respBuff.append(respLn+"\n");
+ }
+ respRdr.close();
+
+ String respString = respBuff.toString();
+
+ LOG.info("Response body :\n"+respString);
+
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ DocumentBuilder db = dbf.newDocumentBuilder();
+
+
+ response = db.parse(new ByteArrayInputStream(respString.getBytes()));
+
+ } catch (Exception e) {
+
+ LOG.error("Caught exception executing REST command", e);
+ }
+
+ return (response);
+ }
+
+
+ public Document get(String urlString) {
+ return(send(urlString, null, "GET"));
+ }
+
+ public Document delete(String urlString) {
+ return(send(urlString, null, "DELETE"));
+ }
+
+ public Document post(String urlString, byte[] msgBytes) {
+ return(send(urlString, msgBytes, "POST"));
+ }
+
+ public Document put(String urlString, byte[] msgBytes) {
+ return(send(urlString, msgBytes, "PUT"));
+ }
}