com.sap.ipe.ble.irp.handler.services.ResultService Maven / Gradle / Ivy
package com.sap.ipe.ble.irp.handler.services;
import com.sap.ipe.ble.irp.exceptions.ExtensionException;
import com.sap.ipe.ble.irp.handler.constants.Constants;
import com.sap.ipe.ble.irp.utils.SecurityUtils;
import com.sap.ipe.ble.irp.utils.Translator;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/***
* ResultService is used to prepare the result to be updated
* This result is the response from Remote Handler
*/
@Service
public class ResultService {
/***
* This method is used to check for security related response data
* Here we delete all the other parameters except the extended parameters.
* @param elements the data elements in the application context
* @param response response from remote handler
* @return data to be shown on UI
* @throws ExtensionException user defined exception
*/
public JSONObject getFinalData(List elements, JSONObject response)throws ExtensionException{
JSONObject data = response.getJSONObject(Constants.DATA);
List parameterName = new ArrayList<>(data.keySet());
for (int i = data.length() - 1; i >= 0; i--) {
//Check the extended fields for unsafe characters; else throw Exception
if(SecurityUtils.containUnsafeCharacters(data.get(parameterName.get(i)).toString())
&& parameterName.get(i).startsWith(Constants.ELEMENT_PREFIX)){
throw new ExtensionException(Translator.toLocale(Constants.UNSAFE_CHAR_MESSAGE, new String[]{parameterName.get(i)}));
}
//Remove all other fields except the extended fields
if (((!elements.contains(parameterName.get(i)) ) || (!parameterName.get(i).startsWith(Constants.ELEMENT_PREFIX)))) {
data.remove(parameterName.get(i));
}
}
return data;
}
}