From 1910b4f007f2cf2f19be8793fd7ad29a729d7997 Mon Sep 17 00:00:00 2001 From: Tarun Gupta Date: Sat, 13 Jun 2015 02:55:03 +0530 Subject: Add InBandRegistrationPayload Element. Adds InBandRegistrationPayload Element, its Parser and Serializer. License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: Test added for InBandRegistrationPayload Serializer, which passes. Change-Id: Ic6cc1329f222b5a57f05ec2299e36b56f5767091 diff --git a/src/com/isode/stroke/elements/InBandRegistrationPayload.java b/src/com/isode/stroke/elements/InBandRegistrationPayload.java new file mode 100644 index 0000000..cc73348 --- /dev/null +++ b/src/com/isode/stroke/elements/InBandRegistrationPayload.java @@ -0,0 +1,343 @@ +/* + * Copyright (c) 2010 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.elements; + +import com.isode.stroke.base.NotNull; +import com.isode.stroke.elements.Payload; +import com.isode.stroke.elements.Form; + +public class InBandRegistrationPayload extends Payload { + + private Form form; + private boolean registered; + private boolean remove; + private String instructions; + private String username; + private String nick; + private String password; + private String name; + private String first; + private String last; + private String email; + private String address; + private String city; + private String state; + private String zip; + private String phone; + private String url; + private String date; + private String misc; + private String text; + private String key; + + /** + * Default Constructor. + */ + public InBandRegistrationPayload() { + this.registered = false; + this.remove = false; + } + + /** + * @return form. + */ + public Form getForm() { + return form; + } + + /** + * @return registered. + */ + public boolean isRegistered() { + return registered; + } + + /** + * @return remove. + */ + public boolean isRemove() { + return remove; + } + + /** + * @return instructions. + */ + public String getInstructions() { + return instructions; + } + + /** + * @return username. + */ + public String getUsername() { + return username; + } + + /** + * @return nick. + */ + public String getNick() { + return nick; + } + + /** + * @return password. + */ + public String getPassword() { + return password; + } + + /** + * @return name. + */ + public String getName() { + return name; + } + + /** + * @return first. + */ + public String getFirst() { + return first; + } + + /** + * @return last. + */ + public String getLast() { + return last; + } + + /** + * @return email. + */ + public String getEMail() { + return email; + } + + /** + * @return address. + */ + public String getAddress() { + return address; + } + + /** + * @return city. + */ + public String getCity() { + return city; + } + + /** + * @return state. + */ + public String getState() { + return state; + } + + /** + * @return zip. + */ + public String getZip() { + return zip; + } + + /** + * @return phone. + */ + public String getPhone() { + return phone; + } + + /** + * @return url. + */ + public String getURL() { + return url; + } + + /** + * @return date. + */ + public String getDate() { + return date; + } + + /** + * @return misc. + */ + public String getMisc() { + return misc; + } + + /** + * @return text. + */ + public String getText() { + return text; + } + + /** + * @return key. + */ + public String getKey() { + return key; + } + + /** + * @param Form. + */ + public void setForm(Form f) { + form = f; + } + + /** + * @param remove. + */ + public void setRemove(boolean b) { + remove = b; + } + + /** + * @param registered. + */ + public void setRegistered(boolean b) { + registered = b; + } + + /** + * @param instructions. + */ + public void setInstructions(String v) { + this.instructions = v; + } + + /** + * @param username. + */ + public void setUsername(String v) { + this.username = v; + } + + /** + * @param nick. + */ + public void setNick(String v) { + this.nick = v; + } + + /** + * @param password. + */ + public void setPassword(String v) { + this.password = v; + } + + /** + * @param name. + */ + public void setName(String v) { + this.name = v; + } + + /** + * @param first. + */ + public void setFirst(String v) { + this.first = v; + } + + /** + * @param last. + */ + public void setLast(String v) { + this.last = v; + } + + /** + * @param email. + */ + public void setEMail(String v) { + this.email = v; + } + + /** + * @param address. + */ + public void setAddress(String v) { + this.address = v; + } + + /** + * @param city. + */ + public void setCity(String v) { + this.city = v; + } + + /** + * @param state. + */ + public void setState(String v) { + this.state = v; + } + + /** + * @param zip. + */ + public void setZip(String v) { + this.zip = v; + } + + /** + * @param phone. + */ + public void setPhone(String v) { + this.phone = v; + } + + /** + * @param url. + */ + public void setURL(String v) { + this.url = v; + } + + /** + * @param date. + */ + public void setDate(String v) { + this.date = v; + } + + /** + * @param misc. + */ + public void setMisc(String v) { + this.misc = v; + } + + /** + * @param text. + */ + public void setText(String v) { + this.text = v; + } + + /** + * @param key. + */ + public void setKey(String v) { + this.key = v; + } +} diff --git a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java index 1872feb..ab8aed5 100644 --- a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java +++ b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java @@ -36,7 +36,7 @@ public class FullPayloadParserFactoryCollection extends PayloadParserFactoryColl addFactory(new FormParserFactory()); addFactory(new GenericPayloadParserFactory("command", "http://jabber.org/protocol/commands", CommandParser.class)); - //addFactery(new InBandRegistrationPayloadParserFactory()); + addFactory(new GenericPayloadParserFactory("query", "jabber:iq:register", InBandRegistrationPayloadParser.class)); addFactory(new SearchPayloadParserFactory()); //addFactory(new StreamInitiationParserFactory()); addFactory(new GenericPayloadParserFactory("query", "http://jabber.org/protocol/bytestreams", BytestreamsParser.class)); diff --git a/src/com/isode/stroke/parser/payloadparsers/InBandRegistrationPayloadParser.java b/src/com/isode/stroke/parser/payloadparsers/InBandRegistrationPayloadParser.java new file mode 100644 index 0000000..e5be60b --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/InBandRegistrationPayloadParser.java @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2010 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.payloadparsers.FormParser; +import com.isode.stroke.parser.payloadparsers.FormParserFactory; +import com.isode.stroke.elements.InBandRegistrationPayload; +import com.isode.stroke.base.NotNull; + +public class InBandRegistrationPayloadParser extends GenericPayloadParser { + + private final int TopLevel = 0; + private final int PayloadLevel = 1; + private int level = TopLevel; + private FormParserFactory formParserFactory; + private FormParser formParser; + private String currentText = ""; + + public InBandRegistrationPayloadParser() { + super(new InBandRegistrationPayload()); + level = TopLevel; + formParser = null; + formParserFactory = new FormParserFactory(); + } + + /** + * @param element, NotNull. + * @param ns. + * @param attributes. + */ + @Override + public void handleStartElement(String element, String ns, AttributeMap attributes) { + NotNull.exceptIfNull(element, "element"); + NotNull.exceptIfNull(ns, "ns"); + if (level == TopLevel) { + + } + else if (level == PayloadLevel) { + if (element.equals("x") && ns.equals("jabber:x:data")) { + assert(formParser == null); + formParser = (FormParser)(formParserFactory.createPayloadParser()); + } + else { + currentText = ""; + } + } + + if (formParser != null) { + formParser.handleStartElement(element, ns, attributes); + } + + ++level; + } + + /** + * @param element, NotNull. + * @param ns. + */ + @Override + public void handleEndElement(String element, String ns) { + NotNull.exceptIfNull(element, "element"); + --level; + + if (formParser != null) { + formParser.handleEndElement(element, ns); + } + + if (level == TopLevel) { + + } + else if (level == PayloadLevel) { + if (formParser != null) { + getPayloadInternal().setForm(formParser.getPayloadInternal()); + formParser = null; + } + else if (element.equals("registered")) { + getPayloadInternal().setRegistered(true); + } + else if (element.equals("remove")) { + getPayloadInternal().setRemove(true); + } + else if (element.equals("instructions")) { + getPayloadInternal().setInstructions(currentText); + } + else if (element.equals("username")) { + getPayloadInternal().setUsername(currentText); + } + else if (element.equals("nick")) { + getPayloadInternal().setNick(currentText); + } + else if (element.equals("password")) { + getPayloadInternal().setPassword(currentText); + } + else if (element.equals("name")) { + getPayloadInternal().setName(currentText); + } + else if (element.equals("first")) { + getPayloadInternal().setFirst(currentText); + } + else if (element.equals("last")) { + getPayloadInternal().setLast(currentText); + } + else if (element.equals("email")) { + getPayloadInternal().setEMail(currentText); + } + else if (element.equals("address")) { + getPayloadInternal().setAddress(currentText); + } + else if (element.equals("city")) { + getPayloadInternal().setCity(currentText); + } + else if (element.equals("state")) { + getPayloadInternal().setState(currentText); + } + else if (element.equals("zip")) { + getPayloadInternal().setZip(currentText); + } + else if (element.equals("phone")) { + getPayloadInternal().setPhone(currentText); + } + else if (element.equals("url")) { + getPayloadInternal().setURL(currentText); + } + else if (element.equals("date")) { + getPayloadInternal().setDate(currentText); + } + else if (element.equals("misc")) { + getPayloadInternal().setMisc(currentText); + } + else if (element.equals("text")) { + getPayloadInternal().setText(currentText); + } + else if (element.equals("key")) { + getPayloadInternal().setKey(currentText); + } + } + } + + /** + * @param data, NotNull. + */ + @Override + public void handleCharacterData(String data) { + NotNull.exceptIfNull(data, "data"); + if (formParser != null) { + formParser.handleCharacterData(data); + } + else { + currentText += data; + } + } +} \ No newline at end of file diff --git a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java index c25ac12..ec35ba3 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java +++ b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java @@ -22,6 +22,7 @@ public class FullPayloadSerializerCollection extends PayloadSerializerCollection addSerializer(new ChatStateSerializer()); //addSerializer(new PrioritySerializer()); addSerializer(new ErrorSerializer()); + addSerializer(new InBandRegistrationPayloadSerializer()); addSerializer(new RosterSerializer()); addSerializer(new MUCPayloadSerializer()); addSerializer(new MUCDestroyPayloadSerializer()); diff --git a/src/com/isode/stroke/serializer/payloadserializers/InBandRegistrationPayloadSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/InBandRegistrationPayloadSerializer.java new file mode 100644 index 0000000..01fb7c8 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/InBandRegistrationPayloadSerializer.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2010 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.serializer.payloadserializers; + +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.xml.XMLTextNode; +import com.isode.stroke.serializer.xml.XMLElement; +import com.isode.stroke.serializer.xml.XMLRawTextNode; +import com.isode.stroke.serializer.payloadserializers.FormSerializer; +import com.isode.stroke.elements.InBandRegistrationPayload; +import com.isode.stroke.elements.Form; +import com.isode.stroke.base.NotNull; + +public class InBandRegistrationPayloadSerializer extends GenericPayloadSerializer { + + public InBandRegistrationPayloadSerializer() { + super(InBandRegistrationPayload.class); + } + + public String serializePayload(InBandRegistrationPayload registration) { + XMLElement registerElement = new XMLElement("query", "jabber:iq:register"); + + if (registration.isRegistered()) { + registerElement.addNode(new XMLElement("registered")); + } + + if (registration.isRemove()) { + registerElement.addNode(new XMLElement("remove")); + } + + if (registration.getInstructions() != null) { + registerElement.addNode(new XMLElement("instructions", "", registration.getInstructions())); + } + + + if (registration.getUsername() != null) { + registerElement.addNode(new XMLElement("username", "", registration.getUsername())); + } + + if (registration.getNick() != null) { + registerElement.addNode(new XMLElement("nick", "", registration.getNick())); + } + + if (registration.getPassword() != null) { + registerElement.addNode(new XMLElement("password", "", registration.getPassword())); + } + + if (registration.getName() != null) { + registerElement.addNode(new XMLElement("name", "", registration.getName())); + } + + if (registration.getFirst() != null) { + registerElement.addNode(new XMLElement("first", "", registration.getFirst())); + } + + if (registration.getLast() != null) { + registerElement.addNode(new XMLElement("last", "", registration.getLast())); + } + + if (registration.getEMail() != null) { + registerElement.addNode(new XMLElement("email", "", registration.getEMail())); + } + + if (registration.getAddress() != null) { + registerElement.addNode(new XMLElement("address", "", registration.getAddress())); + } + + if (registration.getCity() != null) { + registerElement.addNode(new XMLElement("city", "", registration.getCity())); + } + + if (registration.getState() != null) { + registerElement.addNode(new XMLElement("state", "", registration.getState())); + } + + if (registration.getZip() != null) { + registerElement.addNode(new XMLElement("zip", "", registration.getZip())); + } + + if (registration.getPhone() != null) { + registerElement.addNode(new XMLElement("phone", "", registration.getPhone())); + } + + if (registration.getURL() != null) { + registerElement.addNode(new XMLElement("url", "", registration.getURL())); + } + + if (registration.getDate() != null) { + registerElement.addNode(new XMLElement("date", "", registration.getDate())); + } + + if (registration.getMisc() != null) { + registerElement.addNode(new XMLElement("misc", "", registration.getMisc())); + } + + if (registration.getText() != null) { + registerElement.addNode(new XMLElement("text", "", registration.getText())); + } + + if (registration.getKey() != null) { + registerElement.addNode(new XMLElement("key", "", registration.getKey())); + } + + if (registration.getForm() != null) { + Form form = registration.getForm(); + registerElement.addNode(new XMLRawTextNode(new FormSerializer().serialize(form))); + } + + return registerElement.serialize(); + } +} \ No newline at end of file diff --git a/test/com/isode/stroke/parser/payloadparsers/InBandRegistrationPayloadParserTest.java b/test/com/isode/stroke/parser/payloadparsers/InBandRegistrationPayloadParserTest.java new file mode 100644 index 0000000..9e93b37 --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/InBandRegistrationPayloadParserTest.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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.elements.InBandRegistrationPayload; +import com.isode.stroke.elements.Form; +import com.isode.stroke.parser.payloadparsers.InBandRegistrationPayloadParser; +import com.isode.stroke.parser.payloadparsers.PayloadsParserTester; +import com.isode.stroke.eventloop.DummyEventLoop; + +public class InBandRegistrationPayloadParserTest { + + public InBandRegistrationPayloadParserTest() { + + } + + @Test + public void testParse() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertNotNull(parser.parse("" + + "" + + "")); + + InBandRegistrationPayload payload = (InBandRegistrationPayload)parser.getPayload(); + assertNotNull(payload); + assertTrue(payload.isRegistered()); + } + + @Test + public void testParse_Form() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertNotNull(parser.parse("" + + "Use the enclosed form to register." + + "" + + "Contest Registration" + + "" + + "jabber:iq:register" + + "" + + "" + + "")); + + InBandRegistrationPayload payload = (InBandRegistrationPayload)parser.getPayload(); + assertNotNull(payload); + assertEquals("Use the enclosed form to register.", payload.getInstructions()); + + Form form = payload.getForm(); + assertNotNull(form); + assertEquals("Contest Registration", form.getTitle()); + assertEquals(Form.Type.FORM_TYPE, form.getType()); + assertEquals("jabber:iq:register", form.getFormType()); + + } +} \ No newline at end of file diff --git a/test/com/isode/stroke/serializer/payloadserializers/InBandRegistrationPayloadSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/InBandRegistrationPayloadSerializerTest.java new file mode 100644 index 0000000..6880f5d --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/InBandRegistrationPayloadSerializerTest.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2010-2013 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.serializer.payloadserializers; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import com.isode.stroke.serializer.payloadserializers.InBandRegistrationPayloadSerializer; +import com.isode.stroke.elements.InBandRegistrationPayload; +import com.isode.stroke.elements.Form; +import com.isode.stroke.elements.FormField; + +public class InBandRegistrationPayloadSerializerTest { + + /** + * Default Constructor. + */ + public InBandRegistrationPayloadSerializerTest() { + + } + + @Test + public void testSerialize() { + InBandRegistrationPayloadSerializer testling = new InBandRegistrationPayloadSerializer(); + InBandRegistrationPayload registration = new InBandRegistrationPayload(); + registration.setRegistered(true); + + String expectedResult = + "" + + "" + + ""; + + assertEquals(expectedResult, testling.serialize(registration)); + } + + @Test + public void testSerialize_Form() { + InBandRegistrationPayloadSerializer testling = new InBandRegistrationPayloadSerializer(); + InBandRegistrationPayload registration = new InBandRegistrationPayload(); + registration.setInstructions("Use the enclosed form to register."); + + Form form = new Form(); + form.setTitle("Contest Registration"); + + FormField field = new FormField(FormField.Type.HIDDEN_TYPE, "jabber:iq:register"); + field.setName("FORM_TYPE"); + form.addField(field); + registration.setForm(form); + + String expectedResult = + "" + + "Use the enclosed form to register." + + "" + + "Contest Registration" + + "" + + "jabber:iq:register" + + "" + + "" + + ""; + + assertEquals(expectedResult, testling.serialize(registration)); + } +} \ No newline at end of file -- cgit v0.10.2-6-g49f6