Skip to content

Commit

Permalink
Fix element order in data-service fault messages
Browse files Browse the repository at this point in the history
Fix element ordering in data-service fault messages to match the WSDL definition.
Fixes wso2/product-ei/issues/4664
  • Loading branch information
GDLMadushanka committed Aug 11, 2023
1 parent 087dc02 commit f589a44
Showing 1 changed file with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.wso2.micro.integrator.dataservices.core;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

Expand All @@ -40,28 +41,28 @@
public class DataServiceFault extends Exception {

private static final long serialVersionUID = 1L;

/**
* The type of data service fault, this can be used to identify what kind
* of an error condition occurred.
*/
private String code;

/**
* The detailed explanation of the data service fault.
*/
private String dsFaultMessage;

/**
* The originating data service of the exception, if available.
* The originating data service of the exception, if available.
*/
private DataService sourceDataService;

/**
* The on-going operation/resource name when the data service fault occurs, if available.
*/
private String currentRequestName;

/**
* The current parameters of the current operation/resource, if available.
*/
Expand All @@ -71,7 +72,7 @@ public class DataServiceFault extends Exception {
* This map contains all the properties related to data services fault message
*/
private Map<String, Object> propertyMap = new HashMap<String, Object>();

public DataServiceFault(Throwable nestedException, String code, String dsFaultMessage) {
super(nestedException);
this.code = code;
Expand All @@ -91,7 +92,19 @@ public static OMElement extractFaultMessage(Throwable throwable) {
OMElement root = fac.createOMElement(new QName(
DBConstants.WSO2_DS_NAMESPACE, DBConstants.DS_FAULT_ELEMENT));
OMNamespace ns = root.getNamespace();
for (Map.Entry<String, Object> rootEntry : ((DataServiceFault) throwable).getPropertyMap().entrySet()) {
// Adding data from HashMap to a LikedHashMap to preserve the order
Map<String, Object> propLinkedHashMap = new LinkedHashMap<>();
propLinkedHashMap.put(DBConstants.FaultParams.CURRENT_PARAMS,
((DataServiceFault) throwable).getPropertyMap().get(DBConstants.FaultParams.CURRENT_PARAMS));
propLinkedHashMap.put(DBConstants.FaultParams.CURRENT_REQUEST_NAME,
((DataServiceFault) throwable).getPropertyMap().get(DBConstants.FaultParams.CURRENT_REQUEST_NAME));
propLinkedHashMap.put(DBConstants.FaultParams.NESTED_EXCEPTION,
((DataServiceFault) throwable).getPropertyMap().get(DBConstants.FaultParams.NESTED_EXCEPTION));
propLinkedHashMap.put(DBConstants.FaultParams.SOURCE_DATA_SERVICE,
((DataServiceFault) throwable).getPropertyMap().get(DBConstants.FaultParams.SOURCE_DATA_SERVICE));
propLinkedHashMap.put(DBConstants.FaultParams.DS_CODE,
((DataServiceFault) throwable).getPropertyMap().get(DBConstants.FaultParams.DS_CODE));
for (Map.Entry<String, Object> rootEntry : propLinkedHashMap.entrySet()) {
OMElement keyElement = fac.createOMElement(rootEntry.getKey(), ns);
if (rootEntry.getValue() instanceof Map) {
for (Map.Entry dataServiceEntry : (Set<Map.Entry>) ((Map) rootEntry.getValue()).entrySet()) {
Expand Down Expand Up @@ -123,11 +136,11 @@ public static OMElement extractFaultMessage(Throwable throwable) {
return null;
}
}

public static String extractFaultCode(Throwable throwable) {
if (throwable instanceof DataServiceFault) {
return ((DataServiceFault) throwable).getCode();
} else if (throwable instanceof XMLStreamException) {
} else if (throwable instanceof XMLStreamException) {
return extractFaultCode(((XMLStreamException) throwable).getNestedException());
} else if (throwable != null) {
Throwable cause = throwable.getCause();
Expand All @@ -137,39 +150,39 @@ public static String extractFaultCode(Throwable throwable) {
return FaultCodes.UNKNOWN_ERROR;
}
} else {
return FaultCodes.UNKNOWN_ERROR;
return FaultCodes.UNKNOWN_ERROR;
}
}

public DataServiceFault(Throwable nestedException) {
this(nestedException, null, null);
}

public DataServiceFault(Throwable nestedException, String dsFaultMessage) {
this(nestedException, null, dsFaultMessage);
}

public DataServiceFault(String code, String dsFaultMessage) {
this(null, code, dsFaultMessage);
}

public DataServiceFault(String dsFaultMessage) {
this(null, null, dsFaultMessage);
}

public String getCode() {
return code;
}

public String getDsFaultMessage() {
return dsFaultMessage;
}

@Override
public String getMessage() {
return this.getFullMessage();
}

/**
* Returns a detailed description of the data service fault.
*/
Expand Down Expand Up @@ -201,13 +214,13 @@ public String getFullMessage() {
buff.append("Current Params: " + this.getCurrentParams() + "\n");
getPropertyMap().put(DBConstants.FaultParams.CURRENT_PARAMS, this.getCurrentParams().toString());
}
if (this.getCause() != null) {
if (this.getCause() != null) {
buff.append("Nested Exception:-\n" + this.getCause() + "\n");
getPropertyMap().put(DBConstants.FaultParams.NESTED_EXCEPTION, this.getCause().toString());
}
return buff.toString();
}

@Override
public String toString() {
return this.getFullMessage();
Expand Down

0 comments on commit f589a44

Please sign in to comment.