Java 类org.jivesoftware.smack.sasl.packet.SaslStreamElements.SASLFailure 实例源码

项目:Smack    文件:PacketParserUtilsTest.java   
@Test
public void parseSASLFailureExtended() throws FactoryConfigurationError, TransformerException,
                ParserConfigurationException, XmlPullParserException, IOException, SAXException {
    // @formatter:off
    final String saslFailureString = XMLBuilder.create(SASLFailure.ELEMENT, SaslStreamElements.NAMESPACE)
                    .e(SASLError.account_disabled.toString())
                    .up()
                    .e("text").a("xml:lang", "en")
                        .t("Call 212-555-1212 for assistance.")
                    .up()
                    .e("text").a("xml:lang", "de")
                        .t("Bitte wenden sie sich an (04321) 123-4444")
                    .up()
                    .e("text")
                        .t("Wusel dusel")
                    .asString();
    // @formatter:on
    XmlPullParser parser = TestUtils.getParser(saslFailureString, SASLFailure.ELEMENT);
    SASLFailure saslFailure = PacketParserUtils.parseSASLFailure(parser);
    XmlUnitUtils.assertSimilar(saslFailureString, saslFailure.toXML());
}
项目:Smack    文件:PacketParserUtils.java   
/**
 * Parses SASL authentication error packets.
 * 
 * @param parser the XML parser.
 * @return a SASL Failure packet.
 * @throws IOException 
 * @throws XmlPullParserException 
 */
public static SASLFailure parseSASLFailure(XmlPullParser parser) throws XmlPullParserException, IOException {
    final int initialDepth = parser.getDepth();
    String condition = null;
    Map<String, String> descriptiveTexts = null;
    outerloop: while (true) {
        int eventType = parser.next();
        switch (eventType) {
        case XmlPullParser.START_TAG:
            String name = parser.getName();
            if (name.equals("text")) {
                descriptiveTexts = parseDescriptiveTexts(parser, descriptiveTexts);
            }
            else {
                assert(condition == null);
                condition = parser.getName();
            }
            break;
        case XmlPullParser.END_TAG:
            if (parser.getDepth() == initialDepth) {
                break outerloop;
            }
            break;
        }
    }
    return new SASLFailure(condition, descriptiveTexts);
}
项目:Smack    文件:PacketParserUtilsTest.java   
@Test
public void parseSASLFailureSimple() throws FactoryConfigurationError, SAXException, IOException,
                TransformerException, ParserConfigurationException, XmlPullParserException {
    // @formatter:off
    final String saslFailureString = XMLBuilder.create(SASLFailure.ELEMENT, SaslStreamElements.NAMESPACE)
                    .e(SASLError.account_disabled.toString())
                    .asString();
    // @formatter:on
    XmlPullParser parser = TestUtils.getParser(saslFailureString, SASLFailure.ELEMENT);
    SASLFailure saslFailure = PacketParserUtils.parseSASLFailure(parser);
    assertXMLEqual(saslFailureString, saslFailure.toString());
}
项目:Smack    文件:SASLErrorException.java   
public SASLErrorException(String mechanism, SASLFailure saslFailure) {
    this(mechanism, saslFailure, new HashMap<String, String>());
}
项目:Smack    文件:SASLErrorException.java   
public SASLErrorException(String mechanism, SASLFailure saslFailure, Map<String,String> texts) {
    super("SASLError using " + mechanism + ": " + saslFailure.getSASLErrorString());
    this.mechanism = mechanism;
    this.saslFailure = saslFailure;
    this.texts = texts;
}
项目:Smack    文件:SASLErrorException.java   
public SASLFailure getSASLFailure() {
    return saslFailure;
}
项目:Smack    文件:XMPPBOSHConnection.java   
/**
 * Parse the received packets and notify the corresponding connection.
 *
 * @param event the BOSH client response which includes the received packet.
 */
public void responseReceived(BOSHMessageEvent event) {
    AbstractBody body = event.getBody();
    if (body != null) {
        try {
            if (sessionID == null) {
                sessionID = body.getAttribute(BodyQName.create(XMPPBOSHConnection.BOSH_URI, "sid"));
            }
            if (streamId == null) {
                streamId = body.getAttribute(BodyQName.create(XMPPBOSHConnection.BOSH_URI, "authid"));
            }
            final XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
            parser.setInput(new StringReader(body.toXML()));
            int eventType = parser.getEventType();
            do {
                eventType = parser.next();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    String name = parser.getName();
                    switch (name) {
                    case Message.ELEMENT:
                    case IQ.IQ_ELEMENT:
                    case Presence.ELEMENT:
                        parseAndProcessStanza(parser);
                        break;
                    case "challenge":
                        // The server is challenging the SASL authentication
                        // made by the client
                        final String challengeData = parser.nextText();
                        getSASLAuthentication().challengeReceived(challengeData);
                        break;
                    case "success":
                        send(ComposableBody.builder().setNamespaceDefinition("xmpp",
                                        XMPPBOSHConnection.XMPP_BOSH_NS).setAttribute(
                                        BodyQName.createWithPrefix(XMPPBOSHConnection.XMPP_BOSH_NS, "restart",
                                                        "xmpp"), "true").setAttribute(
                                        BodyQName.create(XMPPBOSHConnection.BOSH_URI, "to"), getServiceName()).build());
                        Success success = new Success(parser.nextText());
                        getSASLAuthentication().authenticated(success);
                        break;
                    case "features":
                        parseFeatures(parser);
                        break;
                    case "failure":
                        if ("urn:ietf:params:xml:ns:xmpp-sasl".equals(parser.getNamespace(null))) {
                            final SASLFailure failure = PacketParserUtils.parseSASLFailure(parser);
                            getSASLAuthentication().authenticationFailed(failure);
                        }
                        break;
                    case "error":
                        throw new StreamErrorException(PacketParserUtils.parseStreamError(parser));
                    }
                    break;
                }
            }
            while (eventType != XmlPullParser.END_DOCUMENT);
        }
        catch (Exception e) {
            if (isConnected()) {
                notifyConnectionError(e);
            }
        }
    }
}
项目:Smack    文件:SASLAuthentication.java   
/**
 * Notification message saying that SASL authentication has failed. The server may have
 * closed the connection depending on the number of possible retries.
 * 
 * @param saslFailure the SASL failure as reported by the server
 * @see <a href="https://tools.ietf.org/html/rfc6120#section-6.5">RFC6120 6.5</a>
 */
public void authenticationFailed(SASLFailure saslFailure) {
    authenticationFailed(new SASLErrorException(currentMechanism.getName(), saslFailure));
}