From 1865c009e9b2a87009731413334e27410b1c46b5 Mon Sep 17 00:00:00 2001 From: pamodaaw Date: Tue, 18 Aug 2020 20:17:00 +0530 Subject: [PATCH] Handle a possible null pointer --- .../Office365ProvisioningConnector.java | 66 +++++++++++++------ 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/components/org.wso2.carbon.identity.outbound.provisioning.connector.office365/src/main/java/org/wso2/carbon/identity/outbound/provisioning/connector/office365/Office365ProvisioningConnector.java b/components/org.wso2.carbon.identity.outbound.provisioning.connector.office365/src/main/java/org/wso2/carbon/identity/outbound/provisioning/connector/office365/Office365ProvisioningConnector.java index 12d3bd9..33ef8f4 100644 --- a/components/org.wso2.carbon.identity.outbound.provisioning.connector.office365/src/main/java/org/wso2/carbon/identity/outbound/provisioning/connector/office365/Office365ProvisioningConnector.java +++ b/components/org.wso2.carbon.identity.outbound.provisioning.connector.office365/src/main/java/org/wso2/carbon/identity/outbound/provisioning/connector/office365/Office365ProvisioningConnector.java @@ -154,11 +154,19 @@ protected String createUser(ProvisioningEntity provisioningEntity) throws Identi jsonResponse.toString()); } } else { - String errorMessage = jsonResponse.getJSONObject("error").getString("message"); - log.error("Received response status code: " + response.getStatusLine().getStatusCode() + " " - + response.getStatusLine().getReasonPhrase() + " with the message '" + errorMessage + - "' while creating the user " + user.getString(Office365ConnectorConstants.OFFICE365_UPN) + - " in the Azure Active Directory."); + if (jsonResponse.has("error")) { + String errorMessage = jsonResponse.getJSONObject("error").getString("message"); + log.error("Received response status code: " + response.getStatusLine().getStatusCode() + " " + + response.getStatusLine().getReasonPhrase() + " with the message '" + errorMessage + + "' while creating the user " + user.getString(Office365ConnectorConstants + .OFFICE365_UPN) + " in the Azure Active Directory."); + + } else { + log.error("Received response status code: " + response.getStatusLine().getStatusCode() + " " + + response.getStatusLine().getReasonPhrase() + " while creating the user " + user + .getString(Office365ConnectorConstants.OFFICE365_UPN) + " in the Azure Active " + + "Directory."); + } if (log.isDebugEnabled()) { log.debug("The response received from server : " + jsonResponse.toString()); @@ -211,12 +219,18 @@ protected void updateUser(ProvisioningEntity provisioningEntity) throws Identity } else { JSONObject jsonResponse = new JSONObject(new JSONTokener(new InputStreamReader( response.getEntity().getContent()))); - String errorMessage = jsonResponse.getJSONObject("error").getString("message"); - log.error("Received response status code: " + response.getStatusLine().getStatusCode() + " " - + response.getStatusLine().getReasonPhrase() + " with the message '" + errorMessage + - "' while updating the user with id " + provisionedUserId + " in the Azure Active " + - "Directory."); + if (jsonResponse.has("error")) { + String errorMessage = jsonResponse.getJSONObject("error").getString("message"); + log.error("Received response status code: " + response.getStatusLine().getStatusCode() + " " + + response.getStatusLine().getReasonPhrase() + " with the message '" + errorMessage + + "' while updating the user with id " + provisionedUserId + " in the Azure Active " + + "Directory."); + } else { + log.error("Received response status code: " + response.getStatusLine().getStatusCode() + " " + + response.getStatusLine().getReasonPhrase() + " while updating the user with id " + + provisionedUserId + " in the Azure Active " + "Directory."); + } if (log.isDebugEnabled()) { log.debug("The response received from server : " + jsonResponse.toString()); @@ -259,12 +273,18 @@ protected void deleteUser(ProvisioningEntity provisioningEntity) throws Identity } else { JSONObject jsonResponse = new JSONObject(new JSONTokener(new InputStreamReader( response.getEntity().getContent()))); - String errorMessage = jsonResponse.getJSONObject("error").getString("message"); - log.error("Received response status code: " + response.getStatusLine().getStatusCode() + " " - + response.getStatusLine().getReasonPhrase() + " with the message '" + errorMessage + - "' while deleting the user with id " + provisionedUserId + " from the Azure Active " + - "Directory."); + if (jsonResponse.has("error")) { + String errorMessage = jsonResponse.getJSONObject("error").getString("message"); + log.error("Received response status code: " + response.getStatusLine().getStatusCode() + " " + + response.getStatusLine().getReasonPhrase() + " with the message '" + errorMessage + + "' while deleting the user with id " + provisionedUserId + " from the Azure Active " + + "Directory."); + } else { + log.error("Received response status code: " + response.getStatusLine().getStatusCode() + " " + + response.getStatusLine().getReasonPhrase() + " while deleting the user with id " + + provisionedUserId + " from the Azure Active Directory."); + } if (log.isDebugEnabled()) { log.debug("The response received from server : " + jsonResponse.toString()); @@ -307,12 +327,18 @@ protected void deleteUserPermanently(ProvisioningEntity provisioningEntity) thro } else { JSONObject jsonResponse = new JSONObject(new JSONTokener(new InputStreamReader( response.getEntity().getContent()))); - String errorMessage = jsonResponse.getJSONObject("error").getString("message"); - log.error("Received response status code: " + response.getStatusLine().getStatusCode() + " " - + response.getStatusLine().getReasonPhrase() + " with the message '" + errorMessage + - "' while permanently removing the user with id " + provisionedUserId + - " in the Azure Active Directory."); + if (jsonResponse.has("error")) { + String errorMessage = jsonResponse.getJSONObject("error").getString("message"); + log.error("Received response status code: " + response.getStatusLine().getStatusCode() + " " + + response.getStatusLine().getReasonPhrase() + " with the message '" + errorMessage + + "' while permanently removing the user with id " + provisionedUserId + + " in the Azure Active Directory."); + } else { + log.error("Received response status code: " + response.getStatusLine().getStatusCode() + " " + + response.getStatusLine().getReasonPhrase() + " while permanently removing the user" + + " with id " + provisionedUserId + " in the Azure Active Directory."); + } if (log.isDebugEnabled()) { log.debug("The response received from server : " + jsonResponse.toString());