* openECOMP : SDN-C
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights
- * reserved.
+ * reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* The AAIClientRESTExecutor class provides CRUD API for AAI Client service.
* @author richtabedzki
*/
-public class AAIClientRESTExecutor implements AAIExecutorInterface {
-
- private final String truststorePath;
- private final String truststorePassword;
- private final String keystorePath;
- private final String keystorePassword;
- private final Boolean ignoreCertificateHostError;
- // authentication credentials
- private String userName;
- private String userPassword;
- private final String applicationId;
-
- /**
- * class Constructor
- * @param props - properties to initialize an instance.
- */
- public AAIClientRESTExecutor(Properties props) {
- super();
-
- userName = props.getProperty(AAIService.CLIENT_NAME);
- userPassword = props.getProperty(AAIService.CLIENT_PWWD);
+public class AAIClientRESTExecutor implements AAIExecutorInterface {
+
+ private final String truststorePath;
+ private final String truststorePassword;
+ private final String keystorePath;
+ private final String keystorePassword;
+ private final Boolean ignoreCertificateHostError;
+ // authentication credentials
+ private String userName;
+ private String userPassword;
+ private final String applicationId;
+
+ /**
+ * class Constructor
+ * @param props - properties to initialize an instance.
+ */
+ public AAIClientRESTExecutor(Properties props) {
+ super();
+
+ userName = props.getProperty(AAIService.CLIENT_NAME);
+ userPassword = props.getProperty(AAIService.CLIENT_PWWD);
if(userName == null || userName.isEmpty()){
- LOG.debug("Basic user name is not set");
+ LOG.debug("Basic user name is not set");
}
if(userPassword == null || userPassword.isEmpty()) {
- LOG.debug("Basic password is not set");
+ LOG.debug("Basic password is not set");
}
- truststorePath = props.getProperty(AAIService.TRUSTSTORE_PATH);
- truststorePassword = props.getProperty(AAIService.TRUSTSTORE_PSSWD);
- keystorePath = props.getProperty(AAIService.KEYSTORE_PATH);
- keystorePassword = props.getProperty(AAIService.KEYSTORE_PSSWD);
-// this.read_timeout = read_timeout;
+ truststorePath = props.getProperty(AAIService.TRUSTSTORE_PATH);
+ truststorePassword = props.getProperty(AAIService.TRUSTSTORE_PSSWD);
+ keystorePath = props.getProperty(AAIService.KEYSTORE_PATH);
+ keystorePassword = props.getProperty(AAIService.KEYSTORE_PSSWD);
+// this.read_timeout = read_timeout;
- String tmpApplicationId =props.getProperty(AAIService.APPLICATION_ID);
- if(tmpApplicationId == null || tmpApplicationId.isEmpty()) {
- tmpApplicationId = "SDNC";
- }
- applicationId = tmpApplicationId;
+ String tmpApplicationId =props.getProperty(AAIService.APPLICATION_ID);
+ if(tmpApplicationId == null || tmpApplicationId.isEmpty()) {
+ tmpApplicationId = "SDNC";
+ }
+ applicationId = tmpApplicationId;
- String iche = props.getProperty(AAIService.CERTIFICATE_HOST_ERROR);
- boolean host_error = false;
- if(iche != null && !iche.isEmpty()) {
- host_error = Boolean.valueOf(iche);
- }
+ String iche = props.getProperty(AAIService.CERTIFICATE_HOST_ERROR);
+ boolean host_error = false;
+ if(iche != null && !iche.isEmpty()) {
+ host_error = Boolean.valueOf(iche);
+ }
- ignoreCertificateHostError = host_error;
+ ignoreCertificateHostError = host_error;
HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
public boolean verify(String string,SSLSession ssls) {
}
});
- if(truststorePath != null && truststorePassword != null && (new File(truststorePath)).exists()) {
- System.setProperty("javax.net.ssl.trustStore", truststorePath);
- System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
- }
+ if(truststorePath != null && truststorePassword != null && (new File(truststorePath)).exists()) {
+ System.setProperty("javax.net.ssl.trustStore", truststorePath);
+ System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
+ }
if(keystorePath != null && keystorePassword != null && (new File(keystorePath)).exists())
{
- DefaultClientConfig config = new DefaultClientConfig();
- //both jersey and HttpURLConnection can use this
- SSLContext ctx = null;
- try {
- ctx = SSLContext.getInstance("TLS");
-
- KeyManagerFactory kmf = null;
- try {
- String storeType = "PKCS12";
- String def = KeyStore.getDefaultType();
- kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
- FileInputStream fin = new FileInputStream(keystorePath);
-
- String extension = keystorePath.substring(keystorePath.lastIndexOf(".") + 1);
-
- if(extension != null && !extension.isEmpty() && extension.equalsIgnoreCase("JKS")) {
- storeType = "JKS";
- }
- KeyStore ks = KeyStore.getInstance(storeType);
-
- char[] pwd = keystorePassword.toCharArray();
- ks.load(fin, pwd);
- kmf.init(ks, pwd);
- } catch (Exception ex) {
- LOG.error("AAIResource", ex);
- }
-
- ctx.init(kmf.getKeyManagers(), null, null);
- config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties( new HostnameVerifier() {
- @Override
- public boolean verify( String s, SSLSession sslSession ) {
- return ignoreCertificateHostError;
- }
- }, ctx));
-
- CTX = ctx;
- LOG.debug("SSLContext created");
-
- } catch (KeyManagementException | NoSuchAlgorithmException exc) {
- LOG.error("AAIResource", exc);
- }
+ DefaultClientConfig config = new DefaultClientConfig();
+ //both jersey and HttpURLConnection can use this
+ SSLContext ctx = null;
+ try {
+ ctx = SSLContext.getInstance("TLS");
+
+ KeyManagerFactory kmf = null;
+ try {
+ String storeType = "PKCS12";
+ String def = KeyStore.getDefaultType();
+ kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+ FileInputStream fin = new FileInputStream(keystorePath);
+
+ String extension = keystorePath.substring(keystorePath.lastIndexOf(".") + 1);
+
+ if(extension != null && !extension.isEmpty() && extension.equalsIgnoreCase("JKS")) {
+ storeType = "JKS";
+ }
+ KeyStore ks = KeyStore.getInstance(storeType);
+
+ char[] pwd = keystorePassword.toCharArray();
+ ks.load(fin, pwd);
+ kmf.init(ks, pwd);
+ } catch (Exception ex) {
+ LOG.error("AAIResource", ex);
+ }
+
+ ctx.init(kmf.getKeyManagers(), null, null);
+ config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties( new HostnameVerifier() {
+ @Override
+ public boolean verify( String s, SSLSession sslSession ) {
+ return ignoreCertificateHostError;
+ }
+ }, ctx));
+
+ CTX = ctx;
+ LOG.debug("SSLContext created");
+
+ } catch (KeyManagementException | NoSuchAlgorithmException exc) {
+ LOG.error("AAIResource", exc);
+ }
}
try {
methodsField.set(null, methods);
} catch (SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {
- e.printStackTrace();
+ LOG.warn("Adding PATCH method", e);
}
LOG.info("AAIResource.ctor initialized.");
- }
+ }
- private static final Logger LOG = LoggerFactory.getLogger(AAIService.class);
- private final MetricLogger ml = new MetricLogger();
+ private static final Logger LOG = LoggerFactory.getLogger(AAIService.class);
+ private final MetricLogger ml = new MetricLogger();
- private SSLContext CTX;
+ private SSLContext CTX;
- private int connection_timeout = 300000;
+ private int connection_timeout = 300000;
- private int read_timeout = 300000;
+ private int read_timeout = 300000;
- /**
- * Returns an String that contains JSON data returned from the AAI Server.
- * <p>
- * This method always returns immediately, whether or not the
- * data exists.
- *
- * @param request an instance of AAIRequiest representing
- * the request made by DirectedGraph node.
- * @return the JSON based representation of data instance requested.
- * @see String
- */
- @Override
- public String get(AAIRequest request) throws AAIServiceException {
- String response = null;
- InputStream inputStream = null;
- HttpURLConnection con = null;
- URL requestUrl = null;
+ /**
+ * Returns an String that contains JSON data returned from the AAI Server.
+ * <p>
+ * This method always returns immediately, whether or not the
+ * data exists.
+ *
+ * @param request an instance of AAIRequiest representing
+ * the request made by DirectedGraph node.
+ * @return the JSON based representation of data instance requested.
+ * @see String
+ */
+ @Override
+ public String get(AAIRequest request) throws AAIServiceException {
+ String response = null;
+ InputStream inputStream = null;
+ HttpURLConnection con = null;
+ URL requestUrl = null;
- StringBuilder errorStringBuilder = new StringBuilder();
+ StringBuilder errorStringBuilder = new StringBuilder();
- try {
+ try {
if(request.getRequestObject() != null) {
- requestUrl = request.getRequestUrl(HttpMethod.POST, null);
- requestUrl = appendDepth(requestUrl, request);
- con = getConfiguredConnection(requestUrl, HttpMethod.POST);
- String json_text = request.toJSONString();
- LOGwriteDateTrace("data", json_text);
- logMetricRequest("POST "+requestUrl.getPath(), json_text, requestUrl.getPath());
- OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
- osw.write(json_text);
- osw.flush();
+ requestUrl = request.getRequestUrl(HttpMethod.POST, null);
+ requestUrl = appendDepth(requestUrl, request);
+ con = getConfiguredConnection(requestUrl, HttpMethod.POST);
+ String json_text = request.toJSONString();
+ LOGwriteDateTrace("data", json_text);
+ logMetricRequest("POST "+requestUrl.getPath(), json_text, requestUrl.getPath());
+ OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
+ osw.write(json_text);
+ osw.flush();
} else {
- requestUrl = request.getRequestUrl(HttpMethod.GET, null);
- requestUrl = appendDepth(requestUrl, request);
- con = getConfiguredConnection(requestUrl, HttpMethod.GET);
- logMetricRequest("GET "+requestUrl.getPath(), "", requestUrl.getPath());
+ requestUrl = request.getRequestUrl(HttpMethod.GET, null);
+ requestUrl = appendDepth(requestUrl, request);
+ con = getConfiguredConnection(requestUrl, HttpMethod.GET);
+ logMetricRequest("GET "+requestUrl.getPath(), "", requestUrl.getPath());
}
// Check for errors
String responseMessage = con.getResponseMessage();
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
- inputStream = con.getInputStream();
+ inputStream = con.getInputStream();
} else {
- inputStream = con.getErrorStream();
+ inputStream = con.getErrorStream();
}
// Process the response
ObjectMapper mapper = AAIService.getObjectMapper();
- if (responseCode == HttpURLConnection.HTTP_OK) {
- StringBuilder stringBuilder = new StringBuilder();
- String line = null;
- while( ( line = reader.readLine() ) != null ) {
- stringBuilder.append( line );
- }
- response = stringBuilder.toString();
- try {
- Object object = mapper.readValue(response, Object.class);
- LOGwriteEndingTrace(HttpURLConnection.HTTP_OK, responseMessage, mapper.writeValueAsString(object));
- } catch(Exception exc) {
- LOGwriteEndingTrace(HttpURLConnection.HTTP_OK, responseMessage, mapper.writeValueAsString(response));
- }
+ if (responseCode == HttpURLConnection.HTTP_OK) {
+ StringBuilder stringBuilder = new StringBuilder();
+ String line = null;
+ while( ( line = reader.readLine() ) != null ) {
+ stringBuilder.append( line );
+ }
+ response = stringBuilder.toString();
+ try {
+ Object object = mapper.readValue(response, Object.class);
+ LOGwriteEndingTrace(HttpURLConnection.HTTP_OK, responseMessage, mapper.writeValueAsString(object));
+ } catch(Exception exc) {
+ LOGwriteEndingTrace(HttpURLConnection.HTTP_OK, responseMessage, mapper.writeValueAsString(response));
+ }
} else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
- LOGwriteEndingTrace(responseCode, responseMessage, "Entry does not exist.");
- ErrorResponse errorresponse = null;
- try {
- errorresponse = mapper.readValue(reader, ErrorResponse.class);
- } catch(Exception exc) {
- errorresponse = new ErrorResponse();
- RequestError requestError = new RequestError();
- ServiceException serviceException = new ServiceException();
- serviceException.setText("Entry does not exist.");
- requestError.setServiceException(serviceException);
- errorresponse.setRequestError(requestError );
- }
- throw new AAIServiceException(responseCode, errorresponse);
+ LOGwriteEndingTrace(responseCode, responseMessage, "Entry does not exist.");
+ ErrorResponse errorresponse = null;
+ try {
+ errorresponse = mapper.readValue(reader, ErrorResponse.class);
+ } catch(Exception exc) {
+ errorresponse = new ErrorResponse();
+ RequestError requestError = new RequestError();
+ ServiceException serviceException = new ServiceException();
+ serviceException.setText("Entry does not exist.");
+ requestError.setServiceException(serviceException);
+ errorresponse.setRequestError(requestError );
+ }
+ throw new AAIServiceException(responseCode, errorresponse);
} else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
- StringBuilder stringBuilder = new StringBuilder();
- String line = null;
- while( ( line = reader.readLine() ) != null ) {
- stringBuilder.append( line );
- }
- LOGwriteEndingTrace(responseCode, responseMessage, stringBuilder.toString());
- ServiceException serviceException = new ServiceException();
- serviceException.setMessageId("HTTP_UNAUTHORIZED");
- serviceException.setText(stringBuilder.toString());
- RequestError requestError = new RequestError();
- requestError.setServiceException(serviceException);
- ErrorResponse errorresponse = new ErrorResponse();
- errorresponse.setRequestError(requestError);
- throw new AAIServiceException(responseCode, errorresponse);
+ StringBuilder stringBuilder = new StringBuilder();
+ String line = null;
+ while( ( line = reader.readLine() ) != null ) {
+ stringBuilder.append( line );
+ }
+ LOGwriteEndingTrace(responseCode, responseMessage, stringBuilder.toString());
+ ServiceException serviceException = new ServiceException();
+ serviceException.setMessageId("HTTP_UNAUTHORIZED");
+ serviceException.setText(stringBuilder.toString());
+ RequestError requestError = new RequestError();
+ requestError.setServiceException(serviceException);
+ ErrorResponse errorresponse = new ErrorResponse();
+ errorresponse.setRequestError(requestError);
+ throw new AAIServiceException(responseCode, errorresponse);
} else {
-// StringBuilder errorStringBuilder = new StringBuilder();
- String line = null;
- while( ( line = reader.readLine() ) != null ) {
- errorStringBuilder.append("\n").append( line );
- }
-
- ErrorResponse errorresponse = mapper.readValue(errorStringBuilder.toString(), ErrorResponse.class);
-// ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
- LOGwriteEndingTrace(responseCode, responseMessage, mapper.writeValueAsString(errorresponse));
- throw new AAIServiceException(responseCode, errorresponse);
+// StringBuilder errorStringBuilder = new StringBuilder();
+ String line = null;
+ while( ( line = reader.readLine() ) != null ) {
+ errorStringBuilder.append("\n").append( line );
+ }
+
+ ErrorResponse errorresponse = mapper.readValue(errorStringBuilder.toString(), ErrorResponse.class);
+// ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
+ LOGwriteEndingTrace(responseCode, responseMessage, mapper.writeValueAsString(errorresponse));
+ throw new AAIServiceException(responseCode, errorresponse);
}
- } catch(AAIServiceException aaiexc) {
- throw aaiexc;
- } catch (Exception exc) {
- LOG.warn(errorStringBuilder.toString(), exc);
- throw new AAIServiceException(exc);
- } finally {
- if(inputStream != null){
- try {
- inputStream.close();
- } catch(Exception exc) {
-
- }
- }
- }
- return response;
- }
-
- /**
- * Returns an String that contains JSON data returned from the AAI Server.
- * <p>
- * This method always returns immediately, whether or not the
- * data exists.
- *
- * @param request an instance of AAIRequiest representing
- * the request made by DirectedGraph node.
- * @return the JSON based representation of data instance requested.
- * @see String
- */
- @Override
- public String post(AAIRequest request) throws AAIServiceException {
- InputStream inputStream = null;
-
- try {
- String resourceVersion = null;
- AAIDatum instance = request.getRequestObject();
-
- try {
- Method getResourceVersionMethod = instance.getClass().getMethod("getResourceVersion");
- if(getResourceVersionMethod != null){
- try {
- Object object = getResourceVersionMethod.invoke(instance);
- if(object != null)
- resourceVersion = object.toString();
- } catch (InvocationTargetException x) {
- Throwable cause = x.getCause();
- }
- }
- } catch(Exception exc) {
- LOG.error("", exc);
- }
-
- URL requestUrl = null;
- HttpURLConnection con = getConfiguredConnection(requestUrl = request.getRequestUrl(HttpMethod.PUT, resourceVersion), HttpMethod.PUT);
- ObjectMapper mapper = AAIService.getObjectMapper();
- String json_text = request.toJSONString();
-
- LOGwriteDateTrace("data", json_text);
- logMetricRequest("PUT "+requestUrl.getPath(), json_text, requestUrl.getPath());
-
- OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
+ } catch(AAIServiceException aaiexc) {
+ throw aaiexc;
+ } catch (Exception exc) {
+ LOG.warn(errorStringBuilder.toString(), exc);
+ throw new AAIServiceException(exc);
+ } finally {
+ if(inputStream != null){
+ try {
+ inputStream.close();
+ } catch(Exception exc) {
+
+ }
+ }
+ }
+ return response;
+ }
+
+ /**
+ * Returns an String that contains JSON data returned from the AAI Server.
+ * <p>
+ * This method always returns immediately, whether or not the
+ * data exists.
+ *
+ * @param request an instance of AAIRequiest representing
+ * the request made by DirectedGraph node.
+ * @return the JSON based representation of data instance requested.
+ * @see String
+ */
+ @Override
+ public String post(AAIRequest request) throws AAIServiceException {
+ InputStream inputStream = null;
+
+ try {
+ String resourceVersion = null;
+ AAIDatum instance = request.getRequestObject();
+
+ try {
+ Method getResourceVersionMethod = instance.getClass().getMethod("getResourceVersion");
+ if(getResourceVersionMethod != null){
+ try {
+ Object object = getResourceVersionMethod.invoke(instance);
+ if(object != null)
+ resourceVersion = object.toString();
+ } catch (InvocationTargetException x) {
+ Throwable cause = x.getCause();
+ }
+ }
+ } catch(Exception exc) {
+ LOG.error("", exc);
+ }
+
+ URL requestUrl = null;
+ HttpURLConnection con = getConfiguredConnection(requestUrl = request.getRequestUrl(HttpMethod.PUT, resourceVersion), HttpMethod.PUT);
+ ObjectMapper mapper = AAIService.getObjectMapper();
+ String json_text = request.toJSONString();
+
+ LOGwriteDateTrace("data", json_text);
+ logMetricRequest("PUT "+requestUrl.getPath(), json_text, requestUrl.getPath());
+
+ OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
osw.write(json_text);
osw.flush();
String responseMessage = con.getResponseMessage();
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
- inputStream = con.getInputStream();
+ inputStream = con.getInputStream();
} else {
- inputStream = con.getErrorStream();
+ inputStream = con.getErrorStream();
}
LOG.debug("HttpURLConnection result:" + responseCode + " : " + responseMessage);
reader = new BufferedReader( new InputStreamReader( inputStream ) );
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
- if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
- StringBuilder stringBuilder = new StringBuilder();
+ if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
+ StringBuilder stringBuilder = new StringBuilder();
- while( ( line = reader.readLine() ) != null ) {
- stringBuilder.append( line );
- }
- LOGwriteEndingTrace(responseCode, responseMessage, (stringBuilder != null) ? stringBuilder.toString() : "{no-data}");
- return stringBuilder.toString();
+ while( ( line = reader.readLine() ) != null ) {
+ stringBuilder.append( line );
+ }
+ LOGwriteEndingTrace(responseCode, responseMessage, (stringBuilder.length() > 0) ? stringBuilder.toString() : "{no-data}");
+ return stringBuilder.toString();
} else {
- ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
- LOGwriteEndingTrace(responseCode, responseMessage, mapper.writeValueAsString(errorresponse));
+ ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
+ LOGwriteEndingTrace(responseCode, responseMessage, mapper.writeValueAsString(errorresponse));
- throw new AAIServiceException(responseCode, errorresponse);
+ throw new AAIServiceException(responseCode, errorresponse);
}
- } catch(AAIServiceException aaiexc) {
- throw aaiexc;
- } catch (Exception exc) {
- LOG.warn("AAIRequestExecutor.post", exc);
- throw new AAIServiceException(exc);
- } finally {
- try {
- if(inputStream != null)
- inputStream.close();
- } catch (Exception exc) {
-
- }
- }
- }
-
- /**
- * Returns Boolean that contains completion state of the command executed.
- * <p>
- * This method always returns immediately, whether or not the
- * data exists.
- *
- * @param request an instance of AAIRequiest representing
- * @param resourceVersion a resource version of the data instacne to be deleted.
- * the request made by DirectedGraph node.
- * @return completion state of the command.
- * @see String
- */
- @Override
- public Boolean delete(AAIRequest request, String resourceVersion) throws AAIServiceException {
- Boolean response = null;
- InputStream inputStream = null;
-
- if(resourceVersion == null) {
- throw new AAIServiceException("resource-version is required for DELETE request");
- }
-
- try {
- URL requestUrl = null;
+ } catch(AAIServiceException aaiexc) {
+ throw aaiexc;
+ } catch (Exception exc) {
+ LOG.warn("AAIRequestExecutor.post", exc);
+ throw new AAIServiceException(exc);
+ } finally {
+ try {
+ if(inputStream != null)
+ inputStream.close();
+ } catch (Exception exc) {
+
+ }
+ }
+ }
+
+ /**
+ * Returns Boolean that contains completion state of the command executed.
+ * <p>
+ * This method always returns immediately, whether or not the
+ * data exists.
+ *
+ * @param request an instance of AAIRequiest representing
+ * @param resourceVersion a resource version of the data instacne to be deleted.
+ * the request made by DirectedGraph node.
+ * @return completion state of the command.
+ * @see String
+ */
+ @Override
+ public Boolean delete(AAIRequest request, String resourceVersion) throws AAIServiceException {
+ Boolean response = null;
+ InputStream inputStream = null;
+
+ if(resourceVersion == null) {
+ throw new AAIServiceException("resource-version is required for DELETE request");
+ }
+
+ try {
+ URL requestUrl = null;
HttpURLConnection conn = getConfiguredConnection(requestUrl = request.getRequestUrl(HttpMethod.DELETE, resourceVersion), HttpMethod.DELETE);
logMetricRequest("DELETE "+requestUrl.getPath(), "", requestUrl.getPath());
conn.setDoOutput(true);
String responseMessage = conn.getResponseMessage();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
- inputStream = conn.getInputStream();
+ inputStream = conn.getInputStream();
} else {
- inputStream = conn.getErrorStream();
+ inputStream = conn.getErrorStream();
}
// Process the response
ObjectMapper mapper = AAIService.getObjectMapper();
- if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
- StringBuilder stringBuilder = new StringBuilder();
-
- while( ( line = reader.readLine() ) != null ) {
- stringBuilder.append( line );
- }
- LOGwriteEndingTrace(responseCode, responseMessage, stringBuilder.toString());
- response = true;
- } else if(responseCode == HttpURLConnection.HTTP_NOT_FOUND ) {
- LOGwriteEndingTrace(responseCode, responseMessage, "Entry does not exist.");
- response = false;
+ if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
+ StringBuilder stringBuilder = new StringBuilder();
+
+ while( ( line = reader.readLine() ) != null ) {
+ stringBuilder.append( line );
+ }
+ LOGwriteEndingTrace(responseCode, responseMessage, stringBuilder.toString());
+ response = true;
+ } else if(responseCode == HttpURLConnection.HTTP_NOT_FOUND ) {
+ LOGwriteEndingTrace(responseCode, responseMessage, "Entry does not exist.");
+ response = false;
} else {
- ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
- LOGwriteEndingTrace(responseCode, responseMessage, mapper.writeValueAsString(errorresponse));
- throw new AAIServiceException(responseCode, errorresponse);
+ ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
+ LOGwriteEndingTrace(responseCode, responseMessage, mapper.writeValueAsString(errorresponse));
+ throw new AAIServiceException(responseCode, errorresponse);
}
- } catch(AAIServiceException aaiexc) {
- throw aaiexc;
- } catch (Exception exc) {
- LOG.warn("delete", exc);
- throw new AAIServiceException(exc);
- } finally {
- if(inputStream != null){
- try {
- inputStream.close();
- } catch(Exception exc) {
-
- }
- }
- }
- return response;
- }
-
- /**
- * Returns an String that contains JSON data returned from the AAI Server.
- * <p>
- * This method always returns immediately, whether or not the
- * data exists.
- *
- * @param request an instance of AAIRequiest representing
- * the request made by DirectedGraph node.
- * @param clas an definition of the class for which data will be returned
- * @return the instance of the class with data.
- * @see String
- */
- @Override
- public Object query(AAIRequest request, Class clas) throws AAIServiceException {
- Object response = null;
- InputStream inputStream = null;
- HttpURLConnection con = null;
- URL requestUrl = null;
-
- try {
+ } catch(AAIServiceException aaiexc) {
+ throw aaiexc;
+ } catch (Exception exc) {
+ LOG.warn("delete", exc);
+ throw new AAIServiceException(exc);
+ } finally {
+ if(inputStream != null){
+ try {
+ inputStream.close();
+ } catch(Exception exc) {
+
+ }
+ }
+ }
+ return response;
+ }
+
+ /**
+ * Returns an String that contains JSON data returned from the AAI Server.
+ * <p>
+ * This method always returns immediately, whether or not the
+ * data exists.
+ *
+ * @param request an instance of AAIRequiest representing
+ * the request made by DirectedGraph node.
+ * @param clas an definition of the class for which data will be returned
+ * @return the instance of the class with data.
+ * @see String
+ */
+ @Override
+ public Object query(AAIRequest request, Class clas) throws AAIServiceException {
+ Object response = null;
+ InputStream inputStream = null;
+ HttpURLConnection con = null;
+ URL requestUrl = null;
+
+ try {
con = getConfiguredConnection(requestUrl = request.getRequestQueryUrl(HttpMethod.GET), HttpMethod.GET);
logMetricRequest("GET "+requestUrl.getPath(), "", requestUrl.getPath());
String responseMessage = con.getResponseMessage();
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
- inputStream = con.getInputStream();
+ inputStream = con.getInputStream();
} else {
- inputStream = con.getErrorStream();
+ inputStream = con.getErrorStream();
}
logMetricResponse(responseCode, responseMessage);
ObjectMapper mapper = AAIService.getObjectMapper();
- if (responseCode == HttpURLConnection.HTTP_OK) {
- // Process the response
- BufferedReader reader = new BufferedReader( new InputStreamReader( inputStream ) );
- response = mapper.readValue(reader, clas);
- LOGwriteEndingTrace(HttpURLConnection.HTTP_OK, "SUCCESS", mapper.writeValueAsString(response));
+ if (responseCode == HttpURLConnection.HTTP_OK) {
+ // Process the response
+ BufferedReader reader = new BufferedReader( new InputStreamReader( inputStream ) );
+ response = mapper.readValue(reader, clas);
+ LOGwriteEndingTrace(HttpURLConnection.HTTP_OK, "SUCCESS", mapper.writeValueAsString(response));
} else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
- LOGwriteEndingTrace(responseCode, "HTTP_NOT_FOUND", "Entry does not exist.");
- return response;
- } else {
- BufferedReader reader = new BufferedReader( new InputStreamReader( inputStream ) );
- ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
- LOGwriteEndingTrace(responseCode, "FAILURE", mapper.writeValueAsString(errorresponse));
- throw new AAIServiceException(responseCode, errorresponse);
+ LOGwriteEndingTrace(responseCode, "HTTP_NOT_FOUND", "Entry does not exist.");
+ return response;
+ } else {
+ BufferedReader reader = new BufferedReader( new InputStreamReader( inputStream ) );
+ ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
+ LOGwriteEndingTrace(responseCode, "FAILURE", mapper.writeValueAsString(errorresponse));
+ throw new AAIServiceException(responseCode, errorresponse);
}
- } catch(AAIServiceException aaiexc) {
- throw aaiexc;
- } catch (Exception exc) {
- LOG.warn("GET", exc);
- throw new AAIServiceException(exc);
- } finally {
- if(inputStream != null){
- try {
- inputStream.close();
- } catch(Exception exc) {
-
- }
- }
- con = null;
- }
- return response;
- }
-
- @Override
- public Boolean patch(AAIRequest request, String resourceVersion) throws AAIServiceException {
- InputStream inputStream = null;
-
- try {
- AAIDatum instance = request.getRequestObject();
- if(instance instanceof ResourceVersion) {
- resourceVersion = ((ResourceVersion)instance).getResourceVersion();
- }
-
- URL requestUrl = null;
- HttpURLConnection con = getConfiguredConnection(requestUrl = request.getRequestUrl("PATCH", resourceVersion), "PATCH");
- ObjectMapper mapper = AAIService.getObjectMapper();
- String json_text = request.toJSONString();
-
- LOGwriteDateTrace("data", json_text);
- logMetricRequest("PATCH "+requestUrl.getPath(), json_text, requestUrl.getPath());
-
- OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
+ } catch(AAIServiceException aaiexc) {
+ throw aaiexc;
+ } catch (Exception exc) {
+ LOG.warn("GET", exc);
+ throw new AAIServiceException(exc);
+ } finally {
+ if(inputStream != null){
+ try {
+ inputStream.close();
+ } catch(Exception exc) {
+
+ }
+ }
+ con = null;
+ }
+ return response;
+ }
+
+ @Override
+ public Boolean patch(AAIRequest request, String resourceVersion) throws AAIServiceException {
+ InputStream inputStream = null;
+
+ try {
+ AAIDatum instance = request.getRequestObject();
+ if(instance instanceof ResourceVersion) {
+ resourceVersion = ((ResourceVersion)instance).getResourceVersion();
+ }
+
+ URL requestUrl = null;
+ HttpURLConnection con = getConfiguredConnection(requestUrl = request.getRequestUrl("PATCH", resourceVersion), "PATCH");
+ ObjectMapper mapper = AAIService.getObjectMapper();
+ String json_text = request.toJSONString();
+
+ LOGwriteDateTrace("data", json_text);
+ logMetricRequest("PATCH "+requestUrl.getPath(), json_text, requestUrl.getPath());
+
+ OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
osw.write(json_text);
osw.flush();
String responseMessage = con.getResponseMessage();
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
- inputStream = con.getInputStream();
+ inputStream = con.getInputStream();
} else {
- inputStream = con.getErrorStream();
+ inputStream = con.getErrorStream();
}
LOG.info("HttpURLConnection result: " + responseCode + " : " + responseMessage);
reader = new BufferedReader( new InputStreamReader( inputStream ) );
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
- if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
- StringBuilder stringBuilder = new StringBuilder();
+ if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
+ StringBuilder stringBuilder = new StringBuilder();
- while( ( line = reader.readLine() ) != null ) {
- stringBuilder.append( line );
- }
- LOGwriteEndingTrace(responseCode, responseMessage, (stringBuilder != null) ? stringBuilder.toString() : "{no-data}");
- return true;
+ while( ( line = reader.readLine() ) != null ) {
+ stringBuilder.append( line );
+ }
+ LOGwriteEndingTrace(responseCode, responseMessage, (stringBuilder != null) ? stringBuilder.toString() : "{no-data}");
+ return true;
} else {
- StringBuilder stringBuilder = new StringBuilder();
+ StringBuilder stringBuilder = new StringBuilder();
+
+ while( ( line = reader.readLine() ) != null ) {
+ stringBuilder.append("\n").append( line );
+ }
+ LOG.info(stringBuilder.toString());
+
- while( ( line = reader.readLine() ) != null ) {
- stringBuilder.append("\n").append( line );
- }
- LOG.info(stringBuilder.toString());
+ ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
+ LOGwriteEndingTrace(responseCode, responseMessage, mapper.writeValueAsString(errorresponse));
+ throw new AAIServiceException(responseCode, errorresponse);
+ }
+ } catch(AAIServiceException aaiexc) {
+ throw aaiexc;
+ } catch (Exception exc) {
+ LOG.warn("AAIRequestExecutor.patch", exc);
+ throw new AAIServiceException(exc);
+ } finally {
+ try {
+ if(inputStream != null)
+ inputStream.close();
+ } catch (Exception exc) {
+
+ }
+ }
+ }
+
+ /**
+ *
+ * @param httpReqUrl
+ * @param method
+ * @return
+ * @throws Exception
+ */
+ protected HttpURLConnection getConfiguredConnection(URL httpReqUrl, String method) throws Exception {
+ HttpURLConnection con = (HttpURLConnection) httpReqUrl.openConnection();
+
+ // Set up the connection properties
+ con.setRequestProperty("Connection", "close");
+ con.setDoInput(true);
+ con.setDoOutput(true);
+ con.setUseCaches(false);
+ con.setConnectTimeout(connection_timeout);
+ con.setReadTimeout(read_timeout);
+ con.setRequestMethod(method);
+ con.setRequestProperty("Accept", "application/json");
+ con.setRequestProperty("Transfer-Encoding","chunked");
+ con.setRequestProperty("Content-Type",
+ "PATCH".equalsIgnoreCase(method) ? "application/merge-patch+json" : "application/json");
+ con.setRequestProperty("X-FromAppId", applicationId);
+ con.setRequestProperty("X-TransactionId", TransactionIdTracker.getNextTransactionId());
+ String mlId = ml.getRequestID();
+ if (mlId != null && !mlId.isEmpty()) {
+ LOG.debug(String.format("MetricLogger requestId = %s", mlId));
+ con.setRequestProperty(MetricLogger.REQUEST_ID, mlId);
+ } else {
+ LOG.debug("MetricLogger requestId is null");
+ }
- ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
- LOGwriteEndingTrace(responseCode, responseMessage, mapper.writeValueAsString(errorresponse));
+ if (userName != null && !userName.isEmpty() && userPassword != null && !userPassword.isEmpty()) {
+ String basicAuth = "Basic " + new String(Base64.encodeBase64((userName + ":" + userPassword).getBytes()));
+ con.setRequestProperty("Authorization", basicAuth);
+ }
- throw new AAIServiceException(responseCode, errorresponse);
+ if (con instanceof HttpsURLConnection && CTX != null) {
+ SSLSocketFactory sockFact = CTX.getSocketFactory();
+ HttpsURLConnection.class.cast(con).setSSLSocketFactory(sockFact);
+ }
+ return con;
+ }
+
+ private URL appendDepth(URL requestUrl, AAIRequest request) throws MalformedURLException {
+
+ String depth = request.requestProperties.getProperty("depth", "1");
+ String path = requestUrl.toString();
+ if(path.contains("?depth=") || path.contains("&depth=")) {
+ return requestUrl;
+ } else {
+ if(path.contains("?")) {
+ path = String.format("%s&depth=%s", path, depth);
+ } else {
+ path = String.format("%s?depth=%s", path, depth);
}
- } catch(AAIServiceException aaiexc) {
- throw aaiexc;
- } catch (Exception exc) {
- LOG.warn("AAIRequestExecutor.patch", exc);
- throw new AAIServiceException(exc);
- } finally {
- try {
- if(inputStream != null)
- inputStream.close();
- } catch (Exception exc) {
-
- }
- }
- }
-
- /**
- *
- * @param httpReqUrl
- * @param method
- * @return
- * @throws Exception
- */
- protected HttpURLConnection getConfiguredConnection(URL httpReqUrl, String method) throws Exception {
- HttpURLConnection con = (HttpURLConnection) httpReqUrl.openConnection();
-
- // Set up the connection properties
- con.setRequestProperty("Connection", "close");
- con.setDoInput(true);
- con.setDoOutput(true);
- con.setUseCaches(false);
- con.setConnectTimeout(connection_timeout);
- con.setReadTimeout(read_timeout);
- con.setRequestMethod(method);
- con.setRequestProperty("Accept", "application/json");
- con.setRequestProperty("Transfer-Encoding","chunked");
- con.setRequestProperty("Content-Type",
- "PATCH".equalsIgnoreCase(method) ? "application/merge-patch+json" : "application/json");
- con.setRequestProperty("X-FromAppId", applicationId);
- con.setRequestProperty("X-TransactionId", TransactionIdTracker.getNextTransactionId());
- String mlId = ml.getRequestID();
- if (mlId != null && !mlId.isEmpty()) {
- LOG.debug(String.format("MetricLogger requestId = %s", mlId));
- con.setRequestProperty(MetricLogger.REQUEST_ID, mlId);
- } else {
- LOG.debug("MetricLogger requestId is null");
- }
-
- if (userName != null && !userName.isEmpty() && userPassword != null && !userPassword.isEmpty()) {
- String basicAuth = "Basic " + new String(Base64.encodeBase64((userName + ":" + userPassword).getBytes()));
- con.setRequestProperty("Authorization", basicAuth);
- }
-
- if (con instanceof HttpsURLConnection && CTX != null) {
- SSLSocketFactory sockFact = CTX.getSocketFactory();
- HttpsURLConnection.class.cast(con).setSSLSocketFactory(sockFact);
- }
- return con;
- }
-
- private URL appendDepth(URL requestUrl, AAIRequest request) throws MalformedURLException {
-
- String depth = request.requestProperties.getProperty("depth", "1");
- String path = requestUrl.toString();
- if(path.contains("?depth=") || path.contains("&depth=")) {
- return requestUrl;
- } else {
- if(path.contains("?")) {
- path = String.format("%s&depth=%s", path, depth);
- } else {
- path = String.format("%s?depth=%s", path, depth);
- }
- return new URL(path);
- }
- }
-
- public void logMetricRequest(String targetServiceName, String msg, String path){
- String svcInstanceId = "";
- String svcName = null;
- String partnerName = null;
- String targetEntity = "A&AI";
- String targetVirtualEntity = null;
-
- targetServiceName = "";
-
- ml.logRequest(svcInstanceId, svcName, partnerName, targetEntity, targetServiceName, targetVirtualEntity, msg);
- }
-
- public void logMetricResponse(int responseCode, String responseDescription){
- ml.logResponse(responseCode < 400 ? "SUCCESS" : "FAILURE", Integer.toString(responseCode), responseDescription);
- }
-
- protected void LOGwriteFirstTrace(String method, String url) {
- String time = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(System.currentTimeMillis());
- LOG.info("A&AI transaction :");
- LOG.info("Request Time : " + time + ", Method : " + method);
- LOG.info("Request URL : "+ url);
- }
-
- protected void LOGwriteDateTrace(String name, String data) {
- LOG.info("Input - " + name + " : " + data);
- }
-
- protected void LOGwriteEndingTrace(int response_code, String comment, String data) {
- LOG.info("Response code : " + response_code +", " + comment);
- LOG.info(String.format("Response data : %s", data));
- }
+ return new URL(path);
+ }
+ }
+
+ public void logMetricRequest(String targetServiceName, String msg, String path){
+ String svcInstanceId = "";
+ String svcName = null;
+ String partnerName = null;
+ String targetEntity = "A&AI";
+ String targetVirtualEntity = null;
+
+ targetServiceName = "";
+
+ ml.logRequest(svcInstanceId, svcName, partnerName, targetEntity, targetServiceName, targetVirtualEntity, msg);
+ }
+
+ public void logMetricResponse(int responseCode, String responseDescription){
+ ml.logResponse(responseCode < 400 ? "SUCCESS" : "FAILURE", Integer.toString(responseCode), responseDescription);
+ }
+
+ protected void LOGwriteFirstTrace(String method, String url) {
+ String time = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(System.currentTimeMillis());
+ LOG.info("A&AI transaction :");
+ LOG.info("Request Time : " + time + ", Method : " + method);
+ LOG.info("Request URL : "+ url);
+ }
+
+ protected void LOGwriteDateTrace(String name, String data) {
+ LOG.info("Input - " + name + " : " + data);
+ }
+
+ protected void LOGwriteEndingTrace(int response_code, String comment, String data) {
+ LOG.info("Response code : " + response_code +", " + comment);
+ LOG.info(String.format("Response data : %s", data));
+ }
}