Search in sources :

Example 6 with ISResponseData

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;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) ISResponseData(cz.metacentrum.perun.core.implApi.modules.pwdmgr.ISResponseData) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IllegalArgumentException(cz.metacentrum.perun.core.api.exceptions.IllegalArgumentException)

Example 7 with ISResponseData

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);
}
Also used : ISResponseData(cz.metacentrum.perun.core.implApi.modules.pwdmgr.ISResponseData) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Aggregations

ISResponseData (cz.metacentrum.perun.core.implApi.modules.pwdmgr.ISResponseData)7 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)4 Test (org.junit.Test)4 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)3 IOException (java.io.IOException)3 SecureRandom (java.security.SecureRandom)2 Random (java.util.Random)2 IllegalArgumentException (cz.metacentrum.perun.core.api.exceptions.IllegalArgumentException)1 PasswordStrengthException (cz.metacentrum.perun.core.api.exceptions.PasswordStrengthException)1 StringReader (java.io.StringReader)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XPath (javax.xml.xpath.XPath)1 XPathExpression (javax.xml.xpath.XPathExpression)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 XPathFactory (javax.xml.xpath.XPathFactory)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 Document (org.w3c.dom.Document)1 InputSource (org.xml.sax.InputSource)1