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.aai;
24 import java.io.BufferedReader;
25 import java.io.ByteArrayInputStream;
27 import java.io.FileInputStream;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.io.OutputStreamWriter;
31 import java.lang.reflect.Field;
32 import java.lang.reflect.InvocationTargetException;
33 import java.lang.reflect.Method;
34 import java.lang.reflect.Modifier;
35 import java.net.HttpURLConnection;
36 import java.net.MalformedURLException;
38 import java.nio.charset.StandardCharsets;
39 import java.security.KeyManagementException;
40 import java.security.KeyStore;
41 import java.security.NoSuchAlgorithmException;
42 import java.text.SimpleDateFormat;
43 import java.util.Properties;
45 import javax.net.ssl.HostnameVerifier;
46 import javax.net.ssl.HttpsURLConnection;
47 import javax.net.ssl.KeyManagerFactory;
48 import javax.net.ssl.SSLContext;
49 import javax.net.ssl.SSLSession;
50 import javax.net.ssl.SSLSocketFactory;
52 import org.apache.commons.codec.binary.Base64;
53 import org.onap.ccsdk.sli.adaptors.aai.AAIService.TransactionIdTracker;
54 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
55 import org.onap.ccsdk.sli.adaptors.aai.data.ErrorResponse;
56 import org.onap.ccsdk.sli.adaptors.aai.data.RequestError;
57 import org.onap.ccsdk.sli.adaptors.aai.data.ResourceVersion;
58 import org.onap.ccsdk.sli.adaptors.aai.data.ServiceException;
59 import org.onap.ccsdk.sli.core.sli.MetricLogger;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
63 import com.fasterxml.jackson.databind.DeserializationFeature;
64 import com.fasterxml.jackson.databind.ObjectMapper;
65 import org.apache.http.impl.EnglishReasonPhraseCatalog;
68 * The AAIClientRESTExecutor class provides CRUD API for AAI Client service.
69 * @author richtabedzki
71 public class AAIClientRESTExecutor implements AAIExecutorInterface {
73 private final String truststorePath;
74 private final String truststorePassword;
75 private final String keystorePath;
76 private final String keystorePassword;
77 private final Boolean ignoreCertificateHostError;
78 // authentication credentials
79 private String userName;
80 private String userPassword;
81 private final String applicationId;
85 * @param props - properties to initialize an instance.
87 public AAIClientRESTExecutor(Properties props) {
90 userName = props.getProperty(AAIService.CLIENT_NAME);
91 userPassword = props.getProperty(AAIService.CLIENT_PWWD);
93 if(userName == null || userName.isEmpty()){
94 LOG.debug("Basic user name is not set");
96 if(userPassword == null || userPassword.isEmpty()) {
97 LOG.debug("Basic password is not set");
100 truststorePath = props.getProperty(AAIService.TRUSTSTORE_PATH);
101 truststorePassword = props.getProperty(AAIService.TRUSTSTORE_PSSWD);
102 keystorePath = props.getProperty(AAIService.KEYSTORE_PATH);
103 keystorePassword = props.getProperty(AAIService.KEYSTORE_PSSWD);
105 String tmpApplicationId =props.getProperty(AAIService.APPLICATION_ID);
106 if(tmpApplicationId == null || tmpApplicationId.isEmpty()) {
107 tmpApplicationId = "SDNC";
109 applicationId = tmpApplicationId;
111 String iche = props.getProperty(AAIService.CERTIFICATE_HOST_ERROR);
112 boolean host_error = false;
113 if(iche != null && !iche.isEmpty()) {
114 host_error = Boolean.valueOf(iche);
117 ignoreCertificateHostError = host_error;
119 HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
120 public boolean verify(String string,SSLSession ssls) {
121 return ignoreCertificateHostError;
125 if(truststorePath != null && truststorePassword != null && (new File(truststorePath)).exists()) {
126 System.setProperty("javax.net.ssl.trustStore", truststorePath);
127 System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
130 if(keystorePath != null && keystorePassword != null && (new File(keystorePath)).exists())
132 //both jersey and HttpURLConnection can use this
133 SSLContext ctx = null;
135 ctx = SSLContext.getInstance("TLS");
137 KeyManagerFactory kmf = null;
138 try (FileInputStream fin = new FileInputStream(keystorePath)){
139 String storeType = "PKCS12";
140 String def = KeyStore.getDefaultType();
141 kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
143 String extension = keystorePath.substring(keystorePath.lastIndexOf(".") + 1);
145 if(extension != null && !extension.isEmpty() && extension.equalsIgnoreCase("JKS")) {
148 KeyStore ks = KeyStore.getInstance(storeType);
150 char[] pwd = keystorePassword.toCharArray();
153 } catch (Exception ex) {
154 LOG.error("AAIResource", ex);
157 ctx.init(kmf.getKeyManagers(), null, null);
160 LOG.debug("SSLContext created");
162 } catch (KeyManagementException | NoSuchAlgorithmException exc) {
163 LOG.error("AAIResource", exc);
168 Field methodsField = HttpURLConnection.class.getDeclaredField("methods");
169 methodsField.setAccessible(true);
170 // get the methods field modifiers
171 Field modifiersField = Field.class.getDeclaredField("modifiers");
172 // bypass the "private" modifier
173 modifiersField.setAccessible(true);
175 // remove the "final" modifier
176 modifiersField.setInt(methodsField, methodsField.getModifiers() & ~Modifier.FINAL);
178 /* valid HTTP methods */
180 "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH"
182 // set the new methods - including patch
183 methodsField.set(null, methods);
185 } catch (SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {
186 LOG.warn("Adding PATCH method", e);
188 LOG.info("AAIResource.ctor initialized.");
192 private static final Logger LOG = LoggerFactory.getLogger(AAIService.class);
193 private static final String NOT_PROVIDED = "NOT PROVIDED";
194 private final MetricLogger ml = new MetricLogger();
196 private SSLContext CTX;
199 private int connection_timeout = 300000;
201 private int read_timeout = 300000;
204 * Returns an String that contains JSON data returned from the AAI Server.
206 * This method always returns immediately, whether or not the
209 * @param request an instance of AAIRequiest representing
210 * the request made by DirectedGraph node.
211 * @return the JSON based representation of data instance requested.
215 public String get(AAIRequest request) throws AAIServiceException {
216 String response = null;
217 InputStream inputStream = null;
218 HttpURLConnection con = null;
219 URL requestUrl = null;
221 StringBuilder errorStringBuilder = new StringBuilder();
225 if(request.getRequestObject() != null) {
226 requestUrl = request.getRequestUrl(HttpMethod.POST, null);
227 requestUrl = appendDepth(requestUrl, request);
228 con = getConfiguredConnection(requestUrl, HttpMethod.POST);
229 String json_text = request.toJSONString();
230 LOGwriteDateTrace("data", json_text);
231 logMetricRequest("POST "+requestUrl.getPath(), json_text, requestUrl.getPath());
232 OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
233 osw.write(json_text);
236 requestUrl = request.getRequestUrl(HttpMethod.GET, null);
237 requestUrl = appendDepth(requestUrl, request);
238 con = getConfiguredConnection(requestUrl, HttpMethod.GET);
239 logMetricRequest("GET "+requestUrl.getPath(), "", requestUrl.getPath());
243 int responseCode = con.getResponseCode();
244 if (responseCode == HttpURLConnection.HTTP_OK) {
245 inputStream = con.getInputStream();
247 inputStream = con.getErrorStream();
249 String responseMessage = null;
251 responseMessage = con.getResponseMessage();
252 } catch(Exception exc) {
253 responseMessage = EnglishReasonPhraseCatalog.INSTANCE.getReason(responseCode,null);
255 if(responseMessage == null)
256 responseMessage = NOT_PROVIDED;
259 // Process the response
260 LOG.info("HttpURLConnection result: {} : {}", responseCode, responseMessage);
261 logMetricResponse(responseCode, responseMessage);
263 if(inputStream == null) inputStream = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
264 BufferedReader reader = new BufferedReader( new InputStreamReader( inputStream ) );
266 ObjectMapper mapper = AAIService.getObjectMapper();
268 if (responseCode == HttpURLConnection.HTTP_OK) {
269 StringBuilder stringBuilder = new StringBuilder();
271 while( ( line = reader.readLine() ) != null ) {
272 stringBuilder.append( line );
274 response = stringBuilder.toString();
276 Object object = mapper.readValue(response, Object.class);
277 LOGwriteEndingTrace(HttpURLConnection.HTTP_OK, responseMessage, mapper.writeValueAsString(object));
278 } catch(Exception exc) {
279 LOGwriteEndingTrace(HttpURLConnection.HTTP_OK, responseMessage, mapper.writeValueAsString(response));
281 } else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
282 LOGwriteEndingTrace(responseCode, responseMessage, "Entry does not exist.");
283 ErrorResponse errorresponse = null;
285 errorresponse = mapper.readValue(reader, ErrorResponse.class);
286 } catch(Exception exc) {
287 errorresponse = new ErrorResponse();
288 RequestError requestError = new RequestError();
289 ServiceException serviceException = new ServiceException();
290 serviceException.setText("Entry does not exist.");
291 requestError.setServiceException(serviceException);
292 errorresponse.setRequestError(requestError );
294 throw new AAIServiceException(responseCode, errorresponse);
295 } else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
296 StringBuilder stringBuilder = new StringBuilder();
298 while( ( line = reader.readLine() ) != null ) {
299 stringBuilder.append( line );
301 LOGwriteEndingTrace(responseCode, responseMessage, stringBuilder.toString());
302 ServiceException serviceException = new ServiceException();
303 serviceException.setMessageId("HTTP_UNAUTHORIZED");
304 serviceException.setText(stringBuilder.toString());
305 RequestError requestError = new RequestError();
306 requestError.setServiceException(serviceException);
307 ErrorResponse errorresponse = new ErrorResponse();
308 errorresponse.setRequestError(requestError);
309 throw new AAIServiceException(responseCode, errorresponse);
312 while( ( line = reader.readLine() ) != null ) {
313 errorStringBuilder.append("\n").append( line );
316 ErrorResponse errorresponse = mapper.readValue(errorStringBuilder.toString(), ErrorResponse.class);
317 LOGwriteEndingTrace(responseCode, responseMessage, mapper.writeValueAsString(errorresponse));
318 throw new AAIServiceException(responseCode, errorresponse);
321 } catch(AAIServiceException aaiexc) {
323 } catch (Exception exc) {
324 LOG.warn(errorStringBuilder.toString(), exc);
325 throw new AAIServiceException(exc);
327 if(inputStream != null){
330 } catch(Exception exc) {
339 * Returns an String that contains JSON data returned from the AAI Server.
341 * This method always returns immediately, whether or not the
344 * @param request an instance of AAIRequiest representing
345 * the request made by DirectedGraph node.
346 * @return the JSON based representation of data instance requested.
350 public String post(AAIRequest request) throws AAIServiceException {
351 InputStream inputStream = null;
354 String resourceVersion = null;
355 AAIDatum instance = request.getRequestObject();
358 Method getResourceVersionMethod = instance.getClass().getMethod("getResourceVersion");
359 if(getResourceVersionMethod != null){
361 Object object = getResourceVersionMethod.invoke(instance);
363 resourceVersion = object.toString();
364 } catch (InvocationTargetException exc) {
368 } catch(Exception exc) {
372 URL requestUrl = request.getRequestUrl(HttpMethod.PUT, resourceVersion);
373 HttpURLConnection con = getConfiguredConnection(requestUrl, HttpMethod.PUT);
374 ObjectMapper mapper = AAIService.getObjectMapper();
375 String jsonText = request.toJSONString();
377 LOGwriteDateTrace("data", jsonText);
378 logMetricRequest("PUT "+requestUrl.getPath(), jsonText, requestUrl.getPath());
380 OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
385 int responseCode = con.getResponseCode();
386 if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
387 inputStream = con.getInputStream();
389 inputStream = con.getErrorStream();
391 String responseMessage = null;
393 responseMessage = con.getResponseMessage();
394 } catch(Exception exc) {
395 responseMessage = EnglishReasonPhraseCatalog.INSTANCE.getReason(responseCode,null);
397 if(responseMessage == null)
398 responseMessage = NOT_PROVIDED;
401 LOG.info("HttpURLConnection result: {} : {}", responseCode, responseMessage);
402 logMetricResponse(responseCode, responseMessage);
404 // Process the response
405 BufferedReader reader;
407 reader = new BufferedReader( new InputStreamReader( inputStream ) );
408 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
410 if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
411 StringBuilder stringBuilder = new StringBuilder();
413 while( ( line = reader.readLine() ) != null ) {
414 stringBuilder.append( line );
416 LOGwriteEndingTrace(responseCode, responseMessage, (stringBuilder.length() > 0) ? stringBuilder.toString() : "{no-data}");
417 return stringBuilder.toString();
419 ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
420 LOGwriteEndingTrace(responseCode, responseMessage, mapper.writeValueAsString(errorresponse));
422 throw new AAIServiceException(responseCode, errorresponse);
424 } catch(AAIServiceException aaiexc) {
426 } catch (Exception exc) {
427 LOG.warn("AAIRequestExecutor.post", exc);
428 throw new AAIServiceException(exc);
431 if(inputStream != null)
433 } catch (Exception exc) {
434 LOG.warn("AAIRequestExecutor.post", exc);
440 * Returns Boolean that contains completion state of the command executed.
442 * This method always returns immediately, whether or not the
445 * @param request an instance of AAIRequiest representing
446 * @param resourceVersion a resource version of the data instacne to be deleted.
447 * the request made by DirectedGraph node.
448 * @return completion state of the command.
452 public Boolean delete(AAIRequest request, String resourceVersion) throws AAIServiceException {
453 Boolean response = null;
454 InputStream inputStream = null;
456 if(resourceVersion == null) {
457 throw new AAIServiceException("resource-version is required for DELETE request");
461 URL requestUrl = request.getRequestUrl(HttpMethod.DELETE, resourceVersion);
462 HttpURLConnection conn = getConfiguredConnection(requestUrl, HttpMethod.DELETE);
463 logMetricRequest("DELETE "+requestUrl.getPath(), "", requestUrl.getPath());
464 conn.setDoOutput(true);
467 int responseCode = conn.getResponseCode();
468 if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
469 inputStream = conn.getInputStream();
471 inputStream = conn.getErrorStream();
473 String responseMessage = null;
475 responseMessage = conn.getResponseMessage();
476 } catch(Exception exc) {
477 responseMessage = EnglishReasonPhraseCatalog.INSTANCE.getReason(responseCode,null);
479 if(responseMessage == null)
480 responseMessage = NOT_PROVIDED;
483 // Process the response
484 LOG.info("HttpURLConnection result: {} : {}", responseCode, responseMessage);
485 logMetricResponse(responseCode, responseMessage);
487 if(inputStream == null) inputStream = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
488 BufferedReader reader = new BufferedReader( new InputStreamReader( inputStream ) );
491 ObjectMapper mapper = AAIService.getObjectMapper();
493 if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
494 StringBuilder stringBuilder = new StringBuilder();
496 while( ( line = reader.readLine() ) != null ) {
497 stringBuilder.append( line );
499 LOGwriteEndingTrace(responseCode, responseMessage, stringBuilder.toString());
501 } else if(responseCode == HttpURLConnection.HTTP_NOT_FOUND ) {
502 LOGwriteEndingTrace(responseCode, responseMessage, "Entry does not exist.");
505 ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
506 LOGwriteEndingTrace(responseCode, responseMessage, mapper.writeValueAsString(errorresponse));
507 throw new AAIServiceException(responseCode, errorresponse);
509 } catch(AAIServiceException aaiexc) {
511 } catch (Exception exc) {
512 LOG.warn("delete", exc);
513 throw new AAIServiceException(exc);
515 if(inputStream != null){
518 } catch(Exception exc) {
519 LOG.warn("delete", exc);
527 * Returns an String that contains JSON data returned from the AAI Server.
529 * This method always returns immediately, whether or not the
532 * @param request an instance of AAIRequiest representing
533 * the request made by DirectedGraph node.
534 * @param clas an definition of the class for which data will be returned
535 * @return the instance of the class with data.
539 public Object query(AAIRequest request, Class clas) throws AAIServiceException {
540 Object response = null;
541 InputStream inputStream = null;
544 URL requestUrl = request.getRequestQueryUrl(HttpMethod.GET);
545 HttpURLConnection con = getConfiguredConnection(requestUrl, HttpMethod.GET);
546 logMetricRequest("GET "+requestUrl.getPath(), "", requestUrl.getPath());
549 int responseCode = con.getResponseCode();
550 if (responseCode == HttpURLConnection.HTTP_OK) {
551 inputStream = con.getInputStream();
553 inputStream = con.getErrorStream();
555 String responseMessage = null;
557 responseMessage = con.getResponseMessage();
558 } catch(Exception exc) {
559 responseMessage = EnglishReasonPhraseCatalog.INSTANCE.getReason(responseCode,null);
561 if(responseMessage == null)
562 responseMessage = NOT_PROVIDED;
565 LOG.info("HttpURLConnection result: {} : {}", responseCode, responseMessage);
566 logMetricResponse(responseCode, responseMessage);
567 ObjectMapper mapper = AAIService.getObjectMapper();
569 if (responseCode == HttpURLConnection.HTTP_OK) {
570 // Process the response
571 BufferedReader reader = new BufferedReader( new InputStreamReader( inputStream ) );
572 response = mapper.readValue(reader, clas);
573 LOGwriteEndingTrace(HttpURLConnection.HTTP_OK, "SUCCESS", mapper.writeValueAsString(response));
574 } else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
575 LOGwriteEndingTrace(responseCode, "HTTP_NOT_FOUND", "Entry does not exist.");
578 BufferedReader reader = new BufferedReader( new InputStreamReader( inputStream ) );
579 ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
580 LOGwriteEndingTrace(responseCode, "FAILURE", mapper.writeValueAsString(errorresponse));
581 throw new AAIServiceException(responseCode, errorresponse);
584 } catch(AAIServiceException aaiexc) {
586 } catch (Exception exc) {
587 LOG.warn("GET", exc);
588 throw new AAIServiceException(exc);
590 if(inputStream != null){
593 } catch(Exception exc) {
594 LOG.warn("GET", exc);
602 public Boolean patch(AAIRequest request, String resourceVersion) throws AAIServiceException {
603 InputStream inputStream = null;
606 AAIDatum instance = request.getRequestObject();
607 if(instance instanceof ResourceVersion) {
608 resourceVersion = ((ResourceVersion)instance).getResourceVersion();
611 URL requestUrl = null;
612 HttpURLConnection con = getConfiguredConnection(requestUrl = request.getRequestUrl("PATCH", resourceVersion), "PATCH");
613 ObjectMapper mapper = AAIService.getObjectMapper();
614 String jsonText = request.toJSONString();
616 LOGwriteDateTrace("data", jsonText);
617 logMetricRequest("PATCH "+requestUrl.getPath(), jsonText, requestUrl.getPath());
619 OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
624 int responseCode = con.getResponseCode();
625 if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
626 inputStream = con.getInputStream();
628 inputStream = con.getErrorStream();
630 String responseMessage = null;
632 responseMessage = con.getResponseMessage();
633 } catch(Exception exc) {
634 responseMessage = EnglishReasonPhraseCatalog.INSTANCE.getReason(responseCode,null);
636 if(responseMessage == null)
637 responseMessage = NOT_PROVIDED;
640 LOG.info("HttpURLConnection result: {} : {}", responseCode, responseMessage);
641 logMetricResponse(responseCode, responseMessage);
643 // Process the response
644 BufferedReader reader;
646 reader = new BufferedReader( new InputStreamReader( inputStream ) );
647 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
649 if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
650 StringBuilder stringBuilder = new StringBuilder();
652 while( ( line = reader.readLine() ) != null ) {
653 stringBuilder.append( line );
655 LOGwriteEndingTrace(responseCode, responseMessage, (stringBuilder.length() > 0) ? stringBuilder.toString() : "{no-data}");
658 StringBuilder stringBuilder = new StringBuilder();
660 while( ( line = reader.readLine() ) != null ) {
661 stringBuilder.append("\n").append( line );
663 LOG.info(stringBuilder.toString());
666 ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
667 LOGwriteEndingTrace(responseCode, responseMessage, mapper.writeValueAsString(errorresponse));
669 throw new AAIServiceException(responseCode, errorresponse);
671 } catch(AAIServiceException aaiexc) {
673 } catch (Exception exc) {
674 LOG.warn("AAIRequestExecutor.patch", exc);
675 throw new AAIServiceException(exc);
678 if(inputStream != null)
680 } catch (Exception exc) {
681 LOG.warn("AAIRequestExecutor.patch", exc);
693 protected HttpURLConnection getConfiguredConnection(URL httpReqUrl, String method) throws Exception {
694 HttpURLConnection con = (HttpURLConnection) httpReqUrl.openConnection();
696 // Set up the connection properties
697 con.setRequestProperty("Connection", "close");
698 con.setDoInput(true);
699 con.setDoOutput(true);
700 con.setUseCaches(false);
701 con.setConnectTimeout(connection_timeout);
702 con.setReadTimeout(read_timeout);
703 con.setRequestMethod(method);
704 con.setRequestProperty("Accept", "application/json");
705 con.setRequestProperty("Transfer-Encoding","chunked");
706 con.setRequestProperty("Content-Type",
707 "PATCH".equalsIgnoreCase(method) ? "application/merge-patch+json" : "application/json");
708 con.setRequestProperty("X-FromAppId", applicationId);
709 con.setRequestProperty("X-TransactionId", TransactionIdTracker.getNextTransactionId());
710 String mlId = ml.getRequestID();
711 if (mlId != null && !mlId.isEmpty()) {
712 LOG.debug(String.format("MetricLogger requestId = %s", mlId));
713 con.setRequestProperty(MetricLogger.REQUEST_ID, mlId);
715 LOG.debug("MetricLogger requestId is null");
718 if (userName != null && !userName.isEmpty() && userPassword != null && !userPassword.isEmpty()) {
719 String basicAuth = "Basic " + new String(Base64.encodeBase64((userName + ":" + userPassword).getBytes()));
720 con.setRequestProperty("Authorization", basicAuth);
723 if (con instanceof HttpsURLConnection && CTX != null) {
724 SSLSocketFactory sockFact = CTX.getSocketFactory();
725 HttpsURLConnection.class.cast(con).setSSLSocketFactory(sockFact);
730 private URL appendDepth(URL requestUrl, AAIRequest request) throws MalformedURLException {
732 String depth = request.requestProperties.getProperty("depth", "1");
733 String path = requestUrl.toString();
734 if(path.contains("?depth=") || path.contains("&depth=")) {
737 if(path.contains("?")) {
738 path = String.format("%s&depth=%s", path, depth);
740 path = String.format("%s?depth=%s", path, depth);
742 return new URL(path);
746 public void logMetricRequest(String targetServiceName, String msg, String path){
747 String svcInstanceId = "";
748 String svcName = null;
749 String partnerName = null;
750 String targetEntity = "A&AI";
751 String targetVirtualEntity = null;
753 ml.logRequest(svcInstanceId, svcName, partnerName, targetEntity, targetServiceName, targetVirtualEntity, msg);
756 public void logMetricResponse(int responseCode, String responseDescription){
757 ml.logResponse(responseCode < 400 ? "COMPLETE" : "ERROR", Integer.toString(responseCode), responseDescription);
760 protected void LOGwriteFirstTrace(String method, String url) {
761 String time = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(System.currentTimeMillis());
762 LOG.info("A&AI transaction :");
763 LOG.info("Request Time : " + time + ", Method : " + method);
764 LOG.info("Request URL : "+ url);
767 protected void LOGwriteDateTrace(String name, String data) {
768 LOG.info("Input - " + name + " : " + data);
771 protected void LOGwriteEndingTrace(int response_code, String comment, String data) {
772 LOG.info("Response code : " + response_code +", " + comment);
773 LOG.info(String.format("Response data : %s", data));