diff options
Diffstat (limited to 'src/com/isode/stroke/parser')
12 files changed, 1098 insertions, 0 deletions
diff --git a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java index 84a0b43..08d9f28 100644 --- a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java +++ b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java @@ -21,6 +21,14 @@ public class FullPayloadParserFactoryCollection extends PayloadParserFactoryColl addFactory(new GenericPayloadParserFactory<LastParser>("query", "jabber:iq:last", LastParser.class)); addFactory(new GenericPayloadParserFactory<BodyParser>("body", BodyParser.class)); addFactory(new GenericPayloadParserFactory<SubjectParser>("subject", SubjectParser.class)); + addFactory(new JingleParserFactory(this)); + addFactory(new GenericPayloadParserFactory<JingleReasonParser>("reason", "urn:xmpp:jingle:1", JingleReasonParser.class)); + addFactory(new GenericPayloadParserFactory<JingleFileTransferFileInfoParser>("file", "", JingleFileTransferFileInfoParser.class)); + addFactory(new JingleFileTransferDescriptionParserFactory(this)); + addFactory(new GenericPayloadParserFactory<JingleFileTransferHashParser>("checksum", "urn:xmpp:jingle:apps:file-transfer:4", JingleFileTransferHashParser.class)); + addFactory(new GenericPayloadParserFactory<JingleIBBTransportMethodPayloadParser>("transport", "urn:xmpp:jingle:transports:ibb:1", JingleIBBTransportMethodPayloadParser.class)); + addFactory(new GenericPayloadParserFactory<JingleS5BTransportMethodPayloadParser>("transport", "urn:xmpp:jingle:transports:s5b:1", JingleS5BTransportMethodPayloadParser.class)); + addFactory(new JingleContentPayloadParserFactory(this)); //addFactory(new GenericPayloadParserFactory<PriorityParser>("priority", PriorityParser.class)); addFactory(new ErrorParserFactory(this)); addFactory(new SoftwareVersionParserFactory()); diff --git a/src/com/isode/stroke/parser/payloadparsers/JingleContentPayloadParser.java b/src/com/isode/stroke/parser/payloadparsers/JingleContentPayloadParser.java new file mode 100644 index 0000000..5f114c2 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/JingleContentPayloadParser.java @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2011 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.parser.PayloadParserFactory; +import com.isode.stroke.parser.PayloadParser; +import com.isode.stroke.elements.JingleContentPayload; +import com.isode.stroke.elements.JingleTransportPayload; +import com.isode.stroke.elements.JingleDescription; +import com.isode.stroke.base.NotNull; + +public class JingleContentPayloadParser extends GenericPayloadParser<JingleContentPayload> { + + private PayloadParserFactoryCollection factories; + private int level; + private PayloadParser currentPayloadParser; + + public JingleContentPayloadParser(PayloadParserFactoryCollection factories) { + super(new JingleContentPayload()); + this.factories = factories; + this.level = 0; + } + + /** + * @param element, NotNull. + * @param ns. + * @param attributes, NotNull. + */ + @Override + public void handleStartElement(String element, String ns, AttributeMap attributes) { + NotNull.exceptIfNull(attributes, "attributes"); + if (level == 0) { + String creator = ""; + if(attributes.getAttributeValue("creator") != null) { + creator = attributes.getAttributeValue("creator"); + } else { + creator = ""; + } + if (creator.equals("initiator")) { + getPayloadInternal().setCreator(JingleContentPayload.Creator.InitiatorCreator); + } else if (creator.equals("responder")) { + getPayloadInternal().setCreator(JingleContentPayload.Creator.ResponderCreator); + } else { + getPayloadInternal().setCreator(JingleContentPayload.Creator.UnknownCreator); + } + if(attributes.getAttributeValue("name") != null) { + getPayloadInternal().setName(attributes.getAttributeValue("name")); + } else { + getPayloadInternal().setName(""); + } + } + + if (level == 1) { + PayloadParserFactory payloadParserFactory = factories.getPayloadParserFactory(element, ns, attributes); + if (payloadParserFactory != null) { + currentPayloadParser = payloadParserFactory.createPayloadParser(); + } + } + + if (level >= 1 && currentPayloadParser != null) { + currentPayloadParser.handleStartElement(element, ns, attributes); + } + + ++level; + } + + /** + * @param element, NotNull. + * @param ns. + */ + @Override + public void handleEndElement(String element, String ns) { + --level; + + if (currentPayloadParser != null) { + if (level >= 1) { + currentPayloadParser.handleEndElement(element, ns); + } + + if (level == 1) { + if(currentPayloadParser.getPayload() instanceof JingleTransportPayload) { + JingleTransportPayload transport = (JingleTransportPayload)(currentPayloadParser.getPayload()); + if (transport != null) { + getPayloadInternal().addTransport(transport); + } + } + if(currentPayloadParser.getPayload() instanceof JingleDescription) { + JingleDescription description = (JingleDescription)(currentPayloadParser.getPayload()); + if (description != null) { + getPayloadInternal().addDescription(description); + } + } + } + } + } + + /** + * @param data, NotNull. + */ + @Override + public void handleCharacterData(String data) { + NotNull.exceptIfNull(data, "data"); + if (level > 1 && currentPayloadParser != null) { + currentPayloadParser.handleCharacterData(data); + } + } +}
\ No newline at end of file diff --git a/src/com/isode/stroke/parser/payloadparsers/JingleContentPayloadParserFactory.java b/src/com/isode/stroke/parser/payloadparsers/JingleContentPayloadParserFactory.java new file mode 100644 index 0000000..d31303e --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/JingleContentPayloadParserFactory.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2011 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.parser.GenericPayloadParserFactory; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.parser.payloadparsers.JingleContentPayloadParser; +import com.isode.stroke.parser.PayloadParserFactory; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.PayloadParser; +import com.isode.stroke.base.NotNull; + +public class JingleContentPayloadParserFactory implements PayloadParserFactory { + + private PayloadParserFactoryCollection factories; + + /** + * JingleContentPayloadParserFactory(); + */ + public JingleContentPayloadParserFactory(PayloadParserFactoryCollection factories) { + this.factories = factories; + } + + /** + * @param attributes. + * @param element, Not Null. + * @param attributes, Not NUll. + */ + public boolean canParse(String element, String ns, AttributeMap attributes) { + NotNull.exceptIfNull(element, "element"); + NotNull.exceptIfNull(attributes, "attributes"); + return element.equals("content") && ns.equals("urn:xmpp:jingle:1"); + } + + /** + * @return PayloadParser() + */ + public PayloadParser createPayloadParser() { + return new JingleContentPayloadParser(factories); + } +} + diff --git a/src/com/isode/stroke/parser/payloadparsers/JingleFileTransferDescriptionParser.java b/src/com/isode/stroke/parser/payloadparsers/JingleFileTransferDescriptionParser.java new file mode 100644 index 0000000..1b74fa9 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/JingleFileTransferDescriptionParser.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2011 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ +/* + * Copyright (c) 2014 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.parser.PayloadParser; +import com.isode.stroke.parser.PayloadParserFactory; +import com.isode.stroke.elements.JingleFileTransferDescription; +import com.isode.stroke.elements.JingleFileTransferFileInfo; +import com.isode.stroke.base.NotNull; + +public class JingleFileTransferDescriptionParser extends GenericPayloadParser<JingleFileTransferDescription> { + + private PayloadParserFactoryCollection factories; + private int level = 0; + private PayloadParser currentPayloadParser; + + public JingleFileTransferDescriptionParser(PayloadParserFactoryCollection factories) { + super(new JingleFileTransferDescription()); + this.factories = factories; + this.level = 0; + } + + /** + * @param element. + * @param ns. + * @param attributes. + */ + @Override + public void handleStartElement(String element, String ns, AttributeMap attributes) { + if (level == 1) { + PayloadParserFactory payloadParserFactory = factories.getPayloadParserFactory(element, ns, attributes); + if (payloadParserFactory != null) { + currentPayloadParser = payloadParserFactory.createPayloadParser(); + } + } + + if (level >= 1 && currentPayloadParser != null) { + currentPayloadParser.handleStartElement(element, ns, attributes); + } + ++level; + } + + /** + * @param element. + * @param ns. + */ + @Override + public void handleEndElement(String element, String ns) { + --level; + if (level >= 1 && currentPayloadParser != null) { + currentPayloadParser.handleEndElement(element, ns); + } + + if (level == 0) { + JingleFileTransferFileInfo info = (JingleFileTransferFileInfo)(currentPayloadParser.getPayload()); + if (info != null) { + getPayloadInternal().setFileInfo(info); + } + } + } + + /** + * @param data, NotNull. + */ + @Override + public void handleCharacterData(String data) { + if (level >= 1 && currentPayloadParser != null) { + currentPayloadParser.handleCharacterData(data); + } + } +}
\ No newline at end of file diff --git a/src/com/isode/stroke/parser/payloadparsers/JingleFileTransferDescriptionParserFactory.java b/src/com/isode/stroke/parser/payloadparsers/JingleFileTransferDescriptionParserFactory.java new file mode 100644 index 0000000..ec884ec --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/JingleFileTransferDescriptionParserFactory.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2011 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ +/* + * Copyright (c) 2014 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.parser.GenericPayloadParserFactory; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.parser.payloadparsers.JingleFileTransferDescriptionParser; +import com.isode.stroke.parser.PayloadParserFactory; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.PayloadParser; +import com.isode.stroke.base.NotNull; + +public class JingleFileTransferDescriptionParserFactory implements PayloadParserFactory { + + private PayloadParserFactoryCollection factories; + + /** + * JingleFileTransferDescriptionParserFactory(); + */ + public JingleFileTransferDescriptionParserFactory(PayloadParserFactoryCollection factories) { + this.factories = factories; + } + + /** + * @param attributes. + * @param element, Not Null. + * @param attributes, Not NUll. + */ + public boolean canParse(String element, String ns, AttributeMap attributes) { + NotNull.exceptIfNull(element, "element"); + NotNull.exceptIfNull(ns, "ns"); + return element.equals("description") && ns.equals("urn:xmpp:jingle:apps:file-transfer:4"); + } + + /** + * @return PayloadParser() + */ + public PayloadParser createPayloadParser() { + return new JingleFileTransferDescriptionParser(factories); + } +} + diff --git a/src/com/isode/stroke/parser/payloadparsers/JingleFileTransferFileInfoParser.java b/src/com/isode/stroke/parser/payloadparsers/JingleFileTransferFileInfoParser.java new file mode 100644 index 0000000..edb40f4 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/JingleFileTransferFileInfoParser.java @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2014 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.elements.JingleFileTransferFileInfo; +import com.isode.stroke.elements.HashElement; +import com.isode.stroke.base.NotNull; +import com.isode.stroke.base.DateTime; +import com.isode.stroke.stringcodecs.Base64; + +public class JingleFileTransferFileInfoParser extends GenericPayloadParser<JingleFileTransferFileInfo> { + + private int level = 0; + private String charData = ""; + private String hashAlg = ""; + private Long rangeOffset = null; + + public JingleFileTransferFileInfoParser() { + super(new JingleFileTransferFileInfo()); + } + + /** + * @param element, NotNull. + * @param ns. + * @param attributes. + */ + @Override + public void handleStartElement(String element, String ns, AttributeMap attributes) { + NotNull.exceptIfNull(element, "element"); + charData = ""; + if (element.equals("hash")) { + if(attributes.getAttributeValue("algo") != null) { + hashAlg = attributes.getAttributeValue("algo"); + } else { + hashAlg = ""; + } + } + else if (element.equals("range")) { + if(attributes.getAttributeValue("offset") != null) { + try { + rangeOffset = Long.parseLong(attributes.getAttributeValue("offset")); + } + catch(NumberFormatException e) { + rangeOffset = null; + } + } else { + rangeOffset = null; + } + } + + ++level; + } + + /** + * @param element, NotNull. + * @param ns. + */ + @Override + public void handleEndElement(String element, String ns) { + NotNull.exceptIfNull(element, "element"); + --level; + if (level == 1) { + if (element.equals("date")) { + getPayloadInternal().setDate(DateTime.stringToDate(charData)); + } + else if (element.equals("desc")) { + getPayloadInternal().setDescription(charData); + } + else if (element.equals("media-type")) { + getPayloadInternal().setMediaType(charData); + } + else if (element.equals("name")) { + getPayloadInternal().setName(charData); + } + else if (element.equals("size")) { + getPayloadInternal().setSize(Long.parseLong(charData)); + } + else if (element.equals("range")) { + getPayloadInternal().setSupportsRangeRequests(true); + if (rangeOffset != null) { + getPayloadInternal().setRangeOffset(rangeOffset.longValue()); + } else { + getPayloadInternal().setRangeOffset(0L); + } + } + else if (element.equals("hash")) { + getPayloadInternal().addHash(new HashElement(hashAlg, Base64.decode(charData))); + } + } + } + + /** + * @param data, NotNull. + */ + @Override + public void handleCharacterData(String data) { + NotNull.exceptIfNull(data, "data"); + charData += data; + } +}
\ No newline at end of file diff --git a/src/com/isode/stroke/parser/payloadparsers/JingleFileTransferHashParser.java b/src/com/isode/stroke/parser/payloadparsers/JingleFileTransferHashParser.java new file mode 100644 index 0000000..89d6817 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/JingleFileTransferHashParser.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2011 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ +/* + * Copyright (c) 2014 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.PayloadParser; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.elements.JingleFileTransferHash; +import com.isode.stroke.elements.JingleFileTransferFileInfo; +import com.isode.stroke.base.NotNull; + +public class JingleFileTransferHashParser extends GenericPayloadParser<JingleFileTransferHash> { + + private int level = 0; + private PayloadParser currentPayloadParser; + + public JingleFileTransferHashParser() { + super(new JingleFileTransferHash()); + this.level = 0; + } + + /** + * @param element, NotNull. + * @param ns. + * @param attributes. + */ + @Override + public void handleStartElement(String element, String ns, AttributeMap attributes) { + NotNull.exceptIfNull(element, "element"); + if (level == 1 && element.equals("file")) { + currentPayloadParser = new JingleFileTransferFileInfoParser(); + } + + if (level >= 1 && currentPayloadParser != null) { + currentPayloadParser.handleStartElement(element, ns, attributes); + } + ++level; + } + + /** + * @param element, NotNull. + * @param ns. + */ + @Override + public void handleEndElement(String element, String ns) { + --level; + if (level >= 1 && currentPayloadParser != null) { + currentPayloadParser.handleEndElement(element, ns); + } + + if (level == 1) { + JingleFileTransferFileInfo info = (JingleFileTransferFileInfo)(currentPayloadParser.getPayload()); + if (info != null) { + getPayloadInternal().setFileInfo(info); + } + } + } + + /** + * @param data, NotNull. + */ + @Override + public void handleCharacterData(String data) { + if (level >= 1 && currentPayloadParser != null) { + currentPayloadParser.handleCharacterData(data); + } + } +}
\ No newline at end of file diff --git a/src/com/isode/stroke/parser/payloadparsers/JingleIBBTransportMethodPayloadParser.java b/src/com/isode/stroke/parser/payloadparsers/JingleIBBTransportMethodPayloadParser.java new file mode 100644 index 0000000..1883903 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/JingleIBBTransportMethodPayloadParser.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.elements.JingleIBBTransportPayload; +import com.isode.stroke.base.NotNull; + +public class JingleIBBTransportMethodPayloadParser extends GenericPayloadParser<JingleIBBTransportPayload> { + + private int level = 0; + + public JingleIBBTransportMethodPayloadParser() { + super(new JingleIBBTransportPayload()); + this.level = 0; + } + + /** + * @param element, NotNull. + * @param ns. + * @param attributes. + */ + @Override + public void handleStartElement(String element, String ns, AttributeMap attributes) { + try { + String blockSize = attributes.getAttributeValue("block-size"); + if (blockSize != null) { + getPayloadInternal().setBlockSize(Integer.parseInt(blockSize)); + } + } + catch (NumberFormatException e) { + + } + if(attributes.getAttributeValue("sid") != null) { + getPayloadInternal().setSessionID(attributes.getAttributeValue("sid")); + } else { + getPayloadInternal().setSessionID(""); + } + ++level; + } + + /** + * @param element, NotNull. + * @param ns. + */ + @Override + public void handleEndElement(String element, String ns) { + --level; + } + + /** + * @param data, NotNull. + */ + @Override + public void handleCharacterData(String data) { + + } +}
\ No newline at end of file diff --git a/src/com/isode/stroke/parser/payloadparsers/JingleParser.java b/src/com/isode/stroke/parser/payloadparsers/JingleParser.java new file mode 100644 index 0000000..1778b02 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/JingleParser.java @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2011 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ +/* + * Copyright (c) 2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.parser.PayloadParser; +import com.isode.stroke.parser.PayloadParserFactory; +import com.isode.stroke.elements.JinglePayload; +import com.isode.stroke.elements.JingleFileTransferHash; +import com.isode.stroke.elements.JingleContentPayload; +import com.isode.stroke.base.NotNull; +import com.isode.stroke.jid.JID; + +public class JingleParser extends GenericPayloadParser<JinglePayload> { + + private PayloadParserFactoryCollection factories; + private int level = 0; + private PayloadParser currentPayloadParser; + + public JingleParser(PayloadParserFactoryCollection factories) { + super(new JinglePayload()); + this.factories = factories; + this.level = 0; + } + + private JinglePayload.Action stringToAction(String str) { + if (str.equals("content-accept")) { + return JinglePayload.Action.ContentAccept; + } else if (str.equals("content-add")) { + return JinglePayload.Action.ContentAdd; + } else if (str.equals("content-modify")) { + return JinglePayload.Action.ContentModify; + } else if (str.equals("content-reject")) { + return JinglePayload.Action.ContentReject; + } else if (str.equals("content-remove")) { + return JinglePayload.Action.ContentRemove; + } else if (str.equals("description-info")) { + return JinglePayload.Action.DescriptionInfo; + } else if (str.equals("security-info")) { + return JinglePayload.Action.SecurityInfo; + } else if (str.equals("session-accept")) { + return JinglePayload.Action.SessionAccept; + } else if (str.equals("session-info")) { + return JinglePayload.Action.SessionInfo; + } else if (str.equals("session-initiate")) { + return JinglePayload.Action.SessionInitiate; + } else if (str.equals("session-terminate")) { + return JinglePayload.Action.SessionTerminate; + } else if (str.equals("transport-accept")) { + return JinglePayload.Action.TransportAccept; + } else if (str.equals("transport-info")) { + return JinglePayload.Action.TransportInfo; + } else if (str.equals("transport-reject")) { + return JinglePayload.Action.TransportReject; + } else if (str.equals("transport-replace")) { + return JinglePayload.Action.TransportReplace; + } else { + return JinglePayload.Action.UnknownAction; + } + } + + /** + * @param element, NotNull. + * @param ns. + * @param attributes. + */ + @Override + public void handleStartElement(String element, String ns, AttributeMap attributes) { + NotNull.exceptIfNull(attributes, "attributes"); + if (level == 0) { + // <jingle > tag + JinglePayload payload = getPayloadInternal(); + if (attributes.getAttributeValue("action") != null) { + payload.setAction(stringToAction(attributes.getAttributeValue("action"))); + } else { + payload.setAction(stringToAction("")); + } + + if(attributes.getAttributeValue("initiator") != null) { + payload.setInitiator(new JID(attributes.getAttributeValue("initiator"))); + } else { + payload.setInitiator(new JID("")); + } + + if(attributes.getAttributeValue("responder") != null) { + payload.setResponder(new JID(attributes.getAttributeValue("responder"))); + } else { + payload.setResponder(new JID("")); + } + + if (attributes.getAttributeValue("sid") != null) { + payload.setSessionID(attributes.getAttributeValue("sid")); + } else { + payload.setSessionID(""); + } + } + + if (level == 1) { + PayloadParserFactory payloadParserFactory = factories.getPayloadParserFactory(element, ns, attributes); + if (payloadParserFactory != null) { + currentPayloadParser = payloadParserFactory.createPayloadParser(); + } + } + + if (level >= 1 && currentPayloadParser != null) { + currentPayloadParser.handleStartElement(element, ns, attributes); + } + + ++level; + } + + /** + * @param element, NotNull. + * @param ns. + */ + @Override + public void handleEndElement(String element, String ns) { + --level; + if (currentPayloadParser != null) { + if (level >= 1) { + currentPayloadParser.handleEndElement(element, ns); + } + + if (level == 1) { + if(currentPayloadParser.getPayload() instanceof JinglePayload.Reason) { + JinglePayload.Reason reason = (JinglePayload.Reason)(currentPayloadParser.getPayload()); + if (reason != null) { + getPayloadInternal().setReason(reason); + } + } + + if(currentPayloadParser.getPayload() instanceof JingleContentPayload) { + JingleContentPayload payload = (JingleContentPayload)(currentPayloadParser.getPayload()); + if (payload != null) { + getPayloadInternal().addContent(payload); + } + } + + if(currentPayloadParser.getPayload() instanceof JingleFileTransferHash) { + JingleFileTransferHash hash = (JingleFileTransferHash)(currentPayloadParser.getPayload()); + if (hash != null) { + getPayloadInternal().addPayload(hash); + } + } + } + } + } + + /** + * @param data, NotNull. + */ + @Override + public void handleCharacterData(String data) { + if (level > 1 && currentPayloadParser != null) { + currentPayloadParser.handleCharacterData(data); + } + } +}
\ No newline at end of file diff --git a/src/com/isode/stroke/parser/payloadparsers/JingleParserFactory.java b/src/com/isode/stroke/parser/payloadparsers/JingleParserFactory.java new file mode 100644 index 0000000..3a6c2e9 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/JingleParserFactory.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2011 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.parser.GenericPayloadParserFactory; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.parser.payloadparsers.JingleParser; +import com.isode.stroke.parser.PayloadParserFactory; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.PayloadParser; +import com.isode.stroke.base.NotNull; + +public class JingleParserFactory implements PayloadParserFactory { + + private PayloadParserFactoryCollection factories; + + /** + * JingleParserFactory(); + */ + public JingleParserFactory(PayloadParserFactoryCollection factories) { + this.factories = factories; + } + + /** + * @param attributes. + * @param element, Not Null. + * @param attributes, Not NUll. + */ + public boolean canParse(String element, String ns, AttributeMap attributes) { + NotNull.exceptIfNull(element, "element"); + NotNull.exceptIfNull(ns, "ns"); + return element.equals("jingle") && ns.equals("urn:xmpp:jingle:1"); + } + + /** + * @return PayloadParser() + */ + public PayloadParser createPayloadParser() { + return new JingleParser(factories); + } +} + diff --git a/src/com/isode/stroke/parser/payloadparsers/JingleReasonParser.java b/src/com/isode/stroke/parser/payloadparsers/JingleReasonParser.java new file mode 100644 index 0000000..5651c64 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/JingleReasonParser.java @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2011 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.parser.PayloadParser; +import com.isode.stroke.parser.PayloadParserFactory; +import com.isode.stroke.elements.JinglePayload; +import com.isode.stroke.elements.JingleFileTransferHash; +import com.isode.stroke.elements.JingleContentPayload; +import com.isode.stroke.base.NotNull; +import com.isode.stroke.jid.JID; + +public class JingleReasonParser extends GenericPayloadParser<JinglePayload.Reason> { + + private int level; + private boolean parseText; + private String text = ""; + + public JingleReasonParser() { + super(new JinglePayload.Reason()); + this.level = 0; + this.parseText = false; + } + + private JinglePayload.Reason.Type stringToReasonType(String type) { + if (type.equals("alternative-session")) { + return JinglePayload.Reason.Type.AlternativeSession; + } else if (type.equals("busy")) { + return JinglePayload.Reason.Type.Busy; + } else if (type.equals("cancel")) { + return JinglePayload.Reason.Type.Cancel; + } else if (type.equals("connectivity-error")) { + return JinglePayload.Reason.Type.ConnectivityError; + } else if (type.equals("decline")) { + return JinglePayload.Reason.Type.Decline; + } else if (type.equals("expired")) { + return JinglePayload.Reason.Type.Expired; + } else if (type.equals("failed-application")) { + return JinglePayload.Reason.Type.FailedApplication; + } else if (type.equals("failed-transport")) { + return JinglePayload.Reason.Type.FailedTransport; + } else if (type.equals("general-error")) { + return JinglePayload.Reason.Type.GeneralError; + } else if (type.equals("gone")) { + return JinglePayload.Reason.Type.Gone; + } else if (type.equals("incompatible-parameters")) { + return JinglePayload.Reason.Type.IncompatibleParameters; + } else if (type.equals("media-error")) { + return JinglePayload.Reason.Type.MediaError; + } else if (type.equals("security-error")) { + return JinglePayload.Reason.Type.SecurityError; + } else if (type.equals("success")) { + return JinglePayload.Reason.Type.Success; + } else if (type.equals("timeout")) { + return JinglePayload.Reason.Type.Timeout; + } else if (type.equals("unsupported-applications")) { + return JinglePayload.Reason.Type.UnsupportedApplications; + } else if (type.equals("unsupported-transports")) { + return JinglePayload.Reason.Type.UnsupportedTransports; + } else { + return JinglePayload.Reason.Type.UnknownType; + } + } + + /** + * @param element, NotNull. + * @param ns. + * @param attributes. + */ + @Override + public void handleStartElement(String element, String ns, AttributeMap attributes) { + NotNull.exceptIfNull(element, "element"); + if (level == 1) { + if (element.equals("text")) { + parseText = true; + } else { + // reason type + getPayloadInternal().type = stringToReasonType(element); + } + } + ++level; + } + + /** + * @param element, NotNull. + * @param ns. + */ + @Override + public void handleEndElement(String element, String ns) { + --level; + if (element.equals("text")) { + parseText = false; + getPayloadInternal().text = text; + } + } + + /** + * @param data, NotNull. + */ + @Override + public void handleCharacterData(String data) { + NotNull.exceptIfNull(data, "data"); + if (parseText) { + text += data; + } + } +}
\ No newline at end of file diff --git a/src/com/isode/stroke/parser/payloadparsers/JingleS5BTransportMethodPayloadParser.java b/src/com/isode/stroke/parser/payloadparsers/JingleS5BTransportMethodPayloadParser.java new file mode 100644 index 0000000..d4a1ae4 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/JingleS5BTransportMethodPayloadParser.java @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2011 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ +/* +* Copyright (c) 2014-2015 Isode Limited. +* All rights reserved.v3. +* See the COPYING file for more information. +*/ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.elements.JingleS5BTransportPayload; +import com.isode.stroke.base.NotNull; +import com.isode.stroke.jid.JID; +import com.isode.stroke.network.HostAddressPort; +import com.isode.stroke.network.HostAddress; +import java.util.logging.Logger; +import java.net.InetAddress; +import java.net.UnknownHostException; + +public class JingleS5BTransportMethodPayloadParser extends GenericPayloadParser<JingleS5BTransportPayload> { + + private int level = 0; + private Logger logger_ = Logger.getLogger(this.getClass().getName()); + + public JingleS5BTransportMethodPayloadParser() { + super(new JingleS5BTransportPayload()); + this.level = 0; + } + + private JingleS5BTransportPayload.Candidate.Type stringToType(String str) { + if (str.equals("direct")) { + return JingleS5BTransportPayload.Candidate.Type.DirectType; + } else if (str.equals("assisted")) { + return JingleS5BTransportPayload.Candidate.Type.AssistedType; + } else if (str.equals("tunnel")) { + return JingleS5BTransportPayload.Candidate.Type.TunnelType; + } else if (str.equals("proxy")) { + return JingleS5BTransportPayload.Candidate.Type.ProxyType; + } else { + logger_.warning("Unknown candidate type; falling back to default!"); + return JingleS5BTransportPayload.Candidate.Type.DirectType; + } + } + /** + * @param element, NotNull. + * @param ns. + * @param attributes. + */ + @Override + public void handleStartElement(String element, String ns, AttributeMap attributes) { + + if (level == 0) { + if(attributes.getAttributeValue("sid") != null) { + getPayloadInternal().setSessionID(attributes.getAttributeValue("sid")); + } else { + getPayloadInternal().setSessionID(""); + } + + String mode = ""; + if(attributes.getAttributeValue("mode") != null) { + mode = attributes.getAttributeValue("mode"); + } else { + mode = "tcp"; + } + + if (mode.equals("tcp")) { + getPayloadInternal().setMode(JingleS5BTransportPayload.Mode.TCPMode); + } else if(mode.equals("udp")) { + getPayloadInternal().setMode(JingleS5BTransportPayload.Mode.UDPMode); + } else { + logger_.warning("Unknown S5B mode; falling back to default!"); + getPayloadInternal().setMode(JingleS5BTransportPayload.Mode.TCPMode); + } + + if(attributes.getAttributeValue("dstaddr") != null) { + getPayloadInternal().setDstAddr(attributes.getAttributeValue("dstaddr")); + } else { + getPayloadInternal().setDstAddr(""); + } + + } else if (level == 1) { + if (element.equals("candidate")) { + JingleS5BTransportPayload.Candidate candidate = new JingleS5BTransportPayload.Candidate(); + candidate.cid = ""; + if(attributes.getAttributeValue("cid") != null) { + candidate.cid = attributes.getAttributeValue("cid"); + } + + int port = -1; + try { + if(attributes.getAttributeValue("port") != null) { + port = Integer.parseInt(attributes.getAttributeValue("port")); + } + } catch (NumberFormatException e) { + + } + + try { + candidate.hostPort = new HostAddressPort(new HostAddress(InetAddress.getByName("")), port); + if(attributes.getAttributeValue("host") != null) { + candidate.hostPort = new HostAddressPort(new HostAddress(InetAddress.getByName(attributes.getAttributeValue("host"))), port); + } + } catch (UnknownHostException e) { + + } + + candidate.jid = new JID(""); + if(attributes.getAttributeValue("jid") != null) { + candidate.jid = new JID(attributes.getAttributeValue("jid")); + } + + int priority = -1; + try { + if(attributes.getAttributeValue("priority") != null) { + priority = Integer.parseInt(attributes.getAttributeValue("priority")); + } + } catch (NumberFormatException e) { + + } + candidate.priority = priority; + + candidate.type = stringToType("direct"); + if(attributes.getAttributeValue("type") != null) { + candidate.type = stringToType(attributes.getAttributeValue("type")); + } + getPayloadInternal().addCandidate(candidate); + + } else if (element.equals("candidate-used")) { + if(attributes.getAttributeValue("cid") != null) { + getPayloadInternal().setCandidateUsed(attributes.getAttributeValue("cid")); + } else { + getPayloadInternal().setCandidateUsed(""); + } + } else if (element.equals("candidate-error")) { + getPayloadInternal().setCandidateError(true); + } else if (element.equals("activated")) { + if(attributes.getAttributeValue("cid") != null) { + getPayloadInternal().setActivated(attributes.getAttributeValue("cid")); + } else { + getPayloadInternal().setActivated(""); + } + } else if (element.equals("proxy-error")) { + getPayloadInternal().setProxyError(true); + } + } + + ++level; + } + + /** + * @param element, NotNull. + * @param ns. + */ + @Override + public void handleEndElement(String element, String ns) { + --level; + } + + /** + * @param data, NotNull. + */ + @Override + public void handleCharacterData(String data) { + + } +}
\ No newline at end of file |