use of com.axway.ats.agent.core.templateactions.exceptions.XmlReaderException in project ats-framework by Axway.
the class XmlReader method loadXmlFile.
private void loadXmlFile() throws XmlReaderException, XmlUtilitiesException {
//FIXME: performance: different actionsXml files could be loaded concurrently but extensive synchronization is needed
synchronized (actionNodesMap) {
if (!actionNodesMap.containsKey(actionsXml)) {
// load the document
BufferedReader br = null;
FileChunkReader fileChunkReader = null;
try {
// load the action nodes and add them to the map
actionNodes = new ArrayList<ActionObject>();
br = new BufferedReader(new InputStreamReader(new FileInputStream(actionsXml)));
fileChunkReader = new FileChunkReader();
StringBuilder request = new StringBuilder(1000);
//if the response doesn't contain parameters we will keep it without the response body part
StringBuilder responseWoBodyBuilder = new StringBuilder(1000);
String response = null;
boolean inRequest = false;
boolean inResponse = false;
boolean hasParametersInResponse = false;
int currentLineNumber = 0;
int startLineMarker = 0;
String line;
while ((line = br.readLine()) != null) {
currentLineNumber++;
if (line.contains("<HTTP_ACTION>")) {
inResponse = false;
inRequest = false;
continue;
} else if (line.contains("</HTTP_ACTION>")) {
actionNodes.add(new ActionObject(actionsXml, request.toString(), response));
continue;
} else if (line.contains("<HTTP_REQUEST ") || line.contains("<HTTP_REQUEST>")) {
// the normal case is "<HTTP_REQUEST ",
// but we also handle here the "<HTTP_REQUEST>" in case the HTTP method attribute is missing
// sometime later an appropriate exception will be thrown
// clear old request data
request.delete(0, request.length());
inRequest = true;
} else if (line.contains("</HTTP_REQUEST>")) {
request.append(line + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
inRequest = false;
continue;
} else if (line.contains("<HTTP_RESPONSE>")) {
startLineMarker = currentLineNumber;
// clear old response data
responseWoBodyBuilder.delete(0, responseWoBodyBuilder.length());
responseWoBodyBuilder.append(line + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
inResponse = true;
continue;
} else if (line.contains("</HTTP_RESPONSE>")) {
responseWoBodyBuilder.append(line + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
// optimization - do not store body if there are no variables in the body
if (hasParametersInResponse) {
response = fileChunkReader.readChunk(startLineMarker, currentLineNumber);
} else {
response = responseWoBodyBuilder.toString();
}
inResponse = false;
hasParametersInResponse = false;
continue;
}
if (inRequest) {
request.append(line + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
} else if (inResponse) {
// search for parameters in the response
if (!hasParametersInResponse && line.contains("${") && line.matches(".*\\$\\{.+\\}.*")) {
hasParametersInResponse = true;
}
// collect the response data without the response body
if (line.contains("<HTTP_HEADER ") || line.contains("<HTTP_RESOURCE_FILE") || line.contains("<HTTP_RESPONSE_RESULT>")) {
responseWoBodyBuilder.append(line + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
}
}
}
actionNodesMap.put(actionsXml, actionNodes);
} catch (Exception e) {
throw new XmlReaderException(actionsXml, e);
} finally {
IoUtils.closeStream(fileChunkReader);
IoUtils.closeStream(br);
}
} else {
actionNodes = actionNodesMap.get(actionsXml);
}
iActionNodes = -1;
}
}
Aggregations