org.jivesoftware.smack.packet.Element Java Examples

The following examples show how to use org.jivesoftware.smack.packet.Element. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XMPPTCPConnection.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Sends the specified element to the server.
 *
 * @param element the element to send.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
protected void sendStreamElement(Element element) throws NotConnectedException, InterruptedException {
    throwNotConnectedExceptionIfDoneAndResumptionNotPossible();
    try {
        queue.put(element);
    }
    catch (InterruptedException e) {
        // put() may throw an InterruptedException for two reasons:
        // 1. If the queue was shut down
        // 2. If the thread was interrupted
        // so we have to check which is the case
        throwNotConnectedExceptionIfDoneAndResumptionNotPossible();
        // If the method above did not throw, then the sending thread was interrupted
        throw e;
    }
}
 
Example #2
Source File: XMPPTCPConnection.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Maybe return the next available element from the queue for writing. If the queue is shut down <b>or</b> a
 * spurious interrupt occurs, <code>null</code> is returned. So it is important to check the 'done' condition in
 * that case.
 *
 * @return the next element for writing or null.
 */
private Element nextStreamElement() {
    // It is important the we check if the queue is empty before removing an element from it
    if (queue.isEmpty()) {
        shouldBundleAndDefer = true;
    }
    Element packet = null;
    try {
        packet = queue.take();
    }
    catch (InterruptedException e) {
        if (!queue.isShutdown()) {
            // Users shouldn't try to interrupt the packet writer thread
            LOGGER.log(Level.WARNING, "Writer thread was interrupted. Don't do that. Use disconnect() instead.", e);
        }
    }
    return packet;
}
 
Example #3
Source File: XMPPTCPConnection.java    From Smack with Apache License 2.0 6 votes vote down vote up
private void drainWriterQueueToUnacknowledgedStanzas() {
    List<Element> elements = new ArrayList<>(queue.size());
    queue.drainTo(elements);
    for (int i = 0; i < elements.size(); i++) {
        Element element = elements.get(i);
        // If the unacknowledgedStanza queue is full, then bail out with a warning message. See SMACK-844.
        if (unacknowledgedStanzas.remainingCapacity() == 0) {
            StreamManagementException.UnacknowledgedQueueFullException exception = StreamManagementException.UnacknowledgedQueueFullException
                    .newWith(i, elements, unacknowledgedStanzas);
            LOGGER.log(Level.WARNING,
                    "Some stanzas may be lost as not all could be drained to the unacknowledged stanzas queue", exception);
            return;
        }
        if (element instanceof Stanza) {
            unacknowledgedStanzas.add((Stanza) element);
        }
    }
}
 
Example #4
Source File: OutOfBandData.java    From Yahala-Messenger with MIT License 5 votes vote down vote up
@Override
public Element parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException {
    String url = null, mime = null;
    long length = -1;
    boolean in_url = false, done = false;

    while (!done) {
        int eventType = parser.next();

        if (eventType == XmlPullParser.START_TAG) {
            if ("url".equals(parser.getName())) {
                in_url = true;
                mime = parser.getAttributeValue(null, "type");
                String _length = parser.getAttributeValue(null, "length");
                try {
                    length = Long.parseLong(_length);
                } catch (Exception e) {
                    // ignored
                }
            }

        } else if (eventType == XmlPullParser.END_TAG) {
            if ("url".equals(parser.getName())) {
                done = true;
            }
        } else if (eventType == XmlPullParser.TEXT && in_url) {
            url = parser.getText();
        }
    }

    if (url != null)
        return new OutOfBandData(url, mime, length);
    else
        return null;
}
 
Example #5
Source File: DataFormTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidation() throws Exception {
    // Build a Form.
    DataForm.Builder df = DataForm.builder(DataForm.Type.form);
    String instruction = "InstructionTest1";
    df.addInstruction(instruction);
    FormField.Builder<?, ?> fieldBuilder = FormField.builder("testField1");

    ValidateElement dv = new RangeValidateElement("xs:integer", "1111", "9999");
    fieldBuilder.addFormFieldChildElement(dv);

    df.addField(fieldBuilder.build());

    DataForm dataForm = df.build();
    String output = dataForm.toXML().toString();
    assertEquals(TEST_OUTPUT_3, output);

    XmlPullParser parser = PacketParserUtils.getParserFor(output);

    dataForm = pr.parse(parser);

    assertNotNull(dataForm.getFields());
    assertEquals(1 , dataForm.getFields().size());
    Element element = ValidateElement.from(dataForm.getFields().get(0));
    assertNotNull(element);
    dv = (ValidateElement) element;

    assertEquals("xs:integer" , dv.getDatatype());

    output = dataForm.toXML().toString();
    assertEquals(TEST_OUTPUT_3, output);
}
 
Example #6
Source File: DataFormTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void testLayout() throws Exception {
    // Build a Form.
    DataForm.Builder df = DataForm.builder(DataForm.Type.form);
    String instruction = "InstructionTest1";
    df.addInstruction(instruction);
    FormField field = FormField.builder("testField1").build();
    df.addField(field);

    DataLayout layout = new DataLayout("Label");
    Fieldref reffield = new Fieldref("testField1");
    layout.getPageLayout().add(reffield);
    Section section = new Section("section Label");
    section.getSectionLayout().add(new Text("SectionText"));
    layout.getPageLayout().add(section);
    layout.getPageLayout().add(new Text("PageText"));

    df.addExtensionElement(layout);

    DataForm dataForm = df.build();
    String output = dataForm.toXML().toString();
    assertEquals(TEST_OUTPUT_2, output);

    XmlPullParser parser = PacketParserUtils.getParserFor(output);

    dataForm = pr.parse(parser);

    assertNotNull(dataForm.getExtensionElements());
    assertEquals(1 , dataForm.getExtensionElements().size());
    Element element = dataForm.getExtensionElements().get(0);
    assertNotNull(element);
    layout = (DataLayout) element;

    assertEquals(3 , layout.getPageLayout().size());

    output = dataForm.toXML().toString();
    assertEquals(TEST_OUTPUT_2, output);
}
 
Example #7
Source File: DataForm.java    From Smack with Apache License 2.0 5 votes vote down vote up
public Builder addExtensionElement(Element element) {
    if (extensionElements == null) {
        extensionElements = new ArrayList<>();
    }
    extensionElements.add(element);
    return this;
}
 
Example #8
Source File: XMPPBOSHConnection.java    From Smack with Apache License 2.0 5 votes vote down vote up
private void sendElement(Element element) {
    try {
        send(ComposableBody.builder().setPayloadXML(element.toXML(BOSH_URI).toString()).build());
        if (element instanceof Stanza) {
            firePacketSendingListeners((Stanza) element);
        }
    }
    catch (BOSHException e) {
        LOGGER.log(Level.SEVERE, "BOSHException in sendStanzaInternal", e);
    }
}
 
Example #9
Source File: StreamManagementException.java    From Smack with Apache License 2.0 5 votes vote down vote up
public static UnacknowledgedQueueFullException newWith(int overflowElementNum, List<Element> elements,
        BlockingQueue<Stanza> unacknowledgedStanzas) {
    final int unacknowledgesStanzasQueueSize = unacknowledgedStanzas.size();
    List<Stanza> localUnacknowledgesStanzas = new ArrayList<>(unacknowledgesStanzasQueueSize);
    localUnacknowledgesStanzas.addAll(unacknowledgedStanzas);
    int droppedElements = elements.size() - overflowElementNum - 1;

    String message = "The queue size " + unacknowledgesStanzasQueueSize + " is not able to fit another "
            + droppedElements + " potential stanzas type top-level stream-elements.";
    return new UnacknowledgedQueueFullException(message, overflowElementNum, droppedElements, elements,
            localUnacknowledgesStanzas);
}
 
Example #10
Source File: StreamManagementException.java    From Smack with Apache License 2.0 5 votes vote down vote up
private UnacknowledgedQueueFullException(String message, int overflowElementNum, int droppedElements, List<Element> elements,
        List<Stanza> unacknowledgesStanzas) {
    super(message);
    this.overflowElementNum = overflowElementNum;
    this.droppedElements = droppedElements;
    this.elements = elements;
    this.unacknowledgesStanzas = unacknowledgesStanzas;
}
 
Example #11
Source File: PhoneCall.java    From Yahala-Messenger with MIT License 5 votes vote down vote up
@Override
public Element parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException {
    String roomId = null;
    boolean videoCall = false;
    boolean in_roomId = false, done = false, callHangUp = false;

    while (!done) {
        int eventType = parser.next();

        if (eventType == XmlPullParser.START_TAG) {
            if ("roomId".equals(parser.getName())) {
                in_roomId = true;
                videoCall = Boolean.valueOf(parser.getAttributeValue(null, "vCall"));
                callHangUp = Boolean.valueOf(parser.getAttributeValue(null, "hangUp"));
            }

        } else if (eventType == XmlPullParser.END_TAG) {
            if ("roomId".equals(parser.getName())) {
                done = true;
            }
        } else if (eventType == XmlPullParser.TEXT && in_roomId) {
            roomId = parser.getText();
        }
    }

    if (roomId != null)
        return new PhoneCall(roomId, videoCall, callHangUp);
    else
        return null;
}
 
Example #12
Source File: XmlStringBuilder.java    From Smack with Apache License 2.0 4 votes vote down vote up
public XmlStringBuilder optElement(Element element) {
    if (element != null) {
        append(element);
    }
    return this;
}
 
Example #13
Source File: XmlStringBuilder.java    From Smack with Apache License 2.0 4 votes vote down vote up
public XmlStringBuilder optAppend(Element element) {
    if (element != null) {
        append(element.toXML(effectiveXmlEnvironment));
    }
    return this;
}
 
Example #14
Source File: XmlStringBuilder.java    From Smack with Apache License 2.0 4 votes vote down vote up
public XmlStringBuilder optAppend(Collection<? extends Element> elements) {
    if (elements != null) {
        append(elements);
    }
    return this;
}
 
Example #15
Source File: XmlStringBuilder.java    From Smack with Apache License 2.0 4 votes vote down vote up
public XmlStringBuilder append(Element element) {
    return append(element.toXML(effectiveXmlEnvironment));
}
 
Example #16
Source File: XmlStringBuilder.java    From Smack with Apache License 2.0 4 votes vote down vote up
public XmlStringBuilder append(Collection<? extends Element> elements) {
    for (Element element : elements) {
        append(element);
    }
    return this;
}
 
Example #17
Source File: SmackTestUtil.java    From Smack with Apache License 2.0 4 votes vote down vote up
public static <E extends Element, P extends Provider<E>> E parse(CharSequence xml, Class<P> providerClass, XmlPullParserKind parserKind)
                throws XmlPullParserException, IOException, SmackParsingException {
    P provider = providerClassToProvider(providerClass);
    return parse(xml, provider, parserKind);
}
 
Example #18
Source File: SmackTestUtil.java    From Smack with Apache License 2.0 4 votes vote down vote up
public static <E extends Element, P extends Provider<E>> E parse(InputStream inputStream, Class<P> providerClass, XmlPullParserKind parserKind)
                throws XmlPullParserException, IOException, SmackParsingException {
    P provider = providerClassToProvider(providerClass);
    return parse(inputStream, provider, parserKind);
}
 
Example #19
Source File: SmackTestUtil.java    From Smack with Apache License 2.0 4 votes vote down vote up
public static <E extends Element, P extends Provider<E>> E parse(Reader reader, Class<P> providerClass, XmlPullParserKind parserKind)
                throws XmlPullParserException, IOException, SmackParsingException {
    P provider = providerClassToProvider(providerClass);
    return parse(reader, provider, parserKind);
}
 
Example #20
Source File: SmackTestUtil.java    From Smack with Apache License 2.0 4 votes vote down vote up
public static <E extends Element> E parse(CharSequence xml, Provider<E> provider, XmlPullParserKind parserKind)
                throws XmlPullParserException, IOException, SmackParsingException {
    String xmlString = xml.toString();
    Reader reader = new StringReader(xmlString);
    return parse(reader, provider, parserKind);
}
 
Example #21
Source File: SmackTestUtil.java    From Smack with Apache License 2.0 4 votes vote down vote up
public static <E extends Element> E parse(InputStream inputStream, Provider<E> provider, XmlPullParserKind parserKind)
                throws XmlPullParserException, IOException, SmackParsingException {
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
    return parse(inputStreamReader, provider, parserKind);
}
 
Example #22
Source File: SmackTestUtil.java    From Smack with Apache License 2.0 4 votes vote down vote up
public static <E extends Element> E parse(Reader reader, Provider<E> provider, XmlPullParserKind parserKind)
                throws XmlPullParserException, IOException, SmackParsingException {
    XmlPullParser parser = getParserFor(reader, parserKind);
    E element = provider.parse(parser);
    return element;
}
 
Example #23
Source File: JingleProvider.java    From jitsi-hammer with Apache License 2.0 4 votes vote down vote up
@Override
public NewJingleIQ parse(XmlPullParser parser, int initialDepth)
        throws XmlPullParserException, IOException
{
    NewJingleIQ jingleIQ = new NewJingleIQ();
    NewJingleAction action = NewJingleAction.parseString(parser.getAttributeValue("", "action"));
    String initiator = parser.getAttributeValue("", "initiator");
    String responder = parser.getAttributeValue("", "responder");
    String sid = parser.getAttributeValue("", "sid");

    jingleIQ.setAction(action);
    jingleIQ.setInitiator(initiator);
    jingleIQ.setResponder(responder);
    jingleIQ.setSID(sid);
    boolean done = false;

    try
    {
        while (!done)
        {
            int eventType = parser.next();
            String elementName = parser.getName();
            String namespace = parser.getNamespace();
            if (eventType == XmlPullParser.START_TAG)
            {
                ExtensionElementProvider provider = ProviderManager.getExtensionProvider(elementName, namespace);
                if (provider != null)
                {
                    Element child = provider.parse(parser);
                    if (child instanceof NewContentPacketExtension)
                    {
                        jingleIQ.addContent((NewContentPacketExtension)child);
                    }
                    else
                    {
                        throw new IOException("JingleProvider doesn't handle child element " + elementName +
                            " in namespace " + namespace);
                    }
                }
                else
                {
                    throw new IOException("JingleProvider: no provider found for element " +
                            elementName + " in namespace " + namespace);
                }
            }

            if (eventType == XmlPullParser.END_TAG && parser.getName().equals("jingle"))
            {
                done = true;
            }
        }
    }
    catch (Exception e)
    {
    }

    return jingleIQ;
}
 
Example #24
Source File: DataForm.java    From Smack with Apache License 2.0 4 votes vote down vote up
public List<Element> getExtensionElements() {
    return extensionElements;
}
 
Example #25
Source File: StreamManagementException.java    From Smack with Apache License 2.0 4 votes vote down vote up
public List<Element> getElements() {
    return elements;
}
 
Example #26
Source File: NewAbstractExtensionElement.java    From jitsi-hammer with Apache License 2.0 2 votes vote down vote up
/**
 * Returns all sub-elements for this <tt>NewAbstractExtensionElement</tt> or
 * <tt>null</tt> if there aren't any.
 * <p>
 * Overriding extensions may need to override this method if they would like
 * to have anything more elaborate than just a list of extensions.
 *
 * @return the {@link List} of elements that this packet extension contains.
 */
public List<Element> getChildExtensions()
{
    return childExtensions;
}
 
Example #27
Source File: NewAbstractExtensionElement.java    From jitsi-hammer with Apache License 2.0 2 votes vote down vote up
/**
 * Adds the specified <tt>childExtension</tt> to the list of extensions
 * registered with this packet.
 * <p/>
 * Overriding extensions may need to override this method if they would like
 * to have anything more elaborate than just a list of extensions (e.g.
 * casting separate instances to more specific.
 *
 * @param childExtension the extension we'd like to add here.
 */
public void addChildExtension(Element childExtension)
{
    childExtensions.add(childExtension);
}