use of cz.metacentrum.perun.core.implApi.modules.pwdmgr.ISResponseData in project perun by CESNET.
the class ISServiceCallerImpl method parseResponse.
/**
* Parse XML response from IS MU to XML document.
*
* @param inputStream Stream to be parsed to Document
* @param requestID ID of request made to IS MU.
* @return XML document for further processing
* @throws InternalErrorException
*/
public ISResponseData parseResponse(InputStream inputStream, int requestID) {
// Create new document factory builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
throw new InternalErrorException("Error when creating newDocumentBuilder. Request ID: " + requestID, ex);
}
String response;
try {
response = convertStreamToString(inputStream, StandardCharsets.UTF_8);
} catch (IOException ex) {
throw new IllegalArgumentException("Unable to convert InputStream to String.", ex);
}
log.trace("[IS Request {}] Response: {}", requestID, response);
log.debug("[IS Request {}] Processing response from IS MU.", requestID);
Document doc;
try {
doc = builder.parse(new InputSource(new StringReader(response)));
} catch (SAXParseException ex) {
throw new InternalErrorException("Error when parsing uri by document builder. Request ID: " + requestID, ex);
} catch (SAXException ex) {
throw new InternalErrorException("Problem with parsing is more complex, not only invalid characters. Request ID: " + requestID, ex);
} catch (IOException ex) {
throw new InternalErrorException("Error when parsing uri by document builder. Problem with input or output. Request ID: " + requestID, ex);
}
// Prepare xpath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression isErrorExpr;
XPathExpression getErrorTextExpr;
XPathExpression getDbErrorTextExpr;
try {
isErrorExpr = xpath.compile("//resp/stav/text()");
getErrorTextExpr = xpath.compile("//resp/error/text()");
getDbErrorTextExpr = xpath.compile("//resp/dberror/text()");
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when compiling xpath query. Request ID: " + requestID, ex);
}
// OK or ERROR
String responseStatus;
try {
responseStatus = (String) isErrorExpr.evaluate(doc, XPathConstants.STRING);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when evaluate xpath query on document to resolve response status. Request ID: " + requestID, ex);
}
log.trace("[IS Request {}] Response of request from IS MU has status: {}", requestID, responseStatus);
log.debug("[IS Request {}] Response of request from IS MU has status: {}", requestID, responseStatus);
ISResponseData responseData = new ISResponseData();
responseData.setStatus(responseStatus);
responseData.setResponse(doc);
if (!IS_OK_STATUS.equals(responseStatus)) {
try {
String error = (String) getErrorTextExpr.evaluate(doc, XPathConstants.STRING);
if (error == null || error.isEmpty()) {
error = (String) getDbErrorTextExpr.evaluate(doc, XPathConstants.STRING);
}
responseData.setError(error.trim());
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when evaluate xpath query on document to resolve error status. Request ID: " + requestID, ex);
}
}
return responseData;
}
use of cz.metacentrum.perun.core.implApi.modules.pwdmgr.ISResponseData in project perun by CESNET.
the class MuPasswordManagerModuleTest method changePasswordExceptionIsThrownIfIsReturnsAnError.
@Test
public void changePasswordExceptionIsThrownIfIsReturnsAnError() throws Exception {
String errorMessage = "Invalid password";
ISResponseData errResponseData = new ISResponseData();
errResponseData.setStatus(IS_ERROR_STATUS);
errResponseData.setError(errorMessage);
when(isServiceCallerMock.call(anyString(), anyInt())).thenReturn(errResponseData);
assertThatExceptionOfType(PasswordStrengthException.class).isThrownBy(() -> module.checkPasswordStrength(sess, null, "Adf.,.2;..df,")).withMessageEndingWith(errorMessage);
}
Aggregations